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 { ScrollView, StyleSheet } from "react-native";
|
||||||
|
|
||||||
import type { DetailRendererProps } from "../../types/renderer.types";
|
import type { DetailRendererProps } from "../../types/renderer.types";
|
||||||
import { asString, humanizeKey } from "../helpers/value";
|
import { asString, humanizeKey } from "../helpers/value";
|
||||||
|
import {
|
||||||
|
findAttributePresentationRule,
|
||||||
|
isEmptyValue,
|
||||||
|
presentationDisplayOrder,
|
||||||
|
} from "../../utils/attribute-presentation";
|
||||||
import { ContentLinkAttributeSections } from "../shared/ContentLinkAttributeSections";
|
import { ContentLinkAttributeSections } from "../shared/ContentLinkAttributeSections";
|
||||||
import { ContentValue } from "../shared/ContentValue";
|
import { ContentValue } from "../shared/ContentValue";
|
||||||
import { Hero } from "../shared/Hero";
|
import { Hero } from "../shared/Hero";
|
||||||
import { SectionCard } from "../shared/SectionCard";
|
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({
|
export function TcmPatternDetailRenderer({
|
||||||
content,
|
content,
|
||||||
data,
|
data,
|
||||||
|
definitions,
|
||||||
presentationAttributes,
|
presentationAttributes,
|
||||||
}: DetailRendererProps) {
|
}: DetailRendererProps) {
|
||||||
const title =
|
const rawTitle =
|
||||||
asString(data.name) ||
|
asString(data.name) ||
|
||||||
asString(data.title) ||
|
asString(data.title) ||
|
||||||
content.title ||
|
content.title ||
|
||||||
"TCM Pattern";
|
"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 (
|
return (
|
||||||
<ScrollView
|
<ScrollView
|
||||||
|
|
@ -28,13 +63,11 @@ export function TcmPatternDetailRenderer({
|
||||||
>
|
>
|
||||||
<Hero title={title} subtitle={content.summary ?? undefined} />
|
<Hero title={title} subtitle={content.summary ?? undefined} />
|
||||||
|
|
||||||
{Object.entries(data)
|
{orderedSections.map(({ key, value, label }) => (
|
||||||
.filter(([key]) => !EXCLUDED_KEYS.has(key))
|
<SectionCard key={key} title={label}>
|
||||||
.map(([key, value]) => (
|
<ContentValue value={value} />
|
||||||
<SectionCard key={key} title={humanizeKey(key)}>
|
</SectionCard>
|
||||||
<ContentValue value={value} />
|
))}
|
||||||
</SectionCard>
|
|
||||||
))}
|
|
||||||
|
|
||||||
<ContentLinkAttributeSections
|
<ContentLinkAttributeSections
|
||||||
data={data}
|
data={data}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue