Files
haex-hub-mirror/src/stores/vault/notifications.ts
haex 6a7f58a513 Fix production build crash by resolving circular import dependency
Moved database schemas from src-tauri/database/schemas/ to src/database/schemas/
to fix bundling issues and resolved circular import dependency that caused
"Cannot access uninitialized variable" error in production builds.

Key changes:
- Moved crdtColumnNames definition into haex.ts to break circular dependency
- Restored .$defaultFn(() => crypto.randomUUID()) calls
- Kept AnySQLiteColumn type annotations
- Removed obsolete TDZ fix script (no longer needed)
- Updated all import paths across stores and configuration files
2025-11-02 00:57:03 +01:00

124 lines
3.5 KiB
TypeScript

import { and, eq, or, type SQLWrapper } from 'drizzle-orm'
import {
haexNotifications,
type InsertHaexNotifications,
} from '~/database/schemas/haex'
import {
isPermissionGranted,
requestPermission,
sendNotification,
} from '@tauri-apps/plugin-notification'
export interface IHaexNotification {
id: string
title: string | null
text?: string | null
icon?: string | null
image?: string | null
alt?: string | null
date: string | null
type?: 'error' | 'success' | 'warning' | 'info' | 'log' | null
}
export const useNotificationStore = defineStore('notificationStore', () => {
const isNotificationAllowed = ref<boolean>(false)
const requestNotificationPermissionAsync = async () => {
console.log('requestNotificationPermissionAsync')
const permission = await requestPermission()
console.log('got permission', permission)
isNotificationAllowed.value = permission === 'granted'
}
const checkNotificationAsync = async () => {
try {
isNotificationAllowed.value = await isPermissionGranted()
} catch (error) {
console.warn('Notification permission check failed:', error)
isNotificationAllowed.value = false
}
return isNotificationAllowed.value
}
const notifications = ref<IHaexNotification[]>([])
const readNotificationsAsync = async (filter?: SQLWrapper[]) => {
const { currentVault } = storeToRefs(useVaultStore())
if (filter) {
return await currentVault.value?.drizzle
.select()
.from(haexNotifications)
.where(and(...filter))
} else {
return await currentVault.value?.drizzle.select().from(haexNotifications)
}
}
const syncNotificationsAsync = async () => {
notifications.value =
(await readNotificationsAsync([eq(haexNotifications.read, false)])) ?? []
}
const addNotificationAsync = async (
notification: Partial<InsertHaexNotifications>,
) => {
const { currentVault } = storeToRefs(useVaultStore())
try {
const _notification: InsertHaexNotifications = {
id: crypto.randomUUID(),
alt: notification.alt,
date: notification.date || new Date().toUTCString(),
icon: notification.icon,
image: notification.image,
read: notification.read || false,
source: notification.source,
text: notification.text,
title: notification.title,
type: notification.type || 'info',
}
await currentVault.value?.drizzle
.insert(haexNotifications)
.values(_notification)
await syncNotificationsAsync()
if (!isNotificationAllowed.value) {
const permission = await requestPermission()
isNotificationAllowed.value = permission === 'granted'
}
if (isNotificationAllowed.value) {
sendNotification({
title: _notification.title!,
body: _notification.text!,
})
}
} catch (error) {
console.error(error)
}
}
const deleteNotificationsAsync = async (notificationIds: string[]) => {
const { currentVault } = storeToRefs(useVaultStore())
const filter = notificationIds.map((id) => eq(haexNotifications.id, id))
console.log('deleteNotificationsAsync', notificationIds)
return currentVault.value?.drizzle
.delete(haexNotifications)
.where(or(...filter))
}
return {
addNotificationAsync,
checkNotificationAsync,
deleteNotificationsAsync,
isNotificationAllowed,
notifications,
readNotificationsAsync,
requestNotificationPermissionAsync,
syncNotificationsAsync,
}
})