import { useState } from 'preact/hooks'; import { CopyToClipboard } from '~web/components/copy-to-clipboard'; import { Icon } from '~web/components/icon'; import { cn } from '~web/utils/helpers'; import { formatForClipboard, formatValuePreview, safeGetValue } from './utils'; const ArrayHeader = ({ length, expanded, onToggle, isNegative, }: { length: number; expanded: boolean; onToggle: () => void; isNegative: boolean; }) => (
Array({length})
); const TreeNode = ({ value, path, isNegative, }: { value: unknown; path: string; isNegative: boolean; }) => { const [isExpanded, setIsExpanded] = useState(false); const canExpand = value !== null && typeof value === 'object' && !(value instanceof Date); if (!canExpand) { return (
{path}: {formatValuePreview(value)}
); } const entries = Object.entries(value as object); return (
{path}: {!isExpanded && ( {value instanceof Date ? formatValuePreview(value) : `{${Object.keys(value).join(', ')}}`} )}
{isExpanded && (
{entries.map(([key, val]) => ( ))}
)}
); }; export const DiffValueView = ({ value, expanded, onToggle, isNegative, }: { value: unknown; expanded: boolean; onToggle: () => void; isNegative: boolean; }) => { const { value: safeValue, error } = safeGetValue(value); if (error) { return {error}; } const isExpandable = safeValue !== null && typeof safeValue === 'object' && !(safeValue instanceof Promise); if (!isExpandable) { return {formatValuePreview(safeValue)}; } if (Array.isArray(safeValue)) { return (
{expanded && (
{safeValue.map((item, index) => ( ))}
)} {({ ClipboardIcon }) => <>{ClipboardIcon}}
); } // Handle objects return (
{!expanded ? ( {formatValuePreview(safeValue)} ) : (
{Object.entries(safeValue as object).map(([key, val]) => ( ))}
)}
{({ ClipboardIcon }) => <>{ClipboardIcon}}
); };