mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
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.
46 lines
1.1 KiB
TypeScript
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);
|
|
}
|