import { Alert, StyleSheet, View } from 'react-native'; import { useTranslation } from 'react-i18next'; import { CommentItem } from '@/components/git/comment'; import { CommentComposer } from '@/components/git/comment-composer'; import { Markdown } from '@/components/git/markdown'; import { ReviewComments } from '@/components/git/review-comments'; import { StatusChecks } from '@/components/git/status-checks'; import { Screen } from '@/components/layout/screen'; import { AppText } from '@/components/ui/app-text'; import { Avatar } from '@/components/ui/avatar'; import { Badge, type BadgeTone, StatusBadge } from '@/components/ui/badge'; import { Button } from '@/components/ui/button'; import { Card } from '@/components/ui/card'; import { LabelChip } from '@/components/ui/label-chip'; import { ErrorState, LoadingView } from '@/components/ui/state-views'; import { apiErrorMessage } from '@/core/api/error-text'; import { safeHost } from '@/core/api/instance-url'; import type { Comment, PullReview } from '@/core/api/types'; import { flattenPages } from '@/core/utils/infinite'; import { useSession } from '@/features/accounts/use-session'; import { useCombinedStatus, useCreateIssueComment, useDeleteIssueComment, useIssueComments, useMergePull, usePull, usePullReviews, useUpdateIssueComment, } from '@/features/issues/queries'; import { formatRelativeTime } from '@/core/utils/format'; import { spacing } from '@/theme/tokens'; import type { RepositoryStackParamList } from '@/navigation/types'; import type { ScreenProps } from '@/navigation/typing'; type Props = ScreenProps; const REVIEW_TONE: Record = { APPROVED: 'success', REQUEST_CHANGES: 'error', COMMENT: 'neutral', PENDING: 'warning', }; function reviewKey(state: string | undefined): string { switch (state) { case 'APPROVED': return 'pulls.reviewStateApproved'; case 'REQUEST_CHANGES': return 'pulls.reviewStateChanges'; case 'COMMENT': return 'pulls.reviewStateComment'; default: return 'pulls.reviewStatePending'; } } /** Full pull request view: state, branches, checks, reviews, comments, review/merge actions. */ export function PullDetailScreen({ navigation, route }: Props) { const { t } = useTranslation(); const { instance, account, token } = useSession(); const { owner, name, index } = route.params; const query = usePull(owner, name, index); const pull = query.data; const checks = useCombinedStatus(owner, name, pull?.head?.sha); const reviewsQuery = usePullReviews(owner, name, index); const commentsQuery = useIssueComments(owner, name, index); const createComment = useCreateIssueComment(owner, name, index); const updateComment = useUpdateIssueComment(owner, name, index); const deleteComment = useDeleteIssueComment(owner, name, index); const merge = useMergePull(owner, name, index); if (query.isPending) return ; if (query.isError) { return ( void query.refetch()} /> ); } if (!pull) return null; const author = pull.user; const state: 'open' | 'closed' | 'merged' | 'draft' = pull.merged ? 'merged' : pull.draft && pull.state === 'open' ? 'draft' : pull.state === 'closed' ? 'closed' : 'open'; const stateLabel = { open: t('pulls.stateOpen'), closed: t('pulls.stateClosed'), merged: t('pulls.stateMerged'), draft: t('pulls.draft'), }[state]; const host = instance ? safeHost(instance.baseUrl) : null; const comments: Comment[] = flattenPages(commentsQuery.data); const reviews: PullReview[] = reviewsQuery.data ?? []; const canAct = state === 'open'; const mergeDisabled = merge.isPending || pull.mergeable === false; const doMerge = (style: 'merge' | 'squash' | 'rebase') => { merge.mutate( { style, deleteBranchAfterMerge: false }, { onSuccess: () => Alert.alert(t('pulls.mergeSuccessTitle'), t('pulls.mergeSuccessBody')), onError: (error) => Alert.alert(t('errors.title'), apiErrorMessage(error, t)), }, ); }; const confirmMerge = () => { Alert.alert(t('pulls.mergeConfirmTitle'), t('pulls.mergeConfirmBody'), [ { text: t('common.cancel'), style: 'cancel' }, { text: t('pulls.mergeSquash'), onPress: () => doMerge('squash') }, { text: t('pulls.mergeCreate'), onPress: () => doMerge('merge') }, ]); }; return ( {pull.title} {`#${pull.number}`} {pull.head?.label && pull.base?.label ? ( {t('pulls.into', { head: pull.head.label, base: pull.base.label })} ) : null} {t('pulls.openedBy', { name: author?.login ?? t('common.unknown') })} ·{' '} {formatRelativeTime(pull.created_at, t)} {/* Checks */} {checks.data ? : null} {/* Actions */}