Files
gitea-native/src/features/repository/screens/repo-overview-screen.tsx
T
JustZoe101 587bf732b5 feat: release asset upload, transfer, Actions secrets/variables/artifacts, admin, teams/projects, file search, reactions, attachments, package version delete
- client: multipart postForm; expo-document-picker + react-native-webview deps
- release assets: upload (DocumentPicker) and delete; run artifacts list
- repository transfer; Actions secrets + variables management screen
- admin (users/orgs/repos) screen gated on is_admin
- org teams, repo projects (read-only); find-file via recursive git tree
- issue reactions toggle and attachments list; package version delete
- endpoint tests (126 total)
2026-10-09 18:40:08 +02:00

435 lines
15 KiB
TypeScript

import { useMemo } from 'react';
import { Alert, Linking, StyleSheet, View } from 'react-native';
import { useTranslation } from 'react-i18next';
import { Markdown } from '@/components/git/markdown';
import { Screen } from '@/components/layout/screen';
import { AppText } from '@/components/ui/app-text';
import { Badge } 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 { parseGiteaVersion, supports } from '@/core/capabilities';
import { buildEditorUrl } from '@/core/utils/editor-url';
import {
useForkRepository,
useIsStarred,
useRepository,
useRepositoryLanguages,
useSubscription,
useToggleStar,
useToggleWatch,
} from '@/features/repositories/queries';
import { decodeBase64, useReadme } from '@/features/repository/queries';
import { useSession } from '@/features/accounts/use-session';
import { useSettingsStore } from '@/features/settings/settings-store';
import { useTheme } from '@/theme/theme-provider';
import { radius, spacing } from '@/theme/tokens';
import type { RepositoryStackParamList } from '@/navigation/types';
import type { ScreenProps } from '@/navigation/typing';
type Props = ScreenProps<RepositoryStackParamList, 'RepoOverview'>;
/** Repository landing page: metadata, quick navigation, stats, languages, README. */
export function RepoOverviewScreen({ navigation, route }: Props) {
const { t } = useTranslation();
const { colors } = useTheme();
const { instance } = useSession();
const editorUrlTemplate = useSettingsStore((s) => s.editorUrlTemplate);
const { owner, name, ref } = route.params;
const repoQuery = useRepository(owner, name);
const repo = repoQuery.data;
const readmeRef = ref ?? repo?.default_branch;
const readmeQuery = useReadme(owner, name, readmeRef);
const languagesQuery = useRepositoryLanguages(owner, name);
const isStarred = useIsStarred(owner, name);
const toggleStar = useToggleStar(owner, name);
const subscription = useSubscription(owner, name);
const toggleWatch = useToggleWatch(owner, name);
const forkRepo = useForkRepository(owner, name);
const version = parseGiteaVersion(instance?.version);
const repoUrl = instance ? `${instance.baseUrl}/${owner}/${name}` : null;
const confirmFork = () => {
Alert.alert(t('repo.forkTitle'), t('repo.forkBody', { name }), [
{ text: t('common.cancel'), style: 'cancel' },
{
text: t('repo.fork'),
onPress: () =>
forkRepo.mutate(
{},
{
onSuccess: (forked) =>
Alert.alert(
t('repo.forkedTitle'),
t('repo.forkedBody', { name: forked.full_name ?? name }),
),
onError: (error) => Alert.alert(t('errors.title'), apiErrorMessage(error, t)),
},
),
},
]);
};
const languages = useMemo(() => {
const entries = Object.entries(languagesQuery.data ?? {});
const total = entries.reduce((sum, [, bytes]) => sum + bytes, 0);
if (total <= 0) return [];
return entries
.map(([language, bytes]) => ({ language, bytes, percent: (bytes / total) * 100 }))
.sort((a, b) => b.bytes - a.bytes)
.slice(0, 6);
}, [languagesQuery.data]);
if (!repo && repoQuery.isPending) return <LoadingView />;
if (!repo && repoQuery.isError) {
return (
<ErrorState
title={t('errors.title')}
body={apiErrorMessage(repoQuery.error, t)}
retryLabel={t('common.retry')}
onRetry={() => void repoQuery.refetch()}
/>
);
}
if (!repo) return null;
const readmeText = decodeReadme(readmeQuery.data?.content);
return (
<Screen scroll padded={false}>
{/* Header */}
<View style={[styles.header, { borderBottomColor: colors.border }]}>
<View style={styles.titleRow}>
<AppText variant="caption" tone="tertiary">
{owner}/
</AppText>
<AppText variant="title" numberOfLines={2} style={styles.name}>
{name}
</AppText>
</View>
<AppText variant="secondary" tone={repo.description ? 'secondary' : 'tertiary'}>
{repo.description ?? t('repo.noDescription')}
</AppText>
<View style={styles.badges}>
{repo.private ? <Badge tone="neutral" icon="lock">{t('repositories.private')}</Badge> : null}
{repo.fork ? <Badge tone="neutral" icon="fork">{t('repositories.fork')}</Badge> : null}
{repo.archived ? <Badge tone="warning" icon="shield">{t('repositories.archived')}</Badge> : null}
{repo.template ? <Badge tone="info" icon="package">{t('repositories.template')}</Badge> : null}
{repo.mirror ? <Badge tone="info" icon="autoRenew">{t('repositories.mirror')}</Badge> : null}
{repo.default_branch ? (
<Badge tone="primary" icon="branch">{repo.default_branch}</Badge>
) : null}
</View>
{repo.topics && repo.topics.length > 0 ? (
<View style={styles.badges}>
{repo.topics.slice(0, 8).map((topic) => (
<LabelChip key={topic} name={topic} />
))}
</View>
) : null}
{repo.html_url ? (
<Button
label={t('repo.viewOnInstance')}
variant="secondary"
iconLeft="externalLink"
onPress={() => void Linking.openURL(repo.html_url as string).catch(() => undefined)}
/>
) : null}
{editorUrlTemplate ? (
<Button
label={t('repo.openInEditor')}
variant="secondary"
iconLeft="externalLink"
onPress={() => {
const url = buildEditorUrl(editorUrlTemplate, {
owner,
name,
cloneUrl: repo.clone_url,
htmlUrl: repo.html_url,
});
void Linking.openURL(url).catch(() => undefined);
}}
/>
) : null}
<View style={styles.actionRow}>
<Button
label={isStarred.data ? t('repo.unstar') : t('repo.star')}
variant="secondary"
size="md"
iconLeft={isStarred.data ? 'star' : 'starOutline'}
loading={toggleStar.isPending}
onPress={() => toggleStar.mutate(isStarred.data ?? false)}
/>
<Button
label={subscription.data?.subscribed ? t('repo.unwatch') : t('repo.watch')}
variant="secondary"
size="md"
iconLeft="bell"
loading={toggleWatch.isPending}
onPress={() => toggleWatch.mutate(subscription.data?.subscribed ?? false)}
/>
<Button
label={t('repo.fork')}
variant="secondary"
size="md"
iconLeft="fork"
loading={forkRepo.isPending}
onPress={confirmFork}
/>
</View>
</View>
{/* Quick navigation */}
<View style={styles.section}>
<AppText variant="subheading" style={styles.sectionTitle}>
{t('repo.browse')}
</AppText>
<Card padded={false} variant="flat" style={styles.card}>
<ListItem
leadingIcon="folder"
title={t('repo.files')}
onPress={() => navigation.navigate('Files', { owner, name, ref: readmeRef })}
/>
<ListItem
leadingIcon="search"
title={t('files.findFile')}
onPress={() => navigation.navigate('FindFile', { owner, name, ref: readmeRef })}
/>
<ListItem
leadingIcon="commit"
title={t('repo.commits')}
onPress={() => navigation.navigate('Commits', { owner, name, sha: readmeRef })}
/>
<ListItem
leadingIcon="branch"
title={t('repo.branches')}
onPress={() => navigation.navigate('Branches', { owner, name })}
/>
<ListItem
leadingIcon="tag"
title={t('repo.tags')}
onPress={() => navigation.navigate('Tags', { owner, name })}
/>
{repo.has_releases !== false ? (
<ListItem
leadingIcon="download"
title={t('releases.title')}
onPress={() => navigation.navigate('Releases', { owner, name })}
/>
) : null}
{repo.has_wiki && supports('wiki', version) && repoUrl ? (
<ListItem
leadingIcon="globe"
title={t('repo.wiki')}
onPress={() => void Linking.openURL(`${repoUrl}/wiki`).catch(() => undefined)}
/>
) : null}
{repo.has_actions !== false ? (
<ListItem
leadingIcon="autoRenew"
title={t('actions.title')}
onPress={() => navigation.navigate('Actions', { owner, name })}
/>
) : null}
{repo.has_projects !== false ? (
<ListItem
leadingIcon="dashboard"
title={t('projects.title')}
onPress={() => navigation.navigate('Projects', { owner, name })}
/>
) : null}
{repo.has_issues !== false ? (
<ListItem
leadingIcon="issue"
title={t('repo.issuesTab')}
rightText={repo.open_issues_count ? String(repo.open_issues_count) : undefined}
onPress={() => navigation.navigate('RepoIssues', { owner, name })}
/>
) : null}
{repo.has_pull_requests !== false ? (
<ListItem
leadingIcon="pullRequest"
title={t('repo.pullsTab')}
rightText={repo.open_pr_counter ? String(repo.open_pr_counter) : undefined}
onPress={() => navigation.navigate('RepoPulls', { owner, name })}
divider={false}
/>
) : null}
</Card>
</View>
{/* Manage */}
{repo.permissions?.push || repo.permissions?.admin || repo.permissions?.pull ? (
<View style={styles.section}>
<AppText variant="subheading" style={styles.sectionTitle}>
{t('repo.manage')}
</AppText>
<Card padded={false} variant="flat" style={styles.card}>
<ListItem
leadingIcon="gear"
title={t('repoSettings.title')}
onPress={() => navigation.navigate('RepoSettings', { owner, name })}
divider={false}
/>
</Card>
</View>
) : null}
{/* Statistics */}
<View style={styles.section}>
<AppText variant="subheading" style={styles.sectionTitle}>
{t('repo.statistics')}
</AppText>
<Card style={styles.card}>
<View style={styles.statsGrid}>
<Stat label={t('repo.stars')} value={repo.stars_count} icon="star" />
<Stat label={t('repo.forks')} value={repo.forks_count} icon="fork" />
<Stat label={t('repo.watchers')} value={repo.watchers_count} icon="eye" />
<Stat label={t('repo.openIssues')} value={repo.open_issues_count} icon="issue" />
<Stat label={t('repo.openPulls')} value={repo.open_pr_counter} icon="pullRequest" />
<Stat label={t('repo.releases')} value={repo.release_counter} icon="tag" />
</View>
</Card>
</View>
{/* Languages */}
{languages.length > 0 ? (
<View style={styles.section}>
<AppText variant="subheading" style={styles.sectionTitle}>
{t('repo.languages')}
</AppText>
<Card style={styles.card}>
<View style={[styles.langBar, { backgroundColor: colors.surfaceSecondary }]}>
{languages.map((entry, index) => (
<View
key={entry.language}
style={{
flex: entry.percent,
backgroundColor: languageColor(index, colors.primary),
}}
/>
))}
</View>
<View style={styles.langList}>
{languages.map((entry, index) => (
<View key={entry.language} style={styles.langItem}>
<View
style={[styles.langDot, { backgroundColor: languageColor(index, colors.primary) }]}
/>
<AppText variant="caption" style={styles.langName} numberOfLines={1}>
{entry.language}
</AppText>
<AppText variant="caption" tone="tertiary">
{entry.percent.toFixed(1)}%
</AppText>
</View>
))}
</View>
</Card>
</View>
) : null}
{/* README */}
<View style={styles.section}>
<AppText variant="subheading" style={styles.sectionTitle}>
{t('repo.readme')}
</AppText>
{readmeQuery.isPending ? (
<View style={styles.card}>
<LoadingView />
</View>
) : readmeText ? (
<Card style={styles.card}>
<Markdown content={readmeText} baseUrl={instance?.baseUrl} />
</Card>
) : (
<Card style={styles.card}>
<AppText variant="secondary" tone="tertiary">
{t('repo.noReadme')}
</AppText>
</Card>
)}
</View>
<View style={{ height: spacing.xxl }} />
</Screen>
);
}
function Stat({
label,
value,
icon,
}: {
label: string;
value: number | undefined;
icon: 'star' | 'fork' | 'eye' | 'issue' | 'pullRequest' | 'tag';
}) {
const { colors } = useTheme();
return (
<View style={styles.stat}>
<Icon name={icon} size={16} color={colors.textTertiary} />
<AppText variant="bodyMedium">{value ?? 0}</AppText>
<AppText variant="caption" tone="tertiary" numberOfLines={1}>
{label}
</AppText>
</View>
);
}
const LANGUAGE_COLORS = ['#609926', '#3178c6', '#e34c26', '#f1e05a', '#563d7c', '#89e051'];
function languageColor(index: number, fallback: string): string {
return LANGUAGE_COLORS[index] ?? fallback;
}
function decodeReadme(content: string | undefined): string {
if (!content) return '';
return decodeBase64(content);
}
const styles = StyleSheet.create({
header: {
paddingHorizontal: spacing.lg,
paddingTop: spacing.lg,
paddingBottom: spacing.lg,
gap: spacing.md,
borderBottomWidth: StyleSheet.hairlineWidth,
},
titleRow: { flexDirection: 'row', alignItems: 'baseline', flexWrap: 'wrap' },
name: { flexShrink: 1 },
badges: { flexDirection: 'row', flexWrap: 'wrap', gap: spacing.sm, alignItems: 'center' },
actionRow: { flexDirection: 'row', flexWrap: 'wrap', gap: spacing.sm },
section: { marginTop: spacing.xl, gap: spacing.sm },
sectionTitle: { paddingHorizontal: spacing.lg },
card: { marginHorizontal: spacing.lg },
statsGrid: { flexDirection: 'row', flexWrap: 'wrap' },
stat: { width: '33.33%', alignItems: 'center', gap: 2, paddingVertical: spacing.sm },
langBar: {
flexDirection: 'row',
height: 10,
borderRadius: radius.pill,
overflow: 'hidden',
marginBottom: spacing.md,
},
langList: { gap: spacing.xs },
langItem: { flexDirection: 'row', alignItems: 'center', gap: spacing.sm },
langDot: { width: 10, height: 10, borderRadius: radius.pill },
langName: { flex: 1 },
});