diff options
author | Samuel Newman <mozzius@protonmail.com> | 2024-05-14 18:57:16 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-14 18:57:16 +0100 |
commit | 5af61ca4e48f5b4e8b2663cf179cb4973dcfddf4 (patch) | |
tree | 8ac8ce8fa3a27f9be1ddb4f308f72603bfb65141 /src/state/queries/messages/actor-declaration.ts | |
parent | 9861494e341b482a522d0ecc6a2194bb12a769fb (diff) | |
download | voidsky-5af61ca4e48f5b4e8b2663cf179cb4973dcfddf4.tar.zst |
[🐴] Settings screen (#3830)
* create settings screen + api * update api package * use putrecord API with validate false * create new RadioGroup component
Diffstat (limited to 'src/state/queries/messages/actor-declaration.ts')
-rw-r--r-- | src/state/queries/messages/actor-declaration.ts | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/state/queries/messages/actor-declaration.ts b/src/state/queries/messages/actor-declaration.ts new file mode 100644 index 000000000..c8cc4acbd --- /dev/null +++ b/src/state/queries/messages/actor-declaration.ts @@ -0,0 +1,64 @@ +import {AppBskyActorDefs} from '@atproto/api' +import {useMutation, useQueryClient} from '@tanstack/react-query' + +import {logger} from '#/logger' +import {useAgent, useSession} from '#/state/session' +import {RQKEY as PROFILE_RKEY} from '../profile' + +export function useUpdateActorDeclaration({ + onSuccess, + onError, +}: { + onSuccess?: () => void + onError?: (error: Error) => void +}) { + const queryClient = useQueryClient() + const {currentAccount} = useSession() + const {getAgent} = useAgent() + + return useMutation({ + mutationFn: async (allowIncoming: 'all' | 'none' | 'following') => { + if (!currentAccount) throw new Error('Not logged in') + // TODO(sam): remove validate: false once PDSes have the new lexicon + const result = await getAgent().api.com.atproto.repo.putRecord({ + collection: 'chat.bsky.actor.declaration', + rkey: 'self', + repo: currentAccount.did, + validate: false, + record: { + $type: 'chat.bsky.actor.declaration', + allowIncoming, + }, + }) + return result + }, + onMutate: allowIncoming => { + if (!currentAccount) return + queryClient.setQueryData( + PROFILE_RKEY(currentAccount?.did), + (old?: AppBskyActorDefs.ProfileViewDetailed) => { + if (!old) return old + return { + ...old, + associated: { + ...old.associated, + chat: { + allowIncoming, + }, + }, + } satisfies AppBskyActorDefs.ProfileViewDetailed + }, + ) + }, + onSuccess, + onError: error => { + logger.error(error) + if (currentAccount) { + queryClient.invalidateQueries({ + queryKey: PROFILE_RKEY(currentAccount.did), + }) + } + onError?.(error) + }, + }) +} |