import { tr } from '../lib/i18n'; import { useState } from 'react'; import { Btn, Dialog, Icon, Select, ErrorNotice } from './ui'; import { api } from '../lib/client'; import { useStore } from '../lib/store'; import type { Task } from '../lib/tasks'; export function TaskEditor({ task, assistant = false, onClose, onSaved }: { task: Task; assistant?: boolean; onClose: () => void; onSaved?: () => void; }) { const [editor, setEditor] = useState({ ...task }); const [step, setStep] = useState<0 | 1>(0), [ai, setAi] = useState(assistant); const [description, setDescription] = useState(''), [suggestion, setSuggestion] = useState(false), [pending, setPending] = useState(false), [error, setError] = useState(''); const library = useStore(s => s.library); const valid = !!editor.title.trim() && !!editor.prompt.trim(); async function action(fn: () => Promise) { setPending(true); setError(''); try { await fn(); window.dispatchEvent(new Event('strata:tasks')); onSaved?.(); } catch (e) { setError(e instanceof Error ? e.message : String(e)); } finally { setPending(false); } } async function save() { await (editor.id ? api.patch(`/api/tasks/${editor.id}`, editor) : api.post('/api/tasks', editor)); onClose(); } return !pending && onClose()} actions={<> step ? setStep(0) : onClose()}>{tr(step ? 'Back' : 'Cancel')} {tr(step === 0 ? setStep(1)}>{tr("Choose schedule ")} : void action(save)}>{tr(pending ? 'Saving…' : editor.id ? 'Save changes' : 'Create task')})} }>
{tr((['Instructions', 'Schedule'] as const).map((label, index) => ))}
{tr(step === 0 ? <> {tr(!editor.id && !ai && !!library?.loaded ? : null)} {tr(ai && !suggestion ?