|
| 1 | +import { useState, memo } from "react"; |
| 2 | +import { JsonValue } from "./DynamicJsonForm"; |
| 3 | +import clsx from "clsx"; |
| 4 | + |
| 5 | +interface JsonViewProps { |
| 6 | + data: unknown; |
| 7 | + name?: string; |
| 8 | + initialExpandDepth?: number; |
| 9 | +} |
| 10 | + |
| 11 | +function tryParseJson(str: string): { success: boolean; data: JsonValue } { |
| 12 | + const trimmed = str.trim(); |
| 13 | + if ( |
| 14 | + !(trimmed.startsWith("{") && trimmed.endsWith("}")) && |
| 15 | + !(trimmed.startsWith("[") && trimmed.endsWith("]")) |
| 16 | + ) { |
| 17 | + return { success: false, data: str }; |
| 18 | + } |
| 19 | + try { |
| 20 | + return { success: true, data: JSON.parse(str) }; |
| 21 | + } catch { |
| 22 | + return { success: false, data: str }; |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +const JsonView = memo( |
| 27 | + ({ data, name, initialExpandDepth = 3 }: JsonViewProps) => { |
| 28 | + const normalizedData = |
| 29 | + typeof data === "string" |
| 30 | + ? tryParseJson(data).success |
| 31 | + ? tryParseJson(data).data |
| 32 | + : data |
| 33 | + : data; |
| 34 | + |
| 35 | + return ( |
| 36 | + <div className="font-mono text-sm transition-all duration-300 "> |
| 37 | + <JsonNode |
| 38 | + data={normalizedData as JsonValue} |
| 39 | + name={name} |
| 40 | + depth={0} |
| 41 | + initialExpandDepth={initialExpandDepth} |
| 42 | + /> |
| 43 | + </div> |
| 44 | + ); |
| 45 | + }, |
| 46 | +); |
| 47 | + |
| 48 | +JsonView.displayName = "JsonView"; |
| 49 | + |
| 50 | +interface JsonNodeProps { |
| 51 | + data: JsonValue; |
| 52 | + name?: string; |
| 53 | + depth: number; |
| 54 | + initialExpandDepth: number; |
| 55 | +} |
| 56 | + |
| 57 | +const JsonNode = memo( |
| 58 | + ({ data, name, depth = 0, initialExpandDepth }: JsonNodeProps) => { |
| 59 | + const [isExpanded, setIsExpanded] = useState(depth < initialExpandDepth); |
| 60 | + |
| 61 | + const getDataType = (value: JsonValue): string => { |
| 62 | + if (Array.isArray(value)) return "array"; |
| 63 | + if (value === null) return "null"; |
| 64 | + return typeof value; |
| 65 | + }; |
| 66 | + |
| 67 | + const dataType = getDataType(data); |
| 68 | + |
| 69 | + const typeStyleMap: Record<string, string> = { |
| 70 | + number: "text-blue-600", |
| 71 | + boolean: "text-amber-600", |
| 72 | + null: "text-purple-600", |
| 73 | + undefined: "text-gray-600", |
| 74 | + string: "text-green-600 break-all whitespace-pre-wrap", |
| 75 | + default: "text-gray-700", |
| 76 | + }; |
| 77 | + |
| 78 | + const renderCollapsible = (isArray: boolean) => { |
| 79 | + const items = isArray |
| 80 | + ? (data as JsonValue[]) |
| 81 | + : Object.entries(data as Record<string, JsonValue>); |
| 82 | + const itemCount = items.length; |
| 83 | + const isEmpty = itemCount === 0; |
| 84 | + |
| 85 | + const symbolMap = { |
| 86 | + open: isArray ? "[" : "{", |
| 87 | + close: isArray ? "]" : "}", |
| 88 | + collapsed: isArray ? "[ ... ]" : "{ ... }", |
| 89 | + empty: isArray ? "[]" : "{}", |
| 90 | + }; |
| 91 | + |
| 92 | + if (isEmpty) { |
| 93 | + return ( |
| 94 | + <div className="flex items-center"> |
| 95 | + {name && ( |
| 96 | + <span className="mr-1 text-gray-600 dark:text-gray-400"> |
| 97 | + {name}: |
| 98 | + </span> |
| 99 | + )} |
| 100 | + <span className="text-gray-500">{symbolMap.empty}</span> |
| 101 | + </div> |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + return ( |
| 106 | + <div className="flex flex-col"> |
| 107 | + <div |
| 108 | + className="flex items-center mr-1 rounded cursor-pointer group hover:bg-gray-800/10 dark:hover:bg-gray-800/20" |
| 109 | + onClick={() => setIsExpanded(!isExpanded)} |
| 110 | + > |
| 111 | + {name && ( |
| 112 | + <span className="mr-1 text-gray-600 dark:text-gray-400 dark:group-hover:text-gray-100 group-hover:text-gray-400"> |
| 113 | + {name}: |
| 114 | + </span> |
| 115 | + )} |
| 116 | + {isExpanded ? ( |
| 117 | + <span className="text-gray-600 dark:text-gray-400 dark:group-hover:text-gray-100 group-hover:text-gray-400"> |
| 118 | + {symbolMap.open} |
| 119 | + </span> |
| 120 | + ) : ( |
| 121 | + <> |
| 122 | + <span className="text-gray-600 dark:group-hover:text-gray-100 group-hover:text-gray-400"> |
| 123 | + {symbolMap.collapsed} |
| 124 | + </span> |
| 125 | + <span className="ml-1 text-gray-700 dark:group-hover:text-gray-100 group-hover:text-gray-400"> |
| 126 | + {itemCount} {itemCount === 1 ? "item" : "items"} |
| 127 | + </span> |
| 128 | + </> |
| 129 | + )} |
| 130 | + </div> |
| 131 | + {isExpanded && ( |
| 132 | + <> |
| 133 | + <div className="pl-2 ml-4 border-l border-gray-200 dark:border-gray-800"> |
| 134 | + {isArray |
| 135 | + ? (items as JsonValue[]).map((item, index) => ( |
| 136 | + <div key={index} className="my-1"> |
| 137 | + <JsonNode |
| 138 | + data={item} |
| 139 | + name={`${index}`} |
| 140 | + depth={depth + 1} |
| 141 | + initialExpandDepth={initialExpandDepth} |
| 142 | + /> |
| 143 | + </div> |
| 144 | + )) |
| 145 | + : (items as [string, JsonValue][]).map(([key, value]) => ( |
| 146 | + <div key={key} className="my-1"> |
| 147 | + <JsonNode |
| 148 | + data={value} |
| 149 | + name={key} |
| 150 | + depth={depth + 1} |
| 151 | + initialExpandDepth={initialExpandDepth} |
| 152 | + /> |
| 153 | + </div> |
| 154 | + ))} |
| 155 | + </div> |
| 156 | + <div className="text-gray-600 dark:text-gray-400"> |
| 157 | + {symbolMap.close} |
| 158 | + </div> |
| 159 | + </> |
| 160 | + )} |
| 161 | + </div> |
| 162 | + ); |
| 163 | + }; |
| 164 | + |
| 165 | + const renderString = (value: string) => { |
| 166 | + const maxLength = 100; |
| 167 | + const isTooLong = value.length > maxLength; |
| 168 | + |
| 169 | + if (!isTooLong) { |
| 170 | + return ( |
| 171 | + <div className="flex mr-1 rounded hover:bg-gray-800/20"> |
| 172 | + {name && ( |
| 173 | + <span className="mr-1 text-gray-600 dark:text-gray-400"> |
| 174 | + {name}: |
| 175 | + </span> |
| 176 | + )} |
| 177 | + <pre className={typeStyleMap.string}>"{value}"</pre> |
| 178 | + </div> |
| 179 | + ); |
| 180 | + } |
| 181 | + |
| 182 | + return ( |
| 183 | + <div className="flex mr-1 rounded group hover:bg-gray-800/20"> |
| 184 | + {name && ( |
| 185 | + <span className="mr-1 text-gray-600 dark:text-gray-400 dark:group-hover:text-gray-100 group-hover:text-gray-400"> |
| 186 | + {name}: |
| 187 | + </span> |
| 188 | + )} |
| 189 | + <pre |
| 190 | + className={clsx( |
| 191 | + typeStyleMap.string, |
| 192 | + "cursor-pointer group-hover:text-green-500", |
| 193 | + )} |
| 194 | + onClick={() => setIsExpanded(!isExpanded)} |
| 195 | + title={isExpanded ? "Click to collapse" : "Click to expand"} |
| 196 | + > |
| 197 | + {isExpanded ? `"${value}"` : `"${value.slice(0, maxLength)}..."`} |
| 198 | + </pre> |
| 199 | + </div> |
| 200 | + ); |
| 201 | + }; |
| 202 | + |
| 203 | + switch (dataType) { |
| 204 | + case "object": |
| 205 | + case "array": |
| 206 | + return renderCollapsible(dataType === "array"); |
| 207 | + case "string": |
| 208 | + return renderString(data as string); |
| 209 | + default: |
| 210 | + return ( |
| 211 | + <div className="flex items-center mr-1 rounded hover:bg-gray-800/20"> |
| 212 | + {name && ( |
| 213 | + <span className="mr-1 text-gray-600 dark:text-gray-400"> |
| 214 | + {name}: |
| 215 | + </span> |
| 216 | + )} |
| 217 | + <span className={typeStyleMap[dataType] || typeStyleMap.default}> |
| 218 | + {data === null ? "null" : String(data)} |
| 219 | + </span> |
| 220 | + </div> |
| 221 | + ); |
| 222 | + } |
| 223 | + }, |
| 224 | +); |
| 225 | + |
| 226 | +JsonNode.displayName = "JsonNode"; |
| 227 | + |
| 228 | +export default JsonView; |
0 commit comments