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
This commit is contained in:
vicrodh committed 2026-01-10 04:25:45 -06:00
1 parent 95488014a6
commit 0e8371d900
2 files changed
+105 -11

No files matched your search

+89
View File
@@ -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<typeof setTimeout> | 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<ToastType, number> = {
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);
}
}
+16 -11
View File
@@ -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<Set<number>>(new Set());
// Toast State
let toast = $state<{ message: string; type: 'success' | 'error' | 'info' } | null>(null);
// Toast State (from store subscription)
let toast = $state<ToastData | null>(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();
};
});