Open History to see the last commit
1import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
2import {
3 getProjectFaviconResourceKey,
4 isProjectFaviconFallbackUrl,
5} from "@t3tools/shared/projectFavicon";
6import { FolderCodeIcon } from "lucide-react";
7import type { ComponentType } from "react";
8import { createElement, lazy, Suspense, useState } from "react";
9import { useAtomValue } from "@effect/atom-react";
10import { projectFaviconUrlAtom, refreshProjectFaviconUrl } from "../state/assets";
11import { deriveProjectIdentity } from "../projectIdentity";
12import { projectIconColorClassName } from "../projectIconColors";
13import { ProjectMonogram } from "./ProjectMonogram";
14import { cn } from "~/lib/utils";
15
16// All lucide icons load as one chunk, only once a project uses a lucide override.
17const LucideProjectIcon = lazy(() =>
18 import("../lib/lucideIconByName").then(({ lucideIconByName }) => ({
19 default: function LucideProjectIcon(props: { name: string; className: string }) {
20 return createElement(lucideIconByName(props.name) ?? FolderCodeIcon, {
21 className: props.className,
22 });
23 },
24 })),
25);
26
27function DynamicProjectIconFallback() {
28 return <FolderCodeIcon className="size-full text-inherit" />;
29}
30
31export type ProjectFaviconProject = Pick<
32 EnvironmentProject,
33 "environmentId" | "workspaceRoot" | "title" | "faviconPath" | "projectIcon"
34>;
35export function ProjectFavicon(input: {
36 project: ProjectFaviconProject;
37 className?: string | undefined;
38 fallbackIcon?: ComponentType<{ className?: string }> | undefined;
39 /** Projects whose icon to show, in order, when this project has none. */
40 fallbackProjects?: ReadonlyArray<ProjectFaviconProject>;
41 /** The monogram shown when none of them has an icon. Defaults to this project's title. */
42 monogramTitle?: string;
43}) {
44 const { project } = input;
45 if (project.projectIcon?.kind === "monogram") {
46 return (
47 <ProjectMonogram
48 text={project.projectIcon.text}
49 color={project.projectIcon.color}
50 className={input.className}
51 />
52 );
53 }
54 if (project.projectIcon?.kind === "emoji") {
55 return (
56 <ProjectFaviconFallback
57 className={input.className}
58 icon={FolderCodeIcon}
59 emoji={project.projectIcon.emoji}
60 />
61 );
62 }
63 if (project.projectIcon?.kind === "lucide") {
64 const colorClassName = projectIconColorClassName(project.projectIcon.color);
65 const iconClassName = cn(
66 "inline-flex size-3.5 shrink-0 items-center justify-center",
67 colorClassName,
68 input.className,
69 );
70 return (
71 <span aria-hidden="true" className={iconClassName}>
72 <Suspense fallback={<DynamicProjectIconFallback />}>
73 <LucideProjectIcon
74 name={project.projectIcon.name}
75 className={cn("size-full", colorClassName)}
76 />
77 </Suspense>
78 </span>
79 );
80 }
81 return <AutomaticProjectFavicon {...input} />;
82}
83
84function AutomaticProjectFavicon(input: Parameters<typeof ProjectFavicon>[0]) {
85 const { project } = input;
86 const target = {
87 environmentId: project.environmentId,
88 cwd: project.workspaceRoot,
89 faviconPath: project.faviconPath,
90 };
91 const src = useAtomValue(projectFaviconUrlAtom(target));
92 const FallbackIcon = input.fallbackIcon ?? FolderCodeIcon;
93 const monogramTitle = input.monogramTitle ?? project.title;
94
95 if (!src || isProjectFaviconFallbackUrl(src)) {
96 const [nextProject, ...laterProjects] = input.fallbackProjects ?? [];
97 if (nextProject) {
98 return (
99 <ProjectFavicon
100 project={nextProject}
101 className={input.className}
102 fallbackIcon={input.fallbackIcon}
103 fallbackProjects={laterProjects}
104 monogramTitle={monogramTitle}
105 />
106 );
107 }
108 return (
109 <ProjectFaviconFallback
110 className={input.className}
111 icon={FallbackIcon}
112 projectName={monogramTitle}
113 />
114 );
115 }
116
117 const cacheKey = getProjectFaviconResourceKey(
118 project.environmentId,
119 project.workspaceRoot,
120 project.faviconPath,
121 );
122
123 return (
124 <ProjectFaviconImage
125 key={cacheKey}
126 src={src}
127 className={input.className}
128 fallbackIcon={FallbackIcon}
129 fallbackProjectName={monogramTitle}
130 // An icon too large to cache inline loads from a signed URL that expires.
131 onRemoteLoadError={() => refreshProjectFaviconUrl(target)}
132 />
133 );
134}
135
136function ProjectFaviconFallback({
137 className,
138 icon: Icon,
139 emoji,
140 projectName,
141}: {
142 readonly className?: string | undefined;
143 readonly icon: ComponentType<{ className?: string }>;
144 readonly emoji?: string | undefined;
145 readonly projectName?: string | undefined;
146}) {
147 if (projectName && projectName.trim().length > 0) {
148 const identity = deriveProjectIdentity(projectName);
149 return (
150 <ProjectMonogram text={identity.monogram} color={identity.color} className={className} />
151 );
152 }
153
154 if (emoji) {
155 return (
156 <span
157 aria-hidden="true"
158 className={cn(
159 "inline-flex size-3.5 shrink-0 items-center justify-center leading-none [container-type:size]",
160 className,
161 )}
162 >
163 <span className="text-[length:80cqh] leading-none">{emoji}</span>
164 </span>
165 );
166 }
167
168 return <Icon className={cn("size-3.5 shrink-0 text-icon-muted", className)} />;
169}
170
171function ProjectFaviconImage({
172 src,
173 className,
174 fallbackIcon: FallbackIcon,
175 fallbackProjectName,
176 onRemoteLoadError,
177}: {
178 readonly src: string;
179 readonly className?: string | undefined;
180 readonly fallbackIcon: ComponentType<{ className?: string }>;
181 readonly fallbackProjectName?: string | undefined;
182 readonly onRemoteLoadError: () => void;
183}) {
184 const [displayedSrc, setDisplayedSrc] = useState<string | null>(() =>
185 src.startsWith("data:image/") ? src : null,
186 );
187 // One refresh per failure: a fresh URL for a file that cannot load would fail again.
188 const [refreshedAfterError, setRefreshedAfterError] = useState(false);
189 const isLoading = displayedSrc !== src;
190 const handleLoadError = (failedSrc: string) => {
191 setDisplayedSrc((currentSrc) => (currentSrc === failedSrc ? null : currentSrc));
192 if (failedSrc.startsWith("data:") || refreshedAfterError) return;
193 setRefreshedAfterError(true);
194 onRemoteLoadError();
195 };
196
197 return (
198 <>
199 {displayedSrc === null ? (
200 <ProjectFaviconFallback
201 className={className}
202 icon={FallbackIcon}
203 projectName={fallbackProjectName}
204 />
205 ) : null}
206 {displayedSrc ? (
207 <img
208 src={displayedSrc}
209 alt=""
210 className={cn("size-3.5 shrink-0 rounded-[25%] object-contain", className)}
211 onError={() => handleLoadError(displayedSrc)}
212 />
213 ) : null}
214 {isLoading ? (
215 // The attribute, not just the class: preflight hides [hidden] with !important, which
216 // unlayered rules such as `.chat-markdown img` cannot override.
217 <img
218 src={src}
219 alt=""
220 hidden
221 className="hidden"
222 onLoad={() => {
223 setDisplayedSrc(src);
224 setRefreshedAfterError(false);
225 }}
226 onError={() => handleLoadError(src)}
227 />
228 ) : null}
229 </>
230 );
231}