Files
SnapOtter/apps/web/src/hooks/use-recent-tools.ts
T
SnapOtter b76dc68682 fix: resolve Sharp 0.35.1 and BullMQ type incompatibilities after dep bumps
Sharp 0.35.1 moved FormatEnum to a namespace export and removed "avif"
from FormatEnum (now a separate literal in toFormat). BullMQ 5.78.1
bundles ioredis 5.10.1 while we have 5.11.1, causing structural type
mismatch. Also fixes new Biome 1.9 lint rules.
2026-06-15 15:20:16 +08:00

46 lines
1.1 KiB
TypeScript

import { useSyncExternalStore } from "react";
const STORAGE_KEY = "snapotter-recent-tools";
const MAX_RECENT = 5;
const EMPTY: string[] = [];
let listeners: Array<() => void> = [];
let cachedRaw: string | null = null;
let cachedParsed: string[] = EMPTY;
function getSnapshot(): string[] {
const raw = localStorage.getItem(STORAGE_KEY);
if (raw !== cachedRaw) {
cachedRaw = raw;
try {
cachedParsed = raw ? JSON.parse(raw) : EMPTY;
} catch {
cachedParsed = EMPTY;
}
}
return cachedParsed;
}
function getServerSnapshot(): string[] {
return EMPTY;
}
function subscribe(listener: () => void) {
listeners.push(listener);
return () => {
listeners = listeners.filter((l) => l !== listener);
};
}
export function recordRecentTool(toolId: string) {
const current = getSnapshot();
const updated = [toolId, ...current.filter((id) => id !== toolId)].slice(0, MAX_RECENT);
localStorage.setItem(STORAGE_KEY, JSON.stringify(updated));
cachedRaw = null;
for (const l of listeners) l();
}
export function useRecentTools(): string[] {
return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
}