062832a1e3
$state runes only work in .svelte and .svelte.ts files. The toasts and theme stores used $state in plain .ts files, which compiled in dev but crashed in production SSR. Renamed and updated all imports. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
36 lines
924 B
TypeScript
36 lines
924 B
TypeScript
let nextId = 0;
|
|
|
|
export type ToastType = 'success' | 'error' | 'warning' | 'info';
|
|
|
|
export type Toast = {
|
|
id: number;
|
|
type: ToastType;
|
|
message: string;
|
|
};
|
|
|
|
let toasts = $state<Toast[]>([]);
|
|
|
|
export function getToasts(): Toast[] {
|
|
return toasts;
|
|
}
|
|
|
|
export function addToast(type: ToastType, message: string, duration = 4000): number {
|
|
const id = nextId++;
|
|
toasts.push({ id, type, message });
|
|
if (duration > 0) {
|
|
setTimeout(() => removeToast(id), duration);
|
|
}
|
|
return id;
|
|
}
|
|
|
|
export function removeToast(id: number) {
|
|
toasts = toasts.filter((t) => t.id !== id);
|
|
}
|
|
|
|
export const toast = {
|
|
success: (msg: string, duration?: number) => addToast('success', msg, duration),
|
|
error: (msg: string, duration?: number) => addToast('error', msg, duration ?? 6000),
|
|
warning: (msg: string, duration?: number) => addToast('warning', msg, duration),
|
|
info: (msg: string, duration?: number) => addToast('info', msg, duration)
|
|
};
|