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";
4import { projectMonogram } from "@t3tools/shared/projectMonogram";
5import type { AppSymbolName } from "../components/ui/AppSymbol";
6
7export type ProjectIconGlyph =
8 | { readonly kind: "emoji"; readonly emoji: string }
9 | { readonly kind: "monogram"; readonly text: string; readonly color: ProjectIconColor };
10
11/** Hermes lacks Intl.Segmenter; fold combining marks into base characters instead. */
12export function countGlyphs(text: string): number {
13 return Array.from(text.replace(/\p{M}/gu, "")).length;
14}
15
16function projectMonogramColor(projectName: string): ProjectIconColor {
17 const seed = projectName.normalize("NFKC").trim().toLocaleLowerCase("en-US") || "project";
18 let index = 0;
19 for (const glyph of seed) {
20 index = (index * 31 + (glyph.codePointAt(0) ?? 0)) % ProjectIconColor.literals.length;
21 }
22 return ProjectIconColor.literals[index] ?? "blue";
23}
24
25export function projectAutoMonogram(projectTitle: string): ProjectIconGlyph | null {
26 if (!projectTitle.trim()) return null;
27 return {
28 kind: "monogram",
29 text: projectMonogram(projectTitle),
30 color: projectMonogramColor(projectTitle),
31 };
32}
33
34export function resolveProjectIconGlyph(
35 projectIcon: ProjectIconOverride | null | undefined,
36 projectTitle: string,
37): ProjectIconGlyph | null {
38 switch (projectIcon?.kind) {
39 case "emoji":
40 return { kind: "emoji", emoji: projectIcon.emoji };
41 case "monogram":
42 return { kind: "monogram", text: projectIcon.text, color: projectIcon.color };
43 case "lucide":
44 return { kind: "monogram", text: projectMonogram(projectTitle), color: projectIcon.color };
45 case undefined:
46 return null;
47 }
48}
49
50const SYMBOL_BY_LUCIDE_NAME: Partial<Record<string, AppSymbolName>> = {
51 "message-square-dashed": "text.bubble",
52};
53
54export function resolveProjectIconSymbol(
55 projectIcon: ProjectIconOverride | null | undefined,
56): { readonly name: AppSymbolName; readonly color: ProjectIconColor } | null {
57 if (projectIcon?.kind !== "lucide") return null;
58 const name = SYMBOL_BY_LUCIDE_NAME[projectIcon.name];
59 return name ? { name, color: projectIcon.color } : null;
60}
61
62export type ProjectChipIcon = { readonly kind: "favicon"; readonly uri: string } | ProjectIconGlyph;
63
64export function resolveProjectChipIcon(
65 project: {
66 readonly title: string;
67 readonly workspaceRoot: string;
68 readonly projectIcon?: ProjectIconOverride | null;
69 } | null,
70 scratchWorkspaceRoot: string | null | undefined,
71 faviconUrl: () => string | null,
72): ProjectChipIcon | null {
73 if (!project || isScratchProject(project, scratchWorkspaceRoot)) return null;
74 const glyph = resolveProjectIconGlyph(project.projectIcon, project.title);
75 if (glyph) return glyph;
76 const url = faviconUrl();
77 if (url && !isProjectFaviconFallbackUrl(url)) return { kind: "favicon", uri: url };
78 return projectAutoMonogram(project.title);
79}
80
81const PROJECT_ICON_COLOR_CLASSES: Record<
82 ProjectIconColor,
83 { readonly text: string; readonly tint: string; readonly background: string }
84> = {
85 gray: { text: "text-gray-500", tint: "accent-gray-500", background: "bg-gray-500/15" },
86 red: { text: "text-red-500", tint: "accent-red-500", background: "bg-red-500/15" },
87 orange: { text: "text-orange-500", tint: "accent-orange-500", background: "bg-orange-500/15" },
88 amber: { text: "text-amber-500", tint: "accent-amber-500", background: "bg-amber-500/15" },
89 yellow: { text: "text-yellow-500", tint: "accent-yellow-500", background: "bg-yellow-500/15" },
90 lime: { text: "text-lime-500", tint: "accent-lime-500", background: "bg-lime-500/15" },
91 green: { text: "text-green-500", tint: "accent-green-500", background: "bg-green-500/15" },
92 emerald: {
93 text: "text-emerald-500",
94 tint: "accent-emerald-500",
95 background: "bg-emerald-500/15",
96 },
97 teal: { text: "text-teal-500", tint: "accent-teal-500", background: "bg-teal-500/15" },
98 cyan: { text: "text-cyan-500", tint: "accent-cyan-500", background: "bg-cyan-500/15" },
99 sky: { text: "text-sky-500", tint: "accent-sky-500", background: "bg-sky-500/15" },
100 blue: { text: "text-blue-500", tint: "accent-blue-500", background: "bg-blue-500/15" },
101 indigo: { text: "text-indigo-500", tint: "accent-indigo-500", background: "bg-indigo-500/15" },
102 violet: { text: "text-violet-500", tint: "accent-violet-500", background: "bg-violet-500/15" },
103 purple: { text: "text-purple-500", tint: "accent-purple-500", background: "bg-purple-500/15" },
104 fuchsia: {
105 text: "text-fuchsia-500",
106 tint: "accent-fuchsia-500",
107 background: "bg-fuchsia-500/15",
108 },
109 pink: { text: "text-pink-500", tint: "accent-pink-500", background: "bg-pink-500/15" },
110 rose: { text: "text-rose-500", tint: "accent-rose-500", background: "bg-rose-500/15" },
111};
112
113export function projectIconColorClassNames(color: ProjectIconColor) {
114 return PROJECT_ICON_COLOR_CLASSES[color];
115}