mirror of
https://github.com/block/buzz.git
synced 2026-11-28 20:22:39 +00:00
Signed-off-by: Wes <wesbillman@users.noreply.github.com> Co-authored-by: Pinky <44b8e82baa6e0e254e0208d68f335c283c94e7b78dd1fa10d5a49d3f13dd0435@sprout-oss.stage.blox.sqprod.co>
42 lines
1.1 KiB
TypeScript
42 lines
1.1 KiB
TypeScript
/**
|
|
* Local cache of community 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-community-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 loadCachedCommunityIcon(relayUrl: string): string | null {
|
|
return loadCache()[relayUrl] ?? null;
|
|
}
|
|
|
|
export function saveCachedCommunityIcon(
|
|
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.
|
|
}
|
|
}
|