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