Files
bsky-social-app/src/components/dms/LeaveConvoPrompt.tsx
T
Samuel Newman 934d2e2170 restore compile-time error matching with isXrpcErrorOf
The migration replaced typed error classes (e instanceof
SomeNsid.SomeError) with getErrorName(e) === 'X' string comparisons,
where a typo'd or undeclared name compiles silently. isXrpcErrorOf
(method, e, name) constrains name to InferMethodError<M> - the errors
tuple the generated lexicon schema declares - restoring the old
compile-time guarantee (verified: a typo'd name fails typecheck).
47 comparison sites across 16 files migrated.

Deliberately left on getErrorName, with comments:
- LoginForm AuthFactorTokenRequired: a LexAuthFactorError from
  PasswordSession.login, not an XrpcError.
- AppealForm AlreadyAppealed: createReport declares NO errors in its
  lexicon; the server sends it anyway (undeclared behavior worth
  raising upstream).
- PostFeedErrorMessage Block detection: generic over feed descriptors,
  source method genuinely ambiguous.
- isErrorMaybeAppPasswordPermissions: generic InvalidToken + message.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-30 18:19:43 +03:00

68 lines
2.1 KiB
TypeScript

import {useLingui} from '@lingui/react/macro'
import {StackActions, useNavigation} from '@react-navigation/native'
import {type NavigationProp} from '#/lib/routes/types'
import {isNetworkError} from '#/lib/strings/errors'
import {isXrpcErrorOf} from '#/lib/xrpc-error'
import {useLeaveConvo} from '#/state/queries/messages/leave-conversation'
import {type DialogOuterProps} from '#/components/Dialog'
import * as Prompt from '#/components/Prompt'
import * as Toast from '#/components/Toast'
import {IS_NATIVE} from '#/env'
import {chat} from '#/lexicons'
export function LeaveConvoPrompt({
control,
convoId,
currentScreen,
hasMessages = true,
}: {
control: DialogOuterProps['control']
convoId: string
currentScreen: 'list' | 'conversation'
hasMessages?: boolean
}) {
const {t: l} = useLingui()
const navigation = useNavigation<NavigationProp>()
const {mutate: leaveConvo} = useLeaveConvo(convoId, {
onMutate: () => {
if (currentScreen === 'conversation') {
navigation.dispatch(
StackActions.replace('Messages', IS_NATIVE ? {animation: 'pop'} : {}),
)
}
},
onError: error => {
let errorMessage = l`Could not leave chat`
if (isNetworkError(error)) {
errorMessage = l`A network error occurred. Please check your internet connection.`
} else if (
isXrpcErrorOf(chat.bsky.convo.leaveConvo, error, 'InvalidConvo')
) {
errorMessage = l`Conversation not found.`
} else if (
isXrpcErrorOf(chat.bsky.convo.leaveConvo, error, 'OwnerCannotLeave')
) {
errorMessage = l`Owner must lock the group before leaving.`
}
Toast.show(errorMessage, {type: 'error'})
},
})
return (
<Prompt.Basic
control={control}
title={l`Leave conversation`}
description={
hasMessages
? l`Are you sure you want to leave this conversation? Your messages will be deleted for you, but not for the other participants.`
: l`Are you sure you want to leave this conversation?`
}
confirmButtonCta={l`Leave`}
confirmButtonColor="negative"
onConfirm={() => leaveConvo()}
/>
)
}