From 0e8371d900c1afff8d5d7113e0c973a10873c359 Mon Sep 17 00:00:00 2001 From: vicrodh <103399283+vicrodh@users.noreply.github.com> Date: Sat, 10 Jan 2026 04:25:45 -0600 Subject: [PATCH] refactor: extract toast notifications into toastStore - Create toastStore.ts with centralized toast state management - Add auto-hide functionality with configurable durations per type - Update +page.svelte to use store subscription instead of local state - Maintains existing toast behavior with improved separation of concerns --- src/lib/stores/toastStore.ts | 89 ++++++++++++++++++++++++++++++++++++ src/routes/+page.svelte | 27 ++++++----- 2 files changed, 105 insertions(+), 11 deletions(-) create mode 100644 src/lib/stores/toastStore.ts diff --git a/src/lib/stores/toastStore.ts b/src/lib/stores/toastStore.ts new file mode 100644 index 000000000..64464bfb5 --- /dev/null +++ b/src/lib/stores/toastStore.ts @@ -0,0 +1,89 @@ +/** + * Toast notification store + * + * Manages toast notifications across the app with auto-hide and queue support. + */ + +export type ToastType = 'success' | 'error' | 'info'; + +export interface Toast { + message: string; + type: ToastType; +} + +// Current toast state +let currentToast: Toast | null = null; + +// Auto-hide timeout +let hideTimeout: ReturnType | null = null; + +// Listeners for state changes +const listeners = new Set<(toast: Toast | null) => void>(); + +/** + * Get the current toast + */ +export function getToast(): Toast | null { + return currentToast; +} + +/** + * Show a toast notification + * @param message The message to display + * @param type The type of toast (success, error, info) + * @param duration How long to show the toast in ms (default: varies by type) + */ +export function showToast(message: string, type: ToastType = 'info', duration?: number): void { + // Clear existing timeout + if (hideTimeout) { + clearTimeout(hideTimeout); + hideTimeout = null; + } + + // Set the toast + currentToast = { message, type }; + notifyListeners(); + + // Auto-hide based on type + const defaultDurations: Record = { + success: 3000, + error: 5000, + info: 3000 + }; + + const hideAfter = duration ?? defaultDurations[type]; + + hideTimeout = setTimeout(() => { + hideToast(); + }, hideAfter); +} + +/** + * Hide the current toast + */ +export function hideToast(): void { + if (hideTimeout) { + clearTimeout(hideTimeout); + hideTimeout = null; + } + currentToast = null; + notifyListeners(); +} + +/** + * Subscribe to toast changes + * @param listener Callback function called when toast changes + * @returns Unsubscribe function + */ +export function subscribe(listener: (toast: Toast | null) => void): () => void { + listeners.add(listener); + // Immediately notify with current state + listener(currentToast); + return () => listeners.delete(listener); +} + +function notifyListeners(): void { + for (const listener of listeners) { + listener(currentToast); + } +} diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index dd2f31a83..58a0c9e0c 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -15,6 +15,14 @@ type DownloadStatus } from '$lib/stores/downloadState'; + // Toast state management + import { + showToast, + hideToast, + subscribe as subscribeToast, + type Toast as ToastData + } from '$lib/stores/toastStore'; + // Components import Sidebar from '$lib/components/Sidebar.svelte'; import NowPlayingBar from '$lib/components/NowPlayingBar.svelte'; @@ -235,8 +243,8 @@ // Local library track IDs in current queue (for distinguishing from Qobuz tracks) let localTrackIds = $state>(new Set()); - // Toast State - let toast = $state<{ message: string; type: 'success' | 'error' | 'info' } | null>(null); + // Toast State (from store subscription) + let toast = $state(null); // Navigation Functions function navigateTo(view: string) { @@ -1314,15 +1322,6 @@ sidebarRef?.refreshPlaylists(); } - // Toast Functions - function showToast(message: string, type: 'success' | 'error' | 'info' = 'info') { - toast = { message, type }; - } - - function hideToast() { - toast = null; - } - // Auth Handlers function handleLoginSuccess(info: { userName: string; subscription: string }) { isLoggedIn = true; @@ -1499,6 +1498,11 @@ downloadStateVersion++; }); + // Subscribe to toast state changes + const unsubscribeToast = subscribeToast((newToast) => { + toast = newToast; + }); + // Restore Last.fm session on app startup (async () => { try { @@ -1529,6 +1533,7 @@ window.removeEventListener('mouseup', handleMouseNavigation); stopDownloadEventListeners(); unsubscribeDownloads(); + unsubscribeToast(); }; });