Open History to see the last commit
1import { isScratchProject } from "@t3tools/client-runtime/state/projects";
2import { ProjectIconColor, type ProjectIconOverride } from "@t3tools/contracts";
3import { isProjectFaviconFallbackUrl } from "@t3tools/shared/projectFavicon";
4
5export type ProjectIconGlyph =
6 | { readonly kind: "emoji"; readonly emoji: string }
7 | { readonly kind: "monogram"; readonly text: string; readonly color: ProjectIconColor };
8
9/**
10 * Hermes has no Intl.Segmenter, so combining marks are folded into their base
11 * character instead of full grapheme segmentation.
12 */
13export function countGlyphs(text: string): number {
14 return Array.from(text.replace(/\p{M}/gu, "")).length;
15}
16
17export function projectMonogram(projectName: string): string {
18 const words =
19 projectName
20 .normalize("NFKC")
21 .trim()
22 .match(/[\p{L}\p{N}]+/gu) ?? [];
23 const firstWord = words[0];
24 if (!firstWord) return "PR";
25
26 const glyphs = Array.from(firstWord);
27 const first = glyphs[0] ?? "P";
28 const second =
29 glyphs.slice(1).find((glyph) => /\p{N}/u.test(glyph)) ??
30 (words.length > 1 ? Array.from(words.at(-1) ?? "")[0] : glyphs.at(-1)) ??
31 first;
32 return Array.from(`${first}${second}`.toUpperCase()).slice(0, 2).join("");
33}
34
35export function projectMonogramColor(projectName: string): ProjectIconColor {
36 const seed = projectName.normalize("NFKC").trim().toLocaleLowerCase("en-US") || "project";
37 let index = 0;
38 for (const glyph of seed) {
39 index = (index * 31 + (glyph.codePointAt(0) ?? 0)) % ProjectIconColor.literals.length;
40 }
41 return ProjectIconColor.literals[index] ?? "blue";
42}
43
44export function projectAutoMonogram(projectTitle: string): ProjectIconGlyph | null {
45 if (!projectTitle.trim()) return null;
46 return {
47 kind: "monogram",
48 text: projectMonogram(projectTitle),
49 color: projectMonogramColor(projectTitle),
50 };
51}
52
53export function resolveProjectIconGlyph(
54 projectIcon: ProjectIconOverride | null | undefined,
55 projectTitle: string,
56): ProjectIconGlyph | null {
57 switch (projectIcon?.kind) {
58 case "emoji":
59 return { kind: "emoji", emoji: projectIcon.emoji };
60 case "monogram":
61 return { kind: "monogram", text: projectIcon.text, color: projectIcon.color };
62 case "lucide":
63 return { kind: "monogram", text: projectMonogram(projectTitle), color: projectIcon.color };
64 case undefined:
65 return null;
66 }
67}
68
69export type ProjectChipIcon = { readonly kind: "favicon"; readonly uri: string } | ProjectIconGlyph;
70
71export function resolveProjectChipIcon(
72 project: {
73 readonly title: string;
74 readonly workspaceRoot: string;
75 readonly projectIcon?: ProjectIconOverride | null;
76 } | null,
77 scratchWorkspaceRoot: string | null | undefined,
78 faviconUrl: () => string | null,
79): ProjectChipIcon | null {
80 if (!project || isScratchProject(project, scratchWorkspaceRoot)) return null;
81 const glyph = resolveProjectIconGlyph(project.projectIcon, project.title);
82 if (glyph) return glyph;
83 const url = faviconUrl();
84 if (url && !isProjectFaviconFallbackUrl(url)) return { kind: "favicon", uri: url };
85 return projectAutoMonogram(project.title);
86}
87
88const PROJECT_ICON_COLOR_CLASSES: Record<
89 ProjectIconColor,
90 { readonly text: string; readonly background: string }
91> = {
92 gray: { text: "text-gray-500", background: "bg-gray-500/15" },
93 red: { text: "text-red-500", background: "bg-red-500/15" },
94 orange: { text: "text-orange-500", background: "bg-orange-500/15" },
95 amber: { text: "text-amber-500", background: "bg-amber-500/15" },
96 yellow: { text: "text-yellow-500", background: "bg-yellow-500/15" },
97 lime: { text: "text-lime-500", background: "bg-lime-500/15" },
98 green: { text: "text-green-500", background: "bg-green-500/15" },
99 emerald: { text: "text-emerald-500", background: "bg-emerald-500/15" },
100 teal: { text: "text-teal-500", background: "bg-teal-500/15" },
101 cyan: { text: "text-cyan-500", background: "bg-cyan-500/15" },
102 sky: { text: "text-sky-500", background: "bg-sky-500/15" },
103 blue: { text: "text-blue-500", background: "bg-blue-500/15" },
104 indigo: { text: "text-indigo-500", background: "bg-indigo-500/15" },
105 violet: { text: "text-violet-500", background: "bg-violet-500/15" },
106 purple: { text: "text-purple-500", background: "bg-purple-500/15" },
107 fuchsia: { text: "text-fuchsia-500", background: "bg-fuchsia-500/15" },
108 pink: { text: "text-pink-500", background: "bg-pink-500/15" },
109 rose: { text: "text-rose-500", background: "bg-rose-500/15" },
110};
111
112export function projectIconColorClassNames(color: ProjectIconColor) {
113 return PROJECT_ICON_COLOR_CLASSES[color];
114}