diff --git a/renderer/components/timelines/Notifications.tsx b/renderer/components/timelines/Notifications.tsx index ac74f1f1..bfebf71f 100644 --- a/renderer/components/timelines/Notifications.tsx +++ b/renderer/components/timelines/Notifications.tsx @@ -7,7 +7,7 @@ import Notification from './notification/Notification' import { Spinner } from '@material-tailwind/react' import { useRouter } from 'next/router' import Detail from '../detail/Detail' -import { Marker, unreadCount } from '@/entities/marker' +import { Marker } from '@/entities/marker' import { FaCheck } from 'react-icons/fa6' import { useToast } from '@/provider/toast' import { useUnreads } from '@/provider/unreads' @@ -78,15 +78,6 @@ export default function Notifications(props: Props) { const u = allNotifications.slice(0, marker.unread_count).map(n => n.id) setPleromaUnreads(u) } - - if (marker) { - const count = unreadCount(marker, unreadNotifications.concat(notifications)) - setUnreads(current => - Object.assign({}, current, { - [props.account.id.toString()]: count - }) - ) - } }, [marker, unreadNotifications, notifications]) const loadNotifications = async (client: MegalodonInterface, maxId?: string): Promise> => { diff --git a/renderer/provider/accounts.tsx b/renderer/provider/accounts.tsx index 1e09cfcc..6a33caf9 100644 --- a/renderer/provider/accounts.tsx +++ b/renderer/provider/accounts.tsx @@ -1,7 +1,7 @@ import { Account } from '@/db' import generateNotification from '@/utils/notification' import { unreadCount } from '@/entities/marker' -import generator, { Entity, WebSocketInterface } from 'megalodon' +import generator, { Entity, MegalodonInterface, WebSocketInterface } from 'megalodon' import { createContext, useContext, useRef, useState } from 'react' import { useUnreads } from './unreads' import { useIntl } from 'react-intl' @@ -88,8 +88,27 @@ export const AccountsProvider: React.FC = ({ children }) => { if (title.length > 0) { new window.Notification(title, { body: body }) } + updateUnreads(account, client) }) } + const updateUnreads = async (account: Account, client: MegalodonInterface) => { + try { + const res = await client.getMarkers(['notifications']) + const marker = res.data as Entity.Marker + if (marker.notifications) { + const notifications = (await client.getNotifications()).data + const count = unreadCount(marker.notifications, notifications) + setUnreads(current => + Object.assign({}, current, { + [account.id?.toString()]: count + }) + ) + } + } catch (err) { + console.error(err) + } + } + return {children} }