import classNames from "classnames"; import { useEffect, useState } from "react"; import { rpc } from "../lib/rpc"; /** * Icons by app path, as data URLs once loaded, for the whole session. Plain rather than a query * per icon, since every new row in the list shows one. */ const icons = new Map(); const loading = new Map>(); function loadIcon(path: string): Promise { let promise = loading.get(path); if (promise == null) { promise = rpc("app_icon", { path }) .then((png) => (png == null ? null : `data:image/png;base64,${png}`)) .catch(() => null) .then((url) => { icons.set(path, url); return url; }); loading.set(path, promise); } return promise; } /** An app's icon, read once per path for the whole session; `className` sets its size */ export function AppIcon({ path, className = "size-4" }: { path: string; className?: string }) { const [url, setUrl] = useState(() => icons.get(path)); useEffect(() => { if (icons.has(path)) { setUrl(icons.get(path)); return; } let current = true; void loadIcon(path).then((loaded) => { if (current) setUrl(loaded); }); return () => { current = false; }; }, [path]); return url == null ? ( ) : ( ); }