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 {useQuery} from '@tanstack/react-query'
|
||||||
|
|
||||||
import {DM_SERVICE_HEADERS} from '#/lib/constants'
|
import {useChatClient} from '#/state/session'
|
||||||
import {useAgent} from '#/state/session'
|
import {chat} from '#/lexicons'
|
||||||
import {STALE} from '..'
|
import {STALE} from '..'
|
||||||
|
|
||||||
const RQKEY_ROOT = 'convo-availability'
|
const RQKEY_ROOT = 'convo-availability'
|
||||||
@@ -11,17 +11,17 @@ export function useGetConvoAvailabilityQuery(
|
|||||||
did: string,
|
did: string,
|
||||||
{enabled = true}: {enabled?: boolean} = {},
|
{enabled = true}: {enabled?: boolean} = {},
|
||||||
) {
|
) {
|
||||||
const agent = useAgent()
|
const client = useChatClient()
|
||||||
|
|
||||||
return useQuery({
|
return useQuery({
|
||||||
queryKey: RQKEY(did),
|
queryKey: RQKEY(did),
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
const {data} = await agent.chat.bsky.convo.getConvoAvailability(
|
return await client.call(chat.bsky.convo.getConvoAvailability, {
|
||||||
{members: [did]},
|
// callers pass an already-resolved actor did
|
||||||
{headers: DM_SERVICE_HEADERS},
|
members: [
|
||||||
)
|
did,
|
||||||
|
] as chat.bsky.convo.getConvoAvailability.$Params['members'],
|
||||||
return data
|
})
|
||||||
},
|
},
|
||||||
staleTime: STALE.INFINITY,
|
staleTime: STALE.INFINITY,
|
||||||
enabled,
|
enabled,
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import {useQuery} from '@tanstack/react-query'
|
import {useQuery} from '@tanstack/react-query'
|
||||||
|
|
||||||
import {DM_SERVICE_HEADERS} from '#/lib/constants'
|
import {useChatClient} from '#/state/session'
|
||||||
import {useAgent} from '#/state/session'
|
import {chat} from '#/lexicons'
|
||||||
import {STALE} from '..'
|
import {STALE} from '..'
|
||||||
import {createQueryKey} from '../util'
|
import {createQueryKey} from '../util'
|
||||||
|
|
||||||
@@ -9,19 +9,14 @@ const chatActorStatusQueryKey = () =>
|
|||||||
createQueryKey('chat-actor-status', {}, {persistedVersion: 1})
|
createQueryKey('chat-actor-status', {}, {persistedVersion: 1})
|
||||||
|
|
||||||
export function useChatActorStatusQuery() {
|
export function useChatActorStatusQuery() {
|
||||||
const agent = useAgent()
|
const client = useChatClient()
|
||||||
|
|
||||||
return useQuery({
|
return useQuery({
|
||||||
gcTime: STALE.INFINITY,
|
gcTime: STALE.INFINITY,
|
||||||
staleTime: STALE.SECONDS.FIFTEEN,
|
staleTime: STALE.SECONDS.FIFTEEN,
|
||||||
queryKey: chatActorStatusQueryKey(),
|
queryKey: chatActorStatusQueryKey(),
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
const {data} = await agent.chat.bsky.actor.getStatus(
|
return await client.call(chat.bsky.actor.getStatus)
|
||||||
{},
|
|
||||||
{headers: DM_SERVICE_HEADERS},
|
|
||||||
)
|
|
||||||
|
|
||||||
return data
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import {useQuery} from '@tanstack/react-query'
|
import {useQuery} from '@tanstack/react-query'
|
||||||
|
|
||||||
import {DM_SERVICE_HEADERS} from '#/lib/constants'
|
import {useChatClient, useSession} from '#/state/session'
|
||||||
import {useAgent, useSession} from '#/state/session'
|
|
||||||
import {useAgeAssurance} from '#/ageAssurance'
|
import {useAgeAssurance} from '#/ageAssurance'
|
||||||
|
import {chat} from '#/lexicons'
|
||||||
import {STALE} from '..'
|
import {STALE} from '..'
|
||||||
|
|
||||||
const RQKEY_ROOT = 'convo-unread-counts'
|
const RQKEY_ROOT = 'convo-unread-counts'
|
||||||
@@ -18,7 +18,7 @@ export const UNREAD_ACCEPTED_CAP = 100
|
|||||||
export const UNREAD_REQUEST_CAP = 100
|
export const UNREAD_REQUEST_CAP = 100
|
||||||
|
|
||||||
export function useUnreadCountsQuery() {
|
export function useUnreadCountsQuery() {
|
||||||
const agent = useAgent()
|
const client = useChatClient()
|
||||||
const {hasSession} = useSession()
|
const {hasSession} = useSession()
|
||||||
const aa = useAgeAssurance()
|
const aa = useAgeAssurance()
|
||||||
const includeGroupChats = !aa.flags.groupChatDisabled
|
const includeGroupChats = !aa.flags.groupChatDisabled
|
||||||
@@ -26,11 +26,9 @@ export function useUnreadCountsQuery() {
|
|||||||
return useQuery({
|
return useQuery({
|
||||||
queryKey: RQKEY(includeGroupChats),
|
queryKey: RQKEY(includeGroupChats),
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
const {data} = await agent.chat.bsky.convo.getUnreadCounts(
|
return await client.call(chat.bsky.convo.getUnreadCounts, {
|
||||||
{includeGroupChats},
|
includeGroupChats,
|
||||||
{headers: DM_SERVICE_HEADERS},
|
})
|
||||||
)
|
|
||||||
return data
|
|
||||||
},
|
},
|
||||||
staleTime: STALE.SECONDS.FIFTEEN,
|
staleTime: STALE.SECONDS.FIFTEEN,
|
||||||
enabled: hasSession,
|
enabled: hasSession,
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ import {
|
|||||||
useInfiniteQuery,
|
useInfiniteQuery,
|
||||||
} from '@tanstack/react-query'
|
} from '@tanstack/react-query'
|
||||||
|
|
||||||
import {DM_SERVICE_HEADERS} from '#/lib/constants'
|
import {useChatClient} from '#/state/session'
|
||||||
import {useAgent} from '#/state/session'
|
import {chat} from '#/lexicons'
|
||||||
|
|
||||||
const DEFAULT_LIMIT = 10
|
const DEFAULT_LIMIT = 10
|
||||||
|
|
||||||
@@ -26,17 +26,16 @@ export function useListConvoRequests({
|
|||||||
enabled?: boolean
|
enabled?: boolean
|
||||||
limit?: number
|
limit?: number
|
||||||
} = {}) {
|
} = {}) {
|
||||||
const agent = useAgent()
|
const client = useChatClient()
|
||||||
|
|
||||||
return useInfiniteQuery({
|
return useInfiniteQuery({
|
||||||
enabled,
|
enabled,
|
||||||
queryKey: RQKEY(limit),
|
queryKey: RQKEY(limit),
|
||||||
queryFn: async ({pageParam}) => {
|
queryFn: async ({pageParam}) => {
|
||||||
const {data} = await agent.chat.bsky.convo.listConvoRequests(
|
return await client.call(chat.bsky.convo.listConvoRequests, {
|
||||||
{limit, cursor: pageParam},
|
limit,
|
||||||
{headers: DM_SERVICE_HEADERS},
|
cursor: pageParam,
|
||||||
)
|
})
|
||||||
return data
|
|
||||||
},
|
},
|
||||||
initialPageParam: undefined as RQPageParam,
|
initialPageParam: undefined as RQPageParam,
|
||||||
getNextPageParam: lastPage => lastPage.cursor,
|
getNextPageParam: lastPage => lastPage.cursor,
|
||||||
|
|||||||
@@ -14,11 +14,11 @@ import {
|
|||||||
} from '@tanstack/react-query'
|
} from '@tanstack/react-query'
|
||||||
import throttle from 'lodash.throttle'
|
import throttle from 'lodash.throttle'
|
||||||
|
|
||||||
import {DM_SERVICE_HEADERS} from '#/lib/constants'
|
|
||||||
import {useCurrentConvoId} from '#/state/messages/current-convo-id'
|
import {useCurrentConvoId} from '#/state/messages/current-convo-id'
|
||||||
import {useMessagesEventBus} from '#/state/messages/events'
|
import {useMessagesEventBus} from '#/state/messages/events'
|
||||||
import {invalidateJoinLinkPreviewsForConvo} from '#/state/queries/join-links'
|
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 * as bsky from '#/types/bsky'
|
||||||
import {RQKEY as CONVO_KEY} from './conversation'
|
import {RQKEY as CONVO_KEY} from './conversation'
|
||||||
import {
|
import {
|
||||||
@@ -119,24 +119,20 @@ export function useListConvosQuery({
|
|||||||
limit?: number
|
limit?: number
|
||||||
lockStatus?: 'unlocked' | 'locked' | 'locked-permanently'
|
lockStatus?: 'unlocked' | 'locked' | 'locked-permanently'
|
||||||
} = {}) {
|
} = {}) {
|
||||||
const agent = useAgent()
|
const client = useChatClient()
|
||||||
|
|
||||||
return useInfiniteQuery({
|
return useInfiniteQuery({
|
||||||
enabled,
|
enabled,
|
||||||
queryKey: RQKEY(status ?? 'all', readState, kind, lockStatus, limit),
|
queryKey: RQKEY(status ?? 'all', readState, kind, lockStatus, limit),
|
||||||
queryFn: async ({pageParam}) => {
|
queryFn: async ({pageParam}) => {
|
||||||
const {data} = await agent.chat.bsky.convo.listConvos(
|
return await client.call(chat.bsky.convo.listConvos, {
|
||||||
{
|
limit,
|
||||||
limit,
|
cursor: pageParam,
|
||||||
cursor: pageParam,
|
readState: readState === 'unread' ? 'unread' : undefined,
|
||||||
readState: readState === 'unread' ? 'unread' : undefined,
|
kind: kind === 'all' ? undefined : kind,
|
||||||
kind: kind === 'all' ? undefined : kind,
|
lockStatus,
|
||||||
lockStatus,
|
status,
|
||||||
status,
|
})
|
||||||
},
|
|
||||||
{headers: DM_SERVICE_HEADERS},
|
|
||||||
)
|
|
||||||
return data
|
|
||||||
},
|
},
|
||||||
initialPageParam: undefined as RQPageParam,
|
initialPageParam: undefined as RQPageParam,
|
||||||
getNextPageParam: lastPage => lastPage.cursor,
|
getNextPageParam: lastPage => lastPage.cursor,
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import {type ChatBskyActorDefs} from '@atproto/api'
|
import {type ChatBskyActorDefs} from '@atproto/api'
|
||||||
import {type QueryClient, useQuery} from '@tanstack/react-query'
|
import {type QueryClient, useQuery} from '@tanstack/react-query'
|
||||||
|
|
||||||
import {DM_SERVICE_HEADERS} from '#/lib/constants'
|
|
||||||
import {STALE} from '#/state/queries'
|
import {STALE} from '#/state/queries'
|
||||||
import {createQueryKey} from '#/state/queries/util'
|
import {createQueryKey} from '#/state/queries/util'
|
||||||
import {useAgent} from '#/state/session'
|
import {useChatClient} from '#/state/session'
|
||||||
|
import {chat} from '#/lexicons'
|
||||||
|
|
||||||
const RQKEY_ROOT = 'listConvoMembers'
|
const RQKEY_ROOT = 'listConvoMembers'
|
||||||
export const listConvoMembersQueryKey = (convoId: string) =>
|
export const listConvoMembersQueryKey = (convoId: string) =>
|
||||||
@@ -20,19 +20,27 @@ export function useListConvoMembersQuery({
|
|||||||
convoId: string
|
convoId: string
|
||||||
placeholderData?: ChatBskyActorDefs.ProfileViewBasic[]
|
placeholderData?: ChatBskyActorDefs.ProfileViewBasic[]
|
||||||
}) {
|
}) {
|
||||||
const agent = useAgent()
|
const client = useChatClient()
|
||||||
|
|
||||||
return useQuery({
|
return useQuery({
|
||||||
queryKey: listConvoMembersQueryKey(convoId),
|
queryKey: listConvoMembersQueryKey(convoId),
|
||||||
queryFn: async () => {
|
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 {
|
do {
|
||||||
const {data} = await agent.chat.bsky.convo.getConvoMembers(
|
const data = await client.call(chat.bsky.convo.getConvoMembers, {
|
||||||
{convoId, cursor, limit: LIMIT},
|
convoId,
|
||||||
{headers: DM_SERVICE_HEADERS},
|
cursor,
|
||||||
)
|
limit: LIMIT,
|
||||||
|
})
|
||||||
members.push(...data.members)
|
members.push(...data.members)
|
||||||
cursor = data.cursor
|
cursor = data.cursor
|
||||||
} while (cursor)
|
} while (cursor)
|
||||||
|
|||||||
Reference in New Issue
Block a user