38 lines
975 B
TypeScript
38 lines
975 B
TypeScript
import { defineStore } from "pinia"
|
|
|
|
export type NotificationType = "success" | "error"
|
|
|
|
export interface Notification {
|
|
id: number
|
|
type: NotificationType
|
|
message: string
|
|
}
|
|
|
|
let counter = 0
|
|
|
|
/** App-wide toast notifications. Surfaced for both successes and errors so the
|
|
* user always gets feedback on async outcomes. */
|
|
export const useNotificationsStore = defineStore("notifications", {
|
|
state: () => ({
|
|
items: [] as Notification[],
|
|
}),
|
|
actions: {
|
|
notify(type: NotificationType, message: string, duration = 4000) {
|
|
const id = ++counter
|
|
this.items.push({ id, type, message })
|
|
if (duration) {
|
|
setTimeout(() => this.dismiss(id), duration)
|
|
}
|
|
return id
|
|
},
|
|
success(message: string) {
|
|
return this.notify("success", message)
|
|
},
|
|
error(message: string) {
|
|
return this.notify("error", message)
|
|
},
|
|
dismiss(id: number) {
|
|
this.items = this.items.filter((n) => n.id !== id)
|
|
},
|
|
},
|
|
})
|