28 lines
591 B
JavaScript
28 lines
591 B
JavaScript
|
|
import { ref } from 'vue'
|
||
|
|
|
||
|
|
// Singleton reactive alert state for the app
|
||
|
|
const current = ref(null)
|
||
|
|
let timeoutId = null
|
||
|
|
|
||
|
|
function show(message) {
|
||
|
|
// message: { heading, message, type: 'success' | 'error' | 'info' }
|
||
|
|
current.value = { ...message, _ts: Date.now() }
|
||
|
|
}
|
||
|
|
|
||
|
|
function clear() {
|
||
|
|
current.value = null
|
||
|
|
}
|
||
|
|
|
||
|
|
function scheduleAutoDismiss(ms = 5000) {
|
||
|
|
if (timeoutId) clearTimeout(timeoutId)
|
||
|
|
if (!current.value) return
|
||
|
|
timeoutId = setTimeout(() => {
|
||
|
|
clear()
|
||
|
|
timeoutId = null
|
||
|
|
}, ms)
|
||
|
|
}
|
||
|
|
|
||
|
|
export function useAlert() {
|
||
|
|
return { current, show, clear, scheduleAutoDismiss }
|
||
|
|
}
|