Move the current agent to a global and reset RQ queries on agent change (#1946)
This commit is contained in:
parent
3043b32468
commit
357c752a21
40 changed files with 218 additions and 260 deletions
|
@ -8,7 +8,7 @@ import {
|
|||
} from '@atproto/api'
|
||||
import chunk from 'lodash.chunk'
|
||||
import {useInfiniteQuery, InfiniteData, QueryKey} from '@tanstack/react-query'
|
||||
import {useSession} from '../../session'
|
||||
import {getAgent} from '../../session'
|
||||
import {useModerationOpts} from '../preferences'
|
||||
import {shouldFilterNotif} from './util'
|
||||
import {useMutedThreads} from '#/state/muted-threads'
|
||||
|
@ -49,7 +49,6 @@ export interface FeedPage {
|
|||
}
|
||||
|
||||
export function useNotificationFeedQuery(opts?: {enabled?: boolean}) {
|
||||
const {agent} = useSession()
|
||||
const moderationOpts = useModerationOpts()
|
||||
const threadMutes = useMutedThreads()
|
||||
const enabled = opts?.enabled !== false
|
||||
|
@ -64,7 +63,7 @@ export function useNotificationFeedQuery(opts?: {enabled?: boolean}) {
|
|||
staleTime: STALE.INFINITY,
|
||||
queryKey: RQKEY(),
|
||||
async queryFn({pageParam}: {pageParam: RQPageParam}) {
|
||||
const res = await agent.listNotifications({
|
||||
const res = await getAgent().listNotifications({
|
||||
limit: PAGE_SIZE,
|
||||
cursor: pageParam,
|
||||
})
|
||||
|
@ -79,7 +78,7 @@ export function useNotificationFeedQuery(opts?: {enabled?: boolean}) {
|
|||
|
||||
// we fetch subjects of notifications (usually posts) now instead of lazily
|
||||
// in the UI to avoid relayouts
|
||||
const subjects = await fetchSubjects(agent, notifsGrouped)
|
||||
const subjects = await fetchSubjects(getAgent(), notifsGrouped)
|
||||
for (const notif of notifsGrouped) {
|
||||
if (notif.subjectUri) {
|
||||
notif.subject = subjects.get(notif.subjectUri)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import React from 'react'
|
||||
import * as Notifications from 'expo-notifications'
|
||||
import BroadcastChannel from '#/lib/broadcast'
|
||||
import {useSession} from '#/state/session'
|
||||
import {useSession, getAgent} from '#/state/session'
|
||||
import {useModerationOpts} from '../preferences'
|
||||
import {shouldFilterNotif} from './util'
|
||||
import {isNative} from '#/platform/detection'
|
||||
|
@ -25,7 +25,7 @@ const apiContext = React.createContext<ApiContext>({
|
|||
})
|
||||
|
||||
export function Provider({children}: React.PropsWithChildren<{}>) {
|
||||
const {hasSession, agent} = useSession()
|
||||
const {hasSession} = useSession()
|
||||
const moderationOpts = useModerationOpts()
|
||||
|
||||
const [numUnread, setNumUnread] = React.useState('')
|
||||
|
@ -60,7 +60,9 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
|
|||
return {
|
||||
async markAllRead() {
|
||||
// update server
|
||||
await agent.updateSeenNotifications(lastSyncRef.current.toISOString())
|
||||
await getAgent().updateSeenNotifications(
|
||||
lastSyncRef.current.toISOString(),
|
||||
)
|
||||
|
||||
// update & broadcast
|
||||
setNumUnread('')
|
||||
|
@ -69,7 +71,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
|
|||
|
||||
async checkUnread() {
|
||||
// count
|
||||
const res = await agent.listNotifications({limit: 40})
|
||||
const res = await getAgent().listNotifications({limit: 40})
|
||||
const filtered = res.data.notifications.filter(
|
||||
notif => !notif.isRead && !shouldFilterNotif(notif, moderationOpts),
|
||||
)
|
||||
|
@ -94,7 +96,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
|
|||
broadcast.postMessage({event: num})
|
||||
},
|
||||
}
|
||||
}, [setNumUnread, agent, moderationOpts])
|
||||
}, [setNumUnread, moderationOpts])
|
||||
checkUnreadRef.current = api.checkUnread
|
||||
|
||||
return (
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue