/** * Home Data Cache Store (SWR) * * Module-level in-memory cache for HomeView data with stale-while-revalidate. * - fresh (< 5 min): Use cache, no revalidation * - stale (5-60 min): Show cache immediately, revalidate in background * - empty (no data / >60 min / genre mismatch): Full skeleton load * * Persisted to sessionStorage for cross-navigation survival. */ import type { DisplayTrack, DiscoverPlaylist, DiscoverAlbum, PlaylistTag } from '$lib/types'; export interface AlbumCardData { id: string; artwork: string; title: string; artist: string; artistId?: number; genre: string; quality?: string; releaseDate?: string; } export interface ArtistCardData { id: number; name: string; image?: string; playCount?: number; } export interface HomeCacheData { // Featured (Qobuz editorial) newReleases: AlbumCardData[]; pressAwards: AlbumCardData[]; mostStreamed: AlbumCardData[]; qobuzissimes: AlbumCardData[]; editorPicks: AlbumCardData[]; // User-specific (ML) recentAlbums: AlbumCardData[]; continueTracks: DisplayTrack[]; topArtists: ArtistCardData[]; favoriteAlbums: AlbumCardData[]; // Discover qobuzPlaylists: DiscoverPlaylist[]; essentialDiscography: DiscoverAlbum[]; playlistTags: PlaylistTag[]; // Release Watch (REST /albums/releaseWatch) releaseWatchAlbums: AlbumCardData[]; // Metadata timestamp: number; genreIds: number[]; // snapshot of genre filter at cache time scrollTop: number; schemaVersion?: number; } export type HomeCacheStatus = 'fresh' | 'stale' | 'empty'; const FRESH_TTL_MS = 5 * 60 * 1000; // 5 minutes const MAX_TTL_MS = 60 * 60 * 1000; // 60 minutes const SESSION_KEY = 'qbz-home-cache'; /** * Bump whenever the shape of HomeCacheData or AlbumCardData changes, * so stale sessionStorage entries from older builds are discarded * instead of being restored with missing fields (which caused the * ribbon field to show up as undefined on cache-fresh loads). */ const CACHE_SCHEMA_VERSION = 2; let cache: HomeCacheData | null = null; // Restore from sessionStorage on module load function restoreFromSession(): void { try { const stored = sessionStorage.getItem(SESSION_KEY); if (stored) { const parsed = JSON.parse(stored); if (parsed?.schemaVersion === CACHE_SCHEMA_VERSION) { cache = parsed; } else { // Older schema — drop it so loadHome() runs fresh. try { sessionStorage.removeItem(SESSION_KEY); } catch {} } } } catch { // sessionStorage not available or corrupted } } function persistToSession(): void { if (!cache) { try { sessionStorage.removeItem(SESSION_KEY); } catch {} return; } try { sessionStorage.setItem(SESSION_KEY, JSON.stringify(cache)); } catch { // sessionStorage full or not available } } // Initialize from sessionStorage restoreFromSession(); function genreIdsMatch(cachedIds: number[], currentIds: number[]): boolean { const cachedSorted = [...cachedIds].sort((a, b) => a - b); const currentSorted = [...currentIds].sort((a, b) => a - b); if (cachedSorted.length !== currentSorted.length) return false; for (let i = 0; i < cachedSorted.length; i++) { if (cachedSorted[i] !== currentSorted[i]) return false; } return true; } export function getHomeCacheStatus(currentGenreIds: number[]): HomeCacheStatus { if (!cache) return 'empty'; // Genre filter mismatch = empty (data is for a different genre) if (!genreIdsMatch(cache.genreIds, currentGenreIds)) return 'empty'; const age = Date.now() - cache.timestamp; // Too old = empty if (age > MAX_TTL_MS) return 'empty'; // Fresh = use as-is if (age <= FRESH_TTL_MS) return 'fresh'; // In between = stale (show cache, revalidate) return 'stale'; } export function getHomeCache(): HomeCacheData | null { return cache; } export function setHomeCache(data: Omit & { genreIds: number[] }): void { cache = { ...data, timestamp: Date.now(), scrollTop: cache?.scrollTop ?? 0, schemaVersion: CACHE_SCHEMA_VERSION, } as HomeCacheData; persistToSession(); } export function clearHomeCache(): void { cache = null; persistToSession(); } export function updateHomeCacheScrollTop(scrollTop: number): void { if (cache) { cache.scrollTop = scrollTop; // Don't persist on every scroll - too frequent } }