177 lines
5.9 KiB
TypeScript
177 lines
5.9 KiB
TypeScript
import React, { useMemo } from "react";
|
|
import { ScrollView, Text } from "react-native";
|
|
|
|
import type { DetailRendererProps } from "../../types/renderer.types";
|
|
import {
|
|
bareAttributeKey,
|
|
findAttributePresentationRule,
|
|
getEmptyDisplayText,
|
|
presentationDisplayOrder,
|
|
shouldRenderAttribute,
|
|
} from "../../utils/attribute-presentation";
|
|
import { asRecord, asRecordArray, asString, asStringArray, humanizeKey } from "../helpers/value";
|
|
import { BulletList } from "../shared/BulletList";
|
|
import { ChipList } from "../shared/ChipList";
|
|
import { ContentValue } from "../shared/ContentValue";
|
|
import { Hero } from "../shared/Hero";
|
|
import { ImageGallery } from "../shared/ImageGallery";
|
|
import { KeyValueGrid } from "../shared/KeyValueGrid";
|
|
import { ObjectSections } from "../shared/ObjectSections";
|
|
import { SectionCard } from "../shared/SectionCard";
|
|
|
|
const RESERVED_KEYS = new Set([
|
|
"point_name",
|
|
"name",
|
|
"title",
|
|
"alt_name_1",
|
|
"alt_name_2",
|
|
"point_images",
|
|
"images",
|
|
"code",
|
|
"sort_order",
|
|
"link",
|
|
]);
|
|
|
|
function isStringArray(value: unknown): value is string[] {
|
|
return Array.isArray(value) && value.every((item) => typeof item === "string");
|
|
}
|
|
|
|
function hasRenderableMethodValues(value: unknown): boolean {
|
|
const record = asRecord(value);
|
|
return ["needling", "needling_response", "moxibustion", "additional_method"].some(
|
|
(key) => typeof record[key] === "string" && record[key].trim().length > 0,
|
|
);
|
|
}
|
|
|
|
export function AcuPointDetailRenderer({
|
|
content,
|
|
data,
|
|
definitions,
|
|
presentationAttributes,
|
|
}: DetailRendererProps) {
|
|
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 definition = definitionByBareKey.get(key);
|
|
const rule = findAttributePresentationRule(definition?.attribute_key ?? key, presentationAttributes);
|
|
if (!shouldRenderAttribute(value, rule)) return [];
|
|
|
|
const label = definition?.label?.trim() || humanizeKey(key);
|
|
const emptyText = getEmptyDisplayText(value, 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, presentationAttributes]);
|
|
|
|
const renderAttributeValue = (key: string, value: unknown, emptyText: string | null) => {
|
|
if (key === "point_type" && isStringArray(value)) {
|
|
return <ChipList items={asStringArray(value)} />;
|
|
}
|
|
|
|
if ((key === "indications" || key === "actions") && isStringArray(value)) {
|
|
return <BulletList items={asStringArray(value)} />;
|
|
}
|
|
|
|
if ((key === "channel" || key === "location" || key === "location_note") && typeof value === "string") {
|
|
return (
|
|
<Text style={{ fontSize: 16, lineHeight: 25, color: key === "location_note" ? "#64748B" : "#334155" }}>
|
|
{value}
|
|
</Text>
|
|
);
|
|
}
|
|
|
|
if (key === "caution" && typeof value === "string") {
|
|
return (
|
|
<Text style={{ fontSize: 16, lineHeight: 25, color: "#6B3E3E" }}>
|
|
{value}
|
|
</Text>
|
|
);
|
|
}
|
|
|
|
if (key === "regional_anatomy" && value !== null && typeof value === "object" && !Array.isArray(value)) {
|
|
return <ObjectSections value={asRecord(value)} />;
|
|
}
|
|
|
|
if (key === "methods" && hasRenderableMethodValues(value)) {
|
|
const methods = asRecord(value);
|
|
return (
|
|
<KeyValueGrid
|
|
rows={[
|
|
{ label: "Needling", value: asString(methods.needling) },
|
|
{ label: "Response", value: asString(methods.needling_response) },
|
|
{ label: "Moxibustion", value: asString(methods.moxibustion) },
|
|
{ label: "Additional", value: asString(methods.additional_method) },
|
|
]}
|
|
/>
|
|
);
|
|
}
|
|
|
|
// 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={{
|
|
paddingHorizontal: 18,
|
|
paddingTop: 16,
|
|
paddingBottom: 40,
|
|
gap: 14,
|
|
}}
|
|
showsVerticalScrollIndicator={false}
|
|
>
|
|
<Hero
|
|
title={asString(data.point_name) || asString(data.name) || content.title}
|
|
subtitle={[asString(data.alt_name_1), asString(data.alt_name_2)]
|
|
.filter(Boolean)
|
|
.join(" · ")}
|
|
/>
|
|
|
|
<ImageGallery images={asRecordArray(data.point_images)} />
|
|
|
|
{orderedAttributes.map((attribute) => (
|
|
<SectionCard key={attribute.key} title={attribute.label}>
|
|
{renderAttributeValue(attribute.key, attribute.value, attribute.emptyText)}
|
|
</SectionCard>
|
|
))}
|
|
</ScrollView>
|
|
);
|
|
}
|