- subscribe/unsubscribe to an issue from the detail screen - dispatch a workflow_dispatch run (workflow + ref) from Actions settings - i18n (en/nl)
257 lines
8.3 KiB
TypeScript
257 lines
8.3 KiB
TypeScript
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<RepositoryStackParamList, 'ActionsSettings'>;
|
|
|
|
/** 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<string | null>(null);
|
|
const [dispatchRefName, setDispatchRefName] = useState('');
|
|
const dispatch = useDispatchWorkflow(owner, name, selectedWorkflow ?? '');
|
|
|
|
if (secrets.isPending || variables.isPending) return <LoadingView />;
|
|
if (secrets.isError || variables.isError) {
|
|
const error = secrets.error ?? variables.error;
|
|
return (
|
|
<ErrorState
|
|
title={t('errors.title')}
|
|
body={apiErrorMessage(error, t)}
|
|
retryLabel={t('common.retry')}
|
|
onRetry={() => {
|
|
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 (
|
|
<Screen scroll padded>
|
|
<AppText variant="subheading">{t('repoSettings.runWorkflow')}</AppText>
|
|
<Card>
|
|
<ChipSelect
|
|
options={(workflows.data ?? []).map((workflow) => ({
|
|
id: String(workflow.id),
|
|
label: workflow.name ?? workflow.path ?? '',
|
|
}))}
|
|
selectedIds={selectedWorkflow ? [selectedWorkflow] : []}
|
|
onToggle={(id) => setSelectedWorkflow(id)}
|
|
emptyLabel={t('repoSettings.noWorkflows')}
|
|
single
|
|
/>
|
|
<Field
|
|
label={t('repoSettings.ref')}
|
|
value={dispatchRefName}
|
|
onChangeText={setDispatchRefName}
|
|
placeholder="main"
|
|
autoCapitalize="none"
|
|
autoCorrect={false}
|
|
/>
|
|
<Button
|
|
label={t('repoSettings.dispatch')}
|
|
onPress={() =>
|
|
dispatch.mutate(
|
|
{ ref: dispatchRefName.trim() },
|
|
{ onError: (error) => Alert.alert(t('errors.title'), apiErrorMessage(error, t)) },
|
|
)
|
|
}
|
|
loading={dispatch.isPending}
|
|
disabled={!selectedWorkflow || dispatchRefName.trim().length === 0}
|
|
fullWidth
|
|
style={styles.spaced}
|
|
/>
|
|
</Card>
|
|
|
|
<AppText variant="subheading">{t('repoSettings.secrets')}</AppText>
|
|
<Card>
|
|
<Field
|
|
label={t('repoSettings.secretName')}
|
|
value={secretName}
|
|
onChangeText={setSecretName}
|
|
autoCapitalize="none"
|
|
autoCorrect={false}
|
|
/>
|
|
<Field
|
|
label={t('repoSettings.secretValue')}
|
|
value={secretValue}
|
|
onChangeText={setSecretValue}
|
|
secureTextEntry
|
|
autoCapitalize="none"
|
|
/>
|
|
<Button
|
|
label={t('common.add')}
|
|
onPress={addSecret}
|
|
loading={putSecret.isPending}
|
|
disabled={secretName.trim().length === 0 || secretValue.length === 0}
|
|
fullWidth
|
|
style={styles.spaced}
|
|
/>
|
|
</Card>
|
|
{secretItems.map((secret) => (
|
|
<Card key={secret.name}>
|
|
<View style={styles.row}>
|
|
<AppText variant="bodyMedium" style={styles.name} numberOfLines={1}>
|
|
{secret.name}
|
|
</AppText>
|
|
<IconButton
|
|
name="trash"
|
|
onPress={() => confirmSecretDelete(secret.name ?? '')}
|
|
accessibilityLabel={t('common.delete')}
|
|
/>
|
|
</View>
|
|
</Card>
|
|
))}
|
|
|
|
<AppText variant="subheading">{t('repoSettings.variables')}</AppText>
|
|
<Card>
|
|
<Field
|
|
label={t('repoSettings.variableName')}
|
|
value={varName}
|
|
onChangeText={setVarName}
|
|
autoCapitalize="none"
|
|
autoCorrect={false}
|
|
/>
|
|
<Field
|
|
label={t('repoSettings.variableValue')}
|
|
value={varValue}
|
|
onChangeText={setVarValue}
|
|
autoCapitalize="none"
|
|
autoCorrect={false}
|
|
/>
|
|
<Button
|
|
label={t('common.add')}
|
|
onPress={addVariable}
|
|
loading={createVariable.isPending}
|
|
disabled={varName.trim().length === 0}
|
|
fullWidth
|
|
style={styles.spaced}
|
|
/>
|
|
</Card>
|
|
{variableItems.map((variable) => (
|
|
<Card key={variable.name}>
|
|
<View style={styles.row}>
|
|
<View style={styles.name}>
|
|
<AppText variant="bodyMedium" numberOfLines={1}>
|
|
{variable.name}
|
|
</AppText>
|
|
{variable.data ? (
|
|
<AppText variant="monoSmall" tone="tertiary" numberOfLines={1}>
|
|
{variable.data}
|
|
</AppText>
|
|
) : null}
|
|
</View>
|
|
<IconButton
|
|
name="trash"
|
|
onPress={() => confirmVariableDelete(variable.name ?? '')}
|
|
accessibilityLabel={t('common.delete')}
|
|
/>
|
|
</View>
|
|
</Card>
|
|
))}
|
|
</Screen>
|
|
);
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
spaced: { marginTop: spacing.md },
|
|
row: { flexDirection: 'row', alignItems: 'center', gap: spacing.md },
|
|
name: { flex: 1 },
|
|
});
|