Files
KVideo/lib/hooks/useSearchCache.ts
T
kuekhaoyang ad9e75bc8c feat: Implement video player components and search functionality
- Added EpisodeList component for displaying a list of episodes with selection functionality.
- Created PlayerError component to handle and display video playback errors.
- Developed VideoMetadata component to show detailed information about the video.
- Implemented VideoPlayer component for video playback with error handling and loading states.
- Introduced EmptyState and NoResults components for search results handling.
- Created ResultsHeader component to display search results summary.
- Developed SearchForm component for user input and search initiation.
- Implemented SourceBadges component to show available video sources.
- Created VideoGrid component to display search results in a grid format.
- Added useSearchCache and useSearchStream hooks for managing search state and caching results.
- Implemented useVideoPlayer hook for fetching and managing video details.
- Added utility function to map source IDs to their respective names.
2025-11-17 13:51:03 +08:00

72 lines
1.6 KiB
TypeScript

import { useRef } from 'react';
interface SearchCache {
query: string;
results: any[];
availableSources: any[];
timestamp: number;
}
const CACHE_KEY = 'kvideo_search_cache';
const CACHE_DURATION = 10 * 60 * 1000; // 10 minutes
export function useSearchCache() {
const hasLoadedCache = useRef(false);
const saveToCache = (
query: string,
results: any[],
sources: any[]
) => {
const cache: SearchCache = {
query,
results,
availableSources: sources,
timestamp: Date.now(),
};
try {
localStorage.setItem(CACHE_KEY, JSON.stringify(cache));
console.log('💾 Saved search to cache:', query, results.length, 'results');
} catch (error) {
console.error('Failed to save cache:', error);
}
};
const loadFromCache = (): SearchCache | null => {
try {
const cached = localStorage.getItem(CACHE_KEY);
if (!cached) return null;
const cache: SearchCache = JSON.parse(cached);
// Check if cache is still valid
if (Date.now() - cache.timestamp > CACHE_DURATION) {
localStorage.removeItem(CACHE_KEY);
return null;
}
return cache;
} catch (error) {
console.error('Failed to load cache:', error);
return null;
}
};
const clearCache = () => {
try {
localStorage.removeItem(CACHE_KEY);
console.log('🗑️ Cache cleared');
} catch (error) {
console.error('Failed to clear cache:', error);
}
};
return {
saveToCache,
loadFromCache,
clearCache,
hasLoadedCache,
};
}