migrate the chat convo read queries to the chat client
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import {useQuery} from '@tanstack/react-query'
|
||||
|
||||
import {DM_SERVICE_HEADERS} from '#/lib/constants'
|
||||
import {useAgent} from '#/state/session'
|
||||
import {useChatClient} from '#/state/session'
|
||||
import {chat} from '#/lexicons'
|
||||
import {STALE} from '..'
|
||||
|
||||
const RQKEY_ROOT = 'convo-availability'
|
||||
@@ -11,17 +11,17 @@ export function useGetConvoAvailabilityQuery(
|
||||
did: string,
|
||||
{enabled = true}: {enabled?: boolean} = {},
|
||||
) {
|
||||
const agent = useAgent()
|
||||
const client = useChatClient()
|
||||
|
||||
return useQuery({
|
||||
queryKey: RQKEY(did),
|
||||
queryFn: async () => {
|
||||
const {data} = await agent.chat.bsky.convo.getConvoAvailability(
|
||||
{members: [did]},
|
||||
{headers: DM_SERVICE_HEADERS},
|
||||
)
|
||||
|
||||
return data
|
||||
return await client.call(chat.bsky.convo.getConvoAvailability, {
|
||||
// callers pass an already-resolved actor did
|
||||
members: [
|
||||
did,
|
||||
] as chat.bsky.convo.getConvoAvailability.$Params['members'],
|
||||
})
|
||||
},
|
||||
staleTime: STALE.INFINITY,
|
||||
enabled,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import {useQuery} from '@tanstack/react-query'
|
||||
|
||||
import {DM_SERVICE_HEADERS} from '#/lib/constants'
|
||||
import {useAgent} from '#/state/session'
|
||||
import {useChatClient} from '#/state/session'
|
||||
import {chat} from '#/lexicons'
|
||||
import {STALE} from '..'
|
||||
import {createQueryKey} from '../util'
|
||||
|
||||
@@ -9,19 +9,14 @@ const chatActorStatusQueryKey = () =>
|
||||
createQueryKey('chat-actor-status', {}, {persistedVersion: 1})
|
||||
|
||||
export function useChatActorStatusQuery() {
|
||||
const agent = useAgent()
|
||||
const client = useChatClient()
|
||||
|
||||
return useQuery({
|
||||
gcTime: STALE.INFINITY,
|
||||
staleTime: STALE.SECONDS.FIFTEEN,
|
||||
queryKey: chatActorStatusQueryKey(),
|
||||
queryFn: async () => {
|
||||
const {data} = await agent.chat.bsky.actor.getStatus(
|
||||
{},
|
||||
{headers: DM_SERVICE_HEADERS},
|
||||
)
|
||||
|
||||
return data
|
||||
return await client.call(chat.bsky.actor.getStatus)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import {useQuery} from '@tanstack/react-query'
|
||||
|
||||
import {DM_SERVICE_HEADERS} from '#/lib/constants'
|
||||
import {useAgent, useSession} from '#/state/session'
|
||||
import {useChatClient, useSession} from '#/state/session'
|
||||
import {useAgeAssurance} from '#/ageAssurance'
|
||||
import {chat} from '#/lexicons'
|
||||
import {STALE} from '..'
|
||||
|
||||
const RQKEY_ROOT = 'convo-unread-counts'
|
||||
@@ -18,7 +18,7 @@ export const UNREAD_ACCEPTED_CAP = 100
|
||||
export const UNREAD_REQUEST_CAP = 100
|
||||
|
||||
export function useUnreadCountsQuery() {
|
||||
const agent = useAgent()
|
||||
const client = useChatClient()
|
||||
const {hasSession} = useSession()
|
||||
const aa = useAgeAssurance()
|
||||
const includeGroupChats = !aa.flags.groupChatDisabled
|
||||
@@ -26,11 +26,9 @@ export function useUnreadCountsQuery() {
|
||||
return useQuery({
|
||||
queryKey: RQKEY(includeGroupChats),
|
||||
queryFn: async () => {
|
||||
const {data} = await agent.chat.bsky.convo.getUnreadCounts(
|
||||
{includeGroupChats},
|
||||
{headers: DM_SERVICE_HEADERS},
|
||||
)
|
||||
return data
|
||||
return await client.call(chat.bsky.convo.getUnreadCounts, {
|
||||
includeGroupChats,
|
||||
})
|
||||
},
|
||||
staleTime: STALE.SECONDS.FIFTEEN,
|
||||
enabled: hasSession,
|
||||
|
||||
@@ -9,8 +9,8 @@ import {
|
||||
useInfiniteQuery,
|
||||
} from '@tanstack/react-query'
|
||||
|
||||
import {DM_SERVICE_HEADERS} from '#/lib/constants'
|
||||
import {useAgent} from '#/state/session'
|
||||
import {useChatClient} from '#/state/session'
|
||||
import {chat} from '#/lexicons'
|
||||
|
||||
const DEFAULT_LIMIT = 10
|
||||
|
||||
@@ -26,17 +26,16 @@ export function useListConvoRequests({
|
||||
enabled?: boolean
|
||||
limit?: number
|
||||
} = {}) {
|
||||
const agent = useAgent()
|
||||
const client = useChatClient()
|
||||
|
||||
return useInfiniteQuery({
|
||||
enabled,
|
||||
queryKey: RQKEY(limit),
|
||||
queryFn: async ({pageParam}) => {
|
||||
const {data} = await agent.chat.bsky.convo.listConvoRequests(
|
||||
{limit, cursor: pageParam},
|
||||
{headers: DM_SERVICE_HEADERS},
|
||||
)
|
||||
return data
|
||||
return await client.call(chat.bsky.convo.listConvoRequests, {
|
||||
limit,
|
||||
cursor: pageParam,
|
||||
})
|
||||
},
|
||||
initialPageParam: undefined as RQPageParam,
|
||||
getNextPageParam: lastPage => lastPage.cursor,
|
||||
|
||||
@@ -14,11 +14,11 @@ import {
|
||||
} from '@tanstack/react-query'
|
||||
import throttle from 'lodash.throttle'
|
||||
|
||||
import {DM_SERVICE_HEADERS} from '#/lib/constants'
|
||||
import {useCurrentConvoId} from '#/state/messages/current-convo-id'
|
||||
import {useMessagesEventBus} from '#/state/messages/events'
|
||||
import {invalidateJoinLinkPreviewsForConvo} from '#/state/queries/join-links'
|
||||
import {useAgent, useSession} from '#/state/session'
|
||||
import {useChatClient, useSession} from '#/state/session'
|
||||
import {chat} from '#/lexicons'
|
||||
import * as bsky from '#/types/bsky'
|
||||
import {RQKEY as CONVO_KEY} from './conversation'
|
||||
import {
|
||||
@@ -119,24 +119,20 @@ export function useListConvosQuery({
|
||||
limit?: number
|
||||
lockStatus?: 'unlocked' | 'locked' | 'locked-permanently'
|
||||
} = {}) {
|
||||
const agent = useAgent()
|
||||
const client = useChatClient()
|
||||
|
||||
return useInfiniteQuery({
|
||||
enabled,
|
||||
queryKey: RQKEY(status ?? 'all', readState, kind, lockStatus, limit),
|
||||
queryFn: async ({pageParam}) => {
|
||||
const {data} = await agent.chat.bsky.convo.listConvos(
|
||||
{
|
||||
limit,
|
||||
cursor: pageParam,
|
||||
readState: readState === 'unread' ? 'unread' : undefined,
|
||||
kind: kind === 'all' ? undefined : kind,
|
||||
lockStatus,
|
||||
status,
|
||||
},
|
||||
{headers: DM_SERVICE_HEADERS},
|
||||
)
|
||||
return data
|
||||
return await client.call(chat.bsky.convo.listConvos, {
|
||||
limit,
|
||||
cursor: pageParam,
|
||||
readState: readState === 'unread' ? 'unread' : undefined,
|
||||
kind: kind === 'all' ? undefined : kind,
|
||||
lockStatus,
|
||||
status,
|
||||
})
|
||||
},
|
||||
initialPageParam: undefined as RQPageParam,
|
||||
getNextPageParam: lastPage => lastPage.cursor,
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import {type ChatBskyActorDefs} from '@atproto/api'
|
||||
import {type QueryClient, useQuery} from '@tanstack/react-query'
|
||||
|
||||
import {DM_SERVICE_HEADERS} from '#/lib/constants'
|
||||
import {STALE} from '#/state/queries'
|
||||
import {createQueryKey} from '#/state/queries/util'
|
||||
import {useAgent} from '#/state/session'
|
||||
import {useChatClient} from '#/state/session'
|
||||
import {chat} from '#/lexicons'
|
||||
|
||||
const RQKEY_ROOT = 'listConvoMembers'
|
||||
export const listConvoMembersQueryKey = (convoId: string) =>
|
||||
@@ -20,19 +20,27 @@ export function useListConvoMembersQuery({
|
||||
convoId: string
|
||||
placeholderData?: ChatBskyActorDefs.ProfileViewBasic[]
|
||||
}) {
|
||||
const agent = useAgent()
|
||||
const client = useChatClient()
|
||||
|
||||
return useQuery({
|
||||
queryKey: listConvoMembersQueryKey(convoId),
|
||||
queryFn: async () => {
|
||||
const members = []
|
||||
let cursor
|
||||
/*
|
||||
* Both locals are annotated because the loop is self-referential: `data`
|
||||
* is inferred from a call whose params include `cursor`, so leaving
|
||||
* `cursor` to be inferred from `data.cursor` is circular. Annotating
|
||||
* `members` with the exported profile type also keeps the hook's result
|
||||
* type unchanged for consumers.
|
||||
*/
|
||||
const members: ChatBskyActorDefs.ProfileViewBasic[] = []
|
||||
let cursor: string | undefined
|
||||
|
||||
do {
|
||||
const {data} = await agent.chat.bsky.convo.getConvoMembers(
|
||||
{convoId, cursor, limit: LIMIT},
|
||||
{headers: DM_SERVICE_HEADERS},
|
||||
)
|
||||
const data = await client.call(chat.bsky.convo.getConvoMembers, {
|
||||
convoId,
|
||||
cursor,
|
||||
limit: LIMIT,
|
||||
})
|
||||
members.push(...data.members)
|
||||
cursor = data.cursor
|
||||
} while (cursor)
|
||||
|
||||
Reference in New Issue
Block a user