Improve unread notif tracking (#2056)
parent
826cbbd4bf
commit
f8c46c08ca
|
@ -29,6 +29,7 @@ import {useUnreadNotificationsApi} from './unread'
|
||||||
import {fetchPage} from './util'
|
import {fetchPage} from './util'
|
||||||
import {FeedPage} from './types'
|
import {FeedPage} from './types'
|
||||||
import {useMutedThreads} from '#/state/muted-threads'
|
import {useMutedThreads} from '#/state/muted-threads'
|
||||||
|
import {STALE} from '..'
|
||||||
|
|
||||||
export type {NotificationType, FeedNotification, FeedPage} from './types'
|
export type {NotificationType, FeedNotification, FeedPage} from './types'
|
||||||
|
|
||||||
|
@ -54,23 +55,30 @@ export function useNotificationFeedQuery(opts?: {enabled?: boolean}) {
|
||||||
QueryKey,
|
QueryKey,
|
||||||
RQPageParam
|
RQPageParam
|
||||||
>({
|
>({
|
||||||
|
staleTime: STALE.INFINITY,
|
||||||
queryKey: RQKEY(),
|
queryKey: RQKEY(),
|
||||||
async queryFn({pageParam}: {pageParam: RQPageParam}) {
|
async queryFn({pageParam}: {pageParam: RQPageParam}) {
|
||||||
// for the first page, we check the cached page held by the unread-checker first
|
let page
|
||||||
if (!pageParam) {
|
if (!pageParam) {
|
||||||
const cachedPage = unreads.getCachedUnreadPage()
|
// for the first page, we check the cached page held by the unread-checker first
|
||||||
if (cachedPage) {
|
page = unreads.getCachedUnreadPage()
|
||||||
return cachedPage
|
|
||||||
}
|
}
|
||||||
}
|
if (!page) {
|
||||||
// do a normal fetch
|
page = await fetchPage({
|
||||||
return fetchPage({
|
|
||||||
limit: PAGE_SIZE,
|
limit: PAGE_SIZE,
|
||||||
cursor: pageParam,
|
cursor: pageParam,
|
||||||
queryClient,
|
queryClient,
|
||||||
moderationOpts,
|
moderationOpts,
|
||||||
threadMutes,
|
threadMutes,
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// if the first page has an unread, mark all read
|
||||||
|
if (!pageParam && page.items[0] && !page.items[0].notification.isRead) {
|
||||||
|
unreads.markAllRead()
|
||||||
|
}
|
||||||
|
|
||||||
|
return page
|
||||||
},
|
},
|
||||||
initialPageParam: undefined,
|
initialPageParam: undefined,
|
||||||
getNextPageParam: lastPage => lastPage.cursor,
|
getNextPageParam: lastPage => lastPage.cursor,
|
||||||
|
|
|
@ -35,7 +35,7 @@ export function Feed({
|
||||||
const [isPTRing, setIsPTRing] = React.useState(false)
|
const [isPTRing, setIsPTRing] = React.useState(false)
|
||||||
|
|
||||||
const moderationOpts = useModerationOpts()
|
const moderationOpts = useModerationOpts()
|
||||||
const {markAllRead, checkUnread} = useUnreadNotificationsApi()
|
const {checkUnread} = useUnreadNotificationsApi()
|
||||||
const {
|
const {
|
||||||
data,
|
data,
|
||||||
isFetching,
|
isFetching,
|
||||||
|
@ -47,15 +47,6 @@ export function Feed({
|
||||||
fetchNextPage,
|
fetchNextPage,
|
||||||
} = useNotificationFeedQuery({enabled: !!moderationOpts})
|
} = useNotificationFeedQuery({enabled: !!moderationOpts})
|
||||||
const isEmpty = !isFetching && !data?.pages[0]?.items.length
|
const isEmpty = !isFetching && !data?.pages[0]?.items.length
|
||||||
const firstItem = data?.pages[0]?.items[0]
|
|
||||||
|
|
||||||
// mark all read on fresh data
|
|
||||||
// (this will fire each time firstItem changes)
|
|
||||||
React.useEffect(() => {
|
|
||||||
if (firstItem) {
|
|
||||||
markAllRead()
|
|
||||||
}
|
|
||||||
}, [firstItem, markAllRead])
|
|
||||||
|
|
||||||
const items = React.useMemo(() => {
|
const items = React.useMemo(() => {
|
||||||
let arr: any[] = []
|
let arr: any[] = []
|
||||||
|
|
Loading…
Reference in New Issue