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

providerInstanceDisplay.ts

85 lines3.0 KB

1import {
2 defaultInstanceIdForDriver,
3 PROVIDER_DISPLAY_NAMES,
4 type ProviderDriverKind,
5 type ServerProvider,
6} from "@t3tools/contracts";
7​
8function humanizeSlug(slug: string): string {
9 return slug
10 .replace(/([a-z])([A-Z])/g, "$1 $2")
11 .replace(/[_-]+/g, " ")
12 .trim()
13 .replace(/\b\w/g, (char) => char.toUpperCase());
15​
16export function resolveProviderInstanceDisplayName(
17 snapshot: Pick<ServerProvider, "instanceId" | "driver" | "displayName">,
18): string {
19 const trimmedSnapshotName = snapshot.displayName?.trim();
20 const kindLabel = PROVIDER_DISPLAY_NAMES[snapshot.driver] ?? humanizeSlug(snapshot.driver);
21 if (trimmedSnapshotName && trimmedSnapshotName !== kindLabel) return trimmedSnapshotName;
22 if (snapshot.instanceId !== defaultInstanceIdForDriver(snapshot.driver)) {
23 const humanized = humanizeSlug(snapshot.instanceId);
24 if (humanized.length > 0) return humanized;
25 }
26 return trimmedSnapshotName || kindLabel;
28​
29export function providerInstanceInitials(label: string): string {
30 const words = label.replace(/[_-]+/g, " ").split(/\s+/u).filter(Boolean);
31 if (words.length === 0) return "";
32 if (words.length === 1) return Array.from(words[0]!).slice(0, 2).join("").toUpperCase();
33 return words
34 .slice(0, 2)
35 .map((word) => Array.from(word)[0]?.toUpperCase() ?? "")
36 .join("");
38​
39export function normalizeProviderAccentColor(value: string | undefined): string | undefined {
40 const trimmed = value?.trim();
41 if (!trimmed) return undefined;
42 return /^#[0-9a-fA-F]{6}$/u.test(trimmed) ? trimmed : undefined;
44​
45export function providerAccentForegroundColor(accentColor: string) {
46 const linearChannel = (offset: number) => {
47 const value = Number.parseInt(accentColor.slice(offset, offset + 2), 16) / 255;
48 return value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4;
49 };
50 const luminance =
51 0.2126 * linearChannel(1) + 0.7152 * linearChannel(3) + 0.0722 * linearChannel(5);
52 const blackContrast = (luminance + 0.05) / 0.05;
53 const whiteContrast = 1.05 / (luminance + 0.05);
54 return blackContrast >= whiteContrast ? "#000000" : "#ffffff";
56​
57export function shouldShowInstanceBadge(
58 entry: {
59 readonly driverKind: ProviderDriverKind;
60 readonly accentColor?: string | undefined;
61 readonly acpRegistryAgentId?: string | undefined;
62 readonly enabled: boolean;
63 },
64 entries: Iterable<{
65 readonly driverKind: ProviderDriverKind;
66 readonly acpRegistryAgentId?: string | undefined;
67 readonly enabled: boolean;
68 }>,
69): boolean {
70 if (entry.accentColor) return true;
71 let sharedProviderCount = 0;
72 for (const candidate of entries) {
73 if (candidate.driverKind !== entry.driverKind) continue;
74 // Historical threads still need to distinguish accounts that are all disabled.
75 if (entry.enabled && !candidate.enabled) continue;
76 if (
77 entry.driverKind === "acpRegistry" &&
78 candidate.acpRegistryAgentId !== entry.acpRegistryAgentId
79 ) {
80 continue;
81 }
82 if (++sharedProviderCount > 1) return true;
83 }
84 return false;