import { useState } from 'react'; import { Alert, StyleSheet, Switch, View } from 'react-native'; import { useTranslation } from 'react-i18next'; import { Screen } from '@/components/layout/screen'; import { AppText } from '@/components/ui/app-text'; import { Button } from '@/components/ui/button'; import { Card } from '@/components/ui/card'; import { Field } from '@/components/ui/field'; import { ListItem } from '@/components/ui/list-item'; import { ErrorState, LoadingView } from '@/components/ui/state-views'; import { apiErrorMessage } from '@/core/api/error-text'; import { useRepository } from '@/features/repositories/queries'; import { useDeleteRepo, useTransferRepo, useUpdateRepo, } from '@/features/repositories/settings-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; type FeatureKey = | 'has_issues' | 'has_pull_requests' | 'has_wiki' | 'has_releases' | 'has_projects' | 'has_packages' | 'has_actions'; /** Repository settings: metadata, feature toggles, danger zone and sub-sections. */ export function RepoSettingsScreen({ navigation, route }: Props) { const { t } = useTranslation(); const { colors } = useTheme(); const { owner, name } = route.params; const repoQuery = useRepository(owner, name); const repo = repoQuery.data; const updateRepo = useUpdateRepo(owner, name); const deleteRepo = useDeleteRepo(owner, name); const transferRepo = useTransferRepo(owner, name); const [newOwner, setNewOwner] = useState(''); const [description, setDescription] = useState(null); const [website, setWebsite] = useState(null); const [isPrivate, setIsPrivate] = useState(null); const [features, setFeatures] = useState>>({}); if (repoQuery.isPending) return ; if (repoQuery.isError) { return ( void repoQuery.refetch()} /> ); } if (!repo) return null; const descriptionValue = description ?? repo.description ?? ''; const websiteValue = website ?? repo.website ?? ''; const privateValue = isPrivate ?? repo.private ?? false; const featureValue = (key: FeatureKey) => features[key] ?? repo[key] ?? true; const save = () => { updateRepo.mutate( { description: descriptionValue, website: websiteValue, private: privateValue, has_issues: featureValue('has_issues'), has_pull_requests: featureValue('has_pull_requests'), has_wiki: featureValue('has_wiki'), has_releases: featureValue('has_releases'), has_projects: featureValue('has_projects'), has_packages: featureValue('has_packages'), has_actions: featureValue('has_actions'), }, { onSuccess: () => { Alert.alert(t('repoSettings.savedTitle'), t('repoSettings.savedBody')); }, onError: (error) => Alert.alert(t('errors.title'), apiErrorMessage(error, t)), }, ); }; const confirmDelete = () => { Alert.alert(t('repoSettings.deleteTitle'), t('repoSettings.deleteBody', { name }), [ { text: t('common.cancel'), style: 'cancel' }, { text: t('common.delete'), style: 'destructive', onPress: () => deleteRepo.mutate(undefined, { onSuccess: () => navigation.getParent()?.goBack(), onError: (error) => Alert.alert(t('errors.title'), apiErrorMessage(error, t)), }), }, ]); }; const featureRows: { key: FeatureKey; label: string }[] = [ { key: 'has_issues', label: t('repo.issuesTab') }, { key: 'has_pull_requests', label: t('repo.pullsTab') }, { key: 'has_wiki', label: t('repo.wiki') }, { key: 'has_releases', label: t('releases.title') }, { key: 'has_projects', label: t('repoSettings.projects') }, { key: 'has_packages', label: t('packages.title') }, { key: 'has_actions', label: t('actions.title') }, ]; return ( {t('repositories.private')} {t('repoSettings.features')} {featureRows.map((row, index) => ( 0 && styles.spaced]}> {row.label} setFeatures((prev) => ({ ...prev, [row.key]: value }))} trackColor={{ true: colors.primary, false: colors.border }} /> ))}