44 lines
1.0 KiB
TypeScript
44 lines
1.0 KiB
TypeScript
import { ref, readonly } from 'vue'
|
|
import { useI18n } from 'vue-i18n'
|
|
|
|
type NotificationType = 'success' | 'error'
|
|
|
|
interface Notification {
|
|
show: boolean
|
|
type: NotificationType
|
|
message: string
|
|
}
|
|
|
|
const notification = ref<Notification>({
|
|
show: false,
|
|
type: 'success',
|
|
message: ''
|
|
})
|
|
|
|
let timeoutId: number | null = null
|
|
|
|
export function useNotification() {
|
|
const { t } = useI18n()
|
|
|
|
const showNotification = (messageKey: string, type: NotificationType = 'success', params?: Record<string, any>) => {
|
|
const message = params ? t(messageKey, params) : t(messageKey)
|
|
|
|
// Очищаем предыдущий таймер, чтобы уведомление не закрылось раньше времени
|
|
if (timeoutId) {
|
|
clearTimeout(timeoutId)
|
|
timeoutId = null
|
|
}
|
|
|
|
notification.value = { show: true, type, message }
|
|
timeoutId = window.setTimeout(() => {
|
|
notification.value.show = false
|
|
timeoutId = null
|
|
}, 3000)
|
|
}
|
|
|
|
return {
|
|
notification: readonly(notification),
|
|
showNotification
|
|
}
|
|
}
|