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

ProjectFavicon.tsx

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