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

242 lines
6.9 KiB
TypeScript

import React from "react";
import { ScrollView, StyleSheet, Text, View } from "react-native";
import type { DetailRendererProps } from "../../types/renderer.types";
import {
findAttributePresentationRule,
isEmptyValue,
presentationDisplayOrder,
} from "../../utils/attribute-presentation";
import { asRecordArray, asString, asStringArray } from "../helpers/value";
import { BulletList } from "../shared/BulletList";
import { ChipList } from "../shared/ChipList";
import { ContentLinkAttributeSections } from "../shared/ContentLinkAttributeSections";
import { ContentValue } from "../shared/ContentValue";
import { Hero } from "../shared/Hero";
import { ImageGallery } from "../shared/ImageGallery";
import { ReferenceChipList } from "../shared/ReferenceChipList";
import { SectionCard } from "../shared/SectionCard";
const POINT_RELATIONSHIP_KEYS = [
"master_point",
"coupled_point",
"luo_point",
"xi_cleft_point",
"intersection_points",
"paired_meridian",
] as const;
interface RelationshipRowProps {
label: string;
value: unknown;
chips?: boolean;
}
function RelationshipRow({ label, value, chips = false }: RelationshipRowProps) {
if (isEmptyValue(value)) return null;
return (
<View style={styles.relationshipRow}>
<Text style={styles.relationshipLabel}>{label}</Text>
{chips ? <ReferenceChipList value={value} /> : <ContentValue value={value} />}
</View>
);
}
export function EovDetailRenderer({
content,
data,
definitions,
presentationAttributes,
}: DetailRendererProps) {
const definitionByKey = React.useMemo(() => {
const map = new Map<string, (typeof definitions)[number]>();
for (const definition of definitions) {
map.set(definition.attribute_key, definition);
map.set(definition.code, definition);
}
return map;
}, [definitions]);
const orderFor = React.useCallback(
(key: string, fallbackIndex = 0) => {
const rule = findAttributePresentationRule(key, presentationAttributes);
const definition = definitionByKey.get(key);
return presentationDisplayOrder(rule, definition?.sort_order, fallbackIndex);
},
[definitionByKey, presentationAttributes],
);
const relationshipRows = React.useMemo(
() => [
{ key: "master_point", label: "Master Point", value: data.master_point },
{ key: "coupled_point", label: "Coupled Point", value: data.coupled_point },
{ key: "luo_point", label: "Luo Point", value: data.luo_point },
{ key: "xi_cleft_point", label: "Xi-Cleft Point", value: data.xi_cleft_point },
{
key: "intersection_points",
label: "Intersection Points",
value: data.intersection_points,
chips: true,
},
{ key: "paired_meridian", label: "Paired Meridian", value: data.paired_meridian },
]
.filter((row) => !isEmptyValue(row.value))
.sort((a, b) => orderFor(a.key) - orderFor(b.key)),
[data, orderFor],
);
const sections = React.useMemo(() => {
const items: Array<{
key: string;
order: number;
render: () => React.ReactNode;
}> = [];
if (relationshipRows.length > 0) {
items.push({
key: "point_relationships",
order: Math.min(...relationshipRows.map((row) => orderFor(row.key))),
render: () => (
<SectionCard title="Point Relationships">
{relationshipRows.map((row) => (
<RelationshipRow
key={row.key}
label={row.label}
value={row.value}
chips={row.chips}
/>
))}
</SectionCard>
),
});
}
if (!isEmptyValue(data.areas_of_effect)) {
items.push({
key: "areas_of_effect",
order: orderFor("areas_of_effect", 100),
render: () => (
<SectionCard title="Areas of Effect">
<ChipList items={asStringArray(data.areas_of_effect)} />
</SectionCard>
),
});
}
if (!isEmptyValue(data.functions)) {
items.push({
key: "functions",
order: orderFor("functions", 110),
render: () => (
<SectionCard title="Functions">
<BulletList items={asStringArray(data.functions)} />
</SectionCard>
),
});
}
if (!isEmptyValue(data.pathway)) {
items.push({
key: "pathway",
order: orderFor("pathway", 120),
render: () => (
<SectionCard title="Pathway">
<Text style={styles.pathway}>{asString(data.pathway)}</Text>
</SectionCard>
),
});
}
const clinicalManifestations = data.clinical_manifestation ?? data.clinical_manifestations;
if (!isEmptyValue(clinicalManifestations)) {
items.push({
key: "clinical_manifestation",
order: orderFor("clinical_manifestation", orderFor("clinical_manifestations", 125)),
render: () => (
<SectionCard title="Clinical Manifestations">
<BulletList items={asStringArray(clinicalManifestations)} />
</SectionCard>
),
});
}
if (!isEmptyValue(data.connecting_channel)) {
items.push({
key: "connecting_channel",
order: orderFor("connecting_channel", 130),
render: () => (
<SectionCard title="Connecting Channel">
<Text style={styles.pathway}>{asString(data.connecting_channel)}</Text>
</SectionCard>
),
});
}
if (!isEmptyValue(data.treatment)) {
items.push({
key: "treatment",
order: orderFor("treatment", 140),
render: () => (
<SectionCard title="Treatment">
<Text style={styles.pathway}>{asString(data.treatment)}</Text>
</SectionCard>
),
});
}
return items.sort((a, b) => a.order - b.order || a.key.localeCompare(b.key));
}, [data, orderFor, relationshipRows]);
return (
<ScrollView
contentContainerStyle={{
paddingHorizontal: 18,
paddingTop: 16,
paddingBottom: 40,
gap: 14,
}}
>
<Hero
title={asString(data.name) || content.title}
subtitle={asStringArray(data.alt_name).join(" · ")}
/>
<ImageGallery images={asRecordArray(data.images)} />
{sections.map((section) => (
<React.Fragment key={section.key}>{section.render()}</React.Fragment>
))}
<ContentLinkAttributeSections
data={data}
definitions={definitions}
presentationAttributes={presentationAttributes}
excludeAttributeKeys={POINT_RELATIONSHIP_KEYS}
/>
</ScrollView>
);
}
const styles = StyleSheet.create({
relationshipRow: {
gap: 5,
paddingBottom: 10,
borderBottomWidth: StyleSheet.hairlineWidth,
borderBottomColor: "#E8ECEF",
},
relationshipLabel: {
fontSize: 12,
lineHeight: 17,
fontWeight: "700",
color: "#597064",
textTransform: "uppercase",
letterSpacing: 0.4,
},
pathway: {
fontSize: 16,
lineHeight: 26,
color: "#334155",
},
});