From 04efdd368e70bb695b831d212d7eaa5b2852130d Mon Sep 17 00:00:00 2001 From: Untone Date: Fri, 20 Oct 2023 21:07:33 +0300 Subject: [PATCH] no-typescript-good --- src/context/inbox.tsx | 11 +++++------ src/context/notifications.tsx | 11 +++++++---- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/context/inbox.tsx b/src/context/inbox.tsx index 6b80eb92..e2762b53 100644 --- a/src/context/inbox.tsx +++ b/src/context/inbox.tsx @@ -3,8 +3,7 @@ import { createContext, createSignal, onMount, useContext } from 'solid-js' import type { Chat, Message, MutationCreateMessageArgs } from '../graphql/types.gen' import { inboxClient } from '../utils/apiClient' import { loadMessages } from '../stores/inbox' -import { SSEMessage, useNotifications } from './notifications' -import { M } from '../components/_shared/Button/Button.module.scss' +import { MessageHandler, SSEMessage, useNotifications } from './notifications' type InboxContextType = { chats: Accessor @@ -30,7 +29,7 @@ export const InboxProvider = (props: { children: JSX.Element }) => { actions: { setMessageHandler } } = useNotifications() - const handleMessage = (m: SSEMessage) => { + const handleMessage = (m) => { console.log('[context.inbox] ', m) // TODO: handle all action types: create update delete join left if (m.action in ['create', 'update', 'delete']) { @@ -42,11 +41,11 @@ export const InboxProvider = (props: { children: JSX.Element }) => { } } - onMount(() => { - setMessageHandler((_) => { + onMount(() => + setMessageHandler((_h) => { return handleMessage }) - }) + ) const loadChats = async () => { try { diff --git a/src/context/notifications.tsx b/src/context/notifications.tsx index 27d91e15..b9ea643d 100644 --- a/src/context/notifications.tsx +++ b/src/context/notifications.tsx @@ -11,6 +11,7 @@ import { getToken } from '../graphql/privateGraphQLClient' import { Author, Message, Reaction, Shout } from '../graphql/types.gen' export interface SSEMessage { + id: string entity: string action: string payload: any // Author | Shout | Reaction | Message @@ -18,7 +19,8 @@ export interface SSEMessage { seen?: boolean } -type MessageHandler = (m: Message) => void +export type MessageHandler = (m: SSEMessage) => void + type NotificationsContextType = { notificationEntities: Record unreadNotificationsCount: Accessor @@ -57,7 +59,7 @@ export const NotificationsProvider = (props: { children: JSX.Element }) => { const notifications = await storage.getAll('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) setUnreadNotificationsCount(totalUnreadCount) @@ -82,12 +84,12 @@ export const NotificationsProvider = (props: { children: JSX.Element }) => { const tx = storage.transaction('notifications', 'readwrite') const store = tx.objectStore('notifications') - await store.put(notification) + await store.put(notification, 'id') await tx.done loadNotifications() } - const [messageHandler, setMessageHandler] = createSignal<(m: SSEMessage) => void>() + const [messageHandler, setMessageHandler] = createSignal(console.warn) createEffect(async () => { if (isAuthenticated()) { @@ -108,6 +110,7 @@ export const NotificationsProvider = (props: { children: JSX.Element }) => { console.log('[context.notifications] Received notification:', m) storeNotification({ ...m, + id: event.id, timestamp: Date.now(), seen: false })