- 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
113 lines
3.7 KiB
TypeScript
113 lines
3.7 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 } from '../../api';
|
|
import { Breadcrumbs, Button, Card, Input, CodeEditor, useToast } from '../../ui';
|
|
import { CodeHelpButton } from '../../components/modals';
|
|
import styles from '../Page.module.css';
|
|
|
|
export function CreateStepPage() {
|
|
const { t } = useTranslation();
|
|
const { id } = useParams<{ id: string }>();
|
|
const navigate = useNavigate();
|
|
const toast = useToast();
|
|
|
|
const [scenario, setScenario] = useState<Scenario | null>(null);
|
|
usePageTitle(t('steps.create_title'));
|
|
const [title, setTitle] = useState('');
|
|
const [execCode, setExecCode] = useState('');
|
|
const [timeoutSeconds, setTimeoutSeconds] = useState<string>('');
|
|
const [saving, setSaving] = useState(false);
|
|
|
|
useEffect(() => {
|
|
if (!id) return;
|
|
scenarios
|
|
.get(id)
|
|
.then(setScenario)
|
|
.catch(() => null);
|
|
}, [id]);
|
|
|
|
const handleSubmit = async (e: SubmitEvent<HTMLFormElement>) => {
|
|
e.preventDefault();
|
|
setSaving(true);
|
|
try {
|
|
await steps.create(id!, {
|
|
title: title.trim() || undefined,
|
|
execCode: execCode.trim() || undefined,
|
|
timeoutSeconds: timeoutSeconds.trim() ? Number(timeoutSeconds) : undefined,
|
|
});
|
|
toast.success(t('steps.created'));
|
|
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.create_title') },
|
|
]}
|
|
/>
|
|
</div>
|
|
|
|
<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_save')}
|
|
</Button>
|
|
</div>
|
|
</Card>
|
|
</form>
|
|
</div>
|
|
);
|
|
}
|