import type { FieldDefinition, Site, SubFieldDefinition } from "@cloudhal/core-contracts"; import { canConvert, FIELD_TYPES, fieldLabel, REFERENCE_FIELD_TYPES, } from "@cloudhal/core-ts/fields"; import { Input, Select } from "@cloudhal/ui"; import type { TFunction } from "../../i18n/index.tsx"; import type { FieldOperationRequest } from "../../lib/api.ts"; import { definitionOf, emptyForm, type FieldForm, type FormProblems, formOf, hasProblems, } from "../../lib/field-form.ts"; import { SUB_TYPES, stage4Problems, subAsDefinition, subDefinition, } from "../../lib/field-form-stage4.ts"; import type { FieldType } from "../../lib/fields.ts"; import { TypeFields } from "./FieldDialog.tsx"; type FT = TFunction<"fields">; export type OperationKind = FieldOperationRequest["op"]; const WHOLE = "__field"; /** What an operation dialog was opened for: a field and an operation, or a run to pick up. */ export interface OperationTarget { kind: OperationKind; def: FieldDefinition; /** An operation already started (a closed dialog, another tab, an agent's approved call). */ resume?: { operationId: string; reverting: boolean } | undefined; } /** * The types a field (or, with `sub`, a repeater's sub-field) can change to (RFC-0041 §6's table, * RFC-0045 §7's rows); a select or a reference can also flip `multiple`. Reference and repeater * targets only where the server takes them. */ export function targetTypes(from: FieldType, opts: { referenceTypes: boolean; sub?: boolean }) { const pool: readonly FieldType[] = opts.sub ? SUB_TYPES : opts.referenceTypes ? [...FIELD_TYPES, ...REFERENCE_FIELD_TYPES] : FIELD_TYPES; return pool.filter((to) => to === from ? !opts.sub && (to === "select" || to === "reference") : canConvert(from, to), ); } /** The dialog's inputs: a new key, the sub-field it applies to, and the target type's form. */ export interface Inputs { key: string; /** A repeater's sub-field key, or "" for the field itself. */ sub: string; type: FieldType; /** The target type's settings (options, collections, sub-fields), as the builder edits them. */ form: FieldForm; } /** The sub-field an operation applies to, as a definition. */ export function subOf(def: FieldDefinition, sub: string): SubFieldDefinition | undefined { return def.type === "repeater" ? def.fields.find((s) => s.key === sub) : undefined; } /** Fresh inputs for `target` (and a sub-field), its first target type pre-chosen. */ export function freshInputs( target: OperationTarget, sub: string, ctx: { locales: readonly string[]; referenceTypes: boolean }, ): Inputs { const subDef = subOf(target.def, sub); const from = (subDef?.type ?? target.def.type) as FieldType; const type = targetTypes(from, { referenceTypes: ctx.referenceTypes, sub: subDef !== undefined })[0] ?? from; return { key: subDef?.key ?? target.def.key, sub, type, form: targetForm(target, sub, type, ctx), }; } /** The target type's form: the field's own settings when it keeps its type, else empty. */ function targetForm( target: OperationTarget, sub: string, type: FieldType, ctx: { locales: readonly string[] }, ): FieldForm { const subDef = subOf(target.def, sub); const own = subDef ? subAsDefinition(subDef) : target.def; if (own.type === type) return formOf(own, ctx.locales); return emptyForm(type, ctx.locales); } /** The request the inputs ask for (`clear`: drop values that can't be converted). */ export function requestOf( target: OperationTarget, inputs: Inputs, clear: boolean, ): FieldOperationRequest { const { def } = target; const sub = inputs.sub ? { sub: inputs.sub } : {}; switch (target.kind) { case "rename_field": return { op: "rename_field", from: def.key, to: inputs.key, ...sub }; case "set_field_localized": return { op: "set_field_localized", key: def.key, localized: !def.localized }; case "remove_field": return { op: "remove_field", key: def.key, ...sub }; default: { const to = definitionOf({ ...inputs.form, type: inputs.type, key: def.key }); return { op: "change_field_type", key: def.key, type: inputs.type, ...sub, ...targetInputs(to, inputs.sub !== ""), ...(clear ? { onInvalid: "clear" as const } : {}), }; } } } /** What a type change sends for its target: options, collections and "Several", or sub-fields. */ function targetInputs(to: FieldDefinition, sub: boolean) { if (to.type === "select") return { options: to.options, ...(sub ? {} : { multiple: to.multiple === true }) }; if (to.type === "reference") return { collections: [...to.collections], ...(sub ? {} : { multiple: to.multiple === true }), ...(!sub && to.maxItems !== undefined ? { maxItems: to.maxItems } : {}), }; if (to.type === "repeater") return { subFields: to.fields.map((f) => subDefinition(subAsDefinition(f))), ...(to.maxItems !== undefined ? { maxItems: to.maxItems } : {}), ...(to.itemLabel ? { itemLabel: to.itemLabel } : {}), }; return {}; } const SLUG = /^[a-z0-9]+(?:-[a-z0-9]+)*$/; /** * What keeps a type change's target from being sent, by the contract's rules and RFC-0045's own: * a select's options (at least one, slug values, no repeats, a label in some language — not * necessarily the site's default, which may have changed since they were written), a reference's * collections and maximum, a list's item fields, maximum and item name. */ export function targetProblems(inputs: Inputs, defaultLocale: string): FormProblems { const form = { ...inputs.form, type: inputs.type }; if (inputs.type === "select") { const values = form.options.map((o) => o.value); if (form.options.length === 0) return { options: "required" }; if (values.some((v) => !SLUG.test(v))) return { options: "value" }; if (new Set(values).size !== values.length) return { options: "duplicate" }; if (form.options.some((o) => !Object.values(o.labels).some((l) => l.trim()))) return { options: "label" }; return {}; } if (inputs.type === "reference" || inputs.type === "repeater") return stage4Problems(form, defaultLocale); return {}; } /** True when the target's settings can't be sent yet; `targetProblems` says why, inline. */ export const targetBlocked = (inputs: Inputs, defaultLocale: string): boolean => hasProblems(targetProblems(inputs, defaultLocale)); /** * The inputs of rename and type change (localization and removal have none but the sub-field): * for a repeater, which sub-field the change applies to; then the new key, or the new type with * the target type's settings — options, target collections and "Several", or the sub-fields of a * new list — drawn with the builder's own parts. */ export function OperationInputs(props: { t: FT; target: OperationTarget; inputs: Inputs; setInputs: (next: Inputs) => void; keyError: string | undefined; locales: readonly string[]; defaultLocale: string; uiLocale: string; disabled: boolean; site: Site; referenceTypes: boolean; }) { const { t, target, inputs, setInputs } = props; const ctx = { locales: props.locales, referenceTypes: props.referenceTypes }; const repeater = target.def.type === "repeater" && target.kind !== "set_field_localized"; const subDef = subOf(target.def, inputs.sub); const from = (subDef?.type ?? target.def.type) as FieldType; return (
{repeater && ( setInputs({ ...inputs, key })} error={props.keyError} maxLength={40} disabled={props.disabled} /> )} {target.kind === "change_field_type" && ( <>