diff --git a/src/index.tsx b/src/index.tsx index 04fbd86..a863ef1 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -6,6 +6,17 @@ import { StatsData, ComparisonEntry } from './utils/types'; import { parseStats } from './utils/formatters'; import './styles.css'; +function debounce) => ReturnType>( + fn: T, + delay: number, +): (...args: Parameters) => void { + let timeoutId: ReturnType; + return (...args: Parameters) => { + clearTimeout(timeoutId); + timeoutId = setTimeout(() => fn(...args), delay); + }; +} + const Analysis = lazy(() => import('./components/Analysis')); const ComparisonView = lazy(() => import('./components/ComparisonView')); @@ -64,6 +75,31 @@ function App() { }; }); + // Debounced save to localStorage + const saveToStorage = debounce( + ( + stats: StatsData | null, + raw: Record | null, + snaps: ComparisonEntry[], + v: 'analysis' | 'compare', + ) => { + try { + localStorage.setItem( + STORAGE_KEY, + JSON.stringify({ + snapshots: snaps, + currentStats: stats, + currentRaw: raw, + view: v, + }), + ); + } catch (e) { + console.warn('Failed to save data:', e); + } + }, + 500, + ); + // Save to localStorage on any change createEffect(() => { const stats = currentStats(); @@ -71,22 +107,9 @@ function App() { const snaps = snapshots(); const v = view(); - // Don't save while still loading initial data if (isLoading()) return; - try { - localStorage.setItem( - STORAGE_KEY, - JSON.stringify({ - snapshots: snaps, - currentStats: stats, - currentRaw: raw, - view: v, - }), - ); - } catch (e) { - console.warn('Failed to save data:', e); - } + saveToStorage(stats, raw, snaps, v); }); const saveSnapshot = () => {