forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import {type AppBskyActorDefs} from '@atproto/api'
2import {useMutation, useQueryClient} from '@tanstack/react-query'
3
4import {logger} from '#/logger'
5import {useAgent, useSession} from '#/state/session'
6import {RQKEY as PROFILE_RKEY} from '../profile'
7
8export function useUpdateActorDeclaration({
9 onSuccess,
10 onError,
11}: {
12 onSuccess?: () => void
13 onError?: (error: Error) => void
14}) {
15 const queryClient = useQueryClient()
16 const {currentAccount} = useSession()
17 const agent = useAgent()
18
19 return useMutation({
20 mutationFn: async (allowIncoming: 'all' | 'none' | 'following') => {
21 if (!currentAccount) throw new Error('Not signed in')
22 const result = await agent.com.atproto.repo.putRecord({
23 repo: currentAccount.did,
24 collection: 'chat.bsky.actor.declaration',
25 rkey: 'self',
26 record: {
27 $type: 'chat.bsky.actor.declaration',
28 allowIncoming,
29 },
30 })
31 return result
32 },
33 onMutate: allowIncoming => {
34 if (!currentAccount) return
35 queryClient.setQueryData(
36 PROFILE_RKEY(currentAccount?.did),
37 (old?: AppBskyActorDefs.ProfileViewDetailed) => {
38 if (!old) return old
39 return {
40 ...old,
41 associated: {
42 ...old.associated,
43 chat: {
44 allowIncoming,
45 },
46 },
47 } satisfies AppBskyActorDefs.ProfileViewDetailed
48 },
49 )
50 },
51 onSuccess,
52 onError: error => {
53 logger.error(error)
54 if (currentAccount) {
55 queryClient.invalidateQueries({
56 queryKey: PROFILE_RKEY(currentAccount.did),
57 })
58 }
59 onError?.(error)
60 },
61 })
62}
63
64// for use in the settings screen for testing
65export function useDeleteActorDeclaration() {
66 const {currentAccount} = useSession()
67 const agent = useAgent()
68
69 return useMutation({
70 mutationFn: async () => {
71 if (!currentAccount) throw new Error('Not signed in')
72 const result = await agent.api.com.atproto.repo.deleteRecord({
73 repo: currentAccount.did,
74 collection: 'chat.bsky.actor.declaration',
75 rkey: 'self',
76 })
77 return result
78 },
79 })
80}