20 lines
502 B
TypeScript
20 lines
502 B
TypeScript
|
|
import { atom } from 'nanostores'
|
||
|
|
|
||
|
|
export interface Toast {
|
||
|
|
id: string
|
||
|
|
message: string
|
||
|
|
type: 'success' | 'error'
|
||
|
|
}
|
||
|
|
|
||
|
|
export const $toasts = atom<Toast[]>([])
|
||
|
|
|
||
|
|
export const addToast = (message: string, type: 'success' | 'error' = 'success') => {
|
||
|
|
const id = Math.random().toString(36).slice(2)
|
||
|
|
$toasts.set([...$toasts.get(), { id, message, type }])
|
||
|
|
setTimeout(() => removeToast(id), 4000)
|
||
|
|
}
|
||
|
|
|
||
|
|
export const removeToast = (id: string) => {
|
||
|
|
$toasts.set($toasts.get().filter(t => t.id !== id))
|
||
|
|
}
|