| 17 | * always the canonical input. |
| 18 | */ |
| 19 | export default function DirectionDialog({ open, seg, onSave, onClose }) { |
| 20 | const { t } = useTranslation(); |
| 21 | const [text, setText] = useState(''); |
| 22 | const [preview, setPreview] = useState(null); |
| 23 | const [parsing, setParsing] = useState(false); |
| 24 | const [saving, setSaving] = useState(false); |
| 25 | |
| 26 | useEffect(() => { |
| 27 | if (open) { |
| 28 | setText(seg?.direction || ''); |
| 29 | setPreview(null); |
| 30 | } |
| 31 | }, [open, seg?.id, seg?.direction]); |
| 32 | |
| 33 | const runPreview = async () => { |
| 34 | if (!text.trim()) { |
| 35 | setPreview(null); |
| 36 | return; |
| 37 | } |
| 38 | setParsing(true); |
| 39 | try { |
| 40 | setPreview(await apiPost('/tools/direction', { text })); |
| 41 | } catch (e) { |
| 42 | toast.error(t('direction.previewFailed', { message: e.message })); |
| 43 | } finally { |
| 44 | setParsing(false); |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | const save = async () => { |
| 49 | setSaving(true); |
| 50 | try { |
| 51 | await onSave?.(text.trim()); |
| 52 | onClose?.(); |
| 53 | } finally { |
| 54 | setSaving(false); |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | if (!open) return null; |
| 59 | |
| 60 | return ( |
| 61 | <Dialog |
| 62 | open |
| 63 | onClose={onClose} |
| 64 | size="md" |
| 65 | title={ |
| 66 | <> |
| 67 | <Sparkles size={14} /> {t('direction.title', { id: seg?.id?.slice?.(0, 6) || '' })} |
| 68 | </> |
| 69 | } |
| 70 | footer={ |
| 71 | <> |
| 72 | {seg?.direction && ( |
| 73 | <Button |
| 74 | variant="ghost" |
| 75 | size="sm" |
| 76 | onClick={() => { |