/**
* The input-injection list of one attached state. Each row injects a value into the
* run input BEFORE the run: either a template `input` jq (picked by name) or a
* custom jq over the record, placed under the `into` run-input field.
*/
import type { ReactNode } from 'react';
import { Field } from '../components/field';
import { CloseIcon } from '../components/icons';
import { TextInput } from '../components/inputs';
import { Button } from '../components/primitives';
import { Select } from '../components/select';
import type { TemplateJqSuggestion } from './BindingJqField';
import { BindingTemplatedJqField } from './BindingTemplatedJqField';
import type { ResolvedTemplateJq } from './catalog';
import type { StateInjection, TemplatedTextCatalog } from './types';
function optionLabel(entry: ResolvedTemplateJq): string {
return entry.description !== undefined && entry.description !== ''
? `${entry.ref} — ${entry.description}`
: entry.ref;
}
const CUSTOM = '__custom__';
function InjectionRow({
injection,
index,
inputJq,
suggestions,
templates,
onChange,
onRemove,
}: {
readonly injection: StateInjection;
readonly index: number;
readonly inputJq: readonly ResolvedTemplateJq[];
readonly suggestions?: readonly TemplateJqSuggestion[];
readonly templates?: TemplatedTextCatalog;
readonly onChange: (injection: StateInjection) => void;
readonly onRemove: () => void;
}): ReactNode {
const isCustom = injection.jq !== null;
const selectValue = isCustom ? CUSTOM : (injection.template_jq ?? '');
return (
{isCustom ? (
{
onChange({ template_jq: null, jq: next ?? { content: '' }, into: injection.into });
}}
suggestions={suggestions}
templates={templates}
/>
) : null}
{
onChange({ ...injection, into: event.target.value });
}}
/>
);
}
export interface InjectionListProps {
readonly injections: readonly StateInjection[];
readonly onChange: (injections: readonly StateInjection[]) => void;
readonly inputJq: readonly ResolvedTemplateJq[];
readonly suggestions?: readonly TemplateJqSuggestion[];
readonly templates?: TemplatedTextCatalog;
}
export function InjectionList({
injections,
onChange,
inputJq,
suggestions,
templates,
}: InjectionListProps): ReactNode {
const add = (): void => {
const first = inputJq[0];
const next: StateInjection =
first !== undefined
? { template_jq: first.ref, jq: null, into: '' }
: { template_jq: null, jq: { content: '' }, into: '' };
onChange([...injections, next]);
};
return (
{injections.map((injection, index) => (
{
onChange(injections.map((current, position) => (position === index ? next : current)));
}}
onRemove={() => {
onChange(injections.filter((_, position) => position !== index));
}}
/>
))}
);
}