diff --git a/src/lib/components/Sidebar.svelte b/src/lib/components/Sidebar.svelte index f0044e5af..34afeb89b 100644 --- a/src/lib/components/Sidebar.svelte +++ b/src/lib/components/Sidebar.svelte @@ -38,6 +38,12 @@ getActiveMenuId, MENU_INACTIVITY_TIMEOUT } from '$lib/stores/floatingMenuStore'; + import { + getSidebarCache, + getSidebarCacheStatus, + setSidebarCache, + clearSidebarCache + } from '$lib/stores/sidebarDataCache'; interface Playlist { id: number; @@ -430,9 +436,9 @@ } export function refreshPlaylists() { + clearSidebarCache(); playlistTooltipCache.clear(); - loadUserPlaylists(); - loadPlaylistSettings(); + loadSidebarData(); } export function refreshPlaylistSettings() { @@ -445,9 +451,9 @@ // Call this when tracks are added/removed from a playlist export function onPlaylistTracksChanged(playlistId: number) { + clearSidebarCache(); invalidatePlaylistTooltip(playlistId); - loadUserPlaylists(); - loadLocalTrackCounts(); + loadSidebarData(); } // Focus and clear the search input (for keybinding) @@ -927,15 +933,34 @@ } } + async function loadSidebarData() { + await Promise.all([ + loadUserPlaylists(), + loadPlaylistSettings(), + loadLocalTrackCounts() + ]); + saveSidebarToCache(); + } + onMount(() => { loadSortPreference(); - loadUserPlaylists(); - loadPlaylistSettings(); - loadLocalTrackCounts(); loadFolders(); // Load playlist folders loadFavoritesPreferences(); // Load favorites tab order loadSidebarCollapseState(); // Load collapse states + // SWR: try cache first for playlists/settings/counts + const cacheStatus = getSidebarCacheStatus(); + if (cacheStatus === 'fresh') { + restoreFromSidebarCache(); + } else if (cacheStatus === 'stale') { + restoreFromSidebarCache(); + // Background refresh + loadSidebarData(); + } else { + // Empty: normal load with loading indicator + loadSidebarData(); + } + // Subscribe to offline state changes const unsubscribeOffline = subscribeOffline(() => { offlineStatus = getOfflineStatus(); @@ -1015,6 +1040,42 @@ } } + function saveSidebarToCache() { + if (isOffline) return; // Don't cache offline data + const settingsArray = Array.from(playlistSettings.values()); + const countsRecord: Record = {}; + for (const [id, count] of localTrackCounts) { + countsRecord[String(id)] = count; + } + setSidebarCache({ + playlists: userPlaylists, + playlistSettings: settingsArray, + localTrackCounts: countsRecord, + }); + } + + function restoreFromSidebarCache(): boolean { + const cached = getSidebarCache(); + if (!cached) return false; + + userPlaylists = cached.playlists as Playlist[]; + + const settingsMap = new Map(); + for (const s of cached.playlistSettings as PlaylistSettings[]) { + settingsMap.set(s.qobuz_playlist_id, s); + } + playlistSettings = settingsMap; + + const countsMap = new Map(); + for (const [id, count] of Object.entries(cached.localTrackCounts)) { + countsMap.set(Number(id), count); + } + localTrackCounts = countsMap; + + playlistsLoading = false; + return true; + } + async function loadUserPlaylists() { playlistsLoading = true; try { diff --git a/src/lib/stores/sidebarDataCache.ts b/src/lib/stores/sidebarDataCache.ts new file mode 100644 index 000000000..bb5098e72 --- /dev/null +++ b/src/lib/stores/sidebarDataCache.ts @@ -0,0 +1,78 @@ +/** + * Sidebar Data Cache Store (SWR) + * + * Module-level cache for sidebar data (playlists, settings, local track counts). + * Uses sessionStorage for persistence across navigation. + * + * Cache invalidation policy: + * - Invalidate on real playlist changes: create, delete, track add/remove + * - Do NOT invalidate on: sort order (local), play counts, UI state + */ + +const SESSION_KEY = 'qbz-sidebar-cache'; + +export interface SidebarCacheData { + playlists: unknown[]; + playlistSettings: unknown[]; + localTrackCounts: Record; + timestamp: number; +} + +const FRESH_TTL_MS = 5 * 60 * 1000; // 5 minutes +const MAX_TTL_MS = 60 * 60 * 1000; // 60 minutes + +export type SidebarCacheStatus = 'fresh' | 'stale' | 'empty'; + +let cache: SidebarCacheData | null = null; + +function restoreFromSession(): void { + try { + const stored = sessionStorage.getItem(SESSION_KEY); + if (stored) { + cache = JSON.parse(stored); + } + } 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(); + +export function getSidebarCacheStatus(): SidebarCacheStatus { + if (!cache) return 'empty'; + + const age = Date.now() - cache.timestamp; + if (age > MAX_TTL_MS) return 'empty'; + if (age <= FRESH_TTL_MS) return 'fresh'; + return 'stale'; +} + +export function getSidebarCache(): SidebarCacheData | null { + return cache; +} + +export function setSidebarCache(data: Omit): void { + cache = { + ...data, + timestamp: Date.now(), + }; + persistToSession(); +} + +export function clearSidebarCache(): void { + cache = null; + persistToSession(); +}