import type { Site } from "@cloudhal/core-contracts";
import { fieldLabel } from "@cloudhal/core-ts/fields";
import { Button, Checkbox, Input, Text } from "@cloudhal/ui";
import { Plus } from "@phosphor-icons/react";
import { useQuery } from "@tanstack/react-query";
import { useI18n } from "../../i18n/index.tsx";
import { collectionName, collectionsQuery } from "../../lib/collections.ts";
import type { FieldForm, FormProblems } from "../../lib/field-form.ts";
import { STAGE4_LIMITS } from "../../lib/field-form-stage4.ts";
import { listFieldDefs } from "../../lib/listings.ts";
import { ConditionRow, freshCondition } from "../listings/ContentListQuery.tsx";
import { SwitchField } from "../TaxonomySettingsDialog.tsx";
/**
* A reference's own settings in the fields builder (RFC-0045 §5): the target collections (a
* `Checkbox.Group` of the site's collections, one to five), and for a field — not a repeater's
* sub-field — "Several" with its maximum (new fields only: a saved field changes it through
* "Change type…") and, with one target collection, conditions on its listing fields (no `now`).
*/
export function ReferenceFields({
site,
form,
set,
isNew,
problems,
sub = false,
noConditions = false,
}: {
site: Site;
form: FieldForm;
set: (next: FieldForm) => void;
isNew: boolean;
problems: FormProblems;
/** A repeater's sub-field: one target, no conditions. */
sub?: boolean;
/** A type change's target: collections, "Several" and the maximum only. */
noConditions?: boolean;
}) {
const { t, locale } = useI18n("fields");
const d = "fields.dialog.reference" as const;
const registry = useQuery(collectionsQuery(site.id));
const keys = [
...new Set([...(registry.data ?? []).map((c) => c.key), ...form.collections]),
].sort();
const name = (key: string) => collectionName(site, key, locale, t("nav.articles"));
const target = form.collections.length === 1 ? form.collections[0] : undefined;
const targetDefs = target ? listFieldDefs(site.collections?.[target]) : [];
return (
{
const collections = keys.filter((k) => next.includes(k));
// Conditions are on the one target's listing fields: a new target keeps only its own.
const only = collections.length === 1 ? collections[0] : undefined;
const fits = new Set(
only ? listFieldDefs(site.collections?.[only]).map((d) => d.key) : [],
);
// Only a new single target drops what doesn't fit it; several (or none) keep them for later.
const where =
!only || only === target ? form.where : form.where.filter((w) => fits.has(w.field));
set({ ...form, collections, where });
}}
>
{keys.map((k) => (
))}
{!sub && (
<>
set({ ...form, multiple })}
disabled={!isNew}
/>
{form.multiple && (
set({ ...form, maxItems })}
error={
problems.maxItems
? t(`${d}.errors.maxItems`, { max: STAGE4_LIMITS.maxItems })
: undefined
}
min={1}
max={STAGE4_LIMITS.maxItems}
/>
)}
{problems.where && (
{t(`${d}.errors.where`)}
)}
{!noConditions && (
)}
>
)}
}
// At the limit it stays focusable, so focus isn't lost after adding the last one.
aria-disabled={full || undefined}
className={full ? "cursor-not-allowed opacity-50" : undefined}
onClick={() => {
if (full || !defs[0]) return;
set({ ...form, where: [...form.where, freshCondition(defs[0], undefined, true)] });
}}
>
{t(`${d}.addCondition`)}