no-typescript-good

This commit is contained in:
Untone 2023-10-20 21:07:33 +03:00
parent 4c5778d0de
commit 04efdd368e
2 changed files with 12 additions and 10 deletions

View File

@ -3,8 +3,7 @@ import { createContext, createSignal, onMount, useContext } from 'solid-js'
import type { Chat, Message, MutationCreateMessageArgs } from '../graphql/types.gen' import type { Chat, Message, MutationCreateMessageArgs } from '../graphql/types.gen'
import { inboxClient } from '../utils/apiClient' import { inboxClient } from '../utils/apiClient'
import { loadMessages } from '../stores/inbox' import { loadMessages } from '../stores/inbox'
import { SSEMessage, useNotifications } from './notifications' import { MessageHandler, SSEMessage, useNotifications } from './notifications'
import { M } from '../components/_shared/Button/Button.module.scss'
type InboxContextType = { type InboxContextType = {
chats: Accessor<Chat[]> chats: Accessor<Chat[]>
@ -30,7 +29,7 @@ export const InboxProvider = (props: { children: JSX.Element }) => {
actions: { setMessageHandler } actions: { setMessageHandler }
} = useNotifications() } = useNotifications()
const handleMessage = (m: SSEMessage) => { const handleMessage = (m) => {
console.log('[context.inbox] ', m) console.log('[context.inbox] ', m)
// TODO: handle all action types: create update delete join left // TODO: handle all action types: create update delete join left
if (m.action in ['create', 'update', 'delete']) { if (m.action in ['create', 'update', 'delete']) {
@ -42,11 +41,11 @@ export const InboxProvider = (props: { children: JSX.Element }) => {
} }
} }
onMount(() => { onMount(() =>
setMessageHandler((_) => { setMessageHandler((_h) => {
return handleMessage return handleMessage
}) })
}) )
const loadChats = async () => { const loadChats = async () => {
try { try {

View File

@ -11,6 +11,7 @@ import { getToken } from '../graphql/privateGraphQLClient'
import { Author, Message, Reaction, Shout } from '../graphql/types.gen' import { Author, Message, Reaction, Shout } from '../graphql/types.gen'
export interface SSEMessage { export interface SSEMessage {
id: string
entity: string entity: string
action: string action: string
payload: any // Author | Shout | Reaction | Message payload: any // Author | Shout | Reaction | Message
@ -18,7 +19,8 @@ export interface SSEMessage {
seen?: boolean seen?: boolean
} }
type MessageHandler = (m: Message) => void export type MessageHandler = (m: SSEMessage) => void
type NotificationsContextType = { type NotificationsContextType = {
notificationEntities: Record<number, SSEMessage> notificationEntities: Record<number, SSEMessage>
unreadNotificationsCount: Accessor<number> unreadNotificationsCount: Accessor<number>
@ -57,7 +59,7 @@ export const NotificationsProvider = (props: { children: JSX.Element }) => {
const notifications = await storage.getAll('notifications') const notifications = await storage.getAll('notifications')
console.log('[context.notifications] Loaded notifications:', notifications) console.log('[context.notifications] Loaded notifications:', notifications)
const totalUnreadCount = notifications.filter((notification) => !notification.read).length const totalUnreadCount = notifications.filter((notification) => !notification.seen).length
console.log('[context.notifications] Total unread count:', totalUnreadCount) console.log('[context.notifications] Total unread count:', totalUnreadCount)
setUnreadNotificationsCount(totalUnreadCount) setUnreadNotificationsCount(totalUnreadCount)
@ -82,12 +84,12 @@ export const NotificationsProvider = (props: { children: JSX.Element }) => {
const tx = storage.transaction('notifications', 'readwrite') const tx = storage.transaction('notifications', 'readwrite')
const store = tx.objectStore('notifications') const store = tx.objectStore('notifications')
await store.put(notification) await store.put(notification, 'id')
await tx.done await tx.done
loadNotifications() loadNotifications()
} }
const [messageHandler, setMessageHandler] = createSignal<(m: SSEMessage) => void>() const [messageHandler, setMessageHandler] = createSignal<MessageHandler>(console.warn)
createEffect(async () => { createEffect(async () => {
if (isAuthenticated()) { if (isAuthenticated()) {
@ -108,6 +110,7 @@ export const NotificationsProvider = (props: { children: JSX.Element }) => {
console.log('[context.notifications] Received notification:', m) console.log('[context.notifications] Received notification:', m)
storeNotification({ storeNotification({
...m, ...m,
id: event.id,
timestamp: Date.now(), timestamp: Date.now(),
seen: false seen: false
}) })