- a (?) button next to the Code label on the step and snippet create/edit pages opens a reference of the script runtime: globals, every context member with examples, and pitfalls such as awaiting getStepOutput and assert - the reference lives in components/modals/code-help.md and is rendered as markdown - Modal takes size="lg" for document-like content; the dialog is capped to the viewport and its body scrolls, focusable when lg so the keyboard can scroll it - docs/scenario.md notes that getStepOutput is async and points to the in-app reference to keep in sync
130 lines
4.5 KiB
TypeScript
130 lines
4.5 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, Save, ClipboardList } from 'lucide-react';
|
|
import { scenarios, steps } from '../../api';
|
|
import type { Scenario, ScenarioStep } from '../../api';
|
|
import { Breadcrumbs, Button, Card, Input, CodeEditor, useToast } from '../../ui';
|
|
import { CodeHelpButton } from '../../components/modals';
|
|
import styles from '../Page.module.css';
|
|
|
|
export function EditStepPage() {
|
|
const { t } = useTranslation();
|
|
const { id, stepId } = useParams<{ id: string; stepId: string }>();
|
|
const navigate = useNavigate();
|
|
const toast = useToast();
|
|
|
|
const [scenario, setScenario] = useState<Scenario | null>(null);
|
|
const [step, setStep] = useState<ScenarioStep | null>(null);
|
|
usePageTitle(
|
|
step ? t('steps.edit_title', { order: step.order }) : t('steps.edit_title', { order: '' }),
|
|
);
|
|
const [title, setTitle] = useState('');
|
|
const [execCode, setExecCode] = useState('');
|
|
const [timeoutSeconds, setTimeoutSeconds] = useState<string>('');
|
|
const [loading, setLoading] = useState(true);
|
|
const [saving, setSaving] = useState(false);
|
|
|
|
useEffect(() => {
|
|
if (!id || !stepId) return;
|
|
Promise.all([scenarios.get(id), steps.get(id, stepId)])
|
|
.then(([sc, st]) => {
|
|
setScenario(sc);
|
|
setStep(st);
|
|
setTitle(st.title ?? '');
|
|
setExecCode(st.execCode ?? '');
|
|
setTimeoutSeconds(st.timeoutSeconds != null ? String(st.timeoutSeconds) : '');
|
|
})
|
|
.finally(() => setLoading(false));
|
|
}, [id, stepId]);
|
|
|
|
const handleSubmit = async (e: SubmitEvent<HTMLFormElement>) => {
|
|
e.preventDefault();
|
|
setSaving(true);
|
|
try {
|
|
await steps.update(id!, stepId!, {
|
|
title: title.trim() || undefined,
|
|
execCode: execCode.trim() || undefined,
|
|
timeoutSeconds: timeoutSeconds.trim() ? Number(timeoutSeconds) : null,
|
|
});
|
|
toast.success(t('steps.updated'));
|
|
navigate(`/scenarios/${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('scenarios.title'),
|
|
icon: <ClipboardList size={14} />,
|
|
onClick: () => navigate('/scenarios'),
|
|
},
|
|
{
|
|
label: scenario?.name ?? `#${id}`,
|
|
onClick: () => navigate(`/scenarios/${id}`),
|
|
},
|
|
{ label: t('steps.edit_title', { order: step ? step.order : stepId }) },
|
|
]}
|
|
/>
|
|
</div>
|
|
|
|
{loading && <p className={styles.muted}>{t('steps.loading')}</p>}
|
|
|
|
{!loading && step && (
|
|
<form onSubmit={handleSubmit} noValidate>
|
|
<Card className={styles.formCardFull}>
|
|
<div className={styles.formFields}>
|
|
<Input
|
|
label={t('steps.form_title')}
|
|
placeholder={t('steps.form_title_placeholder')}
|
|
value={title}
|
|
onChange={(e) => setTitle(e.target.value)}
|
|
/>
|
|
<div className={styles.formField}>
|
|
<div className={styles.labelRow}>
|
|
<label className={styles.fieldLabel}>{t('steps.form_exec_code')}</label>
|
|
<CodeHelpButton />
|
|
</div>
|
|
<CodeEditor value={execCode} onChange={setExecCode} rows={6} />
|
|
</div>
|
|
<Input
|
|
label={t('steps.form_timeout')}
|
|
placeholder={t('steps.form_timeout_placeholder')}
|
|
type="number"
|
|
min={1}
|
|
value={timeoutSeconds}
|
|
onChange={(e) => setTimeoutSeconds(e.target.value)}
|
|
hint={t('steps.form_timeout_hint')}
|
|
/>
|
|
</div>
|
|
|
|
<div className={styles.formActions}>
|
|
<Button
|
|
type="button"
|
|
variant="secondary"
|
|
onClick={() => navigate(`/scenarios/${id}`)}
|
|
>
|
|
<X size={14} />
|
|
{t('steps.action_cancel')}
|
|
</Button>
|
|
<Button type="submit" loading={saving}>
|
|
<Save size={14} />
|
|
{t('steps.action_update')}
|
|
</Button>
|
|
</div>
|
|
</Card>
|
|
</form>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|