Files
iiko-connector/frontend/src/composables/useNotification.ts
2026-04-24 01:44:03 +03:00

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
}
}