Open History to see the last commit
1import { CommandId, MessageId, ProjectId, ThreadId } from "@t3tools/contracts";
2import { String, Predicate } from "effect";
3import { type CxOptions, cx } from "class-variance-authority";
4import { twMerge } from "tailwind-merge";
5import * as Random from "effect/Random";
6import * as Effect from "effect/Effect";
7
8export function cn(...inputs: CxOptions) {
9 return twMerge(cx(inputs));
10}
11
12export function isMacPlatform(platform: string): boolean {
13 return /mac|iphone|ipad|ipod/i.test(platform);
14}
15
16export function isWindowsPlatform(platform: string): boolean {
17 return /^win(dows)?/i.test(platform);
18}
19
20export function isLinuxPlatform(platform: string): boolean {
21 return /linux/i.test(platform);
22}
23
24export function randomUUID(): string {
25 if (typeof crypto.randomUUID === "function") {
26 return crypto.randomUUID();
27 }
28 return Effect.runSync(Random.nextUUIDv4);
29}
30
31export const newCommandId = (): CommandId => CommandId.makeUnsafe(randomUUID());
32
33export const newProjectId = (): ProjectId => ProjectId.makeUnsafe(randomUUID());
34
35export const newThreadId = (): ThreadId => ThreadId.makeUnsafe(randomUUID());
36
37export const newMessageId = (): MessageId => MessageId.makeUnsafe(randomUUID());
38
39const isNonEmptyString = Predicate.compose(Predicate.isString, String.isNonEmpty);
40const firstNonEmptyString = (...values: unknown[]): string => {
41 for (const value of values) {
42 if (isNonEmptyString(value)) {
43 return value;
44 }
45 }
46 throw new Error("No non-empty string provided");
47};
48
49export const resolveServerUrl = (options?: {
50 url?: string | undefined;
51 protocol?: "http" | "https" | "ws" | "wss" | undefined;
52 pathname?: string | undefined;
53 searchParams?: Record<string, string> | undefined;
54}): string => {
55 const rawUrl = firstNonEmptyString(
56 options?.url,
57 window.desktopBridge?.getWsUrl(),
58 import.meta.env.VITE_WS_URL,
59 window.location.origin,
60 );
61
62 const parsedUrl = new URL(rawUrl);
63 if (options?.protocol) {
64 parsedUrl.protocol = options.protocol;
65 }
66 if (options?.pathname) {
67 parsedUrl.pathname = options.pathname;
68 } else {
69 parsedUrl.pathname = "/";
70 }
71 if (options?.searchParams) {
72 parsedUrl.search = new URLSearchParams(options.searchParams).toString();
73 }
74 return parsedUrl.toString();
75};