webapp/src/context/notifications.tsx

122 lines
4.2 KiB
TypeScript
Raw Normal View History

import type { Accessor, JSX } from 'solid-js'
2023-11-28 13:18:25 +00:00
import { createContext, createMemo, createSignal, onMount, useContext } from 'solid-js'
import { createStore } from 'solid-js/store'
import { Portal } from 'solid-js/web'
import { ShowIfAuthenticated } from '../components/_shared/ShowIfAuthenticated'
import { NotificationsPanel } from '../components/NotificationsPanel'
2023-11-28 13:18:25 +00:00
import { notifierClient as apiClient } from '../graphql/client/notifier'
import { Notification } from '../graphql/schema/notifier.gen'
2023-11-28 15:36:00 +00:00
2023-11-28 13:18:25 +00:00
import { SSEMessage, useConnect } from './connect'
2023-10-20 18:07:33 +00:00
type NotificationsContextType = {
2023-11-17 16:22:54 +00:00
notificationEntities: Record<number, Notification>
unreadNotificationsCount: Accessor<number>
sortedNotifications: Accessor<Notification[]>
loadedNotificationsCount: Accessor<number>
totalNotificationsCount: Accessor<number>
actions: {
showNotificationsPanel: () => void
hideNotificationsPanel: () => void
markNotificationAsRead: (notification: Notification) => Promise<void>
markAllNotificationsAsRead: () => Promise<void>
loadNotifications: (options: { limit: number; offset: number }) => Promise<Notification[]>
}
}
export const PAGE_SIZE = 20
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 [totalNotificationsCount, setTotalNotificationsCount] = createSignal(0)
2023-11-17 16:22:54 +00:00
const [notificationEntities, setNotificationEntities] = createStore<Record<number, Notification>>({})
2023-11-28 13:18:25 +00:00
const { addHandler } = useConnect()
const loadNotifications = async (options: { limit: number; offset?: number }) => {
2023-11-28 13:18:25 +00:00
const { notifications, unread, total } = await apiClient.getNotifications(options)
const newNotificationEntities = notifications.reduce((acc, notification) => {
acc[notification.id] = notification
return acc
}, {})
2023-11-28 13:18:25 +00:00
setTotalNotificationsCount(total)
setUnreadNotificationsCount(unread)
2023-11-17 16:22:54 +00:00
setNotificationEntities(newNotificationEntities)
return notifications
}
const sortedNotifications = createMemo(() => {
2023-11-28 13:18:25 +00:00
return Object.values(notificationEntities).sort((a, b) => b.created_at - a.created_at)
})
const loadedNotificationsCount = createMemo(() => Object.keys(notificationEntities).length)
2023-11-28 13:18:25 +00:00
onMount(() => {
addHandler((data: SSEMessage) => {
if (data.entity === 'reaction') {
loadNotifications({ limit: Math.max(PAGE_SIZE, loadedNotificationsCount()) })
} else {
console.error(`[NotificationsProvider] unhandled message type: ${JSON.stringify(data)}`)
}
})
})
const markNotificationAsRead = async (notification: Notification) => {
await apiClient.markNotificationAsRead(notification.id)
2023-11-28 13:18:25 +00:00
const nnn = new Set([...notification.seen, notification.id])
2023-11-28 15:36:00 +00:00
setNotificationEntities(notification.id, 'seen', [...nnn])
setUnreadNotificationsCount((oldCount) => oldCount - 1)
}
const markAllNotificationsAsRead = async () => {
await apiClient.markAllNotificationsAsRead()
loadNotifications({ limit: loadedNotificationsCount() })
}
const showNotificationsPanel = () => {
setIsNotificationsPanelOpen(true)
}
const hideNotificationsPanel = () => {
setIsNotificationsPanelOpen(false)
}
2023-10-16 18:00:22 +00:00
const actions = {
showNotificationsPanel,
hideNotificationsPanel,
markNotificationAsRead,
markAllNotificationsAsRead,
loadNotifications,
2023-10-16 18:00:22 +00:00
}
const value: NotificationsContextType = {
notificationEntities,
sortedNotifications,
unreadNotificationsCount,
loadedNotificationsCount,
totalNotificationsCount,
actions,
}
const handleNotificationPanelClose = () => {
setIsNotificationsPanelOpen(false)
}
return (
<NotificationsContext.Provider value={value}>
{props.children}
<ShowIfAuthenticated>
<Portal>
<NotificationsPanel isOpen={isNotificationsPanelOpen()} onClose={handleNotificationPanelClose} />
</Portal>
</ShowIfAuthenticated>
</NotificationsContext.Provider>
)
}