ff-admin/src/stores/notification.ts

49 lines
1.2 KiB
TypeScript
Raw Normal View History

import { defineStore } from "pinia";
export interface Notification {
id: string;
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()}_${Math.random()}`;
this.notifications.push({
id,
title,
text,
type,
indicator: false,
});
2025-01-12 14:05:08 +01:00
if (timeout != 0) {
setTimeout(() => {
this.notifications[this.notifications.findIndex((n) => n.id === id)].indicator = true;
}, 100);
this.timeouts[id] = setTimeout(() => {
this.revoke(id);
}, timeout);
}
},
revoke(id: string) {
this.notifications.splice(
this.notifications.findIndex((n) => n.id === id),
1
);
clearTimeout(this.timeouts[id]);
delete this.timeouts[id];
},
},
});