mirror of
https://github.com/KuekHaoYang/KVideo.git
synced 2026-08-12 23:33:43 +08:00
- Bump version to 4.8.0 and update dependencies in package.json - Implement user config sync API for cross-device settings persistence - Add resolution badge auto-hide hook for improved user experience - Create useConfigSync hook to manage user settings synchronization with the server
98 lines
2.4 KiB
TypeScript
98 lines
2.4 KiB
TypeScript
'use client';
|
|
|
|
import { useCallback } from 'react';
|
|
import { sortVideos } from '@/lib/utils/sort';
|
|
import type { SortOption } from '@/lib/store/settings-store';
|
|
import type { Video, SourceBadge } from '@/lib/types';
|
|
import { useSearchState } from './useSearchState';
|
|
import { useSearchAction } from './useSearchAction';
|
|
|
|
interface ParallelSearchResult {
|
|
loading: boolean;
|
|
results: Video[];
|
|
availableSources: SourceBadge[];
|
|
completedSources: number;
|
|
totalSources: number;
|
|
totalVideosFound: number;
|
|
performSearch: (query: string, sources?: any[], sortBy?: SortOption) => Promise<void>;
|
|
resetSearch: () => void;
|
|
cancelSearch: () => void;
|
|
loadCachedResults: (results: Video[], sources: any[]) => void;
|
|
applySorting: (sortBy: SortOption) => void;
|
|
loadMore: () => Promise<void>;
|
|
hasMore: boolean;
|
|
loadingMore: boolean;
|
|
}
|
|
|
|
export function useParallelSearch(
|
|
onCacheUpdate: (query: string, results: any[], sources: any[]) => void,
|
|
onUrlUpdate: (query: string) => void
|
|
): ParallelSearchResult {
|
|
const state = useSearchState();
|
|
const {
|
|
loading,
|
|
results,
|
|
availableSources,
|
|
completedSources,
|
|
totalSources,
|
|
totalVideosFound,
|
|
currentPage,
|
|
maxPageCount,
|
|
loadingMore,
|
|
setResults,
|
|
setAvailableSources,
|
|
setTotalVideosFound,
|
|
resetState,
|
|
} = state;
|
|
|
|
const { performSearch, loadMore: loadMoreAction, cancelSearch } = useSearchAction({
|
|
state,
|
|
onCacheUpdate,
|
|
onUrlUpdate,
|
|
});
|
|
|
|
const hasMore = currentPage < maxPageCount;
|
|
|
|
/**
|
|
* Reset search state
|
|
*/
|
|
const resetSearch = useCallback(() => {
|
|
cancelSearch();
|
|
resetState();
|
|
}, [cancelSearch, resetState]);
|
|
|
|
/**
|
|
* Load cached results
|
|
*/
|
|
const loadCachedResults = useCallback((cachedResults: Video[], cachedSources: any[]) => {
|
|
setResults(cachedResults);
|
|
setAvailableSources(cachedSources);
|
|
setTotalVideosFound(cachedResults.length);
|
|
}, [setResults, setAvailableSources, setTotalVideosFound]);
|
|
|
|
/**
|
|
* Apply sorting to current results
|
|
*/
|
|
const applySorting = useCallback((sortBy: SortOption) => {
|
|
setResults((currentResults) => sortVideos(currentResults, sortBy));
|
|
}, [setResults]);
|
|
|
|
return {
|
|
loading,
|
|
results,
|
|
availableSources,
|
|
completedSources,
|
|
totalSources,
|
|
totalVideosFound,
|
|
performSearch,
|
|
resetSearch,
|
|
cancelSearch,
|
|
loadCachedResults,
|
|
applySorting,
|
|
loadMore: loadMoreAction,
|
|
hasMore,
|
|
loadingMore,
|
|
};
|
|
}
|
|
|