You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
18 lines
676 B
TypeScript
18 lines
676 B
TypeScript
/** Pure toast state — testable without a DOM. */
|
|
|
|
export interface Toast { id: number; tone: 'ok' | 'err'; message: string }
|
|
export interface ToastState { nextId: number; toasts: Toast[] }
|
|
|
|
export const TOAST_LIMIT = 3
|
|
export const initialToasts: ToastState = { nextId: 1, toasts: [] }
|
|
|
|
export function pushToast(s: ToastState, tone: Toast['tone'], message: string): ToastState {
|
|
const toast: Toast = { id: s.nextId, tone, message }
|
|
const toasts = [...s.toasts, toast].slice(-TOAST_LIMIT)
|
|
return { nextId: s.nextId + 1, toasts }
|
|
}
|
|
|
|
export function dismissToast(s: ToastState, id: number): ToastState {
|
|
return { ...s, toasts: s.toasts.filter((t) => t.id !== id) }
|
|
}
|