refactor(web): extract groupDefinitions helper; object-detail uses it (#45)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-08 13:58:12 +02:00
parent c0c86a5859
commit a9a0c4d477
3 changed files with 74 additions and 13 deletions
+4 -13
View File
@@ -4,6 +4,7 @@ import { useTranslation } from "react-i18next";
import type { components } from "../api/schema";
import { useObject, useFieldDefinitions } from "../api/queries";
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";
@@ -14,7 +15,6 @@ import { VisibilityBadge } from "./visibility-badge";
import { buttonVariants } from "@/components/ui/button";
import { Skeleton } from "@/components/ui/skeleton";
type FieldDefinitionView = components["schemas"]["FieldDefinitionView"];
type AdminObjectView = components["schemas"]["AdminObjectView"];
function Field({ label, value }: { label: string; value: ReactNode }) {
@@ -70,17 +70,8 @@ function ObjectDetailLoaded({ object }: { object: AdminObjectView }) {
// 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);
}
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.
@@ -88,10 +79,10 @@ function ObjectDetailLoaded({ object }: { object: AdminObjectView }) {
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 (
<div className="overflow-auto p-4">