142 lines
3.4 KiB
TypeScript
142 lines
3.4 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";
|
|
|
|
export interface ContentValueProps {
|
|
value: unknown;
|
|
textStyle?: StyleProp<TextStyle>;
|
|
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 <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;
|
|
}
|
|
|
|
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]) => (
|
|
<View key={key} style={styles.objectRow}>
|
|
<Text style={styles.objectLabel}>{humanizeKey(key)}</Text>
|
|
<ContentValue value={child} textStyle={textStyle} />
|
|
</View>
|
|
))}
|
|
</View>
|
|
);
|
|
}
|
|
|
|
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,
|
|
},
|
|
objectLabel: {
|
|
fontSize: 12,
|
|
lineHeight: 17,
|
|
fontWeight: "700",
|
|
color: Colors.textSecondary,
|
|
textTransform: "uppercase",
|
|
letterSpacing: 0.35,
|
|
},
|
|
});
|