|
| 1 | +import type { ProjectIconColor, ProjectIconOverride } from "@t3tools/contracts"; |
| 2 | + |
| 3 | +export type ProjectIconGlyph = |
| 4 | + | { readonly kind: "emoji"; readonly emoji: string } |
| 5 | + | { readonly kind: "monogram"; readonly text: string; readonly color: ProjectIconColor }; |
| 6 | + |
| 7 | +/** Code point count; Hermes has no Intl.Segmenter, and monogram text is at most two glyphs. */ |
| 8 | +export function countGlyphs(text: string): number { |
| 9 | + return Array.from(text).length; |
| 10 | +} |
| 11 | + |
| 12 | +/** Mirrors the automatic monogram web derives from a project name when it has no favicon. */ |
| 13 | +export function projectMonogram(projectName: string): string { |
| 14 | + const words = |
| 15 | + projectName |
| 16 | + .normalize("NFKC") |
| 17 | + .trim() |
| 18 | + .match(/[\p{L}\p{N}]+/gu) ?? []; |
| 19 | + const firstWord = words[0]; |
| 20 | + if (!firstWord) return "PR"; |
| 21 | + |
| 22 | + const glyphs = Array.from(firstWord); |
| 23 | + const first = glyphs[0] ?? "P"; |
| 24 | + const second = |
| 25 | + glyphs.slice(1).find((glyph) => /\p{N}/u.test(glyph)) ?? |
| 26 | + (words.length > 1 ? Array.from(words.at(-1) ?? "")[0] : glyphs.at(-1)) ?? |
| 27 | + first; |
| 28 | + return Array.from(`${first}${second}`.toUpperCase()).slice(0, 2).join(""); |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Picks what mobile draws for an assigned project icon. Mobile does not bundle |
| 33 | + * the Lucide set, so a Lucide override keeps its color and falls back to the |
| 34 | + * project's monogram instead of the folder glyph. |
| 35 | + */ |
| 36 | +export function resolveProjectIconGlyph( |
| 37 | + projectIcon: ProjectIconOverride | null | undefined, |
| 38 | + projectTitle: string, |
| 39 | +): ProjectIconGlyph | null { |
| 40 | + switch (projectIcon?.kind) { |
| 41 | + case "emoji": |
| 42 | + return { kind: "emoji", emoji: projectIcon.emoji }; |
| 43 | + case "monogram": |
| 44 | + return { kind: "monogram", text: projectIcon.text, color: projectIcon.color }; |
| 45 | + case "lucide": |
| 46 | + return { kind: "monogram", text: projectMonogram(projectTitle), color: projectIcon.color }; |
| 47 | + case undefined: |
| 48 | + return null; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +const PROJECT_ICON_COLOR_CLASSES: Record< |
| 53 | + ProjectIconColor, |
| 54 | + { readonly text: string; readonly background: string } |
| 55 | +> = { |
| 56 | + gray: { text: "text-gray-500", background: "bg-gray-500/15" }, |
| 57 | + red: { text: "text-red-500", background: "bg-red-500/15" }, |
| 58 | + orange: { text: "text-orange-500", background: "bg-orange-500/15" }, |
| 59 | + amber: { text: "text-amber-500", background: "bg-amber-500/15" }, |
| 60 | + yellow: { text: "text-yellow-500", background: "bg-yellow-500/15" }, |
| 61 | + lime: { text: "text-lime-500", background: "bg-lime-500/15" }, |
| 62 | + green: { text: "text-green-500", background: "bg-green-500/15" }, |
| 63 | + emerald: { text: "text-emerald-500", background: "bg-emerald-500/15" }, |
| 64 | + teal: { text: "text-teal-500", background: "bg-teal-500/15" }, |
| 65 | + cyan: { text: "text-cyan-500", background: "bg-cyan-500/15" }, |
| 66 | + sky: { text: "text-sky-500", background: "bg-sky-500/15" }, |
| 67 | + blue: { text: "text-blue-500", background: "bg-blue-500/15" }, |
| 68 | + indigo: { text: "text-indigo-500", background: "bg-indigo-500/15" }, |
| 69 | + violet: { text: "text-violet-500", background: "bg-violet-500/15" }, |
| 70 | + purple: { text: "text-purple-500", background: "bg-purple-500/15" }, |
| 71 | + fuchsia: { text: "text-fuchsia-500", background: "bg-fuchsia-500/15" }, |
| 72 | + pink: { text: "text-pink-500", background: "bg-pink-500/15" }, |
| 73 | + rose: { text: "text-rose-500", background: "bg-rose-500/15" }, |
| 74 | +}; |
| 75 | + |
| 76 | +export function projectIconColorClassNames(color: ProjectIconColor) { |
| 77 | + return PROJECT_ICON_COLOR_CLASSES[color]; |
| 78 | +} |
0 commit comments