feat(renderers): make meridian detail renderer generic and data-driven
This commit is contained in:
parent
b93b8f672f
commit
8efd3cce49
|
|
@ -2,27 +2,33 @@ import React, { useMemo } from "react";
|
||||||
import { ScrollView, StyleSheet, Text, View } from "react-native";
|
import { ScrollView, StyleSheet, Text, View } from "react-native";
|
||||||
|
|
||||||
import type { DetailRendererProps } from "../../types/renderer.types";
|
import type { DetailRendererProps } from "../../types/renderer.types";
|
||||||
import { asRecord, asString, asStringArray } from "../helpers/value";
|
import {
|
||||||
import { ContentLinkAttributeSections } from "../shared/ContentLinkAttributeSections";
|
bareAttributeKey,
|
||||||
|
findAttributePresentationRule,
|
||||||
|
getEmptyDisplayText,
|
||||||
|
presentationDisplayOrder,
|
||||||
|
shouldRenderAttribute,
|
||||||
|
} from "../../utils/attribute-presentation";
|
||||||
|
import { asRecord, asString, asStringArray, humanizeKey } from "../helpers/value";
|
||||||
|
import { BulletList } from "../shared/BulletList";
|
||||||
import { ContentValue } from "../shared/ContentValue";
|
import { ContentValue } from "../shared/ContentValue";
|
||||||
import { Hero } from "../shared/Hero";
|
import { Hero } from "../shared/Hero";
|
||||||
import { ImageGallery } from "../shared/ImageGallery";
|
import { ImageGallery } from "../shared/ImageGallery";
|
||||||
import { SectionCard } from "../shared/SectionCard";
|
|
||||||
import { BulletList } from "../shared/BulletList";
|
|
||||||
import { ObjectSections } from "../shared/ObjectSections";
|
import { ObjectSections } from "../shared/ObjectSections";
|
||||||
|
import { SectionCard } from "../shared/SectionCard";
|
||||||
|
|
||||||
const GROUPED_POINT_ATTRIBUTE_KEYS = [
|
const RESERVED_KEYS = new Set([
|
||||||
"meeting_points",
|
"name",
|
||||||
|
"title",
|
||||||
|
"code",
|
||||||
|
"sort_order",
|
||||||
|
"link",
|
||||||
|
"images",
|
||||||
|
"luo_connecting_vessel",
|
||||||
"point_categories",
|
"point_categories",
|
||||||
"specific_points_according_to_functions",
|
"specific_points_according_to_functions",
|
||||||
"five_shu_transporting_points",
|
"five_shu_transporting_points",
|
||||||
] as const;
|
]);
|
||||||
|
|
||||||
const RENDERED_REFERENCE_ATTRIBUTE_KEYS = [
|
|
||||||
...GROUPED_POINT_ATTRIBUTE_KEYS,
|
|
||||||
"luo_connecting_vessel",
|
|
||||||
"link",
|
|
||||||
] as const;
|
|
||||||
|
|
||||||
export function MeridianDetailRenderer({
|
export function MeridianDetailRenderer({
|
||||||
content,
|
content,
|
||||||
|
|
@ -50,6 +56,114 @@ export function MeridianDetailRenderer({
|
||||||
isPresent(mergedSpecificPoints) ||
|
isPresent(mergedSpecificPoints) ||
|
||||||
isPresent(mergedFiveShu);
|
isPresent(mergedFiveShu);
|
||||||
|
|
||||||
|
const orderedAttributes = useMemo(() => {
|
||||||
|
const rootDefinitions = definitions.filter((definition) => definition.is_root);
|
||||||
|
const definitionByBareKey = new Map(
|
||||||
|
rootDefinitions.map((definition) => [
|
||||||
|
definition.code || bareAttributeKey(definition.attribute_key),
|
||||||
|
definition,
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
const contractIndex = new Map(
|
||||||
|
presentationAttributes.map((rule, index) => [rule.attribute_key, index]),
|
||||||
|
);
|
||||||
|
|
||||||
|
const keys: string[] = [];
|
||||||
|
const seen = new Set<string>();
|
||||||
|
|
||||||
|
for (const definition of rootDefinitions) {
|
||||||
|
const key = definition.code || bareAttributeKey(definition.attribute_key);
|
||||||
|
if (!key || seen.has(key)) continue;
|
||||||
|
seen.add(key);
|
||||||
|
keys.push(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Backward-compatible fallback when definitions are incomplete: add only
|
||||||
|
// contract attributes that correspond to reconstructed root data keys.
|
||||||
|
for (const rule of presentationAttributes) {
|
||||||
|
const key = bareAttributeKey(rule.attribute_key);
|
||||||
|
if (!key || seen.has(key) || !Object.prototype.hasOwnProperty.call(data, key)) continue;
|
||||||
|
seen.add(key);
|
||||||
|
keys.push(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
return keys
|
||||||
|
.flatMap((key) => {
|
||||||
|
if (RESERVED_KEYS.has(key)) return [];
|
||||||
|
|
||||||
|
const value = data[key];
|
||||||
|
const isMeetingPointsKey = key === "meeting_points";
|
||||||
|
|
||||||
|
// For meeting_points (Meridian Points), check if any point grouping exists
|
||||||
|
if (isMeetingPointsKey && !hasMeridianPoints) return [];
|
||||||
|
|
||||||
|
const definition = definitionByBareKey.get(key);
|
||||||
|
const rule = findAttributePresentationRule(
|
||||||
|
definition?.attribute_key ?? key,
|
||||||
|
presentationAttributes,
|
||||||
|
);
|
||||||
|
|
||||||
|
// If it's meeting_points and we have meridian points, ensure it's not discarded as empty
|
||||||
|
const effectiveValue =
|
||||||
|
isMeetingPointsKey && hasMeridianPoints
|
||||||
|
? isPresent(value)
|
||||||
|
? value
|
||||||
|
: ["__meridian_points__"]
|
||||||
|
: value;
|
||||||
|
|
||||||
|
if (!shouldRenderAttribute(effectiveValue, rule)) return [];
|
||||||
|
|
||||||
|
const label =
|
||||||
|
isMeetingPointsKey
|
||||||
|
? "Meridian Points"
|
||||||
|
: key === "indications_clinical_manifestations"
|
||||||
|
? "Clinical Manifestations"
|
||||||
|
: definition?.label?.trim() || humanizeKey(key);
|
||||||
|
const emptyText = getEmptyDisplayText(effectiveValue, rule);
|
||||||
|
const index = rule ? contractIndex.get(rule.attribute_key) ?? 0 : 0;
|
||||||
|
const order = presentationDisplayOrder(rule, definition?.sort_order, index);
|
||||||
|
|
||||||
|
return [{ key, label, value, emptyText, order }];
|
||||||
|
})
|
||||||
|
.sort((a, b) => a.order - b.order || a.label.localeCompare(b.label));
|
||||||
|
}, [data, definitions, hasMeridianPoints, presentationAttributes]);
|
||||||
|
|
||||||
|
const renderAttributeValue = (key: string, value: unknown, emptyText: string | null) => {
|
||||||
|
if (key === "meeting_points") {
|
||||||
|
return (
|
||||||
|
<View style={styles.groupStack}>
|
||||||
|
<PointGroup label="Meeting Points" value={data.meeting_points} />
|
||||||
|
<PointGroup label="Point Categories" value={pointCategories} />
|
||||||
|
<PointGroup label="Specific Points" value={mergedSpecificPoints} />
|
||||||
|
<PointGroup label="Five-Shu Transporting Points" value={mergedFiveShu} />
|
||||||
|
</View>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (key === "luo_connecting_channel") {
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<BulletList items={asStringArray(value)} />
|
||||||
|
{isPresent(data.luo_connecting_vessel) ? (
|
||||||
|
<ObjectSections value={asRecord(data.luo_connecting_vessel)} />
|
||||||
|
) : null}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Array.isArray(value) && value.every((item) => typeof item === "string")) {
|
||||||
|
return <BulletList items={asStringArray(value)} />;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value !== null && typeof value === "object" && !Array.isArray(value)) {
|
||||||
|
return <ObjectSections value={asRecord(value)} />;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Specialized-field fallback: any unexpected runtime shape is still
|
||||||
|
// rendered through the generic reference-aware value renderer.
|
||||||
|
return <ContentValue value={value} emptyText={emptyText ?? undefined} />;
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ScrollView
|
<ScrollView
|
||||||
contentContainerStyle={{
|
contentContainerStyle={{
|
||||||
|
|
@ -58,56 +172,16 @@ export function MeridianDetailRenderer({
|
||||||
paddingBottom: 40,
|
paddingBottom: 40,
|
||||||
gap: 14,
|
gap: 14,
|
||||||
}}
|
}}
|
||||||
|
showsVerticalScrollIndicator={false}
|
||||||
>
|
>
|
||||||
<Hero title={asString(data.title) || content.title} subtitle={asString(data.name)} />
|
<Hero title={asString(data.title) || content.title} subtitle={asString(data.name)} />
|
||||||
<ImageGallery images={[]} />
|
<ImageGallery images={[]} />
|
||||||
|
|
||||||
<SectionCard title="Primary Channel Pathway">
|
{orderedAttributes.map((attribute) => (
|
||||||
<BulletList items={asStringArray(data.primary_channel_pathway)} />
|
<SectionCard key={attribute.key} title={attribute.label}>
|
||||||
</SectionCard>
|
{renderAttributeValue(attribute.key, attribute.value, attribute.emptyText)}
|
||||||
|
|
||||||
<SectionCard title="Secondary Branches">
|
|
||||||
<ObjectSections value={asRecord(data.secondary_branches)} />
|
|
||||||
</SectionCard>
|
|
||||||
|
|
||||||
<SectionCard title="Divergent Channel">
|
|
||||||
<BulletList items={asStringArray(data.divergent_channel)} />
|
|
||||||
</SectionCard>
|
|
||||||
|
|
||||||
<SectionCard title="Luo-Connecting Channel">
|
|
||||||
<BulletList items={asStringArray(data.luo_connecting_channel)} />
|
|
||||||
<ObjectSections value={asRecord(data.luo_connecting_vessel)} />
|
|
||||||
</SectionCard>
|
|
||||||
|
|
||||||
<SectionCard title="Sinew Channel">
|
|
||||||
<BulletList items={asStringArray(data.sinew_channel)} />
|
|
||||||
</SectionCard>
|
|
||||||
|
|
||||||
<SectionCard title="Symptoms">
|
|
||||||
<ObjectSections value={asRecord(data.symptoms)} />
|
|
||||||
</SectionCard>
|
|
||||||
|
|
||||||
{hasMeridianPoints ? (
|
|
||||||
<SectionCard title="Meridian Points">
|
|
||||||
<View style={styles.groupStack}>
|
|
||||||
<PointGroup label="Meeting Points" value={data.meeting_points} />
|
|
||||||
<PointGroup label="Point Categories" value={pointCategories} />
|
|
||||||
<PointGroup label="Specific Points" value={mergedSpecificPoints} />
|
|
||||||
<PointGroup label="Five-Shu Transporting Points" value={mergedFiveShu} />
|
|
||||||
</View>
|
|
||||||
</SectionCard>
|
</SectionCard>
|
||||||
) : null}
|
))}
|
||||||
|
|
||||||
<SectionCard title="Clinical Manifestations">
|
|
||||||
<BulletList items={asStringArray(data.indications_clinical_manifestations)} />
|
|
||||||
</SectionCard>
|
|
||||||
|
|
||||||
<ContentLinkAttributeSections
|
|
||||||
data={data}
|
|
||||||
definitions={definitions}
|
|
||||||
presentationAttributes={presentationAttributes}
|
|
||||||
excludeAttributeKeys={RENDERED_REFERENCE_ATTRIBUTE_KEYS}
|
|
||||||
/>
|
|
||||||
</ScrollView>
|
</ScrollView>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue