mirror of
https://github.com/supermemoryai/supermemory.git
synced 2026-05-12 14:10:55 +00:00
49 lines
1.4 KiB
TypeScript
49 lines
1.4 KiB
TypeScript
import { type ClassValue, clsx } from "clsx";
|
|
import { twMerge } from "tailwind-merge";
|
|
|
|
export function cn(...inputs: ClassValue[]) {
|
|
return twMerge(clsx(inputs));
|
|
}
|
|
|
|
export function formatBytes(
|
|
bytes: number,
|
|
opts: {
|
|
decimals?: number;
|
|
sizeType?: "accurate" | "normal";
|
|
} = {},
|
|
) {
|
|
const { decimals = 0, sizeType = "normal" } = opts;
|
|
|
|
const sizes = ["Bytes", "KB", "MB", "GB", "TB"];
|
|
const accurateSizes = ["Bytes", "KiB", "MiB", "GiB", "TiB"];
|
|
if (bytes === 0) return "0 Byte";
|
|
const i = Math.floor(Math.log(bytes) / Math.log(1024));
|
|
return `${(bytes / Math.pow(1024, i)).toFixed(decimals)} ${
|
|
sizeType === "accurate" ? accurateSizes[i] ?? "Bytest" : sizes[i] ?? "Bytes"
|
|
}`;
|
|
}
|
|
|
|
export function absoluteUrl(path: string) {
|
|
return `${process.env.NEXT_PUBLIC_APP_URL}${path}`;
|
|
}
|
|
|
|
/**
|
|
* Stole this from the @radix-ui/primitive
|
|
* @see https://github.com/radix-ui/primitives/blob/main/packages/core/primitive/src/primitive.tsx
|
|
*/
|
|
export function composeEventHandlers<E>(
|
|
originalEventHandler?: (event: E) => void,
|
|
ourEventHandler?: (event: E) => void,
|
|
{ checkForDefaultPrevented = true } = {},
|
|
) {
|
|
return function handleEvent(event: E) {
|
|
originalEventHandler?.(event);
|
|
|
|
if (
|
|
checkForDefaultPrevented === false ||
|
|
!(event as unknown as Event).defaultPrevented
|
|
) {
|
|
return ourEventHandler?.(event);
|
|
}
|
|
};
|
|
}
|