import React, { useCallback, useMemo, useRef, useState } from 'react'; import { View, Text, ScrollView, Pressable, StyleSheet, type TextStyle, } from 'react-native'; import { Colors } from '../theme/colors'; import { FontSize, FontWeight, Spacing, Radius } from '../theme/layout'; const MAX_DEPTH = 8; const MAX_CHILDREN = 100; export const JsonView: React.FC<{ data: unknown; maxHeight?: number; /** Top-level object keys to emphasize (e.g. zustand changed fields). */ highlightKeys?: readonly string[]; }> = React.memo(({ data, maxHeight, highlightKeys, }) => { const [collapsed, setCollapsed] = useState>(new Set()); const prevDataRef = useRef(data); if (prevDataRef.current !== data) { prevDataRef.current = data; setCollapsed(new Set()); } const highlightSet = useMemo( () => (highlightKeys && highlightKeys.length > 0 ? new Set(highlightKeys) : null), [highlightKeys], ); const toggle = useCallback((path: string) => { setCollapsed(prev => { const next = new Set(prev); if (next.has(path)) next.delete(path); else next.add(path); return next; }); }, []); return ( ); }); const Node: React.FC<{ value: unknown; depth: number; isLast: boolean; path: string; collapsed: Set; toggle: (path: string) => void; highlightKeys: Set | null; }> = ({ value, depth, isLast, path, collapsed, toggle, highlightKeys }) => { const comma = isLast ? '' : ','; if (value === null) return {`null${comma}`}; if (value === undefined) return {`undefined${comma}`}; if (typeof value === 'boolean') return {`${String(value)}${comma}`}; if (typeof value === 'number') return {`${value}${comma}`}; if (typeof value === 'string') { const display = value.length > 500 ? value.slice(0, 500) + '...' : value; return {`"${display}"${comma}`}; } const entries = Object.entries(value as Record); const isArray = Array.isArray(value); const open = isArray ? '[' : '{'; const close = isArray ? ']' : '}'; if (depth >= MAX_DEPTH) { return {`${open}...${close}${comma}`}; } if (entries.length === 0) return {`${open}${close}${comma}`}; const isCollapsed = collapsed.has(path); const count = entries.length; // Collapsed: show summary if (isCollapsed) { const summary = isArray ? `${open}..${count}..${close}${comma}` : `${open} ${count} keys ${close}${comma}`; return ( toggle(path)} style={s.collapsedRow}> {'▸ '} {summary} ); } const limited = entries.slice(0, MAX_CHILDREN); return ( 0 ? s.indent : undefined}> toggle(path)} style={s.toggleRow}> {'▾ '} {open} {!isArray && count > 3 && ( {` // ${count} keys`} )} {isArray && count > 3 && ( {` // ${count} items`} )} {limited.map(([key, val], i) => { const highlighted = depth === 0 && !isArray && highlightKeys?.has(key); return ( {!isArray && ( {` "${key}"`} )} {!isArray && {': '}} ); })} {entries.length > MAX_CHILDREN && ( {` ... ${entries.length - MAX_CHILDREN} more`} )} {`${close}${comma}`} ); }; const C: React.FC<{ color: string; children: string; selectable?: boolean; weight?: TextStyle['fontWeight']; }> = ({ color, children, selectable, weight, }) => ( {children} ); const s = StyleSheet.create({ scroll: { backgroundColor: Colors.codeBackground, borderRadius: Radius.LG, padding: Spacing.MD, }, block: { borderLeftWidth: 2, borderLeftColor: Colors.codeBorder, }, node: { fontFamily: 'Courier', fontSize: FontSize.SM, lineHeight: 17, color: Colors.codeText, }, indent: { paddingLeft: Spacing.LG, }, line: { flexDirection: 'row', flexWrap: 'wrap', borderRadius: Radius.XS, paddingVertical: 1, }, highlightedLine: { backgroundColor: Colors.warningDim, borderLeftWidth: 2, borderLeftColor: Colors.warning, marginLeft: -2, paddingLeft: 2, }, row: {}, toggleRow: { flexDirection: 'row', flexWrap: 'wrap', alignItems: 'center', }, collapsedRow: { flexDirection: 'row', flexWrap: 'wrap', alignItems: 'center', }, });