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, 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/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 = useMemo(
56 () => (isProjectFaviconFallbackUrl(faviconUrl) ? null : faviconUrl),
57 [faviconUrl],
58 );
59 // Inline images are self-contained; remote URLs key on their revision so signed-token
60 // rotation reuses the disk cache while a changed icon starts from the loading state.
61 const cacheKey = useMemo(
62 () =>
63 renderableFaviconUrl && props.workspaceRoot
64 ? renderableFaviconUrl.startsWith("data:")
65 ? getProjectFaviconResourceKey(
66 props.environmentId,
67 props.workspaceRoot,
68 props.faviconPath,
69 )
70 : getProjectFaviconCacheKey(
71 props.environmentId,
72 props.workspaceRoot,
73 renderableFaviconUrl,
74 )
75 : null,
76 [renderableFaviconUrl, props.environmentId, props.workspaceRoot, props.faviconPath],
77 );
78
79 if (symbol !== null) {
80 return (
81 <SymbolView
82 name={symbol.name}
83 size={size}
84 tintColorClassName={projectIconColorClassNames(symbol.color).tint}
85 type="monochrome"
86 />
87 );
88 }
89
90 if (glyph !== null) {
91 return <ProjectIconGlyphView glyph={glyph} size={size} />;
92 }
93
94 return (
95 <ProjectFaviconImage
96 key={cacheKey}
97 cacheKey={cacheKey}
98 faviconUrl={renderableFaviconUrl}
99 open={props.open}
100 projectTitle={props.projectTitle}
101 size={size}
102 />
103 );
104});
105
106function ProjectIconGlyphView(props: { readonly glyph: ProjectIconGlyph; readonly size: number }) {
107 const { glyph, size } = props;
108 if (glyph.kind === "emoji") {
109 return (
110 <View style={{ width: size, height: size, alignItems: "center", justifyContent: "center" }}>
111 <AppText
112 allowFontScaling={false}
113 style={{
114 fontSize: size * 0.8,
115 lineHeight: size,
116 textAlign: "center",
117 includeFontPadding: false,
118 }}
119 >
120 {glyph.emoji}
121 </AppText>
122 </View>
123 );
124 }
125
126 const colors = projectIconColorClassNames(glyph.color);
127 return (
128 <View
129 className={colors.background}
130 style={{
131 width: size,
132 height: size,
133 borderRadius: size * 0.25,
134 alignItems: "center",
135 justifyContent: "center",
136 }}
137 >
138 <AppText
139 allowFontScaling={false}
140 numberOfLines={1}
141 className={`font-mono ${colors.text}`}
142 style={{
143 fontWeight: "700",
144 fontSize: size * (countGlyphs(glyph.text) === 1 ? 0.6 : 0.515625),
145 lineHeight: size,
146 textAlign: "center",
147 includeFontPadding: false,
148 }}
149 >
150 {glyph.text}
151 </AppText>
152 </View>
153 );
154}
155
156function ProjectFaviconImage(props: {
157 readonly cacheKey: string | null;
158 readonly faviconUrl: string | null;
159 readonly open?: boolean;
160 readonly projectTitle: string;
161 readonly size: number;
162}) {
163 const faviconRequest = useMemo(
164 () => createProjectFaviconRequest(props.cacheKey, props.faviconUrl),
165 [props.cacheKey, props.faviconUrl],
166 );
167 const [activeFaviconRequest, setActiveFaviconRequest] = useState<typeof faviconRequest>(null);
168 useLayoutEffect(() => {
169 if (faviconRequest === null) return;
170
171 const endRequest = beginProjectFaviconRequest(faviconRequest);
172 setActiveFaviconRequest(faviconRequest);
173 return endRequest;
174 }, [faviconRequest]);
175
176 const [status, setStatus] = useState<"loading" | "loaded" | "error">(() =>
177 props.faviconUrl?.startsWith("data:") || hasLoadedProjectFavicon(props.cacheKey)
178 ? "loaded"
179 : "loading",
180 );
181
182 const requestIsActive = faviconRequest !== null && activeFaviconRequest === faviconRequest;
183 const showImage = requestIsActive && status === "loaded";
184 const fallbackGlyph = showImage ? null : projectAutoMonogram(props.projectTitle);
185
186 return (
187 <View
188 style={{
189 width: props.size,
190 height: props.size,
191 alignItems: "center",
192 justifyContent: "center",
193 }}
194 >
195 {showImage ? null : fallbackGlyph ? (
196 <ProjectIconGlyphView glyph={fallbackGlyph} size={props.size} />
197 ) : (
198 <SymbolView
199 name={{ ios: "folder.fill", android: props.open ? "folder_open" : "folder" }}
200 size={props.size}
201 tintColorClassName={"accent-icon-subtle"}
202 type="monochrome"
203 />
204 )}
205
206 {requestIsActive ? (
207 <Image
208 key={faviconRequest.faviconUrl}
209 source={
210 faviconRequest.faviconUrl.startsWith("data:")
211 ? { uri: faviconRequest.faviconUrl }
212 : { uri: faviconRequest.faviconUrl, cacheKey: faviconRequest.cacheKey }
213 }
214 cachePolicy={faviconRequest.faviconUrl.startsWith("data:") ? "memory" : "memory-disk"}
215 recyclingKey={faviconRequest.cacheKey}
216 accessibilityLabel={`${props.projectTitle} favicon`}
217 style={{
218 width: props.size,
219 height: props.size,
220 borderRadius: props.size * 0.16,
221 ...(showImage ? {} : { position: "absolute" as const, opacity: 0 }),
222 }}
223 contentFit="contain"
224 onLoad={() => {
225 if (!markProjectFaviconLoaded(faviconRequest)) return;
226 setStatus("loaded");
227 }}
228 onError={() => {
229 if (!markProjectFaviconFailed(faviconRequest)) return;
230 setStatus("error");
231 }}
232 />
233 ) : null}
234 </View>
235 );
236}