feat(tcm-patterns): dynamically order detail sections by template presentation contracts
This commit is contained in:
parent
53c6077419
commit
f267674f78
|
|
@ -1,25 +1,60 @@
|
|||
import React from "react";
|
||||
import React, { useMemo } from "react";
|
||||
import { ScrollView, StyleSheet } from "react-native";
|
||||
|
||||
import type { DetailRendererProps } from "../../types/renderer.types";
|
||||
import { asString, humanizeKey } from "../helpers/value";
|
||||
import {
|
||||
findAttributePresentationRule,
|
||||
isEmptyValue,
|
||||
presentationDisplayOrder,
|
||||
} from "../../utils/attribute-presentation";
|
||||
import { ContentLinkAttributeSections } from "../shared/ContentLinkAttributeSections";
|
||||
import { ContentValue } from "../shared/ContentValue";
|
||||
import { Hero } from "../shared/Hero";
|
||||
import { SectionCard } from "../shared/SectionCard";
|
||||
|
||||
const EXCLUDED_KEYS = new Set(["code"]);
|
||||
const RESERVED_KEYS = new Set([
|
||||
"id",
|
||||
"name",
|
||||
"title",
|
||||
"code",
|
||||
"sort_order",
|
||||
"link",
|
||||
]);
|
||||
|
||||
export function TcmPatternDetailRenderer({
|
||||
content,
|
||||
data,
|
||||
definitions,
|
||||
presentationAttributes,
|
||||
}: DetailRendererProps) {
|
||||
const title =
|
||||
const rawTitle =
|
||||
asString(data.name) ||
|
||||
asString(data.title) ||
|
||||
content.title ||
|
||||
"TCM Pattern";
|
||||
const title = humanizeKey(rawTitle);
|
||||
|
||||
const orderedSections = useMemo(() => {
|
||||
const definitionByLabel = new Map(
|
||||
(definitions ?? []).map((d) => [d.code, d.label]),
|
||||
);
|
||||
|
||||
return Object.entries(data)
|
||||
.filter(
|
||||
([key, value]) =>
|
||||
!RESERVED_KEYS.has(key) &&
|
||||
!isEmptyValue(value),
|
||||
)
|
||||
.map(([key, value]) => {
|
||||
const rule = findAttributePresentationRule(key, presentationAttributes);
|
||||
const order = presentationDisplayOrder(rule);
|
||||
const rawLabel = definitionByLabel.get(key) || key;
|
||||
const label = humanizeKey(rawLabel);
|
||||
return { key, value, order, label };
|
||||
})
|
||||
.sort((a, b) => a.order - b.order || a.label.localeCompare(b.label));
|
||||
}, [data, definitions, presentationAttributes]);
|
||||
|
||||
return (
|
||||
<ScrollView
|
||||
|
|
@ -28,13 +63,11 @@ export function TcmPatternDetailRenderer({
|
|||
>
|
||||
<Hero title={title} subtitle={content.summary ?? undefined} />
|
||||
|
||||
{Object.entries(data)
|
||||
.filter(([key]) => !EXCLUDED_KEYS.has(key))
|
||||
.map(([key, value]) => (
|
||||
<SectionCard key={key} title={humanizeKey(key)}>
|
||||
<ContentValue value={value} />
|
||||
</SectionCard>
|
||||
))}
|
||||
{orderedSections.map(({ key, value, label }) => (
|
||||
<SectionCard key={key} title={label}>
|
||||
<ContentValue value={value} />
|
||||
</SectionCard>
|
||||
))}
|
||||
|
||||
<ContentLinkAttributeSections
|
||||
data={data}
|
||||
|
|
|
|||
Loading…
Reference in New Issue