- 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
173 lines
5.5 KiB
TypeScript
173 lines
5.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, Braces } from 'lucide-react';
|
|
import { snippets } from '../../api';
|
|
import type { Snippet } from '../../api';
|
|
import { Breadcrumbs, Button, Card, Input, CodeEditor, useToast } from '../../ui';
|
|
import { CodeHelpButton } from '../../components/modals';
|
|
import styles from '../Page.module.css';
|
|
|
|
export function EditSnippetPage() {
|
|
const { t } = useTranslation();
|
|
const { id } = useParams<{ id: string }>();
|
|
const navigate = useNavigate();
|
|
const toast = useToast();
|
|
|
|
const [snippet, setSnippet] = useState<Snippet | null>(null);
|
|
const [alias, setAlias] = useState('');
|
|
usePageTitle(
|
|
snippet ? `${t('snippets.edit_title')} — ${snippet.title}` : t('snippets.edit_title'),
|
|
);
|
|
const [title, setTitle] = useState('');
|
|
const [description, setDescription] = useState('');
|
|
const [code, setCode] = useState('');
|
|
const [aliasError, setAliasError] = useState('');
|
|
const [titleError, setTitleError] = useState('');
|
|
const [codeError, setCodeError] = useState('');
|
|
const [loading, setLoading] = useState(true);
|
|
const [saving, setSaving] = useState(false);
|
|
|
|
useEffect(() => {
|
|
if (!id) return;
|
|
snippets
|
|
.get(id)
|
|
.then((s) => {
|
|
setSnippet(s);
|
|
setAlias(s.alias);
|
|
setTitle(s.title);
|
|
setDescription(s.description ?? '');
|
|
setCode(s.code);
|
|
})
|
|
.finally(() => setLoading(false));
|
|
}, [id]);
|
|
|
|
const handleSubmit = async (e: SubmitEvent<HTMLFormElement>) => {
|
|
e.preventDefault();
|
|
let valid = true;
|
|
if (!alias.trim()) {
|
|
setAliasError(t('snippets.form_alias_required'));
|
|
valid = false;
|
|
}
|
|
if (!title.trim()) {
|
|
setTitleError(t('snippets.form_title_required'));
|
|
valid = false;
|
|
}
|
|
if (!code.trim()) {
|
|
setCodeError(t('snippets.form_code_required'));
|
|
valid = false;
|
|
}
|
|
if (!valid) return;
|
|
setSaving(true);
|
|
try {
|
|
await snippets.update(id!, {
|
|
alias: alias.trim(),
|
|
title: title.trim(),
|
|
description: description.trim() || undefined,
|
|
code: code.trim(),
|
|
});
|
|
toast.success(t('snippets.updated'));
|
|
navigate(`/snippets/${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('snippets.title'),
|
|
icon: <Braces size={14} />,
|
|
onClick: () => navigate('/snippets'),
|
|
},
|
|
{
|
|
label: snippet?.title ?? `#${id}`,
|
|
onClick: () => navigate(`/snippets/${id}`),
|
|
},
|
|
{ label: t('snippets.edit_title') },
|
|
]}
|
|
/>
|
|
</div>
|
|
|
|
{loading && <p className={styles.muted}>{t('snippets.loading')}</p>}
|
|
|
|
{!loading && snippet && (
|
|
<form onSubmit={handleSubmit} noValidate>
|
|
<Card className={styles.formCardFull}>
|
|
<div className={styles.formFields}>
|
|
<Input
|
|
label={t('snippets.form_alias')}
|
|
placeholder={t('snippets.form_alias_placeholder')}
|
|
value={alias}
|
|
onChange={(e) => {
|
|
setAlias(e.target.value);
|
|
setAliasError('');
|
|
}}
|
|
error={aliasError || undefined}
|
|
required
|
|
autoFocus
|
|
/>
|
|
<Input
|
|
label={t('snippets.form_title')}
|
|
placeholder={t('snippets.form_title_placeholder')}
|
|
value={title}
|
|
onChange={(e) => {
|
|
setTitle(e.target.value);
|
|
setTitleError('');
|
|
}}
|
|
error={titleError || undefined}
|
|
required
|
|
/>
|
|
<div className={styles.formField}>
|
|
<label className={styles.fieldLabel}>{t('snippets.form_description')}</label>
|
|
<CodeEditor
|
|
value={description}
|
|
onChange={setDescription}
|
|
language="markdown"
|
|
rows={10}
|
|
/>
|
|
</div>
|
|
<div className={styles.formField}>
|
|
<div className={styles.labelRow}>
|
|
<label className={styles.fieldLabel}>
|
|
{t('snippets.form_code')}
|
|
<span className={styles.requiredMark}> *</span>
|
|
</label>
|
|
<CodeHelpButton />
|
|
</div>
|
|
<CodeEditor
|
|
value={code}
|
|
onChange={(v) => {
|
|
setCode(v);
|
|
setCodeError('');
|
|
}}
|
|
rows={14}
|
|
error={!!codeError}
|
|
/>
|
|
{codeError && <span className={styles.fieldError}>{codeError}</span>}
|
|
</div>
|
|
</div>
|
|
<div className={styles.formActions}>
|
|
<Button type="button" variant="secondary" onClick={() => navigate(`/snippets/${id}`)}>
|
|
<X size={14} />
|
|
{t('snippets.action_cancel')}
|
|
</Button>
|
|
<Button type="submit" disabled={saving}>
|
|
<Save size={14} />
|
|
{t('snippets.action_update')}
|
|
</Button>
|
|
</div>
|
|
</Card>
|
|
</form>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|