import { useCallback, useState } from 'react'; const MAX_HISTORY = 10; export function useSearchHistory(persistenceKey?: string) { const key = persistenceKey ?? 'default'; const [history, setHistory] = useState(() => loadHistory(key)); const addEntry = useCallback( (query: string) => { if (!query.trim()) return; setHistory((prev) => { const filtered = prev.filter((h) => h !== query); const next = [query, ...filtered].slice(0, MAX_HISTORY); saveHistory(key, next); return next; }); }, [key] ); const clearHistory = useCallback(() => { setHistory([]); localStorage.removeItem(getStorageKey(key)); }, [key]); return { addEntry, clearHistory, history }; } function getStorageKey(persistenceKey: string) { return `griddy-search-history-${persistenceKey}`; } function loadHistory(persistenceKey: string): string[] { try { const raw = localStorage.getItem(getStorageKey(persistenceKey)); return raw ? JSON.parse(raw) : []; } catch { return []; } } function saveHistory(persistenceKey: string, history: string[]) { localStorage.setItem(getStorageKey(persistenceKey), JSON.stringify(history)); }