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

projectIcon.ts

135 lines5.5 KB

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