|
| 1 | +import { JSONArrayValueStruct, JSONBooleanValueStruct, JSONNullValueStruct, JSONNumberValueStruct, JSONObjectValueStruct, JSONStringValueStruct, JSONValueStruct, jsonItemCount } from "@/libs/json" |
| 2 | +import React from "react"; |
| 3 | +import { MdDataArray, MdDataObject } from 'react-icons/md'; |
| 4 | +import { BsToggleOff, BsToggleOn } from 'react-icons/bs'; |
| 5 | +import { TiSortAlphabetically, TiSortNumerically } from 'react-icons/ti'; |
| 6 | +import { InlineIcon } from "../lv1/InlineIcon"; |
| 7 | +import _ from "lodash"; |
| 8 | + |
| 9 | + |
| 10 | +const IconString = TiSortAlphabetically; |
| 11 | +const IconNumber = TiSortNumerically; |
| 12 | +const IconTrue = BsToggleOn; |
| 13 | +const IconFalse = BsToggleOff; |
| 14 | +const IconObject = MdDataObject; |
| 15 | +const IconArray = MdDataArray; |
| 16 | + |
| 17 | + |
| 18 | +const ActualIconForType = (props: { |
| 19 | + json: JSONValueStruct; |
| 20 | +}) => { |
| 21 | + switch (props.json.typename) { |
| 22 | + case "string": |
| 23 | + return IconString; |
| 24 | + case "number": |
| 25 | + return IconNumber; |
| 26 | + case "boolean": |
| 27 | + if (props.json.value) { |
| 28 | + return IconTrue; |
| 29 | + } else { |
| 30 | + return IconFalse; |
| 31 | + } |
| 32 | + case "object": |
| 33 | + return IconObject; |
| 34 | + case "array": |
| 35 | + return IconArray; |
| 36 | + default: |
| 37 | + return null; |
| 38 | + } |
| 39 | +}; |
| 40 | + |
| 41 | +const IconForType = (props: { |
| 42 | + json: JSONValueStruct; |
| 43 | +}) => { |
| 44 | + const Icon = ActualIconForType(props); |
| 45 | + if (!Icon) { return null; } |
| 46 | + return <InlineIcon className="text-xl" i={<Icon className="text-xl" />} /> |
| 47 | +} |
| 48 | + |
| 49 | +const JsonNullStructure = (props: { |
| 50 | + jkey?: string; |
| 51 | + js: JSONNullValueStruct; |
| 52 | +}) => { |
| 53 | + return (<div className="json-structure json-null-structure"> |
| 54 | + <p className="value-null">(null)</p> |
| 55 | + </div>); |
| 56 | +} |
| 57 | + |
| 58 | +const JsonStringStructure = (props: { |
| 59 | + jkey?: string; |
| 60 | + js: JSONStringValueStruct; |
| 61 | +}) => { |
| 62 | + return (<div className="json-structure json-string-structure flex items-center"> |
| 63 | + <p className="item-value"> |
| 64 | + "{props.js.value}" |
| 65 | + </p> |
| 66 | + </div>); |
| 67 | +} |
| 68 | + |
| 69 | +const JsonNumberStructure = (props: { |
| 70 | + jkey?: string; |
| 71 | + js: JSONNumberValueStruct; |
| 72 | +}) => { |
| 73 | + return (<div className="json-structure json-number-structure flex items-center"> |
| 74 | + <p className="item-value"> |
| 75 | + {props.js.value} |
| 76 | + </p> |
| 77 | + </div>); |
| 78 | +} |
| 79 | + |
| 80 | +const JsonBooleanStructure = (props: { |
| 81 | + jkey?: string; |
| 82 | + js: JSONBooleanValueStruct; |
| 83 | +}) => { |
| 84 | + return (<div className="json-structure json-boolean-structure flex items-center"> |
| 85 | + <p className="item-value"> |
| 86 | + {props.js.value |
| 87 | + ? <span className="value-true">True</span> |
| 88 | + : <span className="value-false">False</span>} |
| 89 | + </p> |
| 90 | + </div>); |
| 91 | +} |
| 92 | + |
| 93 | +const JsonObjectStructure = (props: { |
| 94 | + jkey?: string; |
| 95 | + js: JSONObjectValueStruct; |
| 96 | +}) => { |
| 97 | + return (<div className="json-structure json-object-structure grid grid-cols-[max-content_1fr] justify-stretch"> |
| 98 | + <p className="schema-key"> |
| 99 | + <span className="p-1">key</span> |
| 100 | + </p> |
| 101 | + <p className="schema-value"> |
| 102 | + <span className="p-1">value</span> |
| 103 | + </p> |
| 104 | + {props.js.subtree.map((v) => { |
| 105 | + const [k, value] = v; |
| 106 | + const key = `${props.jkey ? props.jkey + "." : ""}${k}`; |
| 107 | + const is_structural = value.typename === "object" || value.typename === "array"; |
| 108 | + const keyClassName = is_structural ? "item-key flex items-start" : "item-key flex items-center"; |
| 109 | + const itemCount = jsonItemCount(value); |
| 110 | + return ( |
| 111 | + <React.Fragment key={key}> |
| 112 | + <div className={keyClassName}> |
| 113 | + <div className="sticky top-0 flex items-center"> |
| 114 | + {is_structural ? null : <IconForType json={value} /> } |
| 115 | + <p className="key p-1">{k}</p> |
| 116 | + {is_structural ? <IconForType json={value} /> : null } |
| 117 | + { _.isFinite(itemCount) ? <p className="p-1">({itemCount})</p> : null } |
| 118 | + </div> |
| 119 | + </div> |
| 120 | + <div className="item-value flex items-center pl-1"> |
| 121 | + <JsonStructure jkey={key} js={value} /> |
| 122 | + </div> |
| 123 | + </React.Fragment> |
| 124 | + ); |
| 125 | + })} |
| 126 | + </div>); |
| 127 | +} |
| 128 | + |
| 129 | +const JsonArrayStructure = (props: { |
| 130 | + jkey?: string; |
| 131 | + js: JSONArrayValueStruct; |
| 132 | +}) => { |
| 133 | + return (<div className="json-structure json-array-structure grid grid-cols-[max-content_1fr] justify-stretch"> |
| 134 | + <p className="schema-index"> |
| 135 | + <span className="p-1">idx</span> |
| 136 | + </p> |
| 137 | + <p className="schema-count"> |
| 138 | + </p> |
| 139 | + |
| 140 | + {props.js.subarray.map((v, i) => { |
| 141 | + const key = `${props.jkey ? props.jkey + "." : ""}${i}`; |
| 142 | + const is_structural = v.typename === "object" || v.typename === "array"; |
| 143 | + const keyClassName = is_structural ? "item-index flex items-start" : "item-index flex items-center"; |
| 144 | + const itemCount = jsonItemCount(v); |
| 145 | + return (<React.Fragment key={key}> |
| 146 | + |
| 147 | + <div className={keyClassName}> |
| 148 | + <div className="sticky top-0 flex items-center"> |
| 149 | + {is_structural ? null: <IconForType json={v} /> } |
| 150 | + <p className="index p-1 font-bold">#{i}</p> |
| 151 | + {is_structural ? <IconForType json={v} /> : null } |
| 152 | + { _.isFinite(itemCount) ? <p className="p-1">({itemCount})</p> : null } |
| 153 | + </div> |
| 154 | + </div> |
| 155 | + |
| 156 | + <div className="item-value flex items-center pl-1"> |
| 157 | + <JsonStructure jkey={key} js={v} /> |
| 158 | + </div> |
| 159 | + |
| 160 | + </React.Fragment>) |
| 161 | + })} |
| 162 | + </div>); |
| 163 | +} |
| 164 | + |
| 165 | +export const JsonStructure = (props: { |
| 166 | + jkey?: string; |
| 167 | + js: JSONValueStruct; |
| 168 | +}) => { |
| 169 | + if (props.js.typename === "string") { |
| 170 | + return <JsonStringStructure jkey={props.jkey} js={props.js} /> |
| 171 | + } |
| 172 | + if (props.js.typename === "number") { |
| 173 | + return <JsonNumberStructure jkey={props.jkey} js={props.js} /> |
| 174 | + } |
| 175 | + if (props.js.typename === "boolean") { |
| 176 | + return <JsonBooleanStructure jkey={props.jkey} js={props.js} /> |
| 177 | + } |
| 178 | + if (props.js.typename === "object") { |
| 179 | + return <JsonObjectStructure jkey={props.jkey} js={props.js} /> |
| 180 | + } |
| 181 | + if (props.js.typename === "array") { |
| 182 | + return <JsonArrayStructure jkey={props.jkey} js={props.js} /> |
| 183 | + } |
| 184 | + if (props.js.typename === "null") { |
| 185 | + return <JsonNullStructure jkey={props.jkey} js={props.js} /> |
| 186 | + } |
| 187 | + return null; |
| 188 | +}; |
0 commit comments