Files
oranguru/src/Griddy/features/searchHistory/useSearchHistory.ts
Hein 7244bd33fc refactor(advancedSearch): reorder exports and improve type definitions
refactor(types): reorganize SearchCondition and AdvancedSearchState interfaces
refactor(filterPresets): streamline useFilterPresets hook and localStorage handling
refactor(filtering): clean up ColumnFilterButton and ColumnFilterPopover components
refactor(loading): separate GriddyLoadingOverlay from GriddyLoadingSkeleton
refactor(searchHistory): enhance useSearchHistory hook with persistence
refactor(index): update exports for adapters and core components
refactor(rendering): improve EditableCell and TableCell components for clarity
refactor(rendering): enhance TableHeader and VirtualBody components for better readability
2026-02-15 19:54:33 +02:00

46 lines
1.2 KiB
TypeScript

import { useCallback, useState } from 'react';
const MAX_HISTORY = 10;
export function useSearchHistory(persistenceKey?: string) {
const key = persistenceKey ?? 'default';
const [history, setHistory] = useState<string[]>(() => 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));
}