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"; export interface ContentValueProps { value: unknown; textStyle?: StyleProp; emptyText?: string | null; } /** * 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; } 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]) => ( {humanizeKey(key)} ))} ); } 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, }, objectLabel: { fontSize: 12, lineHeight: 17, fontWeight: "700", color: Colors.textSecondary, textTransform: "uppercase", letterSpacing: 0.35, }, });