UNPKG

@trellixio/roaster-coffee

Version:
45 lines (44 loc) 1.34 kB
import { useQueue } from '@/utils'; export default function useToastsState({ limit }) { const { state, queue, update, cleanQueue } = useQueue({ initialValues: [], limit, }); const showToast = (toast) => { const { id } = toast; update((toasts) => { if (toast.id && toasts.some((n) => n.id === toast.id)) { return toasts; } return [...toasts, Object.assign(Object.assign({}, toast), { id })]; }); return id; }; const updateToast = (toast) => update((toasts) => { const index = toasts.findIndex((n) => n.id === toast.id); if (index === -1) { return toasts; } const newToasts = [...toasts]; newToasts[index] = toast; return newToasts; }); const hideToast = (id) => update((toasts) => toasts.filter((toast) => { if (toast.id === id) { // eslint-disable-next-line @typescript-eslint/no-unused-expressions typeof toast.onClose === 'function' && toast.onClose(toast); return false; } return true; })); const clean = () => update(() => []); return { toasts: state, queue, showToast, updateToast, hideToast, cleanQueue, clean, }; }