// Copyright (c) Cratis. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. import { lazy, useId, useState, useSyncExternalStore } from 'react'; import { RegisteredComponentProps } from '@cratis/scene.react'; import { ArcRuntimeBoundary, BindingKind, BoundConstructor, MissingBinding, resolveExactQuery, subscribeQueryBindings } from '../bindings'; import { stringProperty } from '../properties'; import { SingleResultStatus } from '../data/SingleResultStatus'; import { QueryInput, queryInputError, queryInputs } from './queryInputs'; const SingleResultRuntime = lazy(() => import('../data/SingleResultRuntime')); /** Opt-in, form-local string drafts around the existing optional-single execution owner. */ export function SceneQueryInputForm({ element }: RegisteredComponentProps) { const name = stringProperty(element.properties, 'query'); const target = useSyncExternalStore(subscribeQueryBindings, () => name ? resolveExactQuery(name) : undefined); if (target === 'ambiguous') return
Ambiguous query binding '{name}' on {element.componentName}
; if (!target) return ; const inputs = queryInputs(element.properties); const resultField = stringProperty(element.properties, 'resultField'); if (!inputs || !resultField) return
Invalid query input form configuration
; if (!element.isEnabled) return ; // A different form declaration starts a new session; ordinary host rerenders retain drafts. return ; } interface Props { query: BoundConstructor; inputs: QueryInput[]; resultField: string; label: string; submitLabel: string; } function QueryInputForm({ query, inputs, resultField, label, submitLabel }: Props) { const id = useId(); const [drafts, setDrafts] = useState>(() => Object.fromEntries(inputs.map(input => [input.parameter, '']))); const [errors, setErrors] = useState>({}); const [committed, setCommitted] = useState>(); return
{ event.preventDefault(); const validation = Object.fromEntries(inputs.map(input => [input.parameter, queryInputError(input, drafts[input.parameter])])); setErrors(validation); if (Object.values(validation).some(Boolean)) { setCommitted(undefined); return; } // New identity even for unchanged values: repeat submit is an explicit retry. No trimming, // coercion, generated identifier, property-path evaluation or query while typing. setCommitted(Object.freeze(Object.fromEntries(inputs.map(input => [input.parameter, drafts[input.parameter]])))); }}> {inputs.map((input, index) => { const controlId = `${id}-${index}`; const error = Object.hasOwn(errors, input.parameter) ? errors[input.parameter] : undefined; return
{ setDrafts({ ...drafts, [input.parameter]: event.currentTarget.value }); setErrors({ ...errors, [input.parameter]: undefined }); // Unmount synchronously on edit: runtime cleanup aborts and masks late replies. setCommitted(undefined); }} /> {error && }
; })} {committed ? : } ; }