mirror of
https://github.com/KuekHaoYang/KVideo.git
synced 2026-08-19 10:43:43 +08:00
feat: Implement scroll position management with a new setting, add a back-to-top button, and extend search cache duration.
This commit is contained in:
@@ -12,6 +12,7 @@ export function useHomePage() {
|
||||
const { loadFromCache, saveToCache } = useSearchCache();
|
||||
const hasLoadedCache = useRef(false);
|
||||
const hasSearchedWithSourcesRef = useRef(false);
|
||||
const isInitialCacheLoad = useRef(false);
|
||||
|
||||
const [query, setQuery] = useState('');
|
||||
const [hasSearched, setHasSearched] = useState(false);
|
||||
@@ -55,7 +56,9 @@ export function useHomePage() {
|
||||
|
||||
// Re-sort results when sort preference changes
|
||||
useEffect(() => {
|
||||
if (hasSearched && results.length > 0) {
|
||||
// Skip re-sorting if this is a load from cache, to preserve the "remembered" position
|
||||
// Only re-sort if the user explicitly changes the sortBy option later
|
||||
if (hasSearched && results.length > 0 && !isInitialCacheLoad.current) {
|
||||
applySorting(currentSortBy);
|
||||
}
|
||||
}, [currentSortBy, applySorting, hasSearched, results.length]);
|
||||
@@ -95,6 +98,14 @@ export function useHomePage() {
|
||||
|
||||
const handleSearch = useCallback((searchQuery: string) => {
|
||||
if (!searchQuery.trim()) return;
|
||||
|
||||
// Clear scroll position for this search query to ensure we start at the top on a fresh search
|
||||
const scrollKey = `scroll-pos:/?q=${encodeURIComponent(searchQuery)}`;
|
||||
sessionStorage.removeItem(scrollKey);
|
||||
|
||||
// Reset cache load flag for new search
|
||||
isInitialCacheLoad.current = false;
|
||||
|
||||
setQuery(searchQuery);
|
||||
setHasSearched(true);
|
||||
executeSearch(searchQuery);
|
||||
@@ -111,6 +122,7 @@ export function useHomePage() {
|
||||
if (urlQuery) {
|
||||
setQuery(urlQuery);
|
||||
if (cached && cached.query === urlQuery && cached.results.length > 0) {
|
||||
isInitialCacheLoad.current = true;
|
||||
setHasSearched(true);
|
||||
loadCachedResults(cached.results, cached.availableSources);
|
||||
hasSearchedWithSourcesRef.current = true;
|
||||
|
||||
@@ -8,7 +8,7 @@ interface SearchCache {
|
||||
}
|
||||
|
||||
const CACHE_KEY = 'kvideo_search_cache';
|
||||
const CACHE_DURATION = 10 * 60 * 1000; // 10 minutes
|
||||
const CACHE_DURATION = 24 * 60 * 60 * 1000; // 24 hours
|
||||
|
||||
const MAX_CACHED_RESULTS = 300;
|
||||
|
||||
|
||||
@@ -46,6 +46,7 @@ export interface AppSettings {
|
||||
episodeReverseOrder: boolean; // Persist episode list reverse state
|
||||
fullscreenType: 'native' | 'window'; // Fullscreen mode preference
|
||||
proxyMode: ProxyMode; // Proxy behavior: 'retry' | 'none' | 'always'
|
||||
rememberScrollPosition: boolean; // Remember scroll position when navigating back or refreshing
|
||||
}
|
||||
|
||||
import { exportSettings, importSettings, SEARCH_HISTORY_KEY, WATCH_HISTORY_KEY } from './settings-helpers';
|
||||
@@ -119,6 +120,7 @@ function getDefaultAppSettings(): AppSettings {
|
||||
episodeReverseOrder: false,
|
||||
fullscreenType: 'native',
|
||||
proxyMode: 'retry',
|
||||
rememberScrollPosition: true,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -196,6 +198,7 @@ export const settingsStore = {
|
||||
episodeReverseOrder: parsed.episodeReverseOrder !== undefined ? parsed.episodeReverseOrder : false,
|
||||
fullscreenType: parsed.fullscreenType === 'window' ? 'window' : 'native',
|
||||
proxyMode: (parsed.proxyMode === 'retry' || parsed.proxyMode === 'none' || parsed.proxyMode === 'always') ? parsed.proxyMode : 'retry',
|
||||
rememberScrollPosition: parsed.rememberScrollPosition !== undefined ? parsed.rememberScrollPosition : true,
|
||||
};
|
||||
} catch {
|
||||
// Even if localStorage fails, we should return defaults + ENV subscriptions
|
||||
|
||||
Reference in New Issue
Block a user