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

ProjectChipIconImages.tsx

150 lines4.5 KB

1import type { ProjectIconColor } from "@t3tools/contracts";
2import * as Effect from "effect/Effect";
3import { Atom } from "effect/reactivity";
4import { useRef, useSyncExternalStore } from "react";
5import { Platform, View } from "react-native";
6import Svg, { Rect, Text } from "react-native-svg";
7import { useResolveClassNames } from "uniwind";
8​
9import { countGlyphs, projectIconColorClassNames, type ProjectIconGlyph } from "../lib/projectIcon";
10​
11const TILE_SIZE = 48;
12​
13interface PendingTile {
14 readonly key: string;
15 readonly glyph: ProjectIconGlyph;
16 readonly promise: Promise<string>;
17 readonly resolve: (uri: string) => void;
18 readonly reject: () => void;
20​
21const tiles = new Map<string, PendingTile>();
22let pendingTiles: ReadonlyArray<PendingTile> = [];
23const listeners = new Set<() => void>();
24​
25export function projectGlyphKey(glyph: ProjectIconGlyph): string {
26 return JSON.stringify(
27 glyph.kind === "emoji" ? ["emoji", glyph.emoji] : ["monogram", glyph.text, glyph.color],
28 );
30​
31function decodeGlyphKey(key: string): ProjectIconGlyph {
32 const [kind, text, color] = JSON.parse(key) as [string, string, ProjectIconColor];
33 return kind === "emoji" ? { kind: "emoji", emoji: text } : { kind: "monogram", text, color };
35​
36function rasterize(key: string): Promise<string> {
37 const existing = tiles.get(key);
38 if (existing) return existing.promise;
39 let resolve!: (uri: string) => void;
40 let reject!: () => void;
41 const promise = new Promise<string>((done, fail) => {
42 resolve = done;
43 reject = fail;
44 });
45 const tile = { key, glyph: decodeGlyphKey(key), promise, resolve, reject };
46 tiles.set(key, tile);
47 pendingTiles = [...pendingTiles, tile];
48 queueMicrotask(notify);
49 return promise;
51​
52function finish(tile: PendingTile, base64: string | undefined) {
53 if (base64) {
54 tile.resolve(`data:image/png;base64,${base64.replace(/\s/g, "")}`);
55 } else {
56 tile.reject();
57 }
58 pendingTiles = pendingTiles.filter((pending) => pending !== tile);
59 notify();
61​
62function notify() {
63 for (const listener of listeners) listener();
65​
66function subscribe(listener: () => void) {
67 listeners.add(listener);
68 return () => listeners.delete(listener);
70​
71export const projectGlyphImageAtom = Atom.family((key: string) =>
72 Atom.make(Effect.tryPromise(() => rasterize(key))).pipe(
73 Atom.keepAlive,
74 Atom.withLabel(`mobile-project-glyph-image:${key}`),
75 ),
76);
77​
78export function ProjectChipIconRasterizer() {
79 const pending = useSyncExternalStore(subscribe, () => pendingTiles);
80 return pending.map((tile) => <GlyphCapture key={tile.key} tile={tile} />);
82​
83function captureTile(svg: Svg, tile: PendingTile) {
84 try {
85 svg.toDataURL(
86 (base64?: string) => finish(tile, base64),
87 Platform.OS === "ios" ? { width: TILE_SIZE, height: TILE_SIZE } : undefined,
88 );
89 } catch {
90 finish(tile, undefined);
91 }
93​
94function GlyphCapture(props: { readonly tile: PendingTile }) {
95 const started = useRef(false);
96 const capture = (svg: Svg | null) => {
97 if (!svg || started.current) return;
98 started.current = true;
99 requestAnimationFrame(() => captureTile(svg, props.tile));
100 };
101 return (
102 <View
103 pointerEvents="none"
104 style={{ position: "absolute", left: -TILE_SIZE * 4, top: 0, opacity: 0 }}
105 >
106 <Svg ref={capture} width={TILE_SIZE} height={TILE_SIZE}>
107 {props.tile.glyph.kind === "emoji" ? (
108 <Text
109 x={TILE_SIZE / 2}
110 y={TILE_SIZE * 0.8}
111 textAnchor="middle"
112 fontSize={TILE_SIZE * 0.8}
113 >
114 {props.tile.glyph.emoji}
115 </Text>
116 ) : (
117 <MonogramTile text={props.tile.glyph.text} color={props.tile.glyph.color} />
118 )}
119 </Svg>
120 </View>
121 );
123​
124function MonogramTile(props: { readonly text: string; readonly color: ProjectIconColor }) {
125 const classNames = projectIconColorClassNames(props.color);
126 const background = useResolveClassNames(classNames.background);
127 const foreground = useResolveClassNames(`font-mono ${classNames.text}`);
128 const fontSize = TILE_SIZE * (countGlyphs(props.text) === 1 ? 0.6 : 0.515625);
129 return (
130 <>
131 <Rect
132 width={TILE_SIZE}
133 height={TILE_SIZE}
134 rx={TILE_SIZE * 0.25}
135 fill={String(background.backgroundColor)}
136 />
137 <Text
138 x={TILE_SIZE / 2}
139 y={TILE_SIZE / 2 + fontSize * 0.36}
140 textAnchor="middle"
141 fill={String(foreground.color)}
142 fontFamily={typeof foreground.fontFamily === "string" ? foreground.fontFamily : undefined}
143 fontWeight="700"
144 fontSize={fontSize}
145 >
146 {props.text}
147 </Text>
148 </>
149 );