[🐴] Swap in new package, update usages (#3992)

* Swap in new package, update usages

* Remove uneccessary patch

* Override type in safe place
This commit is contained in:
Eric Bailey 2024-05-14 09:22:09 -05:00 committed by GitHub
parent 107760d551
commit 9173be686c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
22 changed files with 79 additions and 173 deletions

View file

@ -0,0 +1,3 @@
export const DM_SERVICE_HEADERS = {
'atproto-proxy': 'did:web:dms.divy.zone#bsky_chat',
}

View file

@ -1,26 +1,23 @@
import {BskyAgent} from '@atproto-labs/api'
import {ConvoView} from '@atproto-labs/api/dist/client/types/chat/bsky/convo/defs'
import {ChatBskyConvoDefs} from '@atproto/api'
import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query'
import {DM_SERVICE_HEADERS} from '#/state/queries/messages/const'
import {useOnMarkAsRead} from '#/state/queries/messages/list-converations'
import {useDmServiceUrlStorage} from '#/screens/Messages/Temp/useDmServiceUrlStorage'
import {useAgent} from '#/state/session'
import {RQKEY as LIST_CONVOS_KEY} from './list-converations'
import {useHeaders} from './temp-headers'
const RQKEY_ROOT = 'convo'
export const RQKEY = (convoId: string) => [RQKEY_ROOT, convoId]
export function useConvoQuery(convo: ConvoView) {
const headers = useHeaders()
const {serviceUrl} = useDmServiceUrlStorage()
export function useConvoQuery(convo: ChatBskyConvoDefs.ConvoView) {
const {getAgent} = useAgent()
return useQuery({
queryKey: RQKEY(convo.id),
queryFn: async () => {
const agent = new BskyAgent({service: serviceUrl})
const {data} = await agent.api.chat.bsky.convo.getConvo(
const {data} = await getAgent().api.chat.bsky.convo.getConvo(
{convoId: convo.id},
{headers},
{headers: DM_SERVICE_HEADERS},
)
return data.convo
},
@ -29,10 +26,9 @@ export function useConvoQuery(convo: ConvoView) {
}
export function useMarkAsReadMutation() {
const headers = useHeaders()
const {serviceUrl} = useDmServiceUrlStorage()
const optimisticUpdate = useOnMarkAsRead()
const queryClient = useQueryClient()
const {getAgent} = useAgent()
return useMutation({
mutationFn: async ({
@ -44,15 +40,14 @@ export function useMarkAsReadMutation() {
}) => {
if (!convoId) throw new Error('No convoId provided')
const agent = new BskyAgent({service: serviceUrl})
await agent.api.chat.bsky.convo.updateRead(
await getAgent().api.chat.bsky.convo.updateRead(
{
convoId,
messageId,
},
{
encoding: 'application/json',
headers,
headers: DM_SERVICE_HEADERS,
},
)
},

View file

@ -1,10 +1,10 @@
import {BskyAgent, ChatBskyConvoGetConvoForMembers} from '@atproto-labs/api'
import {ChatBskyConvoGetConvoForMembers} from '@atproto/api'
import {useMutation, useQueryClient} from '@tanstack/react-query'
import {logger} from '#/logger'
import {useDmServiceUrlStorage} from '#/screens/Messages/Temp/useDmServiceUrlStorage'
import {DM_SERVICE_HEADERS} from '#/state/queries/messages/const'
import {useAgent} from '#/state/session'
import {RQKEY as CONVO_KEY} from './conversation'
import {useHeaders} from './temp-headers'
export function useGetConvoForMembers({
onSuccess,
@ -14,15 +14,13 @@ export function useGetConvoForMembers({
onError?: (error: Error) => void
}) {
const queryClient = useQueryClient()
const headers = useHeaders()
const {serviceUrl} = useDmServiceUrlStorage()
const {getAgent} = useAgent()
return useMutation({
mutationFn: async (members: string[]) => {
const agent = new BskyAgent({service: serviceUrl})
const {data} = await agent.api.chat.bsky.convo.getConvoForMembers(
const {data} = await getAgent().api.chat.bsky.convo.getConvoForMembers(
{members: members},
{headers},
{headers: DM_SERVICE_HEADERS},
)
return data

View file

@ -1,14 +1,10 @@
import {
BskyAgent,
ChatBskyConvoLeaveConvo,
ChatBskyConvoListConvos,
} from '@atproto-labs/api'
import {ChatBskyConvoLeaveConvo, ChatBskyConvoListConvos} from '@atproto/api'
import {useMutation, useQueryClient} from '@tanstack/react-query'
import {logger} from '#/logger'
import {useDmServiceUrlStorage} from '#/screens/Messages/Temp/useDmServiceUrlStorage'
import {DM_SERVICE_HEADERS} from '#/state/queries/messages/const'
import {useAgent} from '#/state/session'
import {RQKEY as CONVO_LIST_KEY} from './list-converations'
import {useHeaders} from './temp-headers'
export function useLeaveConvo(
convoId: string | undefined,
@ -21,17 +17,15 @@ export function useLeaveConvo(
},
) {
const queryClient = useQueryClient()
const headers = useHeaders()
const {serviceUrl} = useDmServiceUrlStorage()
const {getAgent} = useAgent()
return useMutation({
mutationFn: async () => {
if (!convoId) throw new Error('No convoId provided')
const agent = new BskyAgent({service: serviceUrl})
const {data} = await agent.api.chat.bsky.convo.leaveConvo(
const {data} = await getAgent().api.chat.bsky.convo.leaveConvo(
{convoId},
{headers, encoding: 'application/json'},
{headers: DM_SERVICE_HEADERS, encoding: 'application/json'},
)
return data

View file

@ -1,29 +1,23 @@
import {useCallback, useMemo} from 'react'
import {
BskyAgent,
ChatBskyConvoDefs,
ChatBskyConvoListConvos,
} from '@atproto-labs/api'
import {ChatBskyConvoDefs, ChatBskyConvoListConvos} from '@atproto/api'
import {useInfiniteQuery, useQueryClient} from '@tanstack/react-query'
import {useCurrentConvoId} from '#/state/messages/current-convo-id'
import {useDmServiceUrlStorage} from '#/screens/Messages/Temp/useDmServiceUrlStorage'
import {useHeaders} from './temp-headers'
import {DM_SERVICE_HEADERS} from '#/state/queries/messages/const'
import {useAgent} from '#/state/session'
export const RQKEY = ['convo-list']
type RQPageParam = string | undefined
export function useListConvos({refetchInterval}: {refetchInterval: number}) {
const headers = useHeaders()
const {serviceUrl} = useDmServiceUrlStorage()
const {getAgent} = useAgent()
return useInfiniteQuery({
queryKey: RQKEY,
queryFn: async ({pageParam}) => {
const agent = new BskyAgent({service: serviceUrl})
const {data} = await agent.api.chat.bsky.convo.listConvos(
const {data} = await getAgent().api.chat.bsky.convo.listConvos(
{cursor: pageParam},
{headers},
{headers: DM_SERVICE_HEADERS},
)
return data

View file

@ -1,15 +1,14 @@
import {
BskyAgent,
ChatBskyConvoDefs,
ChatBskyConvoListConvos,
ChatBskyConvoMuteConvo,
} from '@atproto-labs/api'
} from '@atproto/api'
import {InfiniteData, useMutation, useQueryClient} from '@tanstack/react-query'
import {useDmServiceUrlStorage} from '#/screens/Messages/Temp/useDmServiceUrlStorage'
import {DM_SERVICE_HEADERS} from '#/state/queries/messages/const'
import {useAgent} from '#/state/session'
import {RQKEY as CONVO_KEY} from './conversation'
import {RQKEY as CONVO_LIST_KEY} from './list-converations'
import {useHeaders} from './temp-headers'
export function useMuteConvo(
convoId: string | undefined,
@ -22,24 +21,23 @@ export function useMuteConvo(
},
) {
const queryClient = useQueryClient()
const headers = useHeaders()
const {serviceUrl} = useDmServiceUrlStorage()
const {getAgent} = useAgent()
return useMutation({
mutationFn: async ({mute}: {mute: boolean}) => {
if (!convoId) throw new Error('No convoId provided')
const agent = new BskyAgent({service: serviceUrl})
const agent = getAgent()
if (mute) {
const {data} = await agent.api.chat.bsky.convo.muteConvo(
{convoId},
{headers, encoding: 'application/json'},
{headers: DM_SERVICE_HEADERS, encoding: 'application/json'},
)
return data
} else {
const {data} = await agent.api.chat.bsky.convo.unmuteConvo(
{convoId},
{headers, encoding: 'application/json'},
{headers: DM_SERVICE_HEADERS, encoding: 'application/json'},
)
return data
}

View file

@ -1,11 +0,0 @@
import {useSession} from '#/state/session'
// toy auth
export const useHeaders = () => {
const {currentAccount} = useSession()
return {
get Authorization() {
return currentAccount!.did
},
}
}