import { useEffect, useState, SubmitEvent } from 'react'; import { useNavigate, useParams } from 'react-router-dom'; import { useTranslation } from 'react-i18next'; 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 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(null); const [step, setStep] = useState(null); const [title, setTitle] = useState(''); const [execCode, setExecCode] = useState(''); 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 ?? ''); }) .finally(() => setLoading(false)); }, [id, stepId]); const handleSubmit = async (e: SubmitEvent) => { e.preventDefault(); setSaving(true); try { await steps.update(id!, stepId!, { title: title.trim() || undefined, execCode: execCode.trim() || undefined, }); toast.success(t('steps.updated')); navigate(`/scenarios/${id}`); } catch (err) { const message = (err as Error).message; toast.error(message); } finally { setSaving(false); } }; return (
, onClick: () => navigate('/scenarios'), }, { label: scenario?.name ?? `#${id}`, onClick: () => navigate(`/scenarios/${id}`), }, { label: t('steps.edit_title', { order: step ? step.order + 1 : stepId }) }, ]} />
{loading &&

{t('steps.loading')}

} {!loading && step && (
setTitle(e.target.value)} />
)}
); }