import { useMemo } from 'react'; import { StyleSheet, View } from 'react-native'; import { FlashList } from '@shopify/flash-list'; import { useTranslation } from 'react-i18next'; import { Breadcrumb, FileRow } from '@/components/git/file-row'; import { Screen } from '@/components/layout/screen'; import { AppText } from '@/components/ui/app-text'; import { ErrorState, EmptyState, LoadingView } from '@/components/ui/state-views'; import { apiErrorMessage } from '@/core/api/error-text'; import type { ContentsEntry } from '@/core/api/types'; import { useContents } from '@/features/repository/queries'; import { useRepository } from '@/features/repositories/queries'; 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; /** Directory listing with breadcrumb navigation. */ export function FilesScreen({ navigation, route }: Props) { const { t } = useTranslation(); const { colors } = useTheme(); const { owner, name, ref } = route.params; const path = route.params.path ?? ''; const repoQuery = useRepository(owner, name); const effectiveRef = ref ?? repoQuery.data?.default_branch; const query = useContents(owner, name, path, effectiveRef); const entries = useMemo(() => { const list = Array.isArray(query.data) ? (query.data as ContentsEntry[]) : []; return [...list].sort((a, b) => { if (a.type === 'dir' && b.type !== 'dir') return -1; if (a.type !== 'dir' && b.type === 'dir') return 1; return a.name.localeCompare(b.name); }); }, [query.data]); const header = ( navigation.setParams({ path: target || undefined })} /> {effectiveRef ? ( {t('files.currentRef', { ref: effectiveRef })} ) : null} ); const body = () => { if (query.isPending) return ; if (query.isError) { return ( void query.refetch()} /> ); } return ( entry.path} renderItem={({ item }) => ( item.type === 'dir' ? navigation.setParams({ path: item.path }) : navigation.navigate('FileViewer', { owner, name, path: item.path, ref: effectiveRef }) } /> )} refreshing={query.isRefetching} onRefresh={() => void query.refetch()} ListEmptyComponent={} contentContainerStyle={entries.length === 0 ? styles.grow : undefined} /> ); }; return ( {header} {body()} ); } const styles = StyleSheet.create({ header: { gap: spacing.xs, paddingBottom: spacing.xs }, ref: { paddingHorizontal: spacing.lg }, refBar: { borderBottomWidth: StyleSheet.hairlineWidth }, grow: { flexGrow: 1 }, });