mirror of
https://github.com/block/buzz.git
synced 2026-08-18 06:50:31 +02:00
Signed-off-by: Wes <wesbillman@users.noreply.github.com> Co-authored-by: Brain <21994759fc7a6fa6b965551d35cfd7897d262f2495467f2d78694ddcfa6a5c7e@sprout-oss.stage.blox.sqprod.co>
42 lines
1.1 KiB
TypeScript
42 lines
1.1 KiB
TypeScript
/**
|
|
* Local cache of workspace icons keyed by relay URL, so the rail renders
|
|
* icons instantly on boot and for unreachable relays. Values are small
|
|
* data-URLs (or http URLs) from the relay's NIP-11 `icon` field.
|
|
*/
|
|
|
|
const ICON_CACHE_KEY = "buzz-workspace-icons";
|
|
|
|
function loadCache(): Record<string, string> {
|
|
try {
|
|
const raw = localStorage.getItem(ICON_CACHE_KEY);
|
|
const parsed: unknown = raw ? JSON.parse(raw) : null;
|
|
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
return parsed as Record<string, string>;
|
|
}
|
|
} catch {
|
|
// Corrupt cache — fall through to empty.
|
|
}
|
|
return {};
|
|
}
|
|
|
|
export function loadCachedWorkspaceIcon(relayUrl: string): string | null {
|
|
return loadCache()[relayUrl] ?? null;
|
|
}
|
|
|
|
export function saveCachedWorkspaceIcon(
|
|
relayUrl: string,
|
|
icon: string | null,
|
|
): void {
|
|
const cache = loadCache();
|
|
if (icon) {
|
|
cache[relayUrl] = icon;
|
|
} else {
|
|
delete cache[relayUrl];
|
|
}
|
|
try {
|
|
localStorage.setItem(ICON_CACHE_KEY, JSON.stringify(cache));
|
|
} catch {
|
|
// Quota exceeded — the icon still renders from the in-memory query.
|
|
}
|
|
}
|