- add scenario upload and download UI with typed file API helpers - show run artifacts on run detail pages after polling completes - handle multipart uploads and absolute file paths for downloads
164 lines
5.2 KiB
TypeScript
164 lines
5.2 KiB
TypeScript
import { useEffect, useState, SubmitEvent } from 'react';
|
|
import { useNavigate, useParams } from 'react-router-dom';
|
|
import { useTranslation } from 'react-i18next';
|
|
import { usePageTitle } from '../../hooks/usePageTitle';
|
|
import { X, Globe, Save } from 'lucide-react';
|
|
import { environments } from '../../api';
|
|
import type { Environment } from '../../api';
|
|
import { Breadcrumbs, Button, Card, Input, CodeEditor, useToast } from '../../ui';
|
|
import styles from '../Page.module.css';
|
|
|
|
export function EditEnvironmentPage() {
|
|
const { t } = useTranslation();
|
|
const { id } = useParams<{ id: string }>();
|
|
const navigate = useNavigate();
|
|
const toast = useToast();
|
|
|
|
const [env, setEnv] = useState<Environment | null>(null);
|
|
const [name, setName] = useState('');
|
|
usePageTitle(
|
|
env ? `${t('environments.edit_title')} — ${env.name}` : t('environments.edit_title'),
|
|
);
|
|
const [description, setDescription] = useState('');
|
|
const [dataJson, setDataJson] = useState('{}');
|
|
const [nameError, setNameError] = useState('');
|
|
const [dataError, setDataError] = useState('');
|
|
const [loading, setLoading] = useState(true);
|
|
const [saving, setSaving] = useState(false);
|
|
|
|
useEffect(() => {
|
|
if (!id) return;
|
|
environments
|
|
.get(id)
|
|
.then((data) => {
|
|
setEnv(data);
|
|
setName(data.name);
|
|
setDescription(data.description || '');
|
|
setDataJson(JSON.stringify(data.data ?? {}, null, 2));
|
|
})
|
|
.finally(() => setLoading(false));
|
|
}, [id]);
|
|
|
|
const handleSubmit = async (e: SubmitEvent<HTMLFormElement>) => {
|
|
e.preventDefault();
|
|
if (!name.trim()) {
|
|
setNameError(t('environments.form_name_required'));
|
|
return;
|
|
}
|
|
let parsedData: Record<string, string | undefined>;
|
|
try {
|
|
const raw = dataJson.trim() ? JSON.parse(dataJson.trim()) : {};
|
|
if (typeof raw !== 'object' || raw === null || Array.isArray(raw)) {
|
|
throw new Error('invalid');
|
|
}
|
|
parsedData = Object.fromEntries(
|
|
Object.entries(raw as Record<string, unknown>).map(([k, v]) => [
|
|
k,
|
|
v == null ? undefined : String(v),
|
|
]),
|
|
);
|
|
setDataError('');
|
|
} catch {
|
|
setDataError(t('environments.form_data_invalid_json'));
|
|
return;
|
|
}
|
|
setSaving(true);
|
|
try {
|
|
await environments.update(id!, {
|
|
name: name.trim(),
|
|
description: description.trim() || undefined,
|
|
data: parsedData,
|
|
});
|
|
toast.success(t('environments.updated'));
|
|
navigate(`/environments/${id}`);
|
|
} catch (err) {
|
|
const message = (err as Error).message;
|
|
toast.error(message);
|
|
} finally {
|
|
setSaving(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div>
|
|
<div className={styles.pageToolbar}>
|
|
<Breadcrumbs
|
|
items={[
|
|
{
|
|
label: t('environments.title'),
|
|
icon: <Globe size={14} />,
|
|
onClick: () => navigate('/environments'),
|
|
},
|
|
{
|
|
label: env?.name ?? `#${id}`,
|
|
onClick: () => navigate(`/environments/${id}`),
|
|
},
|
|
{ label: t('environments.edit_title') },
|
|
]}
|
|
/>
|
|
</div>
|
|
|
|
{loading && <p className={styles.muted}>{t('environments.loading')}</p>}
|
|
|
|
{!loading && env && (
|
|
<form onSubmit={handleSubmit} noValidate>
|
|
<Card className={styles.formCardFull}>
|
|
<div className={styles.formFields}>
|
|
<Input
|
|
label={t('environments.form_name')}
|
|
placeholder={t('environments.form_name_placeholder')}
|
|
value={name}
|
|
onChange={(e) => {
|
|
setName(e.target.value);
|
|
setNameError('');
|
|
}}
|
|
error={nameError || undefined}
|
|
required
|
|
autoFocus
|
|
/>
|
|
<div className={styles.formField}>
|
|
<label className={styles.fieldLabel}>{t('environments.form_description')}</label>
|
|
<CodeEditor
|
|
language="markdown"
|
|
value={description}
|
|
onChange={setDescription}
|
|
rows={6}
|
|
/>
|
|
</div>
|
|
<div className={styles.formField}>
|
|
<label className={styles.fieldLabel}>{t('environments.form_data')}</label>
|
|
<CodeEditor
|
|
language="json"
|
|
value={dataJson}
|
|
onChange={(v) => {
|
|
setDataJson(v);
|
|
setDataError('');
|
|
}}
|
|
rows={8}
|
|
error={!!dataError}
|
|
/>
|
|
{dataError && <span className={styles.fieldError}>{dataError}</span>}
|
|
</div>
|
|
</div>
|
|
|
|
<div className={styles.formActions}>
|
|
<Button
|
|
type="button"
|
|
variant="secondary"
|
|
onClick={() => navigate(`/environments/${id}`)}
|
|
>
|
|
<X size={14} />
|
|
{t('environments.action_cancel')}
|
|
</Button>
|
|
<Button type="submit" loading={saving}>
|
|
<Save size={14} />
|
|
{t('environments.action_update')}
|
|
</Button>
|
|
</div>
|
|
</Card>
|
|
</form>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|