sourcebot/src/lib/utils.ts

98 lines
2.9 KiB
TypeScript
Raw Normal View History

import { type ClassValue, clsx } from "clsx"
import { twMerge } from "tailwind-merge"
2024-09-07 01:24:39 +00:00
import githubLogo from "../../public/github.svg";
import gitlabLogo from "../../public/gitlab.svg";
import { ServiceError } from "./serviceError";
export function cn(...inputs: ClassValue[]) {
2024-09-07 01:24:39 +00:00
return twMerge(clsx(inputs))
}
/**
* Adds a list of (potentially undefined) query parameters to a path.
*
* @param path The path to add the query parameters to.
* @param queryParams A list of key-value pairs (key=param name, value=param value) to add to the path.
* @returns The path with the query parameters added.
*/
export const createPathWithQueryParams = (path: string, ...queryParams: [string, string | null][]) => {
// Filter out undefined values
// eslint-disable-next-line @typescript-eslint/no-unused-vars
queryParams = queryParams.filter(([_key, value]) => value !== null);
if (queryParams.length === 0) {
return path;
}
const queryString = queryParams.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value ?? '')}`).join('&');
return `${path}?${queryString}`;
2024-09-07 01:24:39 +00:00
}
type CodeHostInfo = {
type: "github" | "gitlab";
repoName: string;
costHostName: string;
repoLink: string;
icon: string;
}
export const getRepoCodeHostInfo = (repoName: string): CodeHostInfo | undefined => {
if (repoName.startsWith("github.com")) {
return {
type: "github",
repoName: repoName.substring("github.com/".length),
costHostName: "GitHub",
repoLink: `https://${repoName}`,
icon: githubLogo,
}
}
if (repoName.startsWith("gitlab.com")) {
return {
type: "gitlab",
repoName: repoName.substring("gitlab.com/".length),
costHostName: "GitLab",
repoLink: `https://${repoName}`,
icon: gitlabLogo,
}
}
return undefined;
}
export const getCodeHostFilePreviewLink = (repoName: string, filePath: string): string | undefined => {
const info = getRepoCodeHostInfo(repoName);
if (info?.type === "github") {
return `${info.repoLink}/blob/HEAD/${filePath}`;
2024-09-07 01:24:39 +00:00
}
if (info?.type === "gitlab") {
return `${info.repoLink}/-/blob/HEAD/${filePath}`;
2024-09-07 01:24:39 +00:00
}
return undefined;
}
export const isServiceError = (data: unknown): data is ServiceError => {
return typeof data === 'object' &&
data !== null &&
'statusCode' in data &&
'errorCode' in data &&
'message' in data;
}
2024-09-17 04:37:34 +00:00
export const getEnv = (env: string | undefined, defaultValue = '') => {
return env ?? defaultValue;
}
export const getEnvNumber = (env: string | undefined, defaultValue: number = 0) => {
return Number(env) ?? defaultValue;
}
export const getEnvBoolean = (env: string | undefined, defaultValue: boolean) => {
if (!env) {
return defaultValue;
}
return env === 'true' || env === '1';
}