Open History to see the last commit
1import type { ProjectIconColor, ProjectIconOverride } from "@t3tools/contracts";
2
3export type ProjectIconGlyph =
4 | { readonly kind: "emoji"; readonly emoji: string }
5 | { readonly kind: "monogram"; readonly text: string; readonly color: ProjectIconColor };
6
7/**
8 * Hermes has no Intl.Segmenter, so combining marks are folded into their base
9 * character instead of full grapheme segmentation.
10 */
11export function countGlyphs(text: string): number {
12 return Array.from(text.replace(/\p{M}/gu, "")).length;
13}
14
15export function projectMonogram(projectName: string): string {
16 const words =
17 projectName
18 .normalize("NFKC")
19 .trim()
20 .match(/[\p{L}\p{N}]+/gu) ?? [];
21 const firstWord = words[0];
22 if (!firstWord) return "PR";
23
24 const glyphs = Array.from(firstWord);
25 const first = glyphs[0] ?? "P";
26 const second =
27 glyphs.slice(1).find((glyph) => /\p{N}/u.test(glyph)) ??
28 (words.length > 1 ? Array.from(words.at(-1) ?? "")[0] : glyphs.at(-1)) ??
29 first;
30 return Array.from(`${first}${second}`.toUpperCase()).slice(0, 2).join("");
31}
32
33export function resolveProjectIconGlyph(
34 projectIcon: ProjectIconOverride | null | undefined,
35 projectTitle: string,
36): ProjectIconGlyph | null {
37 switch (projectIcon?.kind) {
38 case "emoji":
39 return { kind: "emoji", emoji: projectIcon.emoji };
40 case "monogram":
41 return { kind: "monogram", text: projectIcon.text, color: projectIcon.color };
42 case "lucide":
43 return { kind: "monogram", text: projectMonogram(projectTitle), color: projectIcon.color };
44 case undefined:
45 return null;
46 }
47}
48
49const PROJECT_ICON_COLOR_CLASSES: Record<
50 ProjectIconColor,
51 { readonly text: string; readonly background: string }
52> = {
53 gray: { text: "text-gray-500", background: "bg-gray-500/15" },
54 red: { text: "text-red-500", background: "bg-red-500/15" },
55 orange: { text: "text-orange-500", background: "bg-orange-500/15" },
56 amber: { text: "text-amber-500", background: "bg-amber-500/15" },
57 yellow: { text: "text-yellow-500", background: "bg-yellow-500/15" },
58 lime: { text: "text-lime-500", background: "bg-lime-500/15" },
59 green: { text: "text-green-500", background: "bg-green-500/15" },
60 emerald: { text: "text-emerald-500", background: "bg-emerald-500/15" },
61 teal: { text: "text-teal-500", background: "bg-teal-500/15" },
62 cyan: { text: "text-cyan-500", background: "bg-cyan-500/15" },
63 sky: { text: "text-sky-500", background: "bg-sky-500/15" },
64 blue: { text: "text-blue-500", background: "bg-blue-500/15" },
65 indigo: { text: "text-indigo-500", background: "bg-indigo-500/15" },
66 violet: { text: "text-violet-500", background: "bg-violet-500/15" },
67 purple: { text: "text-purple-500", background: "bg-purple-500/15" },
68 fuchsia: { text: "text-fuchsia-500", background: "bg-fuchsia-500/15" },
69 pink: { text: "text-pink-500", background: "bg-pink-500/15" },
70 rose: { text: "text-rose-500", background: "bg-rose-500/15" },
71};
72
73export function projectIconColorClassNames(color: ProjectIconColor) {
74 return PROJECT_ICON_COLOR_CLASSES[color];
75}