game-village-politics-frontend/app/composables/useNotices.ts

27 lines
615 B
TypeScript
Raw Permalink Normal View History

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 }
}