import type { ReactNode } from "react"; import { Link, useParams } from "react-router-dom"; import { useTranslation } from "react-i18next"; import type { components } from "../api/schema"; import { useObject, useFieldDefinitions } from "../api/queries"; import { formatDate } from "../lib/format-date"; import { DeleteObjectDialog } from "./delete-object-dialog"; import { FlexibleFieldValue } from "./flexible-field-value"; import { PublishControl } from "./publish-control"; import { VisibilityBadge } from "./visibility-badge"; import { buttonVariants } from "@/components/ui/button"; import { Skeleton } from "@/components/ui/skeleton"; type FieldDefinitionView = components["schemas"]["FieldDefinitionView"]; function Field({ label, value }: { label: string; value: ReactNode }) { const empty = value === null || value === undefined || value === ""; return (
{t("objects.loadError")}
; if (!object) return{t("objects.notFound")}
; // Prefer the active locale's label, then English, then the raw key. const lang = i18n.language.startsWith("sv") ? "sv" : "en"; const labelFor = (key: string) => { const labels = definitions?.find((d) => d.key === key)?.labels; const byLang = labels?.find((l) => l.lang === lang)?.label; const byEnglish = labels?.find((l) => l.lang === "en")?.label; return byLang ?? byEnglish ?? key; }; // Iterate definitions (stable order) and keep only defs whose key has a // non-null value on this object, grouped by def.group. Ungrouped defs fall // into a trailing "Other" group. const other = t("fields.other"); const present = (definitions ?? []).filter((d) => object.fields[d.key] != null); const groups: { group: string; defs: FieldDefinitionView[] }[] = []; for (const def of present) { const isOther = !def.group?.trim(); const group = isOther ? other : def.group!; let bucket = groups.find((x) => x.group === group); if (!bucket) { bucket = { group, defs: [] }; groups.push(bucket); } bucket.defs.push(def); } // Defensive: a key present in object.fields with no matching definition (e.g. a // definition removed after the value was set). Render it muted under "Other" // rather than silently dropping data; the raw key is the label. const definedKeys = new Set((definitions ?? []).map((d) => d.key)); const orphans = Object.entries(object.fields).filter( ([key, value]) => !definedKeys.has(key) && value != null, ); if (orphans.length > 0 && !groups.some((g) => g.group === other)) { groups.push({ group: other, defs: [] }); } groups.sort((a, b) => Number(a.group === other) - Number(b.group === other)); return (