Skip to content
Source
Sign inSign up

utils.ts

55 lines1.8 KB

1import { MessageId, ProjectId, ThreadId } from "@t3tools/contracts";
2import { isMacPlatform } from "@t3tools/shared/keybindings";
3import { type CxOptions, cx } from "class-variance-authority";
4import * as Hex from "effect/encoding/Hex";
5import { extendTailwindMerge } from "tailwind-merge";
6import { DraftId } from "../composerDraftStore";
7​
8// Register extra font sizes and background images so tailwind-merge does not
9// treat text-2xs or bg-composer-seam-below as colors.
10const twMerge = extendTailwindMerge({
11 extend: {
12 theme: { text: ["2xs", "3xs", "4xs", "5xs"] },
13 classGroups: { "bg-image": [{ bg: ["composer-seam-below", "composer-seam-above"] }] },
14 },
15});
16​
17export function cn(...inputs: CxOptions) {
18 return twMerge(cx(inputs));
20​
21export { isMacPlatform };
22​
23export function isWindowsPlatform(platform: string): boolean {
24 return /^win(dows)?/i.test(platform);
26​
27export function normalizeSearchText(value: string): string {
28 return value.normalize("NFKD").replace(/\p{M}/gu, "").toLowerCase().replace(/\s+/g, " ").trim();
30​
31export function getLocalFileManagerName(platform: string): string {
32 if (isMacPlatform(platform)) {
33 return "Finder";
34 }
35 if (isWindowsPlatform(platform)) {
36 return "File Explorer";
37 }
38 return "Files";
40​
41export function randomUUID(): string {
42 const bytes = globalThis.crypto.getRandomValues(new Uint8Array(16));
43 bytes[6] = (bytes[6]! & 0x0f) | 0x40;
44 bytes[8] = (bytes[8]! & 0x3f) | 0x80;
45 const hex = Hex.encode(bytes);
46 return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
48​
49export const newProjectId = (): ProjectId => ProjectId.make(randomUUID());
50​
51export const newThreadId = (): ThreadId => ThreadId.make(randomUUID());
52​
53export const newDraftId = (): DraftId => DraftId.make(randomUUID());
54​
55export const newMessageId = (): MessageId => MessageId.make(randomUUID());