Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

utils.ts

55 lines1.9 KB

1import { MessageId, ProjectId, ThreadId } from "@t3tools/contracts";
2import { type CxOptions, cx } from "class-variance-authority";
3import * as Encoding from "effect/Encoding";
4import { extendTailwindMerge } from "tailwind-merge";
5import { DraftId } from "../composerDraftStore";
6​
7// The theme's extra font sizes (index.css). Unregistered, tailwind-merge reads
8// text-2xs as a colour and drops it next to text-muted-foreground.
9const twMerge = extendTailwindMerge({ extend: { theme: { text: ["2xs", "3xs", "4xs", "5xs"] } } });
10​
11export function cn(...inputs: CxOptions) {
12 return twMerge(cx(inputs));
14​
15export function isMacPlatform(platform: string): boolean {
16 return /mac|iphone|ipad|ipod/i.test(platform);
18​
19export function isWindowsPlatform(platform: string): boolean {
20 return /^win(dows)?/i.test(platform);
22​
23export function normalizeSearchText(value: string): string {
24 return value.normalize("NFKD").replace(/\p{M}/gu, "").toLowerCase().replace(/\s+/g, " ").trim();
26​
27export function getLocalFileManagerName(platform: string): string {
28 if (isMacPlatform(platform)) {
29 return "Finder";
30 }
31 if (isWindowsPlatform(platform)) {
32 return "File Explorer";
33 }
34 return "Files";
36​
37export function randomHex(byteLength: number): string {
38 return Encoding.encodeHex(globalThis.crypto.getRandomValues(new Uint8Array(byteLength)));
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 = Encoding.encodeHex(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());