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 && (
)}
);
}
/**
* The article's tags (RFC-0002 §8): a Kumo `Autocomplete` offering existing tags first — by name,
* synonym or slug — and "Create" only when nothing matches, with a warning when the new tag looks
* like one that exists. Chosen tags are chips; at most `maxPerEntry`.
*/
export function TagPicker({
tax,
items,
value,
onChange,
onCreate,
name,
locale,
label,
error,
}: {
tax: Taxonomy;
items: readonly TermListItem[];
value: Assignment;
onChange: (next: Assignment) => void;
/** Creates an active tag (people may) and returns it. */
onCreate(name: string, slug: string): Promise;
name: (t: Term) => string;
locale: string;
label: string;
error?: string | undefined;
}) {
const t = useT();
const [query, setQuery] = useState("");
const [creating, setCreating] = useState(false);
const [createError, setCreateError] = useState();
const max = tax.maxPerEntry ?? 50;
const full = value.ids.length >= max;
const terms = items.map((i) => i.term);
const byId = new Map(terms.map((x) => [x.id, x]));
const usage = new Map(items.map((i) => [i.term.id, i.usage]));
const suggestions = full
? []
: tagSuggestions(terms, query, { locale, selected: new Set(value.ids), usage });
const keyOf = (s: TagSuggestion) => (s.kind === "term" ? s.term.id : `create:${s.slug}`);
const byKey = new Map(suggestions.map((s) => [keyOf(s), s]));
const choose = async (s: TagSuggestion) => {
setQuery("");
setCreateError(undefined);
if (s.kind === "term") {
onChange({ ...value, ids: [...value.ids, s.term.id] });
return;
}
if (!s.slug) {
setCreateError(t("pickers.createNoSlug"));
return;
}
setCreating(true);
try {
const term = await onCreate(s.name, s.slug);
onChange({ ...value, ids: [...value.ids, term.id] });
} catch (e) {
setCreateError(apiErrorText(t, e));
} finally {
setCreating(false);
}
};
return (
keyOf(s)}
onValueChange={(v: string, details: { reason: string }) => {
if (details.reason === "item-press") {
const s = byKey.get(v);
if (s) void choose(s);
return;
}
setQuery(v);
}}
label={label}
disabled={full || creating}
error={createError ?? error}
description={full ? t("pickers.atMost", { max }) : t("pickers.tagHint", { max })}
>
{(s: TagSuggestion) => (
{s.kind === "term" ? (
{name(s.term)}
{s.via === "altName" && s.matched && (
{t("pickers.viaSynonym", { value: s.matched })}
)}
{s.via === "slug" && s.matched && (
{s.matched}
)}
{s.via === "similar" && (
{t("pickers.similar")}
)}
{s.term.status === "proposed" && (
}>
{t("taxonomy.state.proposed")}
)}
) : (
{t("pickers.create", { name: s.name })}
{s.similar.length > 0 && (
{t("pickers.nearDuplicate", {
names: listOf(s.similar.slice(0, 3).map((x) => name(x))),
})}
)}
)}
)}
{value.ids.length > 0 && (
{value.ids.map((id) => {
const term = byId.get(id);
const chip = (
: undefined}
removeLabel={t("pickers.remove", { name: term ? name(term) : id })}
onRemove={() => onChange({ ...value, ids: value.ids.filter((x) => x !== id) })}
>
{term ? name(term) : id}
);
return term?.status === "proposed" ? (
{chip}
) : (
chip
);
})}
)}
);
}