- subscribe/unsubscribe to an issue from the detail screen - dispatch a workflow_dispatch run (workflow + ref) from Actions settings - i18n (en/nl)
330 lines
11 KiB
TypeScript
330 lines
11 KiB
TypeScript
import { Linking, Pressable, 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 { Screen } from '@/components/layout/screen';
|
|
import { AppText } from '@/components/ui/app-text';
|
|
import { Avatar } from '@/components/ui/avatar';
|
|
import { Badge, StatusBadge } from '@/components/ui/badge';
|
|
import { Button } from '@/components/ui/button';
|
|
import { Card } from '@/components/ui/card';
|
|
import { Icon } from '@/components/ui/icon';
|
|
import { LabelChip } from '@/components/ui/label-chip';
|
|
import { ListItem } from '@/components/ui/list-item';
|
|
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 } from '@/core/api/types';
|
|
import { flattenPages } from '@/core/utils/infinite';
|
|
import { useSession } from '@/features/accounts/use-session';
|
|
import {
|
|
useCreateIssueComment,
|
|
useDeleteIssueComment,
|
|
useIssue,
|
|
useIssueAttachments,
|
|
useIssueComments,
|
|
useIssueReactions,
|
|
useIssueSubscription,
|
|
useToggleIssueReaction,
|
|
useToggleIssueSubscription,
|
|
useUpdateIssue,
|
|
useUpdateIssueComment,
|
|
} from '@/features/issues/queries';
|
|
import { formatRelativeTime } from '@/core/utils/format';
|
|
import { useTheme } from '@/theme/theme-provider';
|
|
import { spacing } from '@/theme/tokens';
|
|
import type { RepositoryStackParamList } from '@/navigation/types';
|
|
import type { ScreenProps } from '@/navigation/typing';
|
|
|
|
type Props = ScreenProps<RepositoryStackParamList, 'IssueDetail'>;
|
|
|
|
const REACTION_EMOJI: Record<string, string> = {
|
|
'+1': '👍',
|
|
'-1': '👎',
|
|
laugh: '😄',
|
|
hooray: '🎉',
|
|
confused: '😕',
|
|
heart: '❤️',
|
|
rocket: '🚀',
|
|
eyes: '👀',
|
|
};
|
|
|
|
/** Full issue view: state, author, labels, milestone, body, comments and composer. */
|
|
export function IssueDetailScreen({ navigation, route }: Props) {
|
|
const { t } = useTranslation();
|
|
const { colors } = useTheme();
|
|
const { instance, account, token } = useSession();
|
|
const { owner, name, index } = route.params;
|
|
|
|
const query = useIssue(owner, name, index);
|
|
const issue = query.data;
|
|
const commentsQuery = useIssueComments(owner, name, index);
|
|
const updateIssue = useUpdateIssue(owner, name, index);
|
|
const createComment = useCreateIssueComment(owner, name, index);
|
|
const updateComment = useUpdateIssueComment(owner, name, index);
|
|
const deleteComment = useDeleteIssueComment(owner, name, index);
|
|
const reactionsQuery = useIssueReactions(owner, name, index);
|
|
const toggleReaction = useToggleIssueReaction(owner, name, index);
|
|
const attachmentsQuery = useIssueAttachments(owner, name, index);
|
|
const subscription = useIssueSubscription(owner, name, index, account?.login);
|
|
const toggleSubscription = useToggleIssueSubscription(owner, name, index, account?.login);
|
|
|
|
if (query.isPending) return <LoadingView />;
|
|
if (query.isError) {
|
|
return (
|
|
<ErrorState
|
|
title={t('issues.notFound')}
|
|
body={apiErrorMessage(query.error, t)}
|
|
retryLabel={t('common.retry')}
|
|
onRetry={() => void query.refetch()}
|
|
/>
|
|
);
|
|
}
|
|
if (!issue) return null;
|
|
|
|
const author = issue.user;
|
|
const state = issue.state === 'closed' ? 'closed' : 'open';
|
|
const host = instance ? safeHost(instance.baseUrl) : null;
|
|
const comments: Comment[] = flattenPages(commentsQuery.data);
|
|
|
|
const toggleState = () => {
|
|
updateIssue.mutate({ state: state === 'closed' ? 'open' : 'closed' });
|
|
};
|
|
|
|
return (
|
|
<Screen scroll padded>
|
|
<View style={styles.titleBlock}>
|
|
<AppText variant="heading">
|
|
{issue.title} <AppText variant="heading" tone="tertiary">{`#${issue.number}`}</AppText>
|
|
</AppText>
|
|
<View style={styles.stateRow}>
|
|
<StatusBadge label={state === 'closed' ? t('issues.closed') : t('issues.open')} state={state} />
|
|
{issue.is_locked ? <Badge tone="neutral" icon="lock">{`#${issue.number}`}</Badge> : null}
|
|
<AppText variant="caption" tone="tertiary">
|
|
{t('issues.openedBy', { name: author?.login ?? t('common.unknown') })} ·{' '}
|
|
{formatRelativeTime(issue.created_at, t)}
|
|
</AppText>
|
|
</View>
|
|
</View>
|
|
|
|
<View style={styles.actionRow}>
|
|
<Button
|
|
label={t('common.edit')}
|
|
variant="secondary"
|
|
size="md"
|
|
iconLeft="edit"
|
|
onPress={() => navigation.navigate('IssueCompose', { owner, name, index })}
|
|
/>
|
|
<Button
|
|
label={state === 'closed' ? t('issues.reopen') : t('issues.close')}
|
|
variant="secondary"
|
|
size="md"
|
|
iconLeft={state === 'closed' ? 'refresh' : 'check'}
|
|
loading={updateIssue.isPending}
|
|
onPress={toggleState}
|
|
/>
|
|
{account?.login ? (
|
|
<Button
|
|
label={subscription.data?.subscribed ? t('issues.unsubscribe') : t('issues.subscribe')}
|
|
variant="secondary"
|
|
size="md"
|
|
iconLeft="bell"
|
|
loading={toggleSubscription.isPending}
|
|
onPress={() => toggleSubscription.mutate(subscription.data?.subscribed ?? false)}
|
|
/>
|
|
) : null}
|
|
</View>
|
|
|
|
<View style={styles.authorRow}>
|
|
<Avatar url={author?.avatar_url} name={author?.login ?? '?'} size={28} />
|
|
<AppText variant="bodyMedium" style={styles.authorName}>
|
|
{author?.login ?? t('common.unknown')}
|
|
</AppText>
|
|
</View>
|
|
|
|
{issue.labels && issue.labels.length > 0 ? (
|
|
<View style={styles.labels}>
|
|
{issue.labels.map((label, labelIndex) => (
|
|
<LabelChip
|
|
key={label.id ?? `${label.name}-${labelIndex}`}
|
|
name={label.name}
|
|
color={label.color}
|
|
/>
|
|
))}
|
|
</View>
|
|
) : null}
|
|
|
|
<Card style={styles.bodyCard}>
|
|
{issue.body && issue.body.trim().length > 0 ? (
|
|
<Markdown content={issue.body} baseUrl={instance?.baseUrl} />
|
|
) : (
|
|
<AppText variant="secondary" tone="tertiary">
|
|
{t('issues.noDescription')}
|
|
</AppText>
|
|
)}
|
|
</Card>
|
|
|
|
<View style={styles.metaGrid}>
|
|
{issue.assignees && issue.assignees.length > 0 ? (
|
|
<MetaRow
|
|
icon="person"
|
|
label={t('issues.assignedTo', {
|
|
name: issue.assignees.map((a) => a.login).join(', '),
|
|
})}
|
|
/>
|
|
) : null}
|
|
{issue.milestone ? (
|
|
<MetaRow icon="tag" label={`${t('issues.milestone')}: ${issue.milestone.title}`} />
|
|
) : null}
|
|
<MetaRow
|
|
icon="mail"
|
|
label={t('issues.comments', { count: issue.comments ?? comments.length })}
|
|
color={colors.textSecondary}
|
|
/>
|
|
</View>
|
|
|
|
{/* Reactions */}
|
|
<View style={styles.reactions}>
|
|
{Object.entries(REACTION_EMOJI).map(([content, emoji]) => {
|
|
const list = (reactionsQuery.data ?? []).filter((reaction) => reaction.content === content);
|
|
const mine = list.some((reaction) => reaction.user?.login === account?.login);
|
|
return (
|
|
<Pressable
|
|
key={content}
|
|
onPress={() => toggleReaction.mutate({ content, mine })}
|
|
accessibilityRole="button"
|
|
accessibilityLabel={content}
|
|
style={[
|
|
styles.reactionChip,
|
|
{
|
|
borderColor: mine ? colors.primary : colors.border,
|
|
backgroundColor: mine ? colors.primaryMuted : colors.surface,
|
|
},
|
|
]}
|
|
>
|
|
<AppText variant="caption">{emoji}</AppText>
|
|
{list.length > 0 ? (
|
|
<AppText variant="caption" tone={mine ? 'primary' : 'tertiary'}>
|
|
{list.length}
|
|
</AppText>
|
|
) : null}
|
|
</Pressable>
|
|
);
|
|
})}
|
|
</View>
|
|
|
|
{/* Attachments */}
|
|
{attachmentsQuery.data && attachmentsQuery.data.length > 0 ? (
|
|
<Card padded={false} variant="flat">
|
|
{attachmentsQuery.data.map((attachment, attachmentIndex) => (
|
|
<ListItem
|
|
key={attachment.id ?? attachment.name ?? attachmentIndex}
|
|
leadingIcon="file"
|
|
title={attachment.name ?? ''}
|
|
onPress={() =>
|
|
attachment.browser_download_url
|
|
? void Linking.openURL(attachment.browser_download_url).catch(() => undefined)
|
|
: undefined
|
|
}
|
|
divider={attachmentIndex < (attachmentsQuery.data?.length ?? 0) - 1}
|
|
/>
|
|
))}
|
|
</Card>
|
|
) : null}
|
|
|
|
{/* Comments */}
|
|
<AppText variant="subheading" style={styles.sectionTitle}>
|
|
{t('comments.title')}
|
|
</AppText>
|
|
{commentsQuery.isPending ? (
|
|
<LoadingView label={t('comments.loading')} />
|
|
) : commentsQuery.isError ? (
|
|
<ErrorState
|
|
title={t('errors.title')}
|
|
body={apiErrorMessage(commentsQuery.error, t)}
|
|
retryLabel={t('common.retry')}
|
|
onRetry={() => void commentsQuery.refetch()}
|
|
/>
|
|
) : 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>
|
|
);
|
|
}
|
|
|
|
function MetaRow({
|
|
icon,
|
|
label,
|
|
color,
|
|
}: {
|
|
icon: 'person' | 'tag' | 'mail';
|
|
label: string;
|
|
color?: string;
|
|
}) {
|
|
const { colors } = useTheme();
|
|
return (
|
|
<View style={styles.metaRow}>
|
|
<Icon name={icon} size={15} color={color ?? colors.textTertiary} />
|
|
<AppText variant="caption" tone="secondary">
|
|
{label}
|
|
</AppText>
|
|
</View>
|
|
);
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
titleBlock: { gap: spacing.sm },
|
|
stateRow: { flexDirection: 'row', alignItems: 'center', gap: spacing.sm, flexWrap: 'wrap' },
|
|
actionRow: { flexDirection: 'row', gap: spacing.sm, flexWrap: 'wrap' },
|
|
authorRow: { flexDirection: 'row', alignItems: 'center', gap: spacing.sm },
|
|
authorName: { flex: 1 },
|
|
labels: { flexDirection: 'row', flexWrap: 'wrap', gap: spacing.sm },
|
|
bodyCard: { minHeight: 80 },
|
|
metaGrid: { gap: spacing.sm },
|
|
metaRow: { flexDirection: 'row', alignItems: 'center', gap: spacing.sm },
|
|
reactions: { flexDirection: 'row', flexWrap: 'wrap', gap: spacing.sm },
|
|
reactionChip: {
|
|
flexDirection: 'row',
|
|
alignItems: 'center',
|
|
gap: spacing.xs,
|
|
borderWidth: 1,
|
|
borderRadius: 999,
|
|
paddingHorizontal: spacing.sm,
|
|
paddingVertical: 2,
|
|
minHeight: 32,
|
|
},
|
|
sectionTitle: { marginTop: spacing.md },
|
|
});
|