mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
34 lines
861 B
TypeScript
34 lines
861 B
TypeScript
import { useSyncExternalStore } from "react";
|
|||
|
|
|
||
|
|
const STORAGE_KEY = "snapotter-recent-tools";
|
||
|
|
const MAX_RECENT = 5;
|
||
|
|
|
||
|
|
let listeners: Array<() => void> = [];
|
||
|
|
|
||
|
|
function getSnapshot(): string[] {
|
||
|
|
try {
|
||
|
|
const raw = localStorage.getItem(STORAGE_KEY);
|
||
|
|
return raw ? JSON.parse(raw) : [];
|
||
|
|
} catch {
|
||
|
|
return [];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
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));
|
||
|
|
listeners.forEach((l) => l());
|
||
|
|
}
|
||
|
|
|
||
|
|
export function useRecentTools(): string[] {
|
||
|
|
return useSyncExternalStore(subscribe, getSnapshot, () => []);
|
||
|
|
}
|