cdb8d4bfb8
Co-authored-by: DS Boyce <260543580+ds-boyce@users.noreply.github.com> Co-authored-by: Samuel Newman <mozzius@protonmail.com> Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
85 lines
2.2 KiB
TypeScript
85 lines
2.2 KiB
TypeScript
import {
|
|
ChatBskyConvoDefs,
|
|
type ChatBskyGroupCreateJoinLink,
|
|
type ChatBskyGroupDefs,
|
|
} from '@atproto/api'
|
|
import {useMutation, useQueryClient} from '@tanstack/react-query'
|
|
|
|
import {DM_SERVICE_HEADERS} from '#/lib/constants'
|
|
import {logger} from '#/logger'
|
|
import {useAgent} from '#/state/session'
|
|
import {
|
|
rollbackConvoOptimistic,
|
|
updateConvoOptimistic,
|
|
} from './utils/convo-cache'
|
|
|
|
export function useCreateJoinLink(
|
|
convoId: string | undefined,
|
|
{
|
|
onSuccess,
|
|
onError,
|
|
}: {
|
|
onSuccess?: (data: ChatBskyGroupCreateJoinLink.OutputSchema) => void
|
|
onError?: (error: Error) => void
|
|
},
|
|
) {
|
|
const queryClient = useQueryClient()
|
|
const agent = useAgent()
|
|
|
|
return useMutation({
|
|
mutationFn: async ({
|
|
joinRule,
|
|
requireApproval,
|
|
}: {
|
|
joinRule: ChatBskyGroupDefs.JoinRule
|
|
requireApproval: boolean
|
|
}) => {
|
|
if (!convoId) throw new Error('No convoId provided')
|
|
const {data} = await agent.chat.bsky.group.createJoinLink(
|
|
{convoId, joinRule, requireApproval},
|
|
{headers: DM_SERVICE_HEADERS, encoding: 'application/json'},
|
|
)
|
|
return data
|
|
},
|
|
onMutate: ({joinRule, requireApproval}) => {
|
|
if (!convoId) return
|
|
return updateConvoOptimistic(queryClient, convoId, prev => {
|
|
if (!ChatBskyConvoDefs.isGroupConvo(prev.kind)) return undefined
|
|
return {
|
|
...prev,
|
|
kind: {
|
|
...prev.kind,
|
|
joinLink: {
|
|
$type: 'chat.bsky.group.defs#joinLinkView',
|
|
code: '',
|
|
enabledStatus: 'enabled',
|
|
joinRule,
|
|
requireApproval,
|
|
createdAt: new Date().toISOString(),
|
|
},
|
|
},
|
|
}
|
|
})
|
|
},
|
|
onSuccess: data => {
|
|
if (convoId) {
|
|
updateConvoOptimistic(queryClient, convoId, prev => {
|
|
if (!ChatBskyConvoDefs.isGroupConvo(prev.kind)) return undefined
|
|
return {
|
|
...prev,
|
|
kind: {...prev.kind, joinLink: data.joinLink},
|
|
}
|
|
})
|
|
}
|
|
onSuccess?.(data)
|
|
},
|
|
onError: (e, _variables, context) => {
|
|
logger.error(e)
|
|
if (convoId && context) {
|
|
rollbackConvoOptimistic(queryClient, convoId, context)
|
|
}
|
|
onError?.(e)
|
|
},
|
|
})
|
|
}
|