acupedia-mobile-ui/src/features/content/renderers/details/TcmPatternDetailRenderer.tsx

90 lines
2.3 KiB
TypeScript

import React, { useMemo } from "react";
import { ScrollView, StyleSheet } from "react-native";
import type { DetailRendererProps } from "../../types/renderer.types";
import {
asString,
buildDefinitionLabelMap,
humanizeKey,
resolveAttributeLabel,
} 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 RESERVED_KEYS = new Set([
"id",
"name",
"title",
"code",
"sort_order",
"link",
]);
export function TcmPatternDetailRenderer({
content,
data,
definitions,
presentationAttributes,
}: DetailRendererProps) {
const rawTitle =
asString(data.name) ||
asString(data.title) ||
content.title ||
"TCM Pattern";
const title = humanizeKey(rawTitle);
const orderedSections = useMemo(() => {
const definitionByLabel = buildDefinitionLabelMap(definitions);
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 label = resolveAttributeLabel(key, definitionByLabel);
return { key, value, order, label };
})
.sort((a, b) => a.order - b.order || a.label.localeCompare(b.label));
}, [data, definitions, presentationAttributes]);
return (
<ScrollView
contentContainerStyle={styles.content}
showsVerticalScrollIndicator={false}
>
<Hero title={title} subtitle={content.summary ?? undefined} />
{orderedSections.map(({ key, value, label }) => (
<SectionCard key={key} title={label}>
<ContentValue value={value} />
</SectionCard>
))}
<ContentLinkAttributeSections
data={data}
presentationAttributes={presentationAttributes}
/>
</ScrollView>
);
}
const styles = StyleSheet.create({
content: {
paddingHorizontal: 18,
paddingTop: 16,
paddingBottom: 40,
gap: 14,
},
});