From 8efd3cce494d32525e71109e3de25b21392f67f1 Mon Sep 17 00:00:00 2001 From: ShivaniKhaire Date: Wed, 14 Oct 2026 11:27:39 +0530 Subject: [PATCH] feat(renderers): make meridian detail renderer generic and data-driven --- .../details/MeridianDetailRenderer.tsx | 190 ++++++++++++------ 1 file changed, 132 insertions(+), 58 deletions(-) diff --git a/src/features/content/renderers/details/MeridianDetailRenderer.tsx b/src/features/content/renderers/details/MeridianDetailRenderer.tsx index 6eb7894..d73a867 100644 --- a/src/features/content/renderers/details/MeridianDetailRenderer.tsx +++ b/src/features/content/renderers/details/MeridianDetailRenderer.tsx @@ -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(); + + 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 ( + + + + + + + ); + } + + if (key === "luo_connecting_channel") { + return ( + <> + + {isPresent(data.luo_connecting_vessel) ? ( + + ) : null} + + ); + } + + if (Array.isArray(value) && value.every((item) => typeof item === "string")) { + return ; + } + + if (value !== null && typeof value === "object" && !Array.isArray(value)) { + return ; + } + + // Specialized-field fallback: any unexpected runtime shape is still + // rendered through the generic reference-aware value renderer. + return ; + }; + return ( - - - - - - - - - - - - - - - - - - - - - - - - - - {hasMeridianPoints ? ( - - - - - - - + {orderedAttributes.map((attribute) => ( + + {renderAttributeValue(attribute.key, attribute.value, attribute.emptyText)} - ) : null} - - - - - - + ))} ); }