import React from "react"; import { StyleSheet, Text, View, type StyleProp, type TextStyle, } from "react-native"; import { Colors } from "@/theme/colors"; import { Spacing } from "@/theme/spacing"; import { ContentLink } from "../../components/navigation/ContentLink"; import { parseNavigableContentReference } from "../../utils/content-reference"; import { humanizeKey } from "../helpers/value"; import { BulletList } from "./BulletList"; import { ReferenceChipList } from "./ReferenceChipList"; export interface ContentValueProps { value: unknown; textStyle?: StyleProp; emptyText?: string | null; } function parseBullets(text: string): string[] { if (text.includes("\n")) { return text .split("\n") .map((s) => s.replace(/^[•\-\*\d+\.]\s*/, "").trim()) .filter((s) => s.length > 0); } if (text.includes(";")) { return text .split(";") .map((s) => s.trim().replace(/^and\s+/i, "")) .filter((s) => s.length > 0); } const parts = text.split(/,\s*(?![^()]*\))/); if (parts.length > 1) { return parts .map((s) => s.trim().replace(/\.$/, "").replace(/^and\s+/i, "")) .filter((s) => s.length > 0); } return [text]; } /** * Reference-aware fallback renderer for arbitrary nested attribute values. * * Explicit hydrated references are intercepted before generic object * rendering. No point-code heuristics are used. */ export function ContentValue({ value, textStyle, emptyText = null, }: ContentValueProps) { const reference = parseNavigableContentReference(value); if (reference) { return ; } if (value === null || value === undefined || value === "") { return emptyText ? ( {emptyText} ) : null; } if (Array.isArray(value)) { const visible = value.filter( (item) => item !== null && item !== undefined && item !== "", ); if (!visible.length) { return emptyText ? ( {emptyText} ) : null; } if (visible.every((item) => typeof item === "string")) { return ; } if (visible.some((item) => parseNavigableContentReference(item) !== null)) { return ; } return ( {visible.map((item, index) => ( ))} ); } if (typeof value === "object") { const entries = Object.entries(value as Record).filter( ([, child]) => child !== null && child !== undefined && child !== "" && (!Array.isArray(child) || child.length > 0), ); if (!entries.length) { return emptyText ? ( {emptyText} ) : null; } return ( {entries.map(([key, child], index) => { const isClinical = key.toLowerCase().includes("clinical") || key.toLowerCase().includes("manifestation"); const renderChild = () => { if ( isClinical && typeof child === "string" && child.trim().length > 0 ) { const bullets = parseBullets(child); if (bullets.length > 1) { return ; } } return ; }; return ( 0 && styles.divider, ]} > {humanizeKey(key)} {renderChild()} ); })} ); } if (typeof value === "string") { const text = value.trim(); if (text.includes("\n") || text.includes(";")) { const bullets = parseBullets(text); if (bullets.length > 1) { return ; } } if (text.includes(",")) { const parts = parseBullets(text); if ( parts.length > 1 && parts.every((p) => p.length > 0 && p.length < 50 && !p.endsWith(".")) ) { return ; } } } return ( {typeof value === "boolean" ? (value ? "Yes" : "No") : String(value)} ); } function contentValueKey(value: unknown, index: number): string { const reference = parseNavigableContentReference(value); if (reference?.type === "content_ref") { return `content-${reference.target.content_id}`; } if (reference?.type === "template_ref") { return `template-${reference.target.module_code}-${reference.target.template_code}`; } if (typeof value === "string" || typeof value === "number") { return `${String(value)}-${index}`; } return `value-${index}`; } const styles = StyleSheet.create({ text: { fontSize: 16, lineHeight: 25, color: "#334155", }, stack: { gap: Spacing.sm, }, object: { gap: Spacing.sm, }, objectRow: { gap: 4, }, divider: { paddingTop: 12, borderTopWidth: StyleSheet.hairlineWidth, borderTopColor: "#E8ECEF", }, objectLabel: { fontSize: 12, lineHeight: 17, fontWeight: "700", color: Colors.textSecondary, textTransform: "uppercase", letterSpacing: 0.35, }, });