feat(m0001): support general_information, organ/category headings, and connection badges

This commit is contained in:
ShivaniKhaire 2026-12-05 11:34:20 +05:30
parent 40d9feb869
commit 09f0fae67d
1 changed files with 100 additions and 385 deletions

View File

@ -1,80 +1,63 @@
import React, { useMemo, useState } from "react";
import {
LayoutAnimation,
Platform,
Pressable,
ScrollView,
StyleSheet,
Text,
UIManager,
View,
} from "react-native";
import { Ionicons } from "@expo/vector-icons";
import React, { useMemo } from "react";
import { ScrollView, Text, View } from "react-native";
import { useRouter } from "expo-router";
import { moderateScale, verticalScale } from "react-native-size-matters";
import { Colors, Radius } from "@/theme";
import { useSettingsStore } from "@/store/settings.store";
import type { DetailRendererProps } from "../../types/renderer.types";
import { useModuleTemplateContentDetailsQuery } from "../../hooks/use-content-queries";
import {
buildContentReferenceRoute,
parseNavigableContentReference,
} from "../../utils/content-reference";
import {
asRecord,
asRecordArray,
asString,
asStringArray,
} from "../helpers/value";
import { asRecordArray, asString, asStringArray } from "../helpers/value";
import { BulletList } from "../shared/BulletList";
import { ContentLinkAttributeSections } from "../shared/ContentLinkAttributeSections";
import { Hero } from "../shared/Hero";
import { OverviewCard } from "../shared/OverviewCard";
import { Images } from "./categoryParts";
if (Platform.OS === "android" && UIManager.setLayoutAnimationEnabledExperimental) {
UIManager.setLayoutAnimationEnabledExperimental(true);
}
import {
buildCategoryOverviewParagraphs,
extractPointRefLabel,
navigateToPointRef,
PointCategoryAccordionCard,
pointCategoryStyles,
PointLinkChip,
type PointsCatalogMap,
usePointsCatalogMap,
} from "./pointCategoryShared";
interface PointItemProps {
key?: React.Key;
item: Record<string, unknown>;
defaultExpanded: boolean;
moduleCode: string;
pointsCatalogMap?: Map<string, { contentId: string; title: string }>;
pointsCatalogMap?: PointsCatalogMap;
}
function formatPointHeading(
meridian: string,
chineseName: string,
primaryLabel: string,
pointName: string,
pointCode?: string,
): string {
const cleanChinese = chineseName.replace(/^\((.*)\)$/, "$1").trim();
const cleanName = pointName.replace(/^\((.*)\)$/, "$1").trim();
const cleanCode = (pointCode || "").trim();
// Strip meridian abbreviation codes or point codes in parentheses,
// e.g. (ST), (LU-9), (LI), (LV), (LR), (PC), (GB), (HT), etc.
const cleanedMeridian = meridian
// Strip meridian abbreviation codes or point codes in parentheses.
const cleanedPrimary = primaryLabel
.replace(/\s*\(\s*[A-Z]{1,4}(?:[\s\-_]*\d+)?\s*\)\s*/g, " ")
.replace(/\s+/g, " ")
.trim();
const parts: string[] = [];
if (cleanedMeridian) {
parts.push(cleanedMeridian);
if (cleanedPrimary) {
parts.push(cleanedPrimary);
}
if (
cleanCode &&
!cleanedMeridian.toLowerCase().includes(cleanCode.toLowerCase())
!cleanedPrimary.toLowerCase().includes(cleanCode.toLowerCase())
) {
parts.push(cleanCode);
}
if (
cleanChinese &&
!cleanedMeridian.toLowerCase().includes(cleanChinese.toLowerCase())
cleanName &&
!cleanedPrimary.toLowerCase().includes(cleanName.toLowerCase())
) {
parts.push(`(${cleanChinese})`);
parts.push(`(${cleanName})`);
}
return parts.join(" ") || "Point";
@ -87,200 +70,88 @@ function PointAccordionCard({
pointsCatalogMap,
}: PointItemProps) {
const router = useRouter();
const [expanded, setExpanded] = useState(defaultExpanded);
const meridian = asString(item.meridian).trim();
const chineseName = asString(item.chinese_name).trim();
const primaryLabel =
asString(item.meridian).trim() ||
asString(item.organ).trim() ||
asString(item.category).trim();
const pointName =
asString(item.chinese_name).trim() || asString(item.pinyin_name).trim();
const element = asString(item.element).trim();
const yinYang = asString(item.yin_yang).trim();
const connection = asString(item.connection).trim();
const caution = asString(item.caution).trim();
const cautionList = asStringArray(item.caution);
const actions = asStringArray(item.actions);
const indications = asStringArray(item.indications);
// Resolve point reference for linkable button and heading label
const rawPoint = asRecord(item.point);
const directRef = parseNavigableContentReference(item.point);
const pointLabel = extractPointRefLabel(item.point);
// Extract candidate point codes/labels: e.g. "LU 9", "LU-9"
const pointLabel =
directRef?.label?.trim() ||
asString(rawPoint.label).trim() ||
(rawPoint.target && typeof rawPoint.target === "object"
? asString((rawPoint.target as Record<string, unknown>).source_code).trim()
: "") ||
"";
const title = useMemo(
() => formatPointHeading(primaryLabel, pointName, pointLabel),
[primaryLabel, pointName, pointLabel],
);
// Format header title: e.g. "Lung LU 9 (Taiyuan)"
const title = useMemo(() => {
return formatPointHeading(meridian, chineseName, pointLabel);
}, [meridian, chineseName, pointLabel]);
// Construct button display label: e.g. "LU-9 (Taiyuan)" or "LU 9 (Taiyuan)"
const pointButtonLabel = useMemo(() => {
const baseCode = pointLabel || "Point Details";
if (chineseName && !baseCode.toLowerCase().includes(chineseName.toLowerCase())) {
return `${baseCode} (${chineseName})`;
if (pointName && !baseCode.toLowerCase().includes(pointName.toLowerCase())) {
return `${baseCode} (${pointName})`;
}
return baseCode;
}, [pointLabel, chineseName]);
}, [pointLabel, pointName]);
// Resolve navigation destination
const navigateToPoint = () => {
if (directRef) {
router.push(buildContentReferenceRoute(directRef) as never);
return;
}
if (rawPoint.target && typeof rawPoint.target === "object") {
const target = rawPoint.target as Record<string, unknown>;
const targetModule = asString(target.module_code).trim() || moduleCode || "m0001";
const targetTemplate = asString(target.template_code).trim() || "m0001_ct0003";
const contentId = asString(target.content_id).trim();
const sourceCode = asString(target.source_code).trim();
if (contentId) {
router.push(
`/modules/${encodeURIComponent(targetModule)}/${encodeURIComponent(targetTemplate)}/${encodeURIComponent(contentId)}` as never,
);
return;
}
// Fallback: match by catalog map (from m0001_ct0003) using sourceCode or label
if (pointsCatalogMap && sourceCode) {
const found =
pointsCatalogMap.get(sourceCode.toLowerCase()) ||
pointsCatalogMap.get(sourceCode.replace(/[- ]/g, "").toLowerCase());
if (found) {
router.push(
`/modules/${encodeURIComponent(targetModule)}/${encodeURIComponent(targetTemplate)}/${encodeURIComponent(found.contentId)}` as never,
);
return;
}
}
}
// Secondary fallback matching by pointLabel in catalog
if (pointsCatalogMap && pointLabel) {
const found =
pointsCatalogMap.get(pointLabel.toLowerCase()) ||
pointsCatalogMap.get(pointLabel.replace(/[- ]/g, "").toLowerCase());
if (found) {
router.push(
`/modules/${encodeURIComponent(moduleCode || "m0001")}/m0001_ct0003/${encodeURIComponent(found.contentId)}` as never,
);
}
}
};
const toggle = () => {
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);
setExpanded((prev) => !prev);
};
const badges = [element, yinYang, connection].filter(Boolean);
return (
<View style={styles.card}>
{/* Accordion Header */}
<Pressable
accessibilityRole="button"
accessibilityState={{ expanded }}
accessibilityLabel={`${expanded ? "Collapse" : "Expand"} ${title}`}
onPress={toggle}
style={({ pressed }) => [
styles.cardHeader,
expanded && styles.cardHeaderExpanded,
pressed && styles.pressed,
]}
>
<View style={styles.headerLeft}>
<View style={styles.headerAccent} />
<Text style={styles.headerTitle} numberOfLines={2}>
{title}
</Text>
</View>
<View style={styles.headerRight}>
{element ? (
<View style={styles.elementBadge}>
<Text style={styles.elementBadgeText}>{element}</Text>
</View>
) : null}
{yinYang ? (
<View style={styles.elementBadge}>
<Text style={styles.elementBadgeText}>{yinYang}</Text>
</View>
) : null}
<View style={styles.chevron}>
<Ionicons
name={expanded ? "chevron-up" : "chevron-down"}
size={moderateScale(16)}
color={Colors.primary}
/>
</View>
</View>
</Pressable>
{/* Accordion Body (Expanded State) */}
{expanded ? (
<View style={styles.cardBody}>
{/* Actions */}
{actions.length > 0 ? (
<View style={styles.sectionRow}>
<Text style={styles.sectionLabel}>ACTIONS</Text>
{actions.length === 1 ? (
<Text style={styles.sectionValue}>{actions[0]}</Text>
) : (
<BulletList items={actions} />
)}
</View>
) : null}
{/* Indications */}
{indications.length > 0 ? (
<View style={styles.sectionRow}>
<Text style={styles.sectionLabel}>INDICATIONS</Text>
<BulletList items={indications} />
</View>
) : null}
{/* Caution (same UI as Actions and Indications) */}
{caution ? (
<View style={styles.sectionRow}>
<Text style={styles.sectionLabel}>CAUTION</Text>
{cautionList.length > 1 ? (
<BulletList items={cautionList} />
) : (
<Text style={styles.sectionValue}>{caution}</Text>
)}
</View>
) : null}
{/* Linkable Point Button (matching standard ReferenceChipList styling) */}
{pointLabel ? (
<Pressable
accessibilityRole="link"
accessibilityLabel={`View point ${pointButtonLabel}`}
onPress={navigateToPoint}
style={({ pressed }) => [
styles.pointLinkButton,
pressed && styles.pressed,
]}
>
<Text style={styles.pointLinkLabel}>{pointButtonLabel}</Text>
<Ionicons
name="chevron-forward"
size={moderateScale(14)}
color={Colors.primaryDark}
/>
</Pressable>
) : null}
<PointCategoryAccordionCard
title={title}
badges={badges}
defaultExpanded={defaultExpanded}
>
{actions.length > 0 ? (
<View style={pointCategoryStyles.sectionRow}>
<Text style={pointCategoryStyles.sectionLabel}>ACTIONS</Text>
{actions.length === 1 ? (
<Text style={pointCategoryStyles.sectionValue}>{actions[0]}</Text>
) : (
<BulletList items={actions} />
)}
</View>
) : null}
</View>
{indications.length > 0 ? (
<View style={pointCategoryStyles.sectionRow}>
<Text style={pointCategoryStyles.sectionLabel}>INDICATIONS</Text>
<BulletList items={indications} />
</View>
) : null}
{caution ? (
<View style={pointCategoryStyles.sectionRow}>
<Text style={pointCategoryStyles.sectionLabel}>CAUTION</Text>
{cautionList.length > 1 ? (
<BulletList items={cautionList} />
) : (
<Text style={pointCategoryStyles.sectionValue}>{caution}</Text>
)}
</View>
) : null}
{pointLabel ? (
<View style={pointCategoryStyles.pointLinksRow}>
<PointLinkChip
label={pointButtonLabel}
onPress={() =>
navigateToPointRef(router, item.point, moduleCode, pointsCatalogMap)
}
/>
</View>
) : null}
</PointCategoryAccordionCard>
);
}
/** Shared category detail renderer supporting Yuan-Source, Xi-Cleft, Luo, Back-Shu, Front-Mu, and Influential points. */
export function YuanSourceDetailRenderer({
moduleCode = "m0001",
content,
@ -292,65 +163,40 @@ export function YuanSourceDetailRenderer({
const accordionDefaultExpanded = useSettingsStore(
(state) => state.accordionDefaultExpanded,
);
// Fetch points from m0001_ct0003 for robust ID navigation fallback
const { data: pointsCatalog } = useModuleTemplateContentDetailsQuery({
module_code: moduleCode || "m0001",
template_code: "m0001_ct0003",
});
const pointsCatalogMap = useMemo(() => {
const map = new Map<string, { contentId: string; title: string }>();
if (pointsCatalog?.items) {
for (const item of pointsCatalog.items) {
const id = item.content.id;
const title = item.content.title;
const code = asString(item.data.code).trim().toLowerCase();
const name = asString(item.data.name).trim().toLowerCase();
if (code) {
map.set(code, { contentId: id, title });
map.set(code.replace(/[- ]/g, ""), { contentId: id, title });
}
if (name) {
map.set(name, { contentId: id, title });
}
}
}
return map;
}, [pointsCatalog]);
const pointsCatalogMap = usePointsCatalogMap(moduleCode);
const rawTitle =
asString(data.title) || asString(data.name) || content.title || fallbackTitle;
const altNames = asStringArray(data.alt_name);
const descriptionParagraphs = asStringArray(data.description);
const alias = asString(data.alias).trim();
const subtitle =
altNames.length > 0 ? altNames.join(" · ") : alias || undefined;
const overviewParagraphs = useMemo(
() => buildCategoryOverviewParagraphs(data),
[data],
);
const points = asRecordArray(data.points);
return (
<ScrollView
contentContainerStyle={styles.container}
contentContainerStyle={pointCategoryStyles.container}
showsVerticalScrollIndicator={false}
>
{/* Screen Title & Subtitle */}
<Hero
title={rawTitle}
subtitle={altNames.length > 0 ? altNames.join(" · ") : undefined}
/>
<Hero title={rawTitle} subtitle={subtitle} />
{/* Visual images (if any assigned) */}
<Images data={data} />
{/* Overview Section */}
{descriptionParagraphs.length > 0 ? (
{overviewParagraphs.length > 0 ? (
<OverviewCard
title="Overview"
variant="bullets"
paragraphs={descriptionParagraphs}
paragraphs={overviewParagraphs}
/>
) : null}
{/* Points Accordion List */}
{points.length > 0 ? (
<View style={styles.pointsList}>
<View style={pointCategoryStyles.cardsList}>
{points.map((pointItem, index) => (
<PointAccordionCard
key={index}
@ -363,143 +209,12 @@ export function YuanSourceDetailRenderer({
</View>
) : null}
{/* Generic content link attributes (excluding points already rendered above) */}
<ContentLinkAttributeSections
data={data}
definitions={definitions}
presentationAttributes={presentationAttributes}
excludeAttributeKeys={["points", "m0001_ct0009.points", "m0001_ct0011.points"]}
excludeAttributeKeys={["points", "pairings"]}
/>
</ScrollView>
);
}
const styles = StyleSheet.create({
container: {
paddingHorizontal: moderateScale(16),
paddingTop: verticalScale(14),
paddingBottom: verticalScale(40),
gap: verticalScale(14),
},
pointsList: {
gap: verticalScale(12),
},
card: {
backgroundColor: "#FFFFFF",
borderRadius: moderateScale(16),
borderWidth: 1,
borderColor: "#E2E8F0",
overflow: "hidden",
},
cardHeader: {
minHeight: verticalScale(54),
paddingHorizontal: moderateScale(14),
paddingVertical: verticalScale(12),
flexDirection: "row",
alignItems: "center",
justifyContent: "space-between",
backgroundColor: "#FFFFFF",
gap: moderateScale(10),
},
cardHeaderExpanded: {
backgroundColor: "#F0F7F3",
borderBottomWidth: 1,
borderBottomColor: "#E2E8F0",
},
pressed: {
opacity: 0.75,
},
headerLeft: {
flex: 1,
flexDirection: "row",
alignItems: "center",
gap: moderateScale(10),
},
headerAccent: {
width: moderateScale(4),
height: verticalScale(22),
borderRadius: moderateScale(2),
backgroundColor: Colors.primary,
},
headerTitle: {
flex: 1,
fontSize: moderateScale(15),
lineHeight: moderateScale(21),
fontWeight: "700",
color: "#1E293B",
},
headerRight: {
flexDirection: "row",
alignItems: "center",
gap: moderateScale(8),
},
elementBadge: {
paddingHorizontal: moderateScale(9),
paddingVertical: verticalScale(3),
borderRadius: moderateScale(10),
backgroundColor: "#E8F5E9",
borderWidth: 1,
borderColor: "#C6E4CE",
},
elementBadgeText: {
fontSize: 11,
lineHeight: 16,
fontWeight: "600",
color: "#285A44",
},
chevron: {
width: moderateScale(28),
height: moderateScale(28),
borderRadius: moderateScale(14),
backgroundColor: "#FFFFFFB8",
alignItems: "center",
justifyContent: "center",
borderWidth: 1,
borderColor: "#E2E8F0",
},
cardBody: {
paddingHorizontal: moderateScale(16),
paddingTop: verticalScale(14),
paddingBottom: verticalScale(16),
gap: verticalScale(12),
backgroundColor: "#FFFFFF",
},
sectionRow: {
gap: verticalScale(6),
paddingBottom: verticalScale(10),
borderBottomWidth: StyleSheet.hairlineWidth,
borderBottomColor: "#E8ECEF",
},
sectionLabel: {
fontSize: 12,
lineHeight: 17,
fontWeight: "700",
color: "#597064",
letterSpacing: 0.15,
},
sectionValue: {
fontSize: 16,
lineHeight: 25,
color: "#334155",
},
pointLinkButton: {
alignSelf: "flex-start",
minHeight: verticalScale(34),
paddingHorizontal: moderateScale(12),
paddingVertical: verticalScale(6),
borderRadius: Radius.full,
backgroundColor: "#E8F5E9",
borderWidth: 1,
borderColor: "#CDE5D0",
flexDirection: "row",
alignItems: "center",
gap: moderateScale(6),
marginTop: verticalScale(6),
},
pointLinkLabel: {
fontSize: moderateScale(13),
lineHeight: moderateScale(18),
fontWeight: "700",
color: Colors.primaryDark,
},
});