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 { useLang } from "../lib/use-lang"; import { groupDefinitions } from "../lib/group-fields"; import { formatDate } from "../lib/format-date"; import { useDocumentTitle } from "../lib/use-document-title"; import { useBreadcrumb } from "../shell/use-breadcrumb"; 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 AdminObjectView = components["schemas"]["AdminObjectView"]; function Field({ label, value }: { label: string; value: ReactNode }) { const empty = value === null || value === undefined || value === ""; return (
{label}
{empty ? "—" : value}
); } export function ObjectDetail() { const { t } = useTranslation(); const { id } = useParams(); const { data: object, isLoading, isError } = useObject(id!); if (isLoading) { return (
); } if (isError) return

{t("objects.loadError")}

; if (!object) return

{t("objects.notFound")}

; return ; } function ObjectDetailLoaded({ object }: { object: AdminObjectView }) { const { t } = useTranslation(); const { data: definitions } = useFieldDefinitions(); useDocumentTitle(object.object_number); useBreadcrumb([{ label: t("nav.objects"), to: "/objects" }, { label: object.object_number }]); // Prefer the active locale's label, then English, then the raw key. const lang = useLang(); 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 = groupDefinitions(present, other); // 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: [] }); } return (

{object.object_name}

{t("actions.edit")}
{groups.map((g) => (
{g.group}
{g.defs.map((d) => ( } /> ))} {g.group === other && orphans.map(([key, value]) => ( {typeof value === "object" ? JSON.stringify(value) : String(value)} } /> ))}
))}
); }