chatbot-template/lib/utils.ts

44 lines
978 B
TypeScript
Raw Normal View History

2024-10-11 18:00:22 +05:30
import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";
2023-05-19 12:33:56 -04:00
export function cn(...inputs: ClassValue[]) {
2024-10-11 18:00:22 +05:30
return twMerge(clsx(inputs));
2023-05-19 12:33:56 -04:00
}
2024-10-11 18:00:22 +05:30
interface ApplicationError extends Error {
info: string;
status: number;
2023-05-19 12:33:56 -04:00
}
2023-06-16 15:20:42 +04:00
2024-10-11 18:00:22 +05:30
export const fetcher = async (url: string) => {
const res = await fetch(url);
2024-03-14 20:00:52 +03:00
2024-10-11 18:00:22 +05:30
if (!res.ok) {
const error = new Error(
"An error occurred while fetching the data.",
) as ApplicationError;
2024-03-19 04:37:30 +03:00
2024-10-11 18:00:22 +05:30
error.info = await res.json();
error.status = res.status;
2024-03-19 04:37:30 +03:00
2024-10-11 18:00:22 +05:30
throw error;
2024-03-19 04:37:30 +03:00
}
2024-08-24 23:23:37 -05:00
2024-10-11 18:00:22 +05:30
return res.json();
};
2024-08-24 23:23:37 -05:00
2024-10-11 18:00:22 +05:30
export function getLocalStorage(key: string) {
if (typeof window !== "undefined") {
return JSON.parse(localStorage.getItem(key) || "[]");
}
return [];
2024-08-24 23:23:37 -05:00
}
2024-10-11 18:00:22 +05:30
export function generateUUID(): string {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
const r = (Math.random() * 16) | 0;
const v = c === "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
2024-08-24 23:23:37 -05:00
}