Open History to see the last commit
1import { SymbolView } from "./ui/AppSymbol";
2import { AppText } from "./ui/AppText";
3import { Image } from "expo-image";
4import { memo, useLayoutEffect, 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/reactivity";
14import { projectFaviconUrlAtom } from "../state/assets";
15import {
16 countGlyphs,
17 projectAutoMonogram,
18 projectIconColorClassNames,
19 resolveProjectIconGlyph,
20 resolveProjectIconSymbol,
21 type ProjectIconGlyph,
22} from "../lib/projectIcon";
23
24import {
25 beginProjectFaviconRequest,
26 createProjectFaviconRequest,
27 hasLoadedProjectFavicon,
28 markProjectFaviconFailed,
29 markProjectFaviconLoaded,
30} from "../lib/projectFaviconRequests";
31
32const EMPTY_FAVICON_URL = Atom.make<string | null>(null);
33
34export const ProjectFavicon = memo(function ProjectFavicon(props: {
35 readonly environmentId: EnvironmentId;
36 readonly open?: boolean;
37 readonly size?: number;
38 readonly projectTitle: string;
39 readonly workspaceRoot?: string | null;
40 readonly faviconPath?: string | null;
41 readonly projectIcon?: ProjectIconOverride | null;
42}) {
43 const size = props.size ?? 42;
44 const symbol = resolveProjectIconSymbol(props.projectIcon);
45 const glyph = resolveProjectIconGlyph(props.projectIcon, props.projectTitle);
46 const faviconUrl = useAtomValue(
47 props.workspaceRoot == null || glyph !== null
48 ? EMPTY_FAVICON_URL
49 : projectFaviconUrlAtom({
50 environmentId: props.environmentId,
51 cwd: props.workspaceRoot,
52 faviconPath: props.faviconPath,
53 }),
54 );
55 const renderableFaviconUrl = isProjectFaviconFallbackUrl(faviconUrl) ? null : faviconUrl;
56 // Key remote images by revision so token rotation reuses the cache while changed icons reload.
57 const cacheKey =
58 renderableFaviconUrl && props.workspaceRoot
59 ? renderableFaviconUrl.startsWith("data:")
60 ? getProjectFaviconResourceKey(props.environmentId, props.workspaceRoot, props.faviconPath)
61 : getProjectFaviconCacheKey(props.environmentId, props.workspaceRoot, renderableFaviconUrl)
62 : null;
63
64 if (symbol !== null) {
65 return (
66 <SymbolView
67 name={symbol.name}
68 size={size}
69 tintColorClassName={projectIconColorClassNames(symbol.color).tint}
70 type="monochrome"
71 />
72 );
73 }
74
75 if (glyph !== null) {
76 return <ProjectIconGlyphView glyph={glyph} size={size} />;
77 }
78
79 return (
80 <ProjectFaviconImage
81 key={cacheKey}
82 cacheKey={cacheKey}
83 faviconUrl={renderableFaviconUrl}
84 open={props.open}
85 projectTitle={props.projectTitle}
86 size={size}
87 />
88 );
89});
90
91function ProjectIconGlyphView(props: { readonly glyph: ProjectIconGlyph; readonly size: number }) {
92 const { glyph, size } = props;
93 if (glyph.kind === "emoji") {
94 return (
95 <View style={{ width: size, height: size, alignItems: "center", justifyContent: "center" }}>
96 <AppText
97 allowFontScaling={false}
98 style={{
99 fontSize: size * 0.8,
100 lineHeight: size,
101 textAlign: "center",
102 includeFontPadding: false,
103 }}
104 >
105 {glyph.emoji}
106 </AppText>
107 </View>
108 );
109 }
110
111 const colors = projectIconColorClassNames(glyph.color);
112 return (
113 <View
114 className={colors.background}
115 style={{
116 width: size,
117 height: size,
118 borderRadius: size * 0.25,
119 alignItems: "center",
120 justifyContent: "center",
121 }}
122 >
123 <AppText
124 allowFontScaling={false}
125 numberOfLines={1}
126 className={`font-mono ${colors.text}`}
127 style={{
128 fontWeight: "700",
129 fontSize: size * (countGlyphs(glyph.text) === 1 ? 0.6 : 0.515625),
130 lineHeight: size,
131 textAlign: "center",
132 includeFontPadding: false,
133 }}
134 >
135 {glyph.text}
136 </AppText>
137 </View>
138 );
139}
140
141function ProjectFaviconImage(props: {
142 readonly cacheKey: string | null;
143 readonly faviconUrl: string | null;
144 readonly open?: boolean;
145 readonly projectTitle: string;
146 readonly size: number;
147}) {
148 const faviconRequest = createProjectFaviconRequest(props.cacheKey, props.faviconUrl);
149 // Registers with the module-level loader before the Image's native onLoad/onError can fire,
150 // so stale loads for a superseded URL under the same cacheKey are ignored.
151 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- marks this favicon URL as the current request in the module registry before the native Image reports load or error
152 useLayoutEffect(() => {
153 if (faviconRequest === null) return;
154 return beginProjectFaviconRequest(faviconRequest);
155 }, [faviconRequest]);
156
157 const [status, setStatus] = useState<"loading" | "loaded" | "error">(() =>
158 props.faviconUrl?.startsWith("data:") || hasLoadedProjectFavicon(props.cacheKey)
159 ? "loaded"
160 : "loading",
161 );
162
163 const showImage = faviconRequest !== null && status === "loaded";
164 const fallbackGlyph = showImage ? null : projectAutoMonogram(props.projectTitle);
165
166 return (
167 <View
168 style={{
169 width: props.size,
170 height: props.size,
171 alignItems: "center",
172 justifyContent: "center",
173 }}
174 >
175 {showImage ? null : fallbackGlyph ? (
176 <ProjectIconGlyphView glyph={fallbackGlyph} size={props.size} />
177 ) : (
178 <SymbolView
179 name={{ ios: "folder.fill", android: props.open ? "folder_open" : "folder" }}
180 size={props.size}
181 tintColorClassName={"accent-icon-subtle"}
182 type="monochrome"
183 />
184 )}
185
186 {faviconRequest !== null ? (
187 <Image
188 key={faviconRequest.faviconUrl}
189 source={
190 faviconRequest.faviconUrl.startsWith("data:")
191 ? { uri: faviconRequest.faviconUrl }
192 : { uri: faviconRequest.faviconUrl, cacheKey: faviconRequest.cacheKey }
193 }
194 cachePolicy={faviconRequest.faviconUrl.startsWith("data:") ? "memory" : "memory-disk"}
195 recyclingKey={faviconRequest.cacheKey}
196 accessibilityLabel={`${props.projectTitle} favicon`}
197 style={{
198 width: props.size,
199 height: props.size,
200 borderRadius: props.size * 0.16,
201 ...(showImage ? {} : { position: "absolute" as const, opacity: 0 }),
202 }}
203 contentFit="contain"
204 onLoad={() => {
205 if (!markProjectFaviconLoaded(faviconRequest)) return;
206 setStatus("loaded");
207 }}
208 onError={() => {
209 if (!markProjectFaviconFailed(faviconRequest)) return;
210 setStatus("error");
211 }}
212 />
213 ) : null}
214 </View>
215 );
216}