- API: Actions (workflows/runs/jobs/logs getText), repo create/edit/delete, collaborators, webhooks, branch protections, orgs, packages, user keys/GPG keys/tokens, release create/edit/delete - hooks: features/actions, repositories/settings-queries, features/profile - screens: Actions + run detail (expandable job logs), RepoSettings, Collaborators, Webhooks, Protections, CreateRepo, Organizations, OrgRepos, Packages, Credentials, ReleaseCompose; releases list/detail management - navigation + i18n (en/nl) wired for all new screens
189 lines
6.3 KiB
TypeScript
189 lines
6.3 KiB
TypeScript
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 { Badge } from '@/components/ui/badge';
|
|
import { Button } from '@/components/ui/button';
|
|
import { Card } from '@/components/ui/card';
|
|
import { Field } from '@/components/ui/field';
|
|
import { IconButton } from '@/components/ui/icon-button';
|
|
import { EmptyState, ErrorState, LoadingView } from '@/components/ui/state-views';
|
|
import { apiErrorMessage } from '@/core/api/error-text';
|
|
import {
|
|
useBranchProtections,
|
|
useCreateBranchProtection,
|
|
useDeleteBranchProtection,
|
|
} 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<RepositoryStackParamList, 'Protections'>;
|
|
|
|
/** Branch protection rules: list, create a basic rule, delete. */
|
|
export function ProtectionsScreen({ route }: Props) {
|
|
const { t } = useTranslation();
|
|
const { colors } = useTheme();
|
|
const { owner, name } = route.params;
|
|
|
|
const query = useBranchProtections(owner, name);
|
|
const create = useCreateBranchProtection(owner, name);
|
|
const remove = useDeleteBranchProtection(owner, name);
|
|
|
|
const [branchName, setBranchName] = useState('');
|
|
const [requiredApprovals, setRequiredApprovals] = useState('1');
|
|
const [signedCommits, setSignedCommits] = useState(false);
|
|
const [statusCheck, setStatusCheck] = useState(false);
|
|
|
|
if (query.isPending) return <LoadingView />;
|
|
if (query.isError) {
|
|
return (
|
|
<ErrorState
|
|
title={t('errors.title')}
|
|
body={apiErrorMessage(query.error, t)}
|
|
retryLabel={t('common.retry')}
|
|
onRetry={() => void query.refetch()}
|
|
/>
|
|
);
|
|
}
|
|
|
|
const rules = query.data ?? [];
|
|
|
|
const submit = () => {
|
|
const pattern = branchName.trim();
|
|
if (pattern.length === 0) return;
|
|
create.mutate(
|
|
{
|
|
branch_name: pattern,
|
|
required_approvals: Number(requiredApprovals) || 0,
|
|
enable_push: false,
|
|
require_signed_commits: signedCommits,
|
|
enable_status_check: statusCheck,
|
|
},
|
|
{
|
|
onSuccess: () => setBranchName(''),
|
|
onError: (error) => Alert.alert(t('errors.title'), apiErrorMessage(error, t)),
|
|
},
|
|
);
|
|
};
|
|
|
|
const confirmDelete = (rule: string) => {
|
|
Alert.alert(t('repoSettings.deleteRuleTitle'), t('repoSettings.deleteRuleBody', { name: rule }), [
|
|
{ text: t('common.cancel'), style: 'cancel' },
|
|
{
|
|
text: t('common.delete'),
|
|
style: 'destructive',
|
|
onPress: () =>
|
|
remove.mutate(rule, {
|
|
onError: (error) => Alert.alert(t('errors.title'), apiErrorMessage(error, t)),
|
|
}),
|
|
},
|
|
]);
|
|
};
|
|
|
|
return (
|
|
<Screen scroll padded>
|
|
<Card>
|
|
<Field
|
|
label={t('repoSettings.branchName')}
|
|
value={branchName}
|
|
onChangeText={setBranchName}
|
|
placeholder="main"
|
|
autoCapitalize="none"
|
|
autoCorrect={false}
|
|
mono
|
|
/>
|
|
<Field
|
|
label={t('repoSettings.requiredApprovals')}
|
|
value={requiredApprovals}
|
|
onChangeText={setRequiredApprovals}
|
|
keyboardType="number-pad"
|
|
/>
|
|
<View style={styles.switchRow}>
|
|
<AppText variant="bodyMedium" style={styles.switchLabel}>
|
|
{t('repoSettings.requireSignedCommits')}
|
|
</AppText>
|
|
<Switch
|
|
value={signedCommits}
|
|
onValueChange={setSignedCommits}
|
|
trackColor={{ true: colors.primary, false: colors.border }}
|
|
/>
|
|
</View>
|
|
<View style={styles.switchRow}>
|
|
<AppText variant="bodyMedium" style={styles.switchLabel}>
|
|
{t('repoSettings.requireStatusChecks')}
|
|
</AppText>
|
|
<Switch
|
|
value={statusCheck}
|
|
onValueChange={setStatusCheck}
|
|
trackColor={{ true: colors.primary, false: colors.border }}
|
|
/>
|
|
</View>
|
|
<Button
|
|
label={t('common.add')}
|
|
onPress={submit}
|
|
loading={create.isPending}
|
|
disabled={branchName.trim().length === 0}
|
|
fullWidth
|
|
style={styles.spaced}
|
|
/>
|
|
</Card>
|
|
|
|
{rules.length === 0 ? (
|
|
<EmptyState icon="shield" title={t('repoSettings.noProtections')} />
|
|
) : (
|
|
rules.map((rule) => {
|
|
const label = rule.rule_name || rule.branch_name || '';
|
|
return (
|
|
<Card key={rule.rule_name ?? rule.branch_name}>
|
|
<View style={styles.row}>
|
|
<View style={styles.body}>
|
|
<AppText variant="bodyMedium" numberOfLines={1}>
|
|
{label}
|
|
</AppText>
|
|
<View style={styles.metaRow}>
|
|
{rule.enable_push === false ? (
|
|
<Badge tone="warning">{t('repoSettings.pushRestricted')}</Badge>
|
|
) : null}
|
|
{rule.required_approvals ? (
|
|
<Badge tone="info">
|
|
{t('repoSettings.approvalsBadge', { count: rule.required_approvals })}
|
|
</Badge>
|
|
) : null}
|
|
{rule.require_signed_commits ? (
|
|
<Badge tone="success">{t('repoSettings.signedBadge')}</Badge>
|
|
) : null}
|
|
</View>
|
|
</View>
|
|
<IconButton
|
|
name="trash"
|
|
onPress={() => confirmDelete(label)}
|
|
accessibilityLabel={t('common.delete')}
|
|
/>
|
|
</View>
|
|
</Card>
|
|
);
|
|
})
|
|
)}
|
|
</Screen>
|
|
);
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
switchRow: {
|
|
flexDirection: 'row',
|
|
alignItems: 'center',
|
|
justifyContent: 'space-between',
|
|
gap: spacing.md,
|
|
marginTop: spacing.md,
|
|
},
|
|
switchLabel: { flex: 1 },
|
|
spaced: { marginTop: spacing.lg },
|
|
row: { flexDirection: 'row', alignItems: 'center', gap: spacing.md },
|
|
body: { flex: 1, gap: spacing.xs },
|
|
metaRow: { flexDirection: 'row', alignItems: 'center', gap: spacing.sm, flexWrap: 'wrap' },
|
|
});
|