2023-10-14 11:39:24 +00:00
|
|
|
import type { Accessor, JSX } from 'solid-js'
|
2023-11-14 15:10:00 +00:00
|
|
|
|
2023-10-14 11:39:24 +00:00
|
|
|
import { createContext, createEffect, createMemo, createSignal, useContext } from 'solid-js'
|
2023-11-14 15:10:00 +00:00
|
|
|
import { createStore } from 'solid-js/store'
|
2023-10-14 11:39:24 +00:00
|
|
|
import { Portal } from 'solid-js/web'
|
2023-11-14 15:10:00 +00:00
|
|
|
|
2023-10-14 11:39:24 +00:00
|
|
|
import { ShowIfAuthenticated } from '../components/_shared/ShowIfAuthenticated'
|
|
|
|
import { NotificationsPanel } from '../components/NotificationsPanel'
|
|
|
|
import { Notification } from '../graphql/types.gen'
|
2023-11-14 15:10:00 +00:00
|
|
|
import { apiClient } from '../utils/apiClient'
|
|
|
|
import { apiBaseUrl } from '../utils/config'
|
|
|
|
import SSEService, { EventData } from '../utils/sseService'
|
|
|
|
|
|
|
|
import { useSession } from './session'
|
2023-10-20 18:07:33 +00:00
|
|
|
|
2023-10-14 11:39:24 +00:00
|
|
|
type NotificationsContextType = {
|
2023-11-17 16:22:54 +00:00
|
|
|
notificationEntities: Record<number, Notification>
|
2023-10-14 11:39:24 +00:00
|
|
|
unreadNotificationsCount: Accessor<number>
|
|
|
|
sortedNotifications: Accessor<Notification[]>
|
2023-11-01 15:13:54 +00:00
|
|
|
loadedNotificationsCount: Accessor<number>
|
|
|
|
totalNotificationsCount: Accessor<number>
|
2023-10-14 11:39:24 +00:00
|
|
|
actions: {
|
|
|
|
showNotificationsPanel: () => void
|
2023-10-16 17:24:33 +00:00
|
|
|
hideNotificationsPanel: () => void
|
2023-10-14 11:39:24 +00:00
|
|
|
markNotificationAsRead: (notification: Notification) => Promise<void>
|
2023-11-01 15:13:54 +00:00
|
|
|
markAllNotificationsAsRead: () => Promise<void>
|
|
|
|
loadNotifications: (options: { limit: number; offset: number }) => Promise<Notification[]>
|
2023-10-14 11:39:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-01 15:13:54 +00:00
|
|
|
export const PAGE_SIZE = 20
|
2023-10-14 11:39:24 +00:00
|
|
|
const NotificationsContext = createContext<NotificationsContextType>()
|
|
|
|
|
|
|
|
export function useNotifications() {
|
|
|
|
return useContext(NotificationsContext)
|
|
|
|
}
|
|
|
|
|
|
|
|
const sseService = new SSEService()
|
|
|
|
|
|
|
|
export const NotificationsProvider = (props: { children: JSX.Element }) => {
|
|
|
|
const [isNotificationsPanelOpen, setIsNotificationsPanelOpen] = createSignal(false)
|
|
|
|
const [unreadNotificationsCount, setUnreadNotificationsCount] = createSignal(0)
|
2023-11-01 15:13:54 +00:00
|
|
|
const [totalNotificationsCount, setTotalNotificationsCount] = createSignal(0)
|
2023-10-14 11:39:24 +00:00
|
|
|
const { isAuthenticated, user } = useSession()
|
2023-11-17 16:22:54 +00:00
|
|
|
const [notificationEntities, setNotificationEntities] = createStore<Record<number, Notification>>({})
|
2023-10-14 11:39:24 +00:00
|
|
|
|
2023-11-01 15:13:54 +00:00
|
|
|
const loadNotifications = async (options: { limit: number; offset?: number }) => {
|
|
|
|
const { notifications, totalUnreadCount, totalCount } = await apiClient.getNotifications(options)
|
2023-10-14 11:39:24 +00:00
|
|
|
const newNotificationEntities = notifications.reduce((acc, notification) => {
|
|
|
|
acc[notification.id] = notification
|
|
|
|
return acc
|
|
|
|
}, {})
|
|
|
|
|
2023-11-01 15:13:54 +00:00
|
|
|
setTotalNotificationsCount(totalCount)
|
2023-10-14 11:39:24 +00:00
|
|
|
setUnreadNotificationsCount(totalUnreadCount)
|
2023-11-17 16:22:54 +00:00
|
|
|
setNotificationEntities(newNotificationEntities)
|
2023-10-14 11:39:24 +00:00
|
|
|
return notifications
|
|
|
|
}
|
|
|
|
|
|
|
|
const sortedNotifications = createMemo(() => {
|
2023-11-17 16:22:54 +00:00
|
|
|
return Object.values(notificationEntities).sort(
|
|
|
|
(a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime(),
|
|
|
|
)
|
2023-10-14 11:39:24 +00:00
|
|
|
})
|
|
|
|
|
2023-11-01 15:13:54 +00:00
|
|
|
const loadedNotificationsCount = createMemo(() => Object.keys(notificationEntities).length)
|
2023-10-14 11:39:24 +00:00
|
|
|
createEffect(() => {
|
|
|
|
if (isAuthenticated()) {
|
|
|
|
sseService.connect(`${apiBaseUrl}/subscribe/${user().id}`)
|
|
|
|
sseService.subscribeToEvent('message', (data: EventData) => {
|
|
|
|
if (data.type === 'newNotifications') {
|
2023-11-14 10:45:44 +00:00
|
|
|
loadNotifications({ limit: Math.max(PAGE_SIZE, loadedNotificationsCount()) })
|
2023-10-14 11:39:24 +00:00
|
|
|
} else {
|
|
|
|
console.error(`[NotificationsProvider] unknown message type: ${JSON.stringify(data)}`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
sseService.disconnect()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const markNotificationAsRead = async (notification: Notification) => {
|
|
|
|
await apiClient.markNotificationAsRead(notification.id)
|
2023-11-15 17:52:05 +00:00
|
|
|
setNotificationEntities(notification.id, 'seen', true)
|
|
|
|
setUnreadNotificationsCount((oldCount) => oldCount - 1)
|
2023-11-01 15:13:54 +00:00
|
|
|
}
|
|
|
|
const markAllNotificationsAsRead = async () => {
|
|
|
|
await apiClient.markAllNotificationsAsRead()
|
|
|
|
loadNotifications({ limit: loadedNotificationsCount() })
|
2023-10-14 11:39:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 = {
|
|
|
|
showNotificationsPanel,
|
|
|
|
hideNotificationsPanel,
|
2023-11-01 15:13:54 +00:00
|
|
|
markNotificationAsRead,
|
|
|
|
markAllNotificationsAsRead,
|
2023-11-14 15:10:00 +00:00
|
|
|
loadNotifications,
|
2023-10-16 18:00:22 +00:00
|
|
|
}
|
2023-10-14 11:39:24 +00:00
|
|
|
|
|
|
|
const value: NotificationsContextType = {
|
|
|
|
notificationEntities,
|
|
|
|
sortedNotifications,
|
|
|
|
unreadNotificationsCount,
|
2023-11-01 15:13:54 +00:00
|
|
|
loadedNotificationsCount,
|
|
|
|
totalNotificationsCount,
|
2023-11-14 15:10:00 +00:00
|
|
|
actions,
|
2023-10-14 11:39:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const handleNotificationPanelClose = () => {
|
|
|
|
setIsNotificationsPanelOpen(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<NotificationsContext.Provider value={value}>
|
|
|
|
{props.children}
|
|
|
|
<ShowIfAuthenticated>
|
|
|
|
<Portal>
|
|
|
|
<NotificationsPanel isOpen={isNotificationsPanelOpen()} onClose={handleNotificationPanelClose} />
|
|
|
|
</Portal>
|
|
|
|
</ShowIfAuthenticated>
|
|
|
|
</NotificationsContext.Provider>
|
|
|
|
)
|
|
|
|
}
|