import { useState } from 'react'; import { Alert, StyleSheet, 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 { ChipSelect } from '@/components/ui/chip-select'; import { Field } from '@/components/ui/field'; import { IconButton } from '@/components/ui/icon-button'; import { ErrorState, LoadingView } from '@/components/ui/state-views'; import { apiErrorMessage } from '@/core/api/error-text'; import { useCreateRepoVariable, useDeleteRepoSecret, useDeleteRepoVariable, usePutRepoSecret, useRepoSecrets, useRepoVariables, } from '@/features/repositories/settings-queries'; import { useDispatchWorkflow, useWorkflows } from '@/features/actions/queries'; import { spacing } from '@/theme/tokens'; import type { RepositoryStackParamList } from '@/navigation/types'; import type { ScreenProps } from '@/navigation/typing'; type Props = ScreenProps; /** Repository Actions secrets and variables (secrets are write-only). */ export function ActionsSettingsScreen({ route }: Props) { const { t } = useTranslation(); const { owner, name } = route.params; const secrets = useRepoSecrets(owner, name); const variables = useRepoVariables(owner, name); const putSecret = usePutRepoSecret(owner, name); const deleteSecret = useDeleteRepoSecret(owner, name); const createVariable = useCreateRepoVariable(owner, name); const deleteVariable = useDeleteRepoVariable(owner, name); const [secretName, setSecretName] = useState(''); const [secretValue, setSecretValue] = useState(''); const [varName, setVarName] = useState(''); const [varValue, setVarValue] = useState(''); const workflows = useWorkflows(owner, name); const [selectedWorkflow, setSelectedWorkflow] = useState(null); const [dispatchRefName, setDispatchRefName] = useState(''); const dispatch = useDispatchWorkflow(owner, name, selectedWorkflow ?? ''); if (secrets.isPending || variables.isPending) return ; if (secrets.isError || variables.isError) { const error = secrets.error ?? variables.error; return ( { void secrets.refetch(); void variables.refetch(); }} /> ); } const secretItems = secrets.data ?? []; const variableItems = variables.data ?? []; const addSecret = () => { if (secretName.trim().length === 0 || secretValue.length === 0) return; putSecret.mutate( { key: secretName.trim(), value: secretValue }, { onSuccess: () => { setSecretName(''); setSecretValue(''); }, onError: (error) => Alert.alert(t('errors.title'), apiErrorMessage(error, t)), }, ); }; const addVariable = () => { if (varName.trim().length === 0) return; createVariable.mutate( { key: varName.trim(), value: varValue }, { onSuccess: () => { setVarName(''); setVarValue(''); }, onError: (error) => Alert.alert(t('errors.title'), apiErrorMessage(error, t)), }, ); }; const confirmSecretDelete = (key: string) => { Alert.alert(t('repoSettings.deleteSecretTitle'), t('repoSettings.deleteSecretBody', { name: key }), [ { text: t('common.cancel'), style: 'cancel' }, { text: t('common.delete'), style: 'destructive', onPress: () => deleteSecret.mutate(key, { onError: (error) => Alert.alert(t('errors.title'), apiErrorMessage(error, t)), }), }, ]); }; const confirmVariableDelete = (key: string) => { Alert.alert(t('repoSettings.deleteVariableTitle'), t('repoSettings.deleteVariableBody', { name: key }), [ { text: t('common.cancel'), style: 'cancel' }, { text: t('common.delete'), style: 'destructive', onPress: () => deleteVariable.mutate(key, { onError: (error) => Alert.alert(t('errors.title'), apiErrorMessage(error, t)), }), }, ]); }; return ( {t('repoSettings.runWorkflow')} ({ id: String(workflow.id), label: workflow.name ?? workflow.path ?? '', }))} selectedIds={selectedWorkflow ? [selectedWorkflow] : []} onToggle={(id) => setSelectedWorkflow(id)} emptyLabel={t('repoSettings.noWorkflows')} single />