Open History to see the last commit
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>;
35}
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 PROJECT_ICON_EMOJIS: Record<ProjectIconName, string> = {
135 ai: "🤖",
136 book: "📚",
137 braces: "🧩",
138 circuit: "⚡",
139 cloud: "☁️",
140 code: "💻",
141 database: "🗄️",
142 desktop: "🖥️",
143 "folder-code": "🛠️",
144 game: "🎮",
145 image: "🖼️",
146 layers: "✨",
147 mobile: "📱",
148 music: "🎵",
149 package: "📦",
150 security: "🔒",
151 server: "⚙️",
152 shopping: "🛍️",
153 terminal: "⌨️",
154 test: "🧪",
155 video: "🎬",
156 web: "🌐",
157};
158
159const projectIconCache = new Map<string, ProjectIconSelection>();
160
161function projectNameTokens(value: string): ReadonlyArray<string> {
162 return value
163 .replace(/([a-z\d])([A-Z])/g, "$1 $2")
164 .toLowerCase()
165 .split(/[^a-z\d]+/)
166 .filter(Boolean);
167}
168
169function termScore(token: string, term: string): number {
170 if (token === term) return 3;
171 if (term.length >= 4 && (token.startsWith(term) || token.endsWith(term))) return 1;
172 return 0;
173}
174
175function stableIndex(value: string, length: number): number {
176 let hash = 2_166_136_261;
177 for (let index = 0; index < value.length; index += 1) {
178 hash ^= value.charCodeAt(index);
179 hash = Math.imul(hash, 16_777_619);
180 }
181 return (hash >>> 0) % length;
182}
183
184export function selectProjectIcon(
185 projectName: string,
186 workspaceRoot: string,
187): ProjectIconSelection {
188 const name = projectName.trim() || workspaceRoot.split(/[\\/]+/).findLast(Boolean) || "project";
189 const cacheKey = name.toLowerCase();
190 const cachedIcon = projectIconCache.get(cacheKey);
191 if (cachedIcon) return cachedIcon;
192
193 const tokens = projectNameTokens(name);
194 let bestIcon: ProjectIconName | null = null;
195 let bestScore = 0;
196
197 for (const projectClass of PROJECT_ICON_MODEL) {
198 let score = 0;
199 for (const token of tokens) {
200 let tokenScore = 0;
201 for (const term of projectClass.terms) {
202 tokenScore = Math.max(tokenScore, termScore(token, term));
203 }
204 score += tokenScore;
205 }
206 if (score > bestScore) {
207 bestIcon = projectClass.icon;
208 bestScore = score;
209 }
210 }
211
212 const iconName =
213 bestIcon ?? GENERIC_PROJECT_ICONS[stableIndex(cacheKey, GENERIC_PROJECT_ICONS.length)]!;
214 const icon: ProjectIconSelection = {
215 kind: "emoji",
216 icon: iconName,
217 emoji: PROJECT_ICON_EMOJIS[iconName],
218 };
219 projectIconCache.set(cacheKey, icon);
220 return icon;
221}