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

ProjectFavicon.tsx

211 lines6.2 KB

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" />;
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 }>;
39}) {
40 const { project } = input;
41 if (project.projectIcon?.kind === "monogram") {
42 return (
43 <ProjectMonogram
44 text={project.projectIcon.text}
45 color={project.projectIcon.color}
46 className={input.className}
47 />
48 );
49 }
50 if (project.projectIcon?.kind === "emoji") {
51 return (
52 <ProjectFaviconFallback
53 className={input.className}
54 icon={FolderCodeIcon}
55 emoji={project.projectIcon.emoji}
56 />
57 );
58 }
59 if (project.projectIcon?.kind === "lucide") {
60 const colorClassName = projectIconColorClassName(project.projectIcon.color);
61 const iconClassName = cn(
62 "inline-flex size-3.5 shrink-0 items-center justify-center",
63 colorClassName,
64 input.className,
65 );
66 return (
67 <span aria-hidden="true" className={iconClassName}>
68 <Suspense fallback={<DynamicProjectIconFallback />}>
69 <LucideProjectIcon
70 name={project.projectIcon.name}
71 className={cn("size-full", colorClassName)}
72 />
73 </Suspense>
74 </span>
75 );
76 }
77 return <AutomaticProjectFavicon {...input} />;
79​
80function AutomaticProjectFavicon(input: Parameters<typeof ProjectFavicon>[0]) {
81 const { project } = input;
82 const target = {
83 environmentId: project.environmentId,
84 cwd: project.workspaceRoot,
85 faviconPath: project.faviconPath,
86 };
87 const src = useAtomValue(projectFaviconUrlAtom(target));
88 const FallbackIcon = input.fallbackIcon ?? FolderCodeIcon;
89​
90 if (!src || isProjectFaviconFallbackUrl(src)) {
91 return (
92 <ProjectFaviconFallback
93 className={input.className}
94 icon={FallbackIcon}
95 projectName={project.title}
96 />
97 );
98 }
99​
100 const cacheKey = getProjectFaviconResourceKey(
101 project.environmentId,
102 project.workspaceRoot,
103 project.faviconPath,
104 );
105​
106 return (
107 <ProjectFaviconImage
108 key={cacheKey}
109 src={src}
110 className={input.className}
111 fallbackIcon={FallbackIcon}
112 fallbackProjectName={project.title}
113 // An icon too large to cache inline loads from a signed URL that expires.
114 onRemoteLoadError={() => refreshProjectFaviconUrl(target)}
115 />
116 );
118​
119function ProjectFaviconFallback({
120 className,
121 icon: Icon,
122 emoji,
123 projectName,
124}: {
125 readonly className?: string | undefined;
126 readonly icon: ComponentType<{ className?: string }>;
127 readonly emoji?: string | undefined;
128 readonly projectName?: string | undefined;
129}) {
130 if (projectName && projectName.trim().length > 0) {
131 const identity = deriveProjectIdentity(projectName);
132 return (
133 <ProjectMonogram text={identity.monogram} color={identity.color} className={className} />
134 );
135 }
136​
137 if (emoji) {
138 return (
139 <span
140 aria-hidden="true"
141 className={cn(
142 "inline-flex size-3.5 shrink-0 items-center justify-center leading-none [container-type:size]",
143 className,
144 )}
145 >
146 <span className="text-[length:80cqh] leading-none">{emoji}</span>
147 </span>
148 );
149 }
150​
151 return <Icon className={cn("size-3.5 shrink-0 text-icon-muted", className)} />;
153​
154function ProjectFaviconImage({
155 src,
156 className,
157 fallbackIcon: FallbackIcon,
158 fallbackProjectName,
159 onRemoteLoadError,
160}: {
161 readonly src: string;
162 readonly className?: string | undefined;
163 readonly fallbackIcon: ComponentType<{ className?: string }>;
164 readonly fallbackProjectName?: string | undefined;
165 readonly onRemoteLoadError: () => void;
166}) {
167 const [displayedSrc, setDisplayedSrc] = useState<string | null>(() =>
168 src.startsWith("data:image/") ? src : null,
169 );
170 // One refresh per failure: a fresh URL for a file that cannot load would fail again.
171 const [refreshedAfterError, setRefreshedAfterError] = useState(false);
172 const isLoading = displayedSrc !== src;
173 const handleLoadError = (failedSrc: string) => {
174 setDisplayedSrc((currentSrc) => (currentSrc === failedSrc ? null : currentSrc));
175 if (failedSrc.startsWith("data:") || refreshedAfterError) return;
176 setRefreshedAfterError(true);
177 onRemoteLoadError();
178 };
179​
180 return (
181 <>
182 {displayedSrc === null ? (
183 <ProjectFaviconFallback
184 className={className}
185 icon={FallbackIcon}
186 projectName={fallbackProjectName}
187 />
188 ) : null}
189 {displayedSrc ? (
190 <img
191 src={displayedSrc}
192 alt=""
193 className={cn("size-3.5 shrink-0 rounded-[25%] object-contain", className)}
194 onError={() => handleLoadError(displayedSrc)}
195 />
196 ) : null}
197 {isLoading ? (
198 <img
199 src={src}
200 alt=""
201 className="hidden"
202 onLoad={() => {
203 setDisplayedSrc(src);
204 setRefreshedAfterError(false);
205 }}
206 onError={() => handleLoadError(src)}
207 />
208 ) : null}
209 </>
210 );