mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-15 04:29:37 +01:00
39 lines
1.0 KiB
TypeScript
39 lines
1.0 KiB
TypeScript
import type { Notification } from '../types/notification'
|
|
import { useState } from '#imports'
|
|
|
|
export function useToast () {
|
|
const notifications = useState<Notification[]>('notifications', () => [])
|
|
|
|
function add (notification: Partial<Notification>) {
|
|
const body = {
|
|
id: new Date().getTime().toString(),
|
|
...notification
|
|
}
|
|
|
|
const index = notifications.value.findIndex((n: Notification) => n.id === body.id)
|
|
if (index === -1) {
|
|
notifications.value.push(body as Notification)
|
|
}
|
|
|
|
return body
|
|
}
|
|
|
|
function remove (id: string) {
|
|
notifications.value = notifications.value.filter((n: Notification) => n.id !== id)
|
|
}
|
|
|
|
function update (id: string, notification: Partial<Notification>) {
|
|
const index = notifications.value.findIndex((n: Notification) => n.id === id)
|
|
if (index !== -1) {
|
|
const previous = notifications.value[index]
|
|
notifications.value.splice(index, 1, { ...previous, ...notification })
|
|
}
|
|
}
|
|
|
|
return {
|
|
add,
|
|
remove,
|
|
update
|
|
}
|
|
}
|