224 lines
5.7 KiB
TypeScript
224 lines
5.7 KiB
TypeScript
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<TextStyle>;
|
|
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 <ContentLink reference={reference} />;
|
|
}
|
|
|
|
if (value === null || value === undefined || value === "") {
|
|
return emptyText ? (
|
|
<Text style={[styles.text, textStyle]}>{emptyText}</Text>
|
|
) : null;
|
|
}
|
|
|
|
if (Array.isArray(value)) {
|
|
const visible = value.filter(
|
|
(item) => item !== null && item !== undefined && item !== "",
|
|
);
|
|
|
|
if (!visible.length) {
|
|
return emptyText ? (
|
|
<Text style={[styles.text, textStyle]}>{emptyText}</Text>
|
|
) : null;
|
|
}
|
|
|
|
if (visible.every((item) => typeof item === "string")) {
|
|
return <BulletList items={visible as string[]} />;
|
|
}
|
|
|
|
if (visible.some((item) => parseNavigableContentReference(item) !== null)) {
|
|
return <ReferenceChipList value={visible} />;
|
|
}
|
|
|
|
return (
|
|
<View style={styles.stack}>
|
|
{visible.map((item, index) => (
|
|
<ContentValue
|
|
key={contentValueKey(item, index)}
|
|
value={item}
|
|
textStyle={textStyle}
|
|
/>
|
|
))}
|
|
</View>
|
|
);
|
|
}
|
|
|
|
if (typeof value === "object") {
|
|
const entries = Object.entries(value as Record<string, unknown>).filter(
|
|
([, child]) =>
|
|
child !== null &&
|
|
child !== undefined &&
|
|
child !== "" &&
|
|
(!Array.isArray(child) || child.length > 0),
|
|
);
|
|
|
|
if (!entries.length) {
|
|
return emptyText ? (
|
|
<Text style={[styles.text, textStyle]}>{emptyText}</Text>
|
|
) : null;
|
|
}
|
|
|
|
return (
|
|
<View style={styles.object}>
|
|
{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 <BulletList items={bullets} />;
|
|
}
|
|
}
|
|
return <ContentValue value={child} textStyle={textStyle} />;
|
|
};
|
|
|
|
return (
|
|
<View
|
|
key={key}
|
|
style={[
|
|
styles.objectRow,
|
|
index > 0 && styles.divider,
|
|
]}
|
|
>
|
|
<Text style={styles.objectLabel}>{humanizeKey(key)}</Text>
|
|
{renderChild()}
|
|
</View>
|
|
);
|
|
})}
|
|
</View>
|
|
);
|
|
}
|
|
|
|
if (typeof value === "string") {
|
|
const text = value.trim();
|
|
if (text.includes("\n") || text.includes(";")) {
|
|
const bullets = parseBullets(text);
|
|
if (bullets.length > 1) {
|
|
return <BulletList items={bullets} />;
|
|
}
|
|
}
|
|
if (text.includes(",")) {
|
|
const parts = parseBullets(text);
|
|
if (
|
|
parts.length > 1 &&
|
|
parts.every((p) => p.length > 0 && p.length < 50 && !p.endsWith("."))
|
|
) {
|
|
return <BulletList items={parts} />;
|
|
}
|
|
}
|
|
}
|
|
|
|
return (
|
|
<Text style={[styles.text, textStyle]}>
|
|
{typeof value === "boolean" ? (value ? "Yes" : "No") : String(value)}
|
|
</Text>
|
|
);
|
|
}
|
|
|
|
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,
|
|
},
|
|
});
|