Files
litlyx/dashboard/composables/useAlert.ts
2024-07-29 16:07:15 +02:00

34 lines
643 B
TypeScript

export type Alert = {
title: string,
text: string,
icon: string,
ms: number,
id: number
}
const alerts = ref<Alert[]>([]);
const idPool = {
id: 0,
getId() {
return idPool.id++;
}
}
function createAlert(title: string, text: string, icon: string, ms: number) {
const alert: Alert = { title, text, icon, ms, id: idPool.getId() }
alerts.value.push(alert);
setTimeout(() => {
closeAlert(alert.id);
}, ms)
}
function closeAlert(id: number) {
alerts.value = alerts.value.filter(e => e.id != id);
}
export function useAlert() {
return { alerts, createAlert, closeAlert }
}