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:
1 parent
95488014a6
commit
0e8371d900
2 files changed
+105
-11
No files matched your search
@@ -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
@@ -15,6 +15,14 @@
|
|||||||
type DownloadStatus
|
type DownloadStatus
|
||||||
} from '$lib/stores/downloadState';
|
} from '$lib/stores/downloadState';
|
||||||
|
|
||||||
|
// Toast state management
|
||||||
|
import {
|
||||||
|
showToast,
|
||||||
|
hideToast,
|
||||||
|
subscribe as subscribeToast,
|
||||||
|
type Toast as ToastData
|
||||||
|
} from '$lib/stores/toastStore';
|
||||||
|
|
||||||
// Components
|
// Components
|
||||||
import Sidebar from '$lib/components/Sidebar.svelte';
|
import Sidebar from '$lib/components/Sidebar.svelte';
|
||||||
import NowPlayingBar from '$lib/components/NowPlayingBar.svelte';
|
import NowPlayingBar from '$lib/components/NowPlayingBar.svelte';
|
||||||
@@ -235,8 +243,8 @@
|
|||||||
// Local library track IDs in current queue (for distinguishing from Qobuz tracks)
|
// Local library track IDs in current queue (for distinguishing from Qobuz tracks)
|
||||||
let localTrackIds = $state<Set<number>>(new Set());
|
let localTrackIds = $state<Set<number>>(new Set());
|
||||||
|
|
||||||
// Toast State
|
// Toast State (from store subscription)
|
||||||
let toast = $state<{ message: string; type: 'success' | 'error' | 'info' } | null>(null);
|
let toast = $state<ToastData | null>(null);
|
||||||
|
|
||||||
// Navigation Functions
|
// Navigation Functions
|
||||||
function navigateTo(view: string) {
|
function navigateTo(view: string) {
|
||||||
@@ -1314,15 +1322,6 @@
|
|||||||
sidebarRef?.refreshPlaylists();
|
sidebarRef?.refreshPlaylists();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Toast Functions
|
|
||||||
function showToast(message: string, type: 'success' | 'error' | 'info' = 'info') {
|
|
||||||
toast = { message, type };
|
|
||||||
}
|
|
||||||
|
|
||||||
function hideToast() {
|
|
||||||
toast = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Auth Handlers
|
// Auth Handlers
|
||||||
function handleLoginSuccess(info: { userName: string; subscription: string }) {
|
function handleLoginSuccess(info: { userName: string; subscription: string }) {
|
||||||
isLoggedIn = true;
|
isLoggedIn = true;
|
||||||
@@ -1499,6 +1498,11 @@
|
|||||||
downloadStateVersion++;
|
downloadStateVersion++;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Subscribe to toast state changes
|
||||||
|
const unsubscribeToast = subscribeToast((newToast) => {
|
||||||
|
toast = newToast;
|
||||||
|
});
|
||||||
|
|
||||||
// Restore Last.fm session on app startup
|
// Restore Last.fm session on app startup
|
||||||
(async () => {
|
(async () => {
|
||||||
try {
|
try {
|
||||||
@@ -1529,6 +1533,7 @@
|
|||||||
window.removeEventListener('mouseup', handleMouseNavigation);
|
window.removeEventListener('mouseup', handleMouseNavigation);
|
||||||
stopDownloadEventListeners();
|
stopDownloadEventListeners();
|
||||||
unsubscribeDownloads();
|
unsubscribeDownloads();
|
||||||
|
unsubscribeToast();
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user