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

projectIconModel.ts

192 lines4.5 KB

1export const PROJECT_ICON_NAMES = [
2 "ai",
3 "book",
4 "cloud",
5 "database",
6 "desktop",
7 "game",
8 "image",
9 "mobile",
10 "music",
11 "package",
12 "security",
13 "server",
14 "shopping",
15 "terminal",
16 "test",
17 "video",
18 "web",
19 "code",
20 "braces",
21 "circuit",
22 "folder-code",
23 "layers",
24] as const;
25​
26export type ProjectIconName = (typeof PROJECT_ICON_NAMES)[number];
27​
28export type ProjectIconSelection =
29 | { readonly kind: "lucide"; readonly icon: ProjectIconName }
30 | { readonly kind: "emoji"; readonly icon: ProjectIconName; readonly emoji: string };
31​
32interface ProjectIconClass {
33 readonly icon: ProjectIconName;
34 readonly terms: ReadonlyArray<string>;
36​
37const PROJECT_ICON_MODEL: ReadonlyArray<ProjectIconClass> = [
38 { icon: "ai", terms: ["ai", "agent", "bot", "gpt", "llm", "ml", "model", "neural"] },
39 {
40 icon: "mobile",
41 terms: ["android", "expo", "ios", "mobile", "native", "reactnative", "swift"],
42 },
43 {
44 icon: "desktop",
45 terms: ["desktop", "electron", "linux", "mac", "macos", "tauri", "windows"],
46 },
47 {
48 icon: "book",
49 terms: ["book", "docs", "documentation", "guide", "handbook", "manual", "wiki"],
50 },
51 {
52 icon: "security",
53 terms: ["auth", "identity", "oauth", "security", "sso", "vault"],
54 },
55 {
56 icon: "database",
57 terms: [
58 "analytics",
59 "data",
60 "database",
61 "db",
62 "mongo",
63 "mysql",
64 "postgres",
65 "redis",
66 "sql",
67 "storage",
68 ],
69 },
70 {
71 icon: "cloud",
72 terms: [
73 "aws",
74 "azure",
75 "cloud",
76 "deploy",
77 "devops",
78 "docker",
79 "gcp",
80 "infra",
81 "kubernetes",
82 "terraform",
83 ],
84 },
85 {
86 icon: "server",
87 terms: ["api", "backend", "gateway", "server", "service", "worker"],
88 },
89 {
90 icon: "terminal",
91 terms: ["automation", "bash", "cli", "command", "script", "shell", "terminal"],
92 },
93 {
94 icon: "package",
95 terms: ["component", "kit", "lib", "library", "package", "plugin", "sdk", "toolkit"],
96 },
97 {
98 icon: "test",
99 terms: ["benchmark", "e2e", "fixture", "spec", "test", "testing"],
100 },
101 {
102 icon: "shopping",
103 terms: ["cart", "commerce", "market", "shop", "store"],
104 },
105 { icon: "game", terms: ["game", "gaming", "play"] },
106 { icon: "music", terms: ["audio", "music", "podcast", "radio", "sound"] },
107 { icon: "video", terms: ["film", "movie", "stream", "video"] },
108 { icon: "image", terms: ["camera", "gallery", "image", "photo", "picture"] },
109 {
110 icon: "web",
111 terms: [
112 "browser",
113 "frontend",
114 "nextjs",
115 "react",
116 "site",
117 "svelte",
118 "ui",
119 "vue",
120 "web",
121 "website",
122 ],
123 },
124];
125​
126const GENERIC_PROJECT_ICONS: ReadonlyArray<ProjectIconName> = [
127 "code",
128 "braces",
129 "circuit",
130 "folder-code",
131 "layers",
132];
133​
134const projectIconCache = new Map<string, ProjectIconSelection>();
135​
136function projectNameTokens(value: string): ReadonlyArray<string> {
137 return value
138 .replace(/([a-z\d])([A-Z])/g, "$1 $2")
139 .toLowerCase()
140 .split(/[^a-z\d]+/)
141 .filter(Boolean);
143​
144function termScore(token: string, term: string): number {
145 if (token === term) return 3;
146 if (term.length >= 4 && (token.startsWith(term) || token.endsWith(term))) return 1;
147 return 0;
149​
150function stableIndex(value: string, length: number): number {
151 let hash = 2_166_136_261;
152 for (let index = 0; index < value.length; index += 1) {
153 hash ^= value.charCodeAt(index);
154 hash = Math.imul(hash, 16_777_619);
155 }
156 return (hash >>> 0) % length;
158​
159export function selectProjectIcon(
160 projectName: string,
161 workspaceRoot: string,
162): ProjectIconSelection {
163 const name = projectName.trim() || workspaceRoot.split(/[\\/]+/).findLast(Boolean) || "project";
164 const cacheKey = name.toLowerCase();
165 const cachedIcon = projectIconCache.get(cacheKey);
166 if (cachedIcon) return cachedIcon;
167​
168 const tokens = projectNameTokens(name);
169 let bestIcon: ProjectIconName | null = null;
170 let bestScore = 0;
171​
172 for (const projectClass of PROJECT_ICON_MODEL) {
173 let score = 0;
174 for (const token of tokens) {
175 let tokenScore = 0;
176 for (const term of projectClass.terms) {
177 tokenScore = Math.max(tokenScore, termScore(token, term));
178 }
179 score += tokenScore;
180 }
181 if (score > bestScore) {
182 bestIcon = projectClass.icon;
183 bestScore = score;
184 }
185 }
186​
187 const iconName =
188 bestIcon ?? GENERIC_PROJECT_ICONS[stableIndex(cacheKey, GENERIC_PROJECT_ICONS.length)]!;
189 const icon: ProjectIconSelection = { kind: "lucide", icon: iconName };
190 projectIconCache.set(cacheKey, icon);
191 return icon;