Files
t3code/apps/web/src/lib/utils.ts
T
2026-10-05 13:15:14 -07:00

52 lines
1.8 KiB
TypeScript

import { MessageId, ProjectId, ThreadId } from "@t3tools/contracts";
import { type CxOptions, cx } from "class-variance-authority";
import * as Hex from "effect/encoding/Hex";
import { extendTailwindMerge } from "tailwind-merge";
import { DraftId } from "../composerDraftStore";
// The theme's extra font sizes (index.css). Unregistered, tailwind-merge reads
// text-2xs as a colour and drops it next to text-muted-foreground.
const twMerge = extendTailwindMerge({ extend: { theme: { text: ["2xs", "3xs", "4xs", "5xs"] } } });
export function cn(...inputs: CxOptions) {
return twMerge(cx(inputs));
}
export function isMacPlatform(platform: string): boolean {
return /mac|iphone|ipad|ipod/i.test(platform);
}
export function isWindowsPlatform(platform: string): boolean {
return /^win(dows)?/i.test(platform);
}
export function normalizeSearchText(value: string): string {
return value.normalize("NFKD").replace(/\p{M}/gu, "").toLowerCase().replace(/\s+/g, " ").trim();
}
export function getLocalFileManagerName(platform: string): string {
if (isMacPlatform(platform)) {
return "Finder";
}
if (isWindowsPlatform(platform)) {
return "File Explorer";
}
return "Files";
}
export function randomUUID(): string {
const bytes = globalThis.crypto.getRandomValues(new Uint8Array(16));
bytes[6] = (bytes[6]! & 0x0f) | 0x40;
bytes[8] = (bytes[8]! & 0x3f) | 0x80;
const hex = Hex.encode(bytes);
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
}
export const newProjectId = (): ProjectId => ProjectId.make(randomUUID());
export const newThreadId = (): ThreadId => ThreadId.make(randomUUID());
export const newDraftId = (): DraftId => DraftId.make(randomUUID());
export const newMessageId = (): MessageId => MessageId.make(randomUUID());