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

ProjectFavicon.tsx

228 lines7.3 KB

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