27 lines
615 B
TypeScript
27 lines
615 B
TypeScript
export type NoticeType = 'success' | 'error' | 'warning' | 'info'
|
|
|
|
export interface Notice {
|
|
id: number
|
|
text: string
|
|
type: NoticeType
|
|
}
|
|
|
|
let nextId = 1
|
|
|
|
/** Short-lived messages shown at the top of every page. */
|
|
export function useNotices() {
|
|
const list = useState<Notice[]>('notices', () => [])
|
|
|
|
function dismiss(id: number) {
|
|
list.value = list.value.filter((n) => n.id !== id)
|
|
}
|
|
|
|
function push(text: string, type: NoticeType = 'info') {
|
|
const id = nextId++
|
|
list.value = [...list.value, { id, text, type }]
|
|
setTimeout(() => dismiss(id), 6000)
|
|
}
|
|
|
|
return { list, push, dismiss }
|
|
}
|