feat(renderers): make meridian detail renderer generic and data-driven

This commit is contained in:
ShivaniKhaire 2026-10-14 11:27:39 +05:30
parent b93b8f672f
commit 8efd3cce49
1 changed files with 132 additions and 58 deletions

View File

@ -2,27 +2,33 @@ import React, { useMemo } from "react";
import { ScrollView, StyleSheet, Text, View } from "react-native";
import type { DetailRendererProps } from "../../types/renderer.types";
import { asRecord, asString, asStringArray } from "../helpers/value";
import { ContentLinkAttributeSections } from "../shared/ContentLinkAttributeSections";
import {
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 { Hero } from "../shared/Hero";
import { ImageGallery } from "../shared/ImageGallery";
import { SectionCard } from "../shared/SectionCard";
import { BulletList } from "../shared/BulletList";
import { ObjectSections } from "../shared/ObjectSections";
import { SectionCard } from "../shared/SectionCard";
const GROUPED_POINT_ATTRIBUTE_KEYS = [
"meeting_points",
const RESERVED_KEYS = new Set([
"name",
"title",
"code",
"sort_order",
"link",
"images",
"luo_connecting_vessel",
"point_categories",
"specific_points_according_to_functions",
"five_shu_transporting_points",
] as const;
const RENDERED_REFERENCE_ATTRIBUTE_KEYS = [
...GROUPED_POINT_ATTRIBUTE_KEYS,
"luo_connecting_vessel",
"link",
] as const;
]);
export function MeridianDetailRenderer({
content,
@ -50,6 +56,114 @@ export function MeridianDetailRenderer({
isPresent(mergedSpecificPoints) ||
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 (
<ScrollView
contentContainerStyle={{
@ -58,56 +172,16 @@ export function MeridianDetailRenderer({
paddingBottom: 40,
gap: 14,
}}
showsVerticalScrollIndicator={false}
>
<Hero title={asString(data.title) || content.title} subtitle={asString(data.name)} />
<ImageGallery images={[]} />
<SectionCard title="Primary Channel Pathway">
<BulletList items={asStringArray(data.primary_channel_pathway)} />
</SectionCard>
<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>
{orderedAttributes.map((attribute) => (
<SectionCard key={attribute.key} title={attribute.label}>
{renderAttributeValue(attribute.key, attribute.value, attribute.emptyText)}
</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>
);
}