refactor(m0001): extract shared point category accordion and overview helpers
This commit is contained in:
parent
5c6180b6b0
commit
40d9feb869
|
|
@ -0,0 +1,422 @@
|
||||||
|
import React, { useMemo, useState } from "react";
|
||||||
|
import {
|
||||||
|
LayoutAnimation,
|
||||||
|
Platform,
|
||||||
|
Pressable,
|
||||||
|
StyleSheet,
|
||||||
|
Text,
|
||||||
|
UIManager,
|
||||||
|
View,
|
||||||
|
} from "react-native";
|
||||||
|
import { Ionicons } from "@expo/vector-icons";
|
||||||
|
import { useRouter } from "expo-router";
|
||||||
|
import { moderateScale, verticalScale } from "react-native-size-matters";
|
||||||
|
|
||||||
|
import { Colors, Radius } from "@/theme";
|
||||||
|
import { useModuleTemplateContentDetailsQuery } from "../../hooks/use-content-queries";
|
||||||
|
import {
|
||||||
|
buildContentReferenceRoute,
|
||||||
|
parseNavigableContentReference,
|
||||||
|
} from "../../utils/content-reference";
|
||||||
|
import { asRecord, asString, asStringArray } from "../helpers/value";
|
||||||
|
|
||||||
|
if (
|
||||||
|
Platform.OS === "android" &&
|
||||||
|
UIManager.setLayoutAnimationEnabledExperimental
|
||||||
|
) {
|
||||||
|
UIManager.setLayoutAnimationEnabledExperimental(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
export type PointsCatalogMap = Map<
|
||||||
|
string,
|
||||||
|
{ contentId: string; title: string }
|
||||||
|
>;
|
||||||
|
|
||||||
|
/** Loads m0001_ct0003 points catalog for fallback navigation by point code or name. */
|
||||||
|
export function usePointsCatalogMap(moduleCode = "m0001"): PointsCatalogMap {
|
||||||
|
const { data: pointsCatalog } = useModuleTemplateContentDetailsQuery({
|
||||||
|
module_code: moduleCode || "m0001",
|
||||||
|
template_code: "m0001_ct0003",
|
||||||
|
});
|
||||||
|
|
||||||
|
return 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]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Extracts display label from a point content_ref or raw target payload. */
|
||||||
|
export function extractPointRefLabel(rawValue: unknown): string {
|
||||||
|
const directRef = parseNavigableContentReference(rawValue);
|
||||||
|
if (directRef?.label?.trim()) {
|
||||||
|
return directRef.label.trim();
|
||||||
|
}
|
||||||
|
const rawPoint = asRecord(rawValue);
|
||||||
|
const label = asString(rawPoint.label).trim();
|
||||||
|
if (label) return label;
|
||||||
|
if (rawPoint.target && typeof rawPoint.target === "object") {
|
||||||
|
return asString((rawPoint.target as Record<string, unknown>).source_code).trim();
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Navigates to the canonical acupuncture point detail route from a content_ref payload. */
|
||||||
|
export function navigateToPointRef(
|
||||||
|
router: ReturnType<typeof useRouter>,
|
||||||
|
rawValue: unknown,
|
||||||
|
moduleCode: string,
|
||||||
|
pointsCatalogMap?: PointsCatalogMap,
|
||||||
|
): void {
|
||||||
|
const directRef = parseNavigableContentReference(rawValue);
|
||||||
|
if (directRef) {
|
||||||
|
router.push(buildContentReferenceRoute(directRef) as never);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const rawPoint = asRecord(rawValue);
|
||||||
|
const pointLabel = extractPointRefLabel(rawValue);
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatGeneralInfoKey(key: string): string {
|
||||||
|
const trimmed = key.trim();
|
||||||
|
if (trimmed === "Yin_vs_Yang") return "Yin vs. Yang";
|
||||||
|
if (trimmed === "Yuan_vs_Luo") return "Yuan vs. Luo";
|
||||||
|
return trimmed.replace(/_/g, " ");
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatGeneralInfoSubLines(
|
||||||
|
value: unknown,
|
||||||
|
indent = "",
|
||||||
|
lines: string[] = [],
|
||||||
|
): string[] {
|
||||||
|
if (typeof value === "string") {
|
||||||
|
const text = value.trim();
|
||||||
|
if (text) lines.push(text);
|
||||||
|
return lines;
|
||||||
|
}
|
||||||
|
const obj = asRecord(value);
|
||||||
|
const entries = Object.entries(obj);
|
||||||
|
entries.forEach(([k, v], idx) => {
|
||||||
|
const label = formatGeneralInfoKey(k);
|
||||||
|
if (typeof v === "string") {
|
||||||
|
const text = v.trim();
|
||||||
|
if (text) {
|
||||||
|
lines.push(`${indent}${idx + 1}. ${label}: ${text}`);
|
||||||
|
}
|
||||||
|
} else if (v && typeof v === "object") {
|
||||||
|
lines.push(`${indent}${label}:`);
|
||||||
|
formatGeneralInfoSubLines(v, `${indent} `, lines);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return lines;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Merges description strings and structured general_information into Overview bullet items. */
|
||||||
|
export function buildCategoryOverviewParagraphs(
|
||||||
|
data: Record<string, unknown>,
|
||||||
|
): string[] {
|
||||||
|
const paragraphs = [...asStringArray(data.description)];
|
||||||
|
const generalInfo = asRecord(data.general_information);
|
||||||
|
|
||||||
|
for (const [key, value] of Object.entries(generalInfo)) {
|
||||||
|
const label = formatGeneralInfoKey(key);
|
||||||
|
if (typeof value === "string") {
|
||||||
|
const text = value.trim();
|
||||||
|
if (text) {
|
||||||
|
paragraphs.push(`${label}: ${text}`);
|
||||||
|
}
|
||||||
|
} else if (value && typeof value === "object") {
|
||||||
|
const subLines = formatGeneralInfoSubLines(value);
|
||||||
|
if (subLines.length > 0) {
|
||||||
|
paragraphs.push(`${label}:\n${subLines.join("\n")}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return paragraphs;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Renders a pill-style navigable chip linking to an acupuncture point detail screen. */
|
||||||
|
export function PointLinkChip({
|
||||||
|
label,
|
||||||
|
accessibilityLabel,
|
||||||
|
onPress,
|
||||||
|
}: {
|
||||||
|
label: string;
|
||||||
|
accessibilityLabel?: string;
|
||||||
|
onPress: () => void;
|
||||||
|
}) {
|
||||||
|
return (
|
||||||
|
<Pressable
|
||||||
|
accessibilityRole="link"
|
||||||
|
accessibilityLabel={accessibilityLabel || `View point ${label}`}
|
||||||
|
onPress={onPress}
|
||||||
|
style={({ pressed }) => [
|
||||||
|
pointCategoryStyles.pointLinkButton,
|
||||||
|
pressed && pointCategoryStyles.pressed,
|
||||||
|
]}
|
||||||
|
>
|
||||||
|
<Text style={pointCategoryStyles.pointLinkLabel}>{label}</Text>
|
||||||
|
<Ionicons
|
||||||
|
name="chevron-forward"
|
||||||
|
size={moderateScale(14)}
|
||||||
|
color={Colors.primaryDark}
|
||||||
|
/>
|
||||||
|
</Pressable>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Reusable expandable accordion card wrapper matching category detail styling. */
|
||||||
|
export function PointCategoryAccordionCard({
|
||||||
|
title,
|
||||||
|
badges = [],
|
||||||
|
defaultExpanded,
|
||||||
|
children,
|
||||||
|
}: {
|
||||||
|
title: string;
|
||||||
|
badges?: string[];
|
||||||
|
defaultExpanded: boolean;
|
||||||
|
children: React.ReactNode;
|
||||||
|
}) {
|
||||||
|
const [expanded, setExpanded] = useState(defaultExpanded);
|
||||||
|
|
||||||
|
const toggle = () => {
|
||||||
|
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);
|
||||||
|
setExpanded((prev) => !prev);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<View style={pointCategoryStyles.card}>
|
||||||
|
<Pressable
|
||||||
|
accessibilityRole="button"
|
||||||
|
accessibilityState={{ expanded }}
|
||||||
|
accessibilityLabel={`${expanded ? "Collapse" : "Expand"} ${title}`}
|
||||||
|
onPress={toggle}
|
||||||
|
style={({ pressed }) => [
|
||||||
|
pointCategoryStyles.cardHeader,
|
||||||
|
expanded && pointCategoryStyles.cardHeaderExpanded,
|
||||||
|
pressed && pointCategoryStyles.pressed,
|
||||||
|
]}
|
||||||
|
>
|
||||||
|
<View style={pointCategoryStyles.headerLeft}>
|
||||||
|
<View style={pointCategoryStyles.headerAccent} />
|
||||||
|
<Text style={pointCategoryStyles.headerTitle} numberOfLines={2}>
|
||||||
|
{title}
|
||||||
|
</Text>
|
||||||
|
</View>
|
||||||
|
|
||||||
|
<View style={pointCategoryStyles.headerRight}>
|
||||||
|
{badges
|
||||||
|
.filter((b) => b.trim().length > 0)
|
||||||
|
.map((badge, index) => (
|
||||||
|
<View key={`${badge}-${index}`} style={pointCategoryStyles.badge}>
|
||||||
|
<Text style={pointCategoryStyles.badgeText}>{badge}</Text>
|
||||||
|
</View>
|
||||||
|
))}
|
||||||
|
<View style={pointCategoryStyles.chevron}>
|
||||||
|
<Ionicons
|
||||||
|
name={expanded ? "chevron-up" : "chevron-down"}
|
||||||
|
size={moderateScale(16)}
|
||||||
|
color={Colors.primary}
|
||||||
|
/>
|
||||||
|
</View>
|
||||||
|
</View>
|
||||||
|
</Pressable>
|
||||||
|
|
||||||
|
{expanded ? (
|
||||||
|
<View style={pointCategoryStyles.cardBody}>{children}</View>
|
||||||
|
) : null}
|
||||||
|
</View>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export const pointCategoryStyles = StyleSheet.create({
|
||||||
|
container: {
|
||||||
|
paddingHorizontal: moderateScale(16),
|
||||||
|
paddingTop: verticalScale(14),
|
||||||
|
paddingBottom: verticalScale(40),
|
||||||
|
gap: verticalScale(14),
|
||||||
|
},
|
||||||
|
cardsList: {
|
||||||
|
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),
|
||||||
|
},
|
||||||
|
badge: {
|
||||||
|
paddingHorizontal: moderateScale(9),
|
||||||
|
paddingVertical: verticalScale(3),
|
||||||
|
borderRadius: moderateScale(10),
|
||||||
|
backgroundColor: "#E8F5E9",
|
||||||
|
borderWidth: 1,
|
||||||
|
borderColor: "#C6E4CE",
|
||||||
|
},
|
||||||
|
badgeText: {
|
||||||
|
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",
|
||||||
|
},
|
||||||
|
pointLinksRow: {
|
||||||
|
flexDirection: "row",
|
||||||
|
flexWrap: "wrap",
|
||||||
|
gap: moderateScale(8),
|
||||||
|
marginTop: verticalScale(4),
|
||||||
|
},
|
||||||
|
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),
|
||||||
|
},
|
||||||
|
pointLinkLabel: {
|
||||||
|
fontSize: moderateScale(13),
|
||||||
|
lineHeight: moderateScale(18),
|
||||||
|
fontWeight: "700",
|
||||||
|
color: Colors.primaryDark,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
Loading…
Reference in New Issue