|
| 1 | +/** |
| 2 | + * Copyright 2025 The Vitess Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import React, { useState } from 'react'; |
| 18 | +import { JSONTree } from 'react-json-tree'; |
| 19 | + |
| 20 | +const vtAdminTheme = { |
| 21 | + scheme: 'vtadmin', |
| 22 | + author: 'custom', |
| 23 | + base00: '#ffffff', |
| 24 | + base01: '#f6f8fa', |
| 25 | + base02: '#e6e8eb', |
| 26 | + base03: '#8c8c8c', |
| 27 | + base04: '#3c3c3c', |
| 28 | + base05: '#2c2c2c', |
| 29 | + base06: '#0057b8', |
| 30 | + base07: '#000000', |
| 31 | + base08: '#00875a', |
| 32 | + base09: '#2c2c2c', |
| 33 | + base0A: '#e44d26', |
| 34 | + base0B: '#2c2c2c', |
| 35 | + base0C: '#1a73e8', |
| 36 | + base0D: '#3d5afe', |
| 37 | + base0E: '#3cba54', |
| 38 | + base0F: '#ff6f61', |
| 39 | +}; |
| 40 | + |
| 41 | +interface JSONViewTreeProps { |
| 42 | + data: any; |
| 43 | +} |
| 44 | + |
| 45 | +const JSONViewTree: React.FC<JSONViewTreeProps> = ({ data }) => { |
| 46 | + const [expandAll, setExpandAll] = useState(false); |
| 47 | + const [treeKey, setTreeKey] = useState(0); |
| 48 | + |
| 49 | + const handleExpand = () => { |
| 50 | + setExpandAll(true); |
| 51 | + setTreeKey((prev) => prev + 1); |
| 52 | + }; |
| 53 | + |
| 54 | + const handleCollapse = () => { |
| 55 | + setExpandAll(false); |
| 56 | + setTreeKey((prev) => prev + 1); |
| 57 | + }; |
| 58 | + |
| 59 | + const getItemString = (type: string, data: any) => { |
| 60 | + if (Array.isArray(data)) { |
| 61 | + return `${type}[${data.length}]`; |
| 62 | + } |
| 63 | + return type; |
| 64 | + }; |
| 65 | + |
| 66 | + if (!data) return null; |
| 67 | + return ( |
| 68 | + <div className="p-1"> |
| 69 | + <div className="flex mt-2 gap-2"> |
| 70 | + <button onClick={handleExpand} className="btn btn-secondary btn-sm"> |
| 71 | + Expand All |
| 72 | + </button> |
| 73 | + <button onClick={handleCollapse} className="btn btn-danger bg-transparent text-red-500 btn-sm"> |
| 74 | + Collapse All |
| 75 | + </button> |
| 76 | + </div> |
| 77 | + <JSONTree |
| 78 | + key={treeKey} |
| 79 | + data={data} |
| 80 | + theme={{ |
| 81 | + extend: vtAdminTheme, |
| 82 | + nestedNodeItemString: { |
| 83 | + color: vtAdminTheme.base0C, |
| 84 | + }, |
| 85 | + }} |
| 86 | + invertTheme={false} |
| 87 | + hideRoot={true} |
| 88 | + getItemString={getItemString} |
| 89 | + shouldExpandNodeInitially={() => expandAll} |
| 90 | + /> |
| 91 | + </div> |
| 92 | + ); |
| 93 | +}; |
| 94 | + |
| 95 | +export default JSONViewTree; |
0 commit comments