Files
gitea-native/src/features/repository/screens/pull-detail-screen.tsx
T
JustZoe101 ddd834b7ac feat(phase3): comments, issue compose, PR diff viewer, reviews, merge, checks
- API: comments CRUD, create/edit issue, milestones, collaborators, pull files,
  raw unified diff (getText), reviews, merge, combined commit status
- hooks: comment thread + composer mutations, issue compose mutations,
  pull files/diff/reviews, create review, merge, combined status
- UI: TextArea, ChipSelect, CommentItem, CommentComposer, native DiffView
  (parseDiff), StatusChecks
- screens: IssueCompose (create/edit), PullFiles, PullDiff, PullReview; issue
  detail gains thread + close/reopen; PR detail gains reviews, merge, files,
  checks; commit detail shows checks
- navigation/types + i18n (en/nl) updated; centralized safeHost helper
- tests: diff parser, endpoint body shapes (merge capitalisation), client getText
2026-10-09 17:02:08 +02:00

337 lines
12 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 { 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<RepositoryStackParamList, 'PullDetail'>;
const REVIEW_TONE: Record<string, BadgeTone> = {
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 <LoadingView />;
if (query.isError) {
return (
<ErrorState
title={t('pulls.notFound')}
body={apiErrorMessage(query.error, t)}
retryLabel={t('common.retry')}
onRetry={() => 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 (
<Screen scroll padded>
<View style={styles.titleBlock}>
<AppText variant="heading">
{pull.title} <AppText variant="heading" tone="tertiary">{`#${pull.number}`}</AppText>
</AppText>
<View style={styles.stateRow}>
<StatusBadge label={stateLabel} state={state} />
{pull.head?.label && pull.base?.label ? (
<Badge tone="neutral" icon="branch">
{t('pulls.into', { head: pull.head.label, base: pull.base.label })}
</Badge>
) : null}
</View>
<AppText variant="caption" tone="tertiary">
{t('pulls.openedBy', { name: author?.login ?? t('common.unknown') })} ·{' '}
{formatRelativeTime(pull.created_at, t)}
</AppText>
</View>
{/* Checks */}
{checks.data ? <StatusChecks status={checks.data} /> : null}
{/* Actions */}
<View style={styles.actionRow}>
<Button
label={t('pulls.files', { count: pull.changed_files ?? 0 })}
variant="secondary"
size="md"
iconLeft="file"
onPress={() => navigation.navigate('PullFiles', { owner, name, index })}
/>
{canAct ? (
<>
<Button
label={t('pulls.approve')}
variant="secondary"
size="md"
iconLeft="check"
onPress={() =>
navigation.navigate('PullReview', { owner, name, index, event: 'APPROVE' })
}
/>
<Button
label={t('pulls.requestChanges')}
variant="secondary"
size="md"
iconLeft="close"
onPress={() =>
navigation.navigate('PullReview', { owner, name, index, event: 'REQUEST_CHANGES' })
}
/>
<Button
label={t('pulls.comment')}
variant="secondary"
size="md"
iconLeft="mail"
onPress={() =>
navigation.navigate('PullReview', { owner, name, index, event: 'COMMENT' })
}
/>
<Button
label={t('pulls.merge')}
size="md"
iconLeft="pullRequest"
loading={merge.isPending}
disabled={mergeDisabled}
onPress={confirmMerge}
/>
</>
) : null}
</View>
{pull.mergeable === false && canAct ? (
<AppText variant="caption" tone="warning">
{t('pulls.notMergeable')}
</AppText>
) : null}
{pull.labels && pull.labels.length > 0 ? (
<View style={styles.labels}>
{pull.labels.map((label, labelIndex) => (
<LabelChip
key={label.id ?? `${label.name}-${labelIndex}`}
name={label.name}
color={label.color}
/>
))}
</View>
) : null}
<Card style={styles.bodyCard}>
{pull.body && pull.body.trim().length > 0 ? (
<Markdown content={pull.body} baseUrl={instance?.baseUrl} />
) : (
<AppText variant="secondary" tone="tertiary">
{t('pulls.noDescription')}
</AppText>
)}
</Card>
<View style={styles.stats}>
<Badge tone="success">{`+${pull.additions ?? 0}`}</Badge>
<Badge tone="error">{`−${pull.deletions ?? 0}`}</Badge>
<AppText variant="caption" tone="tertiary">
{t('pulls.changedFiles', { count: pull.changed_files ?? 0 })}
</AppText>
<AppText variant="caption" tone="tertiary">
{t('issues.comments', { count: pull.comments ?? comments.length })}
</AppText>
</View>
{pull.requested_reviewers && pull.requested_reviewers.length > 0 ? (
<View style={styles.reviewers}>
<AppText variant="label" tone="secondary">
{t('pulls.reviewers')}
</AppText>
<View style={styles.reviewerRow}>
{pull.requested_reviewers.map((reviewer) => (
<View key={reviewer.id ?? reviewer.login} style={styles.reviewer}>
<Avatar url={reviewer.avatar_url} name={reviewer.login} size={24} />
<AppText variant="caption" tone="secondary">
{reviewer.login}
</AppText>
</View>
))}
</View>
</View>
) : null}
{/* Reviews */}
{reviews.length > 0 ? (
<>
<AppText variant="subheading" style={styles.sectionTitle}>
{t('pulls.reviews')}
</AppText>
{reviews.map((review, reviewIndex) => (
<Card key={review.id ?? reviewIndex}>
<View style={styles.reviewHeader}>
<Avatar url={review.user?.avatar_url} name={review.user?.login ?? '?'} size={24} />
<AppText variant="bodyMedium" style={styles.reviewerName} numberOfLines={1}>
{review.user?.login ?? t('common.unknown')}
</AppText>
<Badge tone={REVIEW_TONE[review.state ?? 'PENDING'] ?? 'neutral'}>
{t(reviewKey(review.state))}
</Badge>
</View>
{review.body && review.body.trim().length > 0 ? (
<Markdown content={review.body} baseUrl={instance?.baseUrl} />
) : null}
</Card>
))}
</>
) : null}
{/* Comments */}
<AppText variant="subheading" style={styles.sectionTitle}>
{t('comments.title')}
</AppText>
{commentsQuery.isPending ? (
<LoadingView label={t('comments.loading')} />
) : comments.length === 0 ? (
<AppText variant="secondary" tone="tertiary">
{t('comments.empty')}
</AppText>
) : (
comments.map((comment) => {
const isOwn = Boolean(account?.login && comment.user?.login === account.login);
const commentId = comment.id;
return (
<CommentItem
key={commentId}
comment={comment}
baseUrl={instance?.baseUrl}
token={token}
host={host}
isOwn={isOwn}
updating={updateComment.isPending}
onUpdate={
commentId
? (body) => updateComment.mutateAsync({ id: commentId, body })
: undefined
}
onDelete={commentId ? () => deleteComment.mutateAsync(commentId) : undefined}
/>
);
})
)}
<CommentComposer
onSubmit={(body) => createComment.mutateAsync(body)}
pending={createComment.isPending}
errorMessage={createComment.isError ? apiErrorMessage(createComment.error, t) : null}
/>
</Screen>
);
}
const styles = StyleSheet.create({
titleBlock: { gap: spacing.sm },
stateRow: { flexDirection: 'row', alignItems: 'center', gap: spacing.sm, flexWrap: 'wrap' },
actionRow: { flexDirection: 'row', flexWrap: 'wrap', gap: spacing.sm },
labels: { flexDirection: 'row', flexWrap: 'wrap', gap: spacing.sm },
bodyCard: { minHeight: 80 },
stats: { flexDirection: 'row', alignItems: 'center', gap: spacing.md, flexWrap: 'wrap' },
reviewers: { gap: spacing.sm },
reviewerRow: { flexDirection: 'row', flexWrap: 'wrap', gap: spacing.lg },
reviewer: { flexDirection: 'row', alignItems: 'center', gap: spacing.sm },
reviewHeader: { flexDirection: 'row', alignItems: 'center', gap: spacing.sm },
reviewerName: { flexShrink: 1, flex: 1, fontWeight: '600' },
sectionTitle: { marginTop: spacing.md },
});