Merge pull request #42 from Troray/fix/infinite-loop-and-search-refactor

fix(search): resolve infinite loop and refactor useHomePage hook
This commit is contained in:
Kuek Hao Yang
2026-01-12 19:18:08 +08:00
committed by GitHub
+39 -29
View File
@@ -3,7 +3,7 @@ import { useRouter, useSearchParams } from 'next/navigation';
import { useSearchCache } from '@/lib/hooks/useSearchCache';
import { useParallelSearch } from '@/lib/hooks/useParallelSearch';
import { useSubscriptionSync } from '@/lib/hooks/useSubscriptionSync';
import { settingsStore } from '@/lib/store/settings-store';
import { settingsStore, type SortOption } from '@/lib/store/settings-store';
export function useHomePage() {
useSubscriptionSync();
@@ -11,10 +11,11 @@ export function useHomePage() {
const searchParams = useSearchParams();
const { loadFromCache, saveToCache } = useSearchCache();
const hasLoadedCache = useRef(false);
const hasSearchedWithSourcesRef = useRef(false);
const [query, setQuery] = useState('');
const [hasSearched, setHasSearched] = useState(false);
const [currentSortBy, setCurrentSortBy] = useState('default');
const [currentSortBy, setCurrentSortBy] = useState<SortOption>('default');
const onUrlUpdate = useCallback((q: string) => {
router.replace(`/?q=${encodeURIComponent(q)}`, { scroll: false });
@@ -36,10 +37,26 @@ export function useHomePage() {
onUrlUpdate
);
// Core search execution function - extracted to eliminate duplication
const executeSearch = useCallback((searchQuery: string) => {
if (!searchQuery.trim()) return false;
const settings = settingsStore.getSettings();
const enabledSources = settings.sources.filter(s => s.enabled);
if (enabledSources.length === 0) {
return false;
}
performSearch(searchQuery, enabledSources, settings.sortBy);
hasSearchedWithSourcesRef.current = true;
return true;
}, [performSearch]);
// Re-sort results when sort preference changes
useEffect(() => {
if (hasSearched && results.length > 0) {
applySorting(currentSortBy as any);
applySorting(currentSortBy);
}
}, [currentSortBy, applySorting, hasSearched, results.length]);
@@ -59,12 +76,12 @@ export function useHomePage() {
const enabledSources = settings.sources.filter(s => s.enabled);
const hasSources = enabledSources.length > 0;
// If we have a query, and we haven't searched effectively (or result count is 0),
// and we suddenly have sources, retry the search.
if (query && hasSources && (!hasSearched || results.length === 0) && !loading) {
// We simply call handleSearch again which pulls fresh sources from settingsStore
performSearch(query, enabledSources, settings.sortBy);
setHasSearched(true);
// If we have a query, and we haven't searched with sources yet,
// and we suddenly have sources, trigger the search.
if (query && hasSources && !hasSearchedWithSourcesRef.current && !loading) {
if (executeSearch(query)) {
setHasSearched(true);
}
}
};
@@ -74,7 +91,14 @@ export function useHomePage() {
// Subscribe to changes
const unsubscribe = settingsStore.subscribe(updateSettings);
return () => unsubscribe();
}, [query, hasSearched, results.length, loading, performSearch, currentSortBy]);
}, [query, loading, executeSearch, currentSortBy]);
const handleSearch = useCallback((searchQuery: string) => {
if (!searchQuery.trim()) return;
setQuery(searchQuery);
setHasSearched(true);
executeSearch(searchQuery);
}, [executeSearch]);
// Load cached results on mount
useEffect(() => {
@@ -89,36 +113,22 @@ export function useHomePage() {
if (cached && cached.query === urlQuery && cached.results.length > 0) {
setHasSearched(true);
loadCachedResults(cached.results, cached.availableSources);
hasSearchedWithSourcesRef.current = true;
} else {
handleSearch(urlQuery);
}
}
}, [searchParams, loadFromCache, loadCachedResults]);
}, [searchParams, loadFromCache, loadCachedResults, handleSearch]);
const handleSearch = (searchQuery: string) => {
if (!searchQuery.trim()) return;
setQuery(searchQuery);
setHasSearched(true);
const settings = settingsStore.getSettings();
// Filter enabled sources
const enabledSources = settings.sources.filter(s => s.enabled);
if (enabledSources.length === 0) {
// If no sources yet, we can't do much, but the subscription above will catch it
// once sources are loaded by useSubscriptionSync
return;
}
performSearch(searchQuery, enabledSources, currentSortBy as any);
};
const handleReset = () => {
const handleReset = useCallback(() => {
setHasSearched(false);
setQuery('');
hasSearchedWithSourcesRef.current = false;
resetSearch();
router.replace('/', { scroll: false });
};
}, [resetSearch, router]);
return {
query,