fix-idb-window
This commit is contained in:
parent
359bfc3f7a
commit
89fcc13016
|
@ -1,11 +1,11 @@
|
||||||
import type { Accessor, JSX } from 'solid-js'
|
import type { Accessor, JSX } from 'solid-js'
|
||||||
import { createContext, createEffect, createMemo, createSignal, useContext } from 'solid-js'
|
import { createContext, createEffect, createMemo, createSignal, onMount, useContext } from 'solid-js'
|
||||||
import { useSession } from './session'
|
import { useSession } from './session'
|
||||||
import { Portal } from 'solid-js/web'
|
import { Portal } from 'solid-js/web'
|
||||||
import { ShowIfAuthenticated } from '../components/_shared/ShowIfAuthenticated'
|
import { ShowIfAuthenticated } from '../components/_shared/ShowIfAuthenticated'
|
||||||
import { NotificationsPanel } from '../components/NotificationsPanel'
|
import { NotificationsPanel } from '../components/NotificationsPanel'
|
||||||
import { createStore } from 'solid-js/store'
|
import { createStore } from 'solid-js/store'
|
||||||
import { openDB } from 'idb'
|
import { IDBPDatabase, openDB } from 'idb'
|
||||||
import { fetchEventSource } from '@microsoft/fetch-event-source'
|
import { fetchEventSource } from '@microsoft/fetch-event-source'
|
||||||
import { getToken } from '../graphql/privateGraphQLClient'
|
import { getToken } from '../graphql/privateGraphQLClient'
|
||||||
import { Author, Message, Reaction, Shout } from '../graphql/types.gen'
|
import { Author, Message, Reaction, Shout } from '../graphql/types.gen'
|
||||||
|
@ -42,16 +42,19 @@ export const NotificationsProvider = (props: { children: JSX.Element }) => {
|
||||||
const [notificationEntities, setNotificationEntities] = createStore<Record<number, ServerNotification>>(
|
const [notificationEntities, setNotificationEntities] = createStore<Record<number, ServerNotification>>(
|
||||||
{}
|
{}
|
||||||
)
|
)
|
||||||
|
const [db, setDb] = createSignal<Promise<IDBPDatabase<unknown>>>()
|
||||||
const dbPromise = openDB('notifications-db', 1, {
|
onMount(() => {
|
||||||
|
const dbx = openDB('notifications-db', 1, {
|
||||||
upgrade(db) {
|
upgrade(db) {
|
||||||
db.createObjectStore('notifications')
|
db.createObjectStore('notifications')
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
setDb(dbx)
|
||||||
|
})
|
||||||
|
|
||||||
const loadNotifications = async () => {
|
const loadNotifications = async () => {
|
||||||
const db = await dbPromise
|
const storage = await db()
|
||||||
const notifications = await db.getAll('notifications')
|
const notifications = await storage.getAll('notifications')
|
||||||
const totalUnreadCount = notifications.filter((notification) => !notification.read).length
|
const totalUnreadCount = notifications.filter((notification) => !notification.read).length
|
||||||
|
|
||||||
setUnreadNotificationsCount(totalUnreadCount)
|
setUnreadNotificationsCount(totalUnreadCount)
|
||||||
|
@ -70,16 +73,11 @@ export const NotificationsProvider = (props: { children: JSX.Element }) => {
|
||||||
})
|
})
|
||||||
|
|
||||||
const storeNotification = async (notification: ServerNotification) => {
|
const storeNotification = async (notification: ServerNotification) => {
|
||||||
const db = await dbPromise
|
const storage = await db()
|
||||||
const tx = db.transaction('notifications', 'readwrite')
|
const tx = storage.transaction('notifications', 'readwrite')
|
||||||
const store = tx.objectStore('notifications')
|
const store = tx.objectStore('notifications')
|
||||||
const id = Date.now()
|
|
||||||
const data: ServerNotification = {
|
await store.put(notification)
|
||||||
...notification,
|
|
||||||
timestamp: id,
|
|
||||||
seen: false
|
|
||||||
}
|
|
||||||
await store.put(data, id)
|
|
||||||
await tx.done
|
await tx.done
|
||||||
loadNotifications()
|
loadNotifications()
|
||||||
}
|
}
|
||||||
|
@ -122,8 +120,8 @@ export const NotificationsProvider = (props: { children: JSX.Element }) => {
|
||||||
})
|
})
|
||||||
|
|
||||||
const markNotificationAsRead = async (notification: ServerNotification) => {
|
const markNotificationAsRead = async (notification: ServerNotification) => {
|
||||||
const db = await dbPromise
|
const storage = await db()
|
||||||
await db.put('notifications', { ...notification, seen: true })
|
await storage.put('notifications', { ...notification, seen: true })
|
||||||
loadNotifications()
|
loadNotifications()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -106,7 +106,8 @@ export default defineConfig(({ mode, command }) => {
|
||||||
'@tiptap/extension-link',
|
'@tiptap/extension-link',
|
||||||
'@tiptap/extension-image',
|
'@tiptap/extension-image',
|
||||||
'@tiptap/extension-character-count',
|
'@tiptap/extension-character-count',
|
||||||
'clsx'
|
'clsx',
|
||||||
|
'idb'
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user