387 lines
11 KiB
TypeScript
387 lines
11 KiB
TypeScript
import React from "react";
|
|
import { Pressable, ScrollView, StyleSheet, Text, 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 } from "@/theme";
|
|
import type { DetailRendererProps } from "../../types/renderer.types";
|
|
import {
|
|
buildContentReferenceRoute,
|
|
parseNavigableContentReference,
|
|
} from "../../utils/content-reference";
|
|
import { asRecord, asString, asStringArray } from "../helpers/value";
|
|
import { BulletList } from "../shared/BulletList";
|
|
import { ContentLinkAttributeSections } from "../shared/ContentLinkAttributeSections";
|
|
import { ContentValue } from "../shared/ContentValue";
|
|
import { Hero } from "../shared/Hero";
|
|
import { Paragraphs } from "../shared/Paragraphs";
|
|
import { SectionCard } from "../shared/SectionCard";
|
|
|
|
function parseExplanationBullets(value: unknown): string[] {
|
|
if (!value) return [];
|
|
if (Array.isArray(value)) {
|
|
return value.map(asString).filter(Boolean);
|
|
}
|
|
const str = asString(value);
|
|
if (!str) return [];
|
|
|
|
if (str.includes("\n")) {
|
|
return str
|
|
.split(/\n+/)
|
|
.map((s) => s.trim().replace(/^\t+/, ""))
|
|
.filter(Boolean);
|
|
}
|
|
|
|
const regex =
|
|
/(?<=[^\s:,\+])(?<!\band)\s+(?=(?:(?:[A-Z]{2,4}-\d+|RN-\d+|DU-\d+|REN-\d+)(?:[,\s]+(?:[A-Z]{2,4}-\d+|RN-\d+|DU-\d+|REN-\d+))*(?:\s*(?:and|\+)\s*(?:[A-Z]{2,4}-\d+|RN-\d+|DU-\d+|REN-\d+))?(?:\s*\([^)]*\))?:\s*|Cupping\b|Moxa\b))/i;
|
|
|
|
const parts = str
|
|
.split(regex)
|
|
.map((s) => s.trim().replace(/^\t+/, ""))
|
|
.filter(Boolean);
|
|
|
|
return parts.length > 0 ? parts : [str];
|
|
}
|
|
|
|
function parseTreatmentBullets(value: unknown): string[] {
|
|
if (!value) return [];
|
|
if (Array.isArray(value)) {
|
|
return value.map(asString).filter(Boolean);
|
|
}
|
|
const str = asString(value);
|
|
if (!str) return [];
|
|
|
|
if (str.includes("\n")) {
|
|
return str
|
|
.split(/\n+/)
|
|
.map((s) => s.trim().replace(/^\t+/, ""))
|
|
.filter(Boolean);
|
|
}
|
|
|
|
const items = str
|
|
.split(/,\s*(?![^()]*\))/)
|
|
.map((s) => s.trim().replace(/\.$/, ""))
|
|
.filter(Boolean);
|
|
|
|
return items.length > 0 ? items : [str];
|
|
}
|
|
|
|
export function FourLevelsDetailRenderer({
|
|
content,
|
|
data,
|
|
definitions,
|
|
presentationAttributes,
|
|
}: DetailRendererProps) {
|
|
const router = useRouter();
|
|
|
|
const title =
|
|
asString(data.name) ||
|
|
asString(data.title) ||
|
|
content.title;
|
|
|
|
const overview = asString(data.overview);
|
|
const rawPatterns = Array.isArray(data.patterns) ? data.patterns : [];
|
|
const hasPatterns = rawPatterns.length > 0;
|
|
|
|
const clinicalManifestationsRaw =
|
|
data.clinical_manifestation ??
|
|
data.clinical_manifestations ??
|
|
data.clinical_menifestation;
|
|
const clinicalManifestationItems = asStringArray(clinicalManifestationsRaw).filter(
|
|
(item) => item.trim().length > 0,
|
|
);
|
|
const tongue = asString(data.tongue);
|
|
const pulse = asString(data.pulse);
|
|
const pathology = asString(data.pathology);
|
|
const treatmentPrincipleRaw =
|
|
data.treatment_principle ?? data["treatment principle"];
|
|
const treatmentPrincipleItems = asStringArray(treatmentPrincipleRaw).filter(
|
|
(item) => item.trim().length > 0,
|
|
);
|
|
const treatmentPrinciple = asString(treatmentPrincipleRaw);
|
|
const treatment = asString(data.treatment);
|
|
const treatmentBullets = parseTreatmentBullets(treatment);
|
|
const explanation = asString(data.explanation);
|
|
const explanationBullets = parseExplanationBullets(explanation);
|
|
const contentText = asString(data.content);
|
|
|
|
if (hasPatterns) {
|
|
return (
|
|
<ScrollView
|
|
contentContainerStyle={styles.content}
|
|
showsVerticalScrollIndicator={false}
|
|
>
|
|
<Hero title={title} />
|
|
|
|
{/* Direct Overview Card matching Four Levels 1st page */}
|
|
{overview ? (
|
|
<View style={styles.overviewCard}>
|
|
<View style={styles.overviewHeader}>
|
|
<Ionicons name="information-circle" size={20} color={Colors.primary} />
|
|
<Text style={styles.overviewTitle}>Overview</Text>
|
|
</View>
|
|
<Text style={styles.overviewText}>{overview}</Text>
|
|
</View>
|
|
) : null}
|
|
|
|
{/* Patterns List with numbered cards matching 1st page */}
|
|
<View style={styles.patternsSection}>
|
|
<View style={styles.sectionHeaderRow}>
|
|
<Text style={styles.sectionHeading}>Patterns</Text>
|
|
<View style={styles.countBadge}>
|
|
<Text style={styles.countBadgeText}>
|
|
{rawPatterns.length} {rawPatterns.length === 1 ? "pattern" : "patterns"}
|
|
</Text>
|
|
</View>
|
|
</View>
|
|
|
|
<View style={styles.patternsList}>
|
|
{rawPatterns.map((item, idx) => {
|
|
const reference = parseNavigableContentReference(item);
|
|
const raw = asRecord(item);
|
|
const patternTitle =
|
|
reference?.label?.trim() ||
|
|
asString(raw?.label) ||
|
|
asString(raw?.name) ||
|
|
`Pattern ${idx + 1}`;
|
|
|
|
const handlePress = () => {
|
|
if (reference) {
|
|
router.push(buildContentReferenceRoute(reference) as never);
|
|
} else if (raw && typeof raw.target === "object" && raw.target !== null) {
|
|
const target = raw.target as Record<string, unknown>;
|
|
const mod = asString(target.module_code);
|
|
const tpl = asString(target.template_code);
|
|
const cid = asString(target.content_id);
|
|
if (mod && tpl && cid) {
|
|
router.push(
|
|
`/modules/${encodeURIComponent(mod)}/${encodeURIComponent(tpl)}/${encodeURIComponent(cid)}` as never,
|
|
);
|
|
}
|
|
}
|
|
};
|
|
|
|
return (
|
|
<View key={idx} style={styles.patternCard}>
|
|
<Pressable
|
|
accessibilityRole="button"
|
|
accessibilityLabel={`Open ${patternTitle}`}
|
|
onPress={handlePress}
|
|
style={({ pressed }) => [styles.patternRow, pressed && styles.pressed]}
|
|
>
|
|
<View style={styles.iconWrapper}>
|
|
<Text style={styles.levelNumber}>{idx + 1}</Text>
|
|
</View>
|
|
|
|
<View style={styles.textContainer}>
|
|
<Text style={styles.title}>{patternTitle}</Text>
|
|
<Text style={styles.description}>pattern</Text>
|
|
</View>
|
|
|
|
<Ionicons name="chevron-forward" size={20} color="#9CA3AF" />
|
|
</Pressable>
|
|
</View>
|
|
);
|
|
})}
|
|
</View>
|
|
</View>
|
|
|
|
<ContentLinkAttributeSections
|
|
data={data}
|
|
definitions={definitions}
|
|
presentationAttributes={presentationAttributes}
|
|
excludeAttributeKeys={["patterns", "m0016_ct0003.patterns"]}
|
|
/>
|
|
</ScrollView>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<ScrollView
|
|
contentContainerStyle={styles.content}
|
|
showsVerticalScrollIndicator={false}
|
|
>
|
|
<Hero title={title} />
|
|
|
|
{overview ? (
|
|
<SectionCard title="Overview">
|
|
<Paragraphs items={[overview]} />
|
|
</SectionCard>
|
|
) : null}
|
|
|
|
{clinicalManifestationItems.length > 0 ? (
|
|
<SectionCard title="Clinical Manifestations">
|
|
<BulletList items={clinicalManifestationItems} />
|
|
</SectionCard>
|
|
) : null}
|
|
|
|
{tongue ? (
|
|
<SectionCard title="Tongue">
|
|
<Paragraphs items={[tongue]} />
|
|
</SectionCard>
|
|
) : null}
|
|
|
|
{pulse ? (
|
|
<SectionCard title="Pulse">
|
|
<Paragraphs items={[pulse]} />
|
|
</SectionCard>
|
|
) : null}
|
|
|
|
{pathology ? (
|
|
<SectionCard title="Pathology">
|
|
<Paragraphs items={[pathology]} />
|
|
</SectionCard>
|
|
) : null}
|
|
|
|
{treatmentPrincipleItems.length > 0 ? (
|
|
<SectionCard title="Treatment Principle">
|
|
<BulletList items={treatmentPrincipleItems} />
|
|
</SectionCard>
|
|
) : treatmentPrinciple ? (
|
|
<SectionCard title="Treatment Principle">
|
|
<Paragraphs items={[treatmentPrinciple]} />
|
|
</SectionCard>
|
|
) : null}
|
|
|
|
{data.treatment ? (
|
|
<SectionCard title="Treatment">
|
|
{typeof data.treatment === "object" ? (
|
|
<ContentValue value={data.treatment} />
|
|
) : treatmentBullets.length > 0 ? (
|
|
<BulletList items={treatmentBullets} />
|
|
) : (
|
|
<Paragraphs items={[treatment]} />
|
|
)}
|
|
</SectionCard>
|
|
) : null}
|
|
|
|
{explanation ? (
|
|
<SectionCard title="Explanation">
|
|
{explanationBullets.length > 0 ? (
|
|
<BulletList items={explanationBullets} />
|
|
) : (
|
|
<Paragraphs items={[explanation]} />
|
|
)}
|
|
</SectionCard>
|
|
) : null}
|
|
|
|
{contentText ? (
|
|
<SectionCard title="Content">
|
|
<Paragraphs items={[contentText]} />
|
|
</SectionCard>
|
|
) : null}
|
|
|
|
<ContentLinkAttributeSections
|
|
data={data}
|
|
definitions={definitions}
|
|
presentationAttributes={presentationAttributes}
|
|
/>
|
|
</ScrollView>
|
|
);
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
content: {
|
|
paddingHorizontal: moderateScale(16),
|
|
paddingTop: verticalScale(14),
|
|
paddingBottom: verticalScale(40),
|
|
gap: verticalScale(14),
|
|
},
|
|
overviewCard: {
|
|
backgroundColor: "#FFFFFF",
|
|
borderRadius: moderateScale(16),
|
|
borderWidth: 1,
|
|
borderColor: "#E2EAE5",
|
|
padding: moderateScale(16),
|
|
},
|
|
overviewHeader: {
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
marginBottom: verticalScale(8),
|
|
gap: moderateScale(8),
|
|
},
|
|
overviewTitle: {
|
|
fontSize: moderateScale(16),
|
|
fontWeight: "700",
|
|
color: Colors.textPrimary,
|
|
},
|
|
overviewText: {
|
|
fontSize: moderateScale(13),
|
|
lineHeight: verticalScale(19),
|
|
color: Colors.textSecondary,
|
|
},
|
|
patternsSection: {
|
|
gap: verticalScale(10),
|
|
},
|
|
sectionHeaderRow: {
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
justifyContent: "space-between",
|
|
paddingHorizontal: moderateScale(4),
|
|
},
|
|
sectionHeading: {
|
|
fontSize: moderateScale(15),
|
|
fontWeight: "700",
|
|
color: Colors.textPrimary,
|
|
},
|
|
countBadge: {
|
|
backgroundColor: "#E8F5E9",
|
|
paddingHorizontal: moderateScale(10),
|
|
paddingVertical: verticalScale(4),
|
|
borderRadius: moderateScale(12),
|
|
},
|
|
countBadgeText: {
|
|
fontSize: moderateScale(12),
|
|
fontWeight: "600",
|
|
color: Colors.primaryDark,
|
|
},
|
|
patternsList: {
|
|
gap: verticalScale(12),
|
|
},
|
|
patternCard: {
|
|
backgroundColor: "#FFFFFF",
|
|
borderRadius: moderateScale(18),
|
|
borderWidth: 1,
|
|
borderColor: "#E2EAE5",
|
|
overflow: "hidden",
|
|
},
|
|
patternRow: {
|
|
minHeight: moderateScale(68),
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
padding: moderateScale(14),
|
|
},
|
|
pressed: {
|
|
opacity: 0.72,
|
|
},
|
|
iconWrapper: {
|
|
width: moderateScale(40),
|
|
height: moderateScale(40),
|
|
borderRadius: moderateScale(20),
|
|
justifyContent: "center",
|
|
alignItems: "center",
|
|
backgroundColor: "#E8F5E9",
|
|
},
|
|
levelNumber: {
|
|
fontSize: moderateScale(16),
|
|
fontWeight: "800",
|
|
color: Colors.primaryDark,
|
|
},
|
|
textContainer: {
|
|
flex: 1,
|
|
marginLeft: moderateScale(12),
|
|
marginRight: moderateScale(10),
|
|
},
|
|
title: {
|
|
fontSize: moderateScale(15),
|
|
fontWeight: "600",
|
|
color: Colors.textPrimary,
|
|
marginBottom: verticalScale(2),
|
|
},
|
|
description: {
|
|
fontSize: moderateScale(12),
|
|
color: Colors.textSecondary,
|
|
},
|
|
});
|