| import { reactive } from "vue"; |
| import { IToast } from "./ToastInterfaces"; |
| import { randomID } from "@/Core/Utils/MiscUtils"; |
| import { IToastOptions, PopupVariant } from "../Popups/InterfacesAndEnums"; |
| import { showSystemNotification } from "@/UI/MessageAlerts/SystemNotifications"; |
| import { toTitleCase } from "@/Core/Utils/StringUtils"; |
|
|
| |
| |
| |
| export const toasts = reactive<IToast[]>([]); |
|
|
| |
| |
| |
| const toastInstances: { [key: string]: any } = {}; |
|
|
| |
| |
| |
| |
| |
| |
| export function registerToastInstance(id: string, instance: any) { |
| toastInstances[id] = instance; |
| } |
|
|
| |
| |
| |
| |
| |
| export function unregisterToastInstance(id: string) { |
| delete toastInstances[id]; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function addToast( |
| title: string, |
| message: string, |
| variant: PopupVariant = PopupVariant.Info, |
| callBack?: () => void, |
| options?: IToastOptions |
| ) { |
| |
| const isDuplicate = toasts.some( |
| (toast) => toast.title === title && toast.message === message |
| ); |
| if (isDuplicate) { |
| return; |
| } |
|
|
| const toastOptions = { ...options }; |
| |
| if (toastOptions.duration === undefined) { |
| const words = message.trim().split(/\s+/).length; |
| const readingSpeedWPM = 150; |
| const baseDurationMs = 4000; |
| const extraTimePerWordMs = (60 / readingSpeedWPM) * 1000; |
| toastOptions.duration = baseDurationMs + words * extraTimePerWordMs; |
| } |
|
|
| toasts.push({ |
| id: randomID(), |
| title: toTitleCase(title), |
| message, |
| variant, |
| timestamp: new Date().toLocaleTimeString([], { |
| hour: "2-digit", |
| minute: "2-digit", |
| }), |
| callBack, |
| ...toastOptions, |
| }); |
| |
| if ( |
| "Notification" in window && |
| (!document.hasFocus() || Notification.permission === "default") |
| ) { |
| showSystemNotification(title, message); |
| } |
| |
| } |
|
|
| |
| |
| |
| |
| export function clearAllToasts() { |
| Object.values(toastInstances).forEach((instance) => { |
| if (instance) { |
| instance.hide(); |
| } |
| }); |
| } |
| |
| |
| |
| |
| |
| export function removeToast(id: string) { |
| const index = toasts.findIndex((t) => t.id === id); |
| if (index > -1) { |
| toasts.splice(index, 1); |
| } |
| } |
|
|