import { Alert, Button, JsonInput, Stack, TextInput } from '@mantine/core' import { useInputState, useSetState } from '@mantine/hooks' import type { FlowSummary } from '@mokei/flow-client' import { useState } from 'react' import { useFlow } from '../flow/FlowProvider.js' import { isRecord, schemaToFields } from '../flow/schema-fields.js' import { SchemaForm } from './SchemaForm.js' type StartState = { status: 'idle' } | { status: 'starting' } | { status: 'error'; error: string } type StartRunFormProps = { flow: FlowSummary onStarted(runID: string): void } export function StartRunForm({ flow, onStarted }: StartRunFormProps) { const { control, connected, status } = useFlow() const [label, setLabel] = useInputState('') const [{ json, jsonError }, setInput] = useSetState<{ json: string; jsonError?: string }>({ json: '{}', }) const [state, setState] = useState({ status: 'idle' }) const starting = state.status === 'starting' const error = state.status === 'error' ? state.error : undefined const disabled = !connected || status?.state !== 'ready' || starting async function start(input: Record) { if (disabled) return setState({ status: 'starting' }) try { const run = await control.runs.start({ flow: flow.id, input, label: label || undefined }) onStarted(run.runID) setState({ status: 'idle' }) } catch (error) { setState({ status: 'error', error: String(error) }) } } function submitJSON() { let input: unknown try { input = JSON.parse(json) if (!isRecord(input)) throw new Error('Expected object') } catch { setInput({ jsonError: 'Enter a valid JSON object' }) return } setInput({ jsonError: undefined }) void start(input) } return (
{schemaToFields(flow.input) != null ? ( void start(input)} errors={error == null ? [] : [error]} /> ) : (
{ event.preventDefault() submitJSON() }}> {error == null ? null : {error}} setInput({ json })} error={jsonError} formatOnBlur rows={4} />
)}
) }