47 lines
1.1 KiB
TypeScript
47 lines
1.1 KiB
TypeScript
|
import { defineStore } from "pinia";
|
||
|
|
||
|
export interface Notification {
|
||
|
id: number;
|
||
|
title: string;
|
||
|
text: string;
|
||
|
type: NotificationType;
|
||
|
indicator: boolean;
|
||
|
}
|
||
|
|
||
|
export type NotificationType = "info" | "warning" | "error";
|
||
|
|
||
|
export const useNotificationStore = defineStore("notification", {
|
||
|
state: () => {
|
||
|
return {
|
||
|
notifications: [] as Array<Notification>,
|
||
|
timeouts: {} as { [key: string]: any },
|
||
|
};
|
||
|
},
|
||
|
actions: {
|
||
|
push(title: string, text: string, type: NotificationType, timeout: number = 5000) {
|
||
|
let id = Date.now();
|
||
|
this.notifications.push({
|
||
|
id,
|
||
|
title,
|
||
|
text,
|
||
|
type,
|
||
|
indicator: false,
|
||
|
});
|
||
|
setTimeout(() => {
|
||
|
this.notifications[this.notifications.findIndex((n) => n.id === id)].indicator = true;
|
||
|
}, 100);
|
||
|
this.timeouts[id] = setTimeout(() => {
|
||
|
this.revoke(id);
|
||
|
}, timeout);
|
||
|
},
|
||
|
revoke(id: number) {
|
||
|
this.notifications.splice(
|
||
|
this.notifications.findIndex((n) => n.id === id),
|
||
|
1
|
||
|
);
|
||
|
clearTimeout(this.timeouts[id]);
|
||
|
delete this.timeouts[id];
|
||
|
},
|
||
|
},
|
||
|
});
|