2023-10-14 11:39:24 +00:00
|
|
|
import type { Accessor, JSX } from 'solid-js'
|
2023-10-14 23:27:33 +00:00
|
|
|
import { createContext, createEffect, createMemo, createSignal, onMount, useContext } from 'solid-js'
|
2023-10-14 11:39:24 +00:00
|
|
|
import { useSession } from './session'
|
|
|
|
import { Portal } from 'solid-js/web'
|
|
|
|
import { ShowIfAuthenticated } from '../components/_shared/ShowIfAuthenticated'
|
|
|
|
import { NotificationsPanel } from '../components/NotificationsPanel'
|
|
|
|
import { createStore } from 'solid-js/store'
|
2023-10-14 23:27:33 +00:00
|
|
|
import { IDBPDatabase, openDB } from 'idb'
|
2023-10-18 09:46:35 +00:00
|
|
|
// import { fetchEventSource } from '@microsoft/fetch-event-source'
|
2023-10-14 23:05:07 +00:00
|
|
|
import { getToken } from '../graphql/privateGraphQLClient'
|
|
|
|
import { Author, Message, Reaction, Shout } from '../graphql/types.gen'
|
|
|
|
|
|
|
|
export interface ServerNotification {
|
|
|
|
kind: string
|
|
|
|
payload: any // Author | Shout | Reaction | Message
|
|
|
|
timestamp: number
|
|
|
|
seen: boolean
|
|
|
|
}
|
2023-10-14 11:39:24 +00:00
|
|
|
|
2023-10-14 23:05:07 +00:00
|
|
|
type MessageHandler = (m: Message) => void
|
2023-10-14 11:39:24 +00:00
|
|
|
type NotificationsContextType = {
|
2023-10-14 23:05:07 +00:00
|
|
|
notificationEntities: Record<number, ServerNotification>
|
2023-10-14 11:39:24 +00:00
|
|
|
unreadNotificationsCount: Accessor<number>
|
2023-10-14 23:05:07 +00:00
|
|
|
sortedNotifications: Accessor<ServerNotification[]>
|
2023-10-14 11:39:24 +00:00
|
|
|
actions: {
|
|
|
|
showNotificationsPanel: () => void
|
2023-10-16 17:24:33 +00:00
|
|
|
hideNotificationsPanel: () => void
|
2023-10-14 23:05:07 +00:00
|
|
|
markNotificationAsRead: (notification: ServerNotification) => Promise<void>
|
2023-10-16 21:45:22 +00:00
|
|
|
setMessageHandler: (h: MessageHandler) => void
|
2023-10-14 11:39:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const NotificationsContext = createContext<NotificationsContextType>()
|
|
|
|
|
|
|
|
export function useNotifications() {
|
|
|
|
return useContext(NotificationsContext)
|
|
|
|
}
|
|
|
|
|
|
|
|
export const NotificationsProvider = (props: { children: JSX.Element }) => {
|
|
|
|
const [isNotificationsPanelOpen, setIsNotificationsPanelOpen] = createSignal(false)
|
|
|
|
const [unreadNotificationsCount, setUnreadNotificationsCount] = createSignal(0)
|
|
|
|
const { isAuthenticated, user } = useSession()
|
2023-10-14 23:05:07 +00:00
|
|
|
const [notificationEntities, setNotificationEntities] = createStore<Record<number, ServerNotification>>(
|
|
|
|
{}
|
|
|
|
)
|
2023-10-14 23:27:33 +00:00
|
|
|
const [db, setDb] = createSignal<Promise<IDBPDatabase<unknown>>>()
|
|
|
|
onMount(() => {
|
|
|
|
const dbx = openDB('notifications-db', 1, {
|
|
|
|
upgrade(db) {
|
|
|
|
db.createObjectStore('notifications')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
setDb(dbx)
|
2023-10-14 23:05:07 +00:00
|
|
|
})
|
2023-10-14 11:39:24 +00:00
|
|
|
|
|
|
|
const loadNotifications = async () => {
|
2023-10-14 23:27:33 +00:00
|
|
|
const storage = await db()
|
|
|
|
const notifications = await storage.getAll('notifications')
|
2023-10-18 09:46:35 +00:00
|
|
|
console.log('[context.notifications] Loaded notifications:', notifications)
|
|
|
|
|
2023-10-14 23:05:07 +00:00
|
|
|
const totalUnreadCount = notifications.filter((notification) => !notification.read).length
|
2023-10-18 09:46:35 +00:00
|
|
|
console.log('[context.notifications] Total unread count:', totalUnreadCount)
|
2023-10-14 11:39:24 +00:00
|
|
|
|
|
|
|
setUnreadNotificationsCount(totalUnreadCount)
|
2023-10-14 23:05:07 +00:00
|
|
|
setNotificationEntities(
|
|
|
|
notifications.reduce((acc, notification) => {
|
|
|
|
acc[notification.id] = notification
|
|
|
|
return acc
|
|
|
|
}, {})
|
|
|
|
)
|
|
|
|
|
2023-10-14 11:39:24 +00:00
|
|
|
return notifications
|
|
|
|
}
|
|
|
|
|
|
|
|
const sortedNotifications = createMemo(() => {
|
2023-10-14 23:05:07 +00:00
|
|
|
return Object.values(notificationEntities).sort((a, b) => b.timestamp - a.timestamp)
|
2023-10-14 11:39:24 +00:00
|
|
|
})
|
|
|
|
|
2023-10-14 23:05:07 +00:00
|
|
|
const storeNotification = async (notification: ServerNotification) => {
|
2023-10-18 09:46:35 +00:00
|
|
|
console.log('[context.notifications] Storing notification:', notification)
|
|
|
|
|
2023-10-14 23:27:33 +00:00
|
|
|
const storage = await db()
|
|
|
|
const tx = storage.transaction('notifications', 'readwrite')
|
2023-10-14 23:05:07 +00:00
|
|
|
const store = tx.objectStore('notifications')
|
2023-10-14 23:27:33 +00:00
|
|
|
|
|
|
|
await store.put(notification)
|
2023-10-14 23:05:07 +00:00
|
|
|
await tx.done
|
|
|
|
loadNotifications()
|
|
|
|
}
|
|
|
|
|
|
|
|
const [messageHandler, setMessageHandler] = createSignal<(m: Message) => void>()
|
|
|
|
|
2023-10-18 09:56:07 +00:00
|
|
|
createEffect(() => {
|
2023-10-14 11:39:24 +00:00
|
|
|
if (isAuthenticated()) {
|
|
|
|
loadNotifications()
|
|
|
|
|
2023-10-18 09:46:35 +00:00
|
|
|
const token = getToken()
|
|
|
|
const eventSource = new EventSource(`https://chat.discours.io/connect/${token}`)
|
|
|
|
|
|
|
|
eventSource.onmessage = (event) => {
|
|
|
|
const n: { kind: string; payload: any } = JSON.parse(event.data)
|
|
|
|
if (n.kind === 'new_message') {
|
|
|
|
messageHandler()(n.payload)
|
|
|
|
} else {
|
|
|
|
console.log('[context.notifications] Received notification:', n)
|
|
|
|
storeNotification({
|
|
|
|
kind: n.kind,
|
|
|
|
payload: n.payload,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
seen: false
|
|
|
|
})
|
2023-10-14 11:39:24 +00:00
|
|
|
}
|
2023-10-18 09:46:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
eventSource.onerror = (err) => {
|
|
|
|
console.error('[context.notifications] sse connection closed by error', err)
|
|
|
|
eventSource.close()
|
|
|
|
}
|
2023-10-14 11:39:24 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-10-14 23:05:07 +00:00
|
|
|
const markNotificationAsRead = async (notification: ServerNotification) => {
|
2023-10-18 09:46:35 +00:00
|
|
|
console.log('[context.notifications] Marking notification as read:', notification)
|
|
|
|
|
2023-10-14 23:27:33 +00:00
|
|
|
const storage = await db()
|
|
|
|
await storage.put('notifications', { ...notification, seen: true })
|
2023-10-14 11:39:24 +00:00
|
|
|
loadNotifications()
|
|
|
|
}
|
|
|
|
|
|
|
|
const showNotificationsPanel = () => {
|
|
|
|
setIsNotificationsPanelOpen(true)
|
|
|
|
}
|
|
|
|
|
2023-10-16 17:24:33 +00:00
|
|
|
const hideNotificationsPanel = () => {
|
|
|
|
setIsNotificationsPanelOpen(false)
|
|
|
|
}
|
|
|
|
|
2023-10-16 18:00:22 +00:00
|
|
|
const actions = {
|
|
|
|
setMessageHandler,
|
|
|
|
showNotificationsPanel,
|
|
|
|
hideNotificationsPanel,
|
|
|
|
markNotificationAsRead
|
|
|
|
}
|
2023-10-14 11:39:24 +00:00
|
|
|
|
|
|
|
const value: NotificationsContextType = {
|
|
|
|
notificationEntities,
|
|
|
|
sortedNotifications,
|
|
|
|
unreadNotificationsCount,
|
|
|
|
actions
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleNotificationPanelClose = () => {
|
|
|
|
setIsNotificationsPanelOpen(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<NotificationsContext.Provider value={value}>
|
|
|
|
{props.children}
|
|
|
|
<ShowIfAuthenticated>
|
|
|
|
<Portal>
|
|
|
|
<NotificationsPanel isOpen={isNotificationsPanelOpen()} onClose={handleNotificationPanelClose} />
|
|
|
|
</Portal>
|
|
|
|
</ShowIfAuthenticated>
|
|
|
|
</NotificationsContext.Provider>
|
|
|
|
)
|
|
|
|
}
|