blob: ab45325b0ac9fd74c0bdc072e0b8d3030b2c0138 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
import { create } from 'zustand'
export type ToastType = 'success' | 'error' | 'info' | 'warning'
export interface Toast {
id: string
message: string
type: ToastType
}
interface ToastState {
toasts: Toast[]
addToast: (message: string, type?: ToastType, duration?: number) => void
removeToast: (id: string) => void
}
let counter = 0
export const useToastStore = create<ToastState>((set, get) => ({
toasts: [],
addToast: (message, type = 'info', duration = 3500) => {
const id = `toast-${++counter}`
set((s) => ({ toasts: [...s.toasts, { id, message, type }] }))
setTimeout(() => {
get().removeToast(id)
}, duration)
},
removeToast: (id) => {
set((s) => ({ toasts: s.toasts.filter((t) => t.id !== id) }))
},
}))
|