Open History to see the last commit
1import { SymbolView } from "./AppSymbol";
2import { AppText } from "./AppText";
3import { Image } from "expo-image";
4import { useLayoutEffect, useMemo, useState } from "react";
5import { View } from "react-native";
6import type { EnvironmentId, ProjectIconOverride } from "@t3tools/contracts";
7import {
8 getProjectFaviconCacheKey,
9 getProjectFaviconResourceKey,
10 isProjectFaviconFallbackUrl,
11} from "@t3tools/shared/projectFavicon";
12import { useAtomValue } from "@effect/atom-react";
13import { Atom } from "effect/unstable/reactivity";
14import { projectFaviconUrlAtom } from "../state/assets";
15import {
16 countGlyphs,
17 projectIconColorClassNames,
18 resolveProjectIconGlyph,
19 type ProjectIconGlyph,
20} from "../lib/projectIcon";
21
22import {
23 beginProjectFaviconRequest,
24 createProjectFaviconRequest,
25 hasLoadedProjectFavicon,
26 markProjectFaviconFailed,
27 markProjectFaviconLoaded,
28} from "../lib/projectFaviconRequests";
29
30const EMPTY_FAVICON_URL = Atom.make<string | null>(null);
31
32/* ─── Component ──────────────────────────────────────────────────────── */
33export function ProjectFavicon(props: {
34 readonly environmentId: EnvironmentId;
35 readonly open?: boolean;
36 readonly size?: number;
37 readonly projectTitle: string;
38 readonly workspaceRoot?: string | null;
39 readonly faviconPath?: string | null;
40 readonly projectIcon?: ProjectIconOverride | null;
41}) {
42 const size = props.size ?? 42;
43 const glyph = resolveProjectIconGlyph(props.projectIcon, props.projectTitle);
44 const faviconUrl = useAtomValue(
45 props.workspaceRoot == null || glyph !== null
46 ? EMPTY_FAVICON_URL
47 : projectFaviconUrlAtom({
48 environmentId: props.environmentId,
49 cwd: props.workspaceRoot,
50 faviconPath: props.faviconPath,
51 }),
52 );
53 const renderableFaviconUrl = isProjectFaviconFallbackUrl(faviconUrl) ? null : faviconUrl;
54 // Inline images are self-contained; remote URLs key on their revision so signed-token
55 // rotation reuses the disk cache while a changed icon starts from the loading state.
56 const cacheKey =
57 renderableFaviconUrl && props.workspaceRoot
58 ? renderableFaviconUrl.startsWith("data:")
59 ? getProjectFaviconResourceKey(props.environmentId, props.workspaceRoot, props.faviconPath)
60 : getProjectFaviconCacheKey(props.environmentId, props.workspaceRoot, renderableFaviconUrl)
61 : null;
62
63 if (glyph !== null) {
64 return <ProjectIconGlyphView glyph={glyph} size={size} />;
65 }
66
67 return (
68 <ProjectFaviconImage
69 key={cacheKey}
70 cacheKey={cacheKey}
71 faviconUrl={renderableFaviconUrl}
72 open={props.open}
73 projectTitle={props.projectTitle}
74 size={size}
75 />
76 );
77}
78
79function ProjectIconGlyphView(props: { readonly glyph: ProjectIconGlyph; readonly size: number }) {
80 const { glyph, size } = props;
81 if (glyph.kind === "emoji") {
82 return (
83 <View style={{ width: size, height: size, alignItems: "center", justifyContent: "center" }}>
84 <AppText
85 allowFontScaling={false}
86 style={{
87 fontSize: size * 0.8,
88 lineHeight: size,
89 textAlign: "center",
90 includeFontPadding: false,
91 }}
92 >
93 {glyph.emoji}
94 </AppText>
95 </View>
96 );
97 }
98
99 const colors = projectIconColorClassNames(glyph.color);
100 return (
101 <View
102 className={colors.background}
103 style={{
104 width: size,
105 height: size,
106 borderRadius: size * 0.25,
107 alignItems: "center",
108 justifyContent: "center",
109 }}
110 >
111 <AppText
112 allowFontScaling={false}
113 numberOfLines={1}
114 className={`font-mono ${colors.text}`}
115 style={{
116 fontWeight: "700",
117 fontSize: size * (countGlyphs(glyph.text) === 1 ? 0.6 : 0.515625),
118 lineHeight: size,
119 textAlign: "center",
120 includeFontPadding: false,
121 }}
122 >
123 {glyph.text}
124 </AppText>
125 </View>
126 );
127}
128
129function ProjectFaviconImage(props: {
130 readonly cacheKey: string | null;
131 readonly faviconUrl: string | null;
132 readonly open?: boolean;
133 readonly projectTitle: string;
134 readonly size: number;
135}) {
136 const faviconRequest = useMemo(
137 () => createProjectFaviconRequest(props.cacheKey, props.faviconUrl),
138 [props.cacheKey, props.faviconUrl],
139 );
140 const [activeFaviconRequest, setActiveFaviconRequest] = useState<typeof faviconRequest>(null);
141 useLayoutEffect(() => {
142 if (faviconRequest === null) return;
143
144 const endRequest = beginProjectFaviconRequest(faviconRequest);
145 setActiveFaviconRequest(faviconRequest);
146 return endRequest;
147 }, [faviconRequest]);
148
149 const [status, setStatus] = useState<"loading" | "loaded" | "error">(() =>
150 props.faviconUrl?.startsWith("data:") || hasLoadedProjectFavicon(props.cacheKey)
151 ? "loaded"
152 : "loading",
153 );
154
155 const requestIsActive = faviconRequest !== null && activeFaviconRequest === faviconRequest;
156 const showImage = requestIsActive && status === "loaded";
157
158 return (
159 <View
160 style={{
161 width: props.size,
162 height: props.size,
163 alignItems: "center",
164 justifyContent: "center",
165 }}
166 >
167 {/* Folder icon fallback (matches web's FolderIcon) */}
168 {!showImage ? (
169 <SymbolView
170 name={{ ios: "folder.fill", android: props.open ? "folder_open" : "folder" }}
171 size={props.size}
172 tintColorClassName={"accent-icon-subtle"}
173 type="monochrome"
174 />
175 ) : null}
176
177 {/* Favicon image (hidden until loaded) */}
178 {requestIsActive ? (
179 <Image
180 key={faviconRequest.faviconUrl}
181 source={
182 faviconRequest.faviconUrl.startsWith("data:")
183 ? { uri: faviconRequest.faviconUrl }
184 : { uri: faviconRequest.faviconUrl, cacheKey: faviconRequest.cacheKey }
185 }
186 cachePolicy={faviconRequest.faviconUrl.startsWith("data:") ? "memory" : "memory-disk"}
187 recyclingKey={faviconRequest.cacheKey}
188 accessibilityLabel={`${props.projectTitle} favicon`}
189 style={{
190 width: props.size,
191 height: props.size,
192 borderRadius: props.size * 0.16,
193 ...(showImage ? {} : { position: "absolute" as const, opacity: 0 }),
194 }}
195 contentFit="contain"
196 onLoad={() => {
197 if (!markProjectFaviconLoaded(faviconRequest)) return;
198 setStatus("loaded");
199 }}
200 onError={() => {
201 if (!markProjectFaviconFailed(faviconRequest)) return;
202 setStatus("error");
203 }}
204 />
205 ) : null}
206 </View>
207 );
208}