Update unreads in account provider

This commit is contained in:
AkiraFukushima 2024-01-31 23:03:07 +09:00
parent a5065678b6
commit b8b37efd13
No known key found for this signature in database
GPG Key ID: B6E51BAC4DE1A957
2 changed files with 21 additions and 11 deletions

View File

@ -7,7 +7,7 @@ import Notification from './notification/Notification'
import { Spinner } from '@material-tailwind/react' import { Spinner } from '@material-tailwind/react'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
import Detail from '../detail/Detail' import Detail from '../detail/Detail'
import { Marker, unreadCount } from '@/entities/marker' import { Marker } from '@/entities/marker'
import { FaCheck } from 'react-icons/fa6' import { FaCheck } from 'react-icons/fa6'
import { useToast } from '@/provider/toast' import { useToast } from '@/provider/toast'
import { useUnreads } from '@/provider/unreads' 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) const u = allNotifications.slice(0, marker.unread_count).map(n => n.id)
setPleromaUnreads(u) setPleromaUnreads(u)
} }
if (marker) {
const count = unreadCount(marker, unreadNotifications.concat(notifications))
setUnreads(current =>
Object.assign({}, current, {
[props.account.id.toString()]: count
})
)
}
}, [marker, unreadNotifications, notifications]) }, [marker, unreadNotifications, notifications])
const loadNotifications = async (client: MegalodonInterface, maxId?: string): Promise<Array<Entity.Notification>> => { const loadNotifications = async (client: MegalodonInterface, maxId?: string): Promise<Array<Entity.Notification>> => {

View File

@ -1,7 +1,7 @@
import { Account } from '@/db' import { Account } from '@/db'
import generateNotification from '@/utils/notification' import generateNotification from '@/utils/notification'
import { unreadCount } from '@/entities/marker' 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 { createContext, useContext, useRef, useState } from 'react'
import { useUnreads } from './unreads' import { useUnreads } from './unreads'
import { useIntl } from 'react-intl' import { useIntl } from 'react-intl'
@ -88,8 +88,27 @@ export const AccountsProvider: React.FC<Props> = ({ children }) => {
if (title.length > 0) { if (title.length > 0) {
new window.Notification(title, { body: body }) 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 <AccountsContext.Provider value={{ addAccount, removeAccount, removeAll }}>{children}</AccountsContext.Provider> return <AccountsContext.Provider value={{ addAccount, removeAccount, removeAll }}>{children}</AccountsContext.Provider>
} }