import type { Taxonomy, Term } from "@cloudhal/core-contracts"; import { Autocomplete, Badge, Combobox, Select, TermChip, TermChipList, Text, Tooltip, } from "@cloudhal/ui"; import { Hourglass, Plus, Star, Warning } from "@phosphor-icons/react"; import { useState } from "react"; import { listOf, useT } from "../i18n/index.tsx"; import type { TermListItem } from "../lib/api.ts"; import { apiErrorText } from "../lib/api-errors.ts"; import { type Assignment, foldName, type TagSuggestion, tagSuggestions, termTree, } from "../lib/taxonomy.ts"; /** Name, synonyms and slugs, folded — what a term picker matches a query against. */ function termMatches(term: Term, query: string): boolean { const q = foldName(query); if (!q) return true; if (term.slug.includes(q)) return true; for (const label of Object.values(term.labels)) { if (!label) continue; if (foldName(label.name).includes(q)) return true; if ((label.altNames ?? []).some((a) => foldName(a).includes(q))) return true; } return false; } export interface TreeOption { id: string; term: Term; depth: number; /** "News › Local" for a child; empty for a root. */ path: string; } /** Live terms in tree order with their depth and ancestor path, for tree-aware pickers. */ export function treeOptions( items: readonly TermListItem[], name: (t: Term) => string, include: (t: Term) => boolean = (t) => t.status !== "retired", ): TreeOption[] { return termTree(items.filter((i) => include(i.term))).map((row) => ({ id: row.item.term.id, term: row.item.term, depth: row.depth, path: row.ancestors.map(name).join(" › "), })); } /** One option row: indented by depth, the ancestor path muted after the name. */ function OptionLabel({ option, name }: { option: TreeOption; name: string }) { const t = useT(); return ( 0 ? { paddingInlineStart: `${option.depth}rem` } : undefined} > {name} {option.path && ( {t("pickers.under", { path: option.path })} )} {option.term.status === "proposed" && ( }> {t("taxonomy.state.proposed")} )} ); } /** * Tree-aware single choice (merge target, new parent, retire redirect): a Kumo `Combobox` that * matches names, synonyms and slugs; children are indented and carry their path. */ export function TermCombobox({ label, description, options, value, onChange, name, placeholder, error, none, testId, }: { label: string; description?: string | undefined; options: TreeOption[]; value: string | null; onChange: (id: string | null) => void; name: (t: Term) => string; placeholder: string; error?: string | undefined; /** A first option meaning "none" (top level, no redirect), with this label. */ none?: string | undefined; testId?: string | undefined; }) { const t = useT(); const NONE = ""; const ids = [...(none !== undefined ? [NONE] : []), ...options.map((o) => o.id)]; const byId = new Map(options.map((o) => [o.id, o])); const labelOf = (id: string) => (id === NONE ? (none ?? "") : name(byId.get(id)?.term as Term)); return (
onChange(v === NONE || v === null ? null : (v as string))} itemToStringLabel={(id: string) => labelOf(id)} filter={(id: string, q: string) => id === NONE ? !q : termMatches(byId.get(id)?.term as Term, q) } label={label} description={description} error={error} > {t("pickers.noMatch")} {(id: string) => ( {id === NONE ? ( {none} ) : ( )} )}
); } /** * The article's categories (RFC-0002 §8): a tree-aware multi-select `Combobox` with chips, at most * `maxPerEntry`, and the primary category — a single choice is the primary; with several, a * `Select` picks it. A required taxonomy says so; the editor refuses review/publish without one. */ export function CategoryPicker({ tax, items, value, onChange, name, label, required, error, }: { tax: Taxonomy; items: readonly TermListItem[]; value: Assignment; onChange: (next: Assignment) => void; name: (t: Term) => string; label: string; required: boolean; error?: string | undefined; }) { const t = useT(); const options = treeOptions(items, name); const byId = new Map(items.map((i) => [i.term.id, i.term])); const max = tax.maxPerEntry ?? 50; const full = value.ids.length >= max; const primary = value.primary && value.ids.includes(value.primary) ? value.primary : value.ids.length === 1 ? value.ids[0] : undefined; const set = (ids: string[], nextPrimary = value.primary) => onChange({ ids, primary: nextPrimary && ids.includes(nextPrimary) ? nextPrimary : ids.length === 1 ? ids[0] : undefined, }); const optionById = new Map(options.map((o) => [o.id, o])); const termName = (id: string) => { const term = byId.get(id); return term ? name(term) : id; }; return (
o.id)} value={value.ids} onValueChange={(ids) => set((ids as string[]).slice(0, max))} itemToStringLabel={(id: string) => termName(id)} filter={(id: string, q: string) => termMatches(byId.get(id) as Term, q)} label={label} required={required} error={error} description={ full ? t("pickers.atMost", { max }) : required ? t("pickers.categoryRequiredHint", { max }) : t("pickers.categoryHint", { max }) } > ( {id === primary && value.ids.length > 1 && ( )} {termName(id)} )} /> {t("pickers.noCategory")} {(id: string) => ( )} {value.ids.length > 1 && (