fix: Improve useHomePage search logic to retry searches when sources become available and prevent empty queries.

This commit is contained in:
kuekhaoyang
2025-12-30 11:58:47 +08:00
parent 73e5ec5046
commit d8da86054c
3 changed files with 32 additions and 5 deletions
+29 -2
View File
@@ -43,7 +43,25 @@ export function useHomePage() {
useEffect(() => {
const updateSettings = () => {
const settings = settingsStore.getSettings();
setCurrentSortBy(settings.sortBy);
// Update sort preference
if (settings.sortBy !== currentSortBy) {
setCurrentSortBy(settings.sortBy);
}
// Check if we need to re-trigger search due to new sources being loaded
// This fixes the issue where initial visit has 0 sources, then sources are loaded async
// but the search (or lack thereof) is already stuck with empty sources.
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);
}
};
// Initial load
@@ -52,7 +70,7 @@ export function useHomePage() {
// Subscribe to changes
const unsubscribe = settingsStore.subscribe(updateSettings);
return () => unsubscribe();
}, []);
}, [query, hasSearched, results.length, loading, performSearch, currentSortBy]);
// Load cached results on mount
useEffect(() => {
@@ -74,11 +92,20 @@ export function useHomePage() {
}, [searchParams, loadFromCache, loadCachedResults]);
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);
};
+2 -2
View File
@@ -1,12 +1,12 @@
{
"name": "kvideo",
"version": "3.8.5",
"version": "3.8.6",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "kvideo",
"version": "3.8.5",
"version": "3.8.6",
"dependencies": {
"@dnd-kit/core": "^6.3.1",
"@dnd-kit/sortable": "^10.0.0",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "kvideo",
"version": "3.8.5",
"version": "3.8.6",
"private": true,
"scripts": {
"dev": "next dev",