|
| 1 | +/** |
| 2 | + * @component ValueResponseField |
| 3 | + * @type displays |
| 4 | + * @subniche response-fields |
| 5 | + * @status stable |
| 6 | + * @description API response field with name, type, and value display. |
| 7 | + * @accepts children, className, style, ...props |
| 8 | + * @aiDiscoverability none |
| 9 | + * @param {React.ReactNode} description - Primary content rendered by the component. |
| 10 | + * @param {string} [post=null] - Post used by the component. |
| 11 | + * @param {string} [label="value"] - Label text rendered by the component. |
| 12 | + * @param {boolean} [line=true] - Boolean flag that controls component behaviour. |
| 13 | + * @param {React.ReactNode} children - Content rendered inside the component. |
| 14 | + * @param {object} [props] - Additional props forwarded to ResponseField. |
| 15 | + * @param {string} [className=""] - CSS class name. |
| 16 | + * @param {object} [style={}] - Inline style overrides. |
| 17 | + */ |
| 18 | +const ValueResponseField = ({ |
| 19 | + description, |
| 20 | + post = null, |
| 21 | + label = "value", |
| 22 | + line = true, |
| 23 | + children, |
| 24 | + className = "", |
| 25 | + style = {}, |
| 26 | + ...props |
| 27 | +}) => { |
| 28 | + const hasDescription = |
| 29 | + typeof description === "function" || description != null || children != null; |
| 30 | + |
| 31 | + if (!hasDescription) { |
| 32 | + console.warn("[ValueResponseField] Missing required prop: description"); |
| 33 | + return null; |
| 34 | + } |
| 35 | + |
| 36 | + const value = post |
| 37 | + ? [ |
| 38 | + <span> |
| 39 | + <span style={{ color: "gray" }}>{label}: </span> |
| 40 | + <span style={{ color: "var(--lp-color-response-field-value)" }}>{post[0]}</span> |
| 41 | + </span>, |
| 42 | + ] |
| 43 | + : null; |
| 44 | + |
| 45 | + return ( |
| 46 | + <div className={[!line ? "vrf-noline" : "", className].filter(Boolean).join(" ") || undefined} style={style}> |
| 47 | + <style>{` |
| 48 | + .vrf-noline > .field { |
| 49 | + border-bottom: none; |
| 50 | + margin-bottom: -0.5rem; |
| 51 | + padding: 0; |
| 52 | + } |
| 53 | + `}</style> |
| 54 | + <ResponseField {...props} post={value}> |
| 55 | + {typeof description === "function" ? description() : description} |
| 56 | + {children} |
| 57 | + </ResponseField> |
| 58 | + </div> |
| 59 | + ); |
| 60 | +}; |
| 61 | + |
| 62 | +const expandableCode = () => { |
| 63 | + return ( |
| 64 | + <Expandable title="Required Flags"> |
| 65 | + <ResponseField name="flag" type="type" default="hi"> |
| 66 | + Description |
| 67 | + </ResponseField> |
| 68 | + </Expandable> |
| 69 | + ); |
| 70 | +}; |
| 71 | + |
| 72 | +/** |
| 73 | + * @component CustomResponseField |
| 74 | + * @type displays |
| 75 | + * @subniche response-fields |
| 76 | + * @status stable |
| 77 | + * @description Custom-styled API response field with configurable margin. |
| 78 | + * @accepts className, style, ...props |
| 79 | + * @aiDiscoverability none |
| 80 | + * @param {React.ReactNode} description - Primary content rendered by the component. |
| 81 | + * @param {object} [props] - Additional props forwarded to ResponseField. |
| 82 | + * @param {string} [className=""] - CSS class name. |
| 83 | + * @param {object} [style={}] - Inline style overrides. |
| 84 | + */ |
| 85 | +const CustomResponseField = ({ description, className = "", style = {}, ...props }) => { |
| 86 | + const uniqueId = `custom-rf-${Math.random().toString(36).substring(2, 11)}`; |
| 87 | + |
| 88 | + return ( |
| 89 | + <div className={[uniqueId, className].filter(Boolean).join(" ")} style={style}> |
| 90 | + <style>{` |
| 91 | + .${uniqueId} > .field { |
| 92 | + border-bottom: none; |
| 93 | + margin-bottom: -20px; |
| 94 | + } |
| 95 | + `}</style> |
| 96 | + <ResponseField {...props}>{description}</ResponseField> |
| 97 | + </div> |
| 98 | + ); |
| 99 | +}; |
| 100 | + |
| 101 | +/** |
| 102 | + * @component ResponseFieldExpandable |
| 103 | + * @type displays |
| 104 | + * @subniche response-fields |
| 105 | + * @status stable |
| 106 | + * @description Expandable response field that reveals nested content on click. |
| 107 | + * @accepts className, style, ...props |
| 108 | + * @aiDiscoverability none |
| 109 | + * @param {object} [fields={}] - Fields used by the component. |
| 110 | + * @param {object} [props] - Additional props forwarded to Expandable. |
| 111 | + * @param {string} [className=""] - CSS class name. |
| 112 | + * @param {object} [style={}] - Inline style overrides. |
| 113 | + */ |
| 114 | +const ResponseFieldExpandable = ({ fields = {}, className = "", style = {}, ...props }) => { |
| 115 | + const fieldsArray = Array.isArray(fields) ? fields : Object.values(fields); |
| 116 | + if (fieldsArray.length === 0) { |
| 117 | + return null; |
| 118 | + } |
| 119 | + |
| 120 | + return ( |
| 121 | + <Expandable className={className} style={style} {...props}> |
| 122 | + {fieldsArray.map((field, index) => ( |
| 123 | + <ValueResponseField key={index} {...field} /> |
| 124 | + ))} |
| 125 | + </Expandable> |
| 126 | + ); |
| 127 | +}; |
| 128 | + |
| 129 | +/** |
| 130 | + * @component ResponseFieldAccordion |
| 131 | + * @type displays |
| 132 | + * @subniche response-fields |
| 133 | + * @status stable |
| 134 | + * @description Accordion-style response field with collapsible detail section. |
| 135 | + * @accepts className, style, ...props |
| 136 | + * @aiDiscoverability none |
| 137 | + * @param {object} [fields={}] - Fields used by the component. |
| 138 | + * @param {object} [props] - Additional props forwarded to Accordion. |
| 139 | + * @param {string} [className=""] - CSS class name. |
| 140 | + * @param {object} [style={}] - Inline style overrides. |
| 141 | + */ |
| 142 | +const ResponseFieldAccordion = ({ fields = {}, className = "", style = {}, ...props }) => { |
| 143 | + const fieldsArray = Array.isArray(fields) ? fields : Object.values(fields); |
| 144 | + if (fieldsArray.length === 0) { |
| 145 | + console.warn("[ResponseFieldAccordion] Missing or invalid fields"); |
| 146 | + return null; |
| 147 | + } |
| 148 | + |
| 149 | + return ( |
| 150 | + <Accordion className={className} style={style} {...props}> |
| 151 | + {fieldsArray.map((field, index) => ( |
| 152 | + <ValueResponseField key={index} {...field} /> |
| 153 | + ))} |
| 154 | + </Accordion> |
| 155 | + ); |
| 156 | +}; |
| 157 | + |
| 158 | +// Wrapper that chooses accordion or expandable layout at runtime. |
| 159 | +/** |
| 160 | + * @component ResponseFieldGroup |
| 161 | + * @type displays |
| 162 | + * @subniche response-fields |
| 163 | + * @status stable |
| 164 | + * @description Container for grouping multiple response fields with consistent spacing. |
| 165 | + * @accepts className, style, ...props |
| 166 | + * @aiDiscoverability none |
| 167 | + * @param {string} [component="accordion"] - Component used by the component. |
| 168 | + * @param {object} [fields={}] - Fields used by the component. |
| 169 | + * @param {object} [props] - Additional props forwarded to the selected wrapper component. |
| 170 | + * @param {string} [className=""] - CSS class name. |
| 171 | + * @param {object} [style={}] - Inline style overrides. |
| 172 | + */ |
| 173 | +const ResponseFieldGroup = ({ |
| 174 | + component = "accordion", |
| 175 | + fields = {}, |
| 176 | + className = "", |
| 177 | + style = {}, |
| 178 | + ...props |
| 179 | +}) => { |
| 180 | + const fieldsArray = Array.isArray(fields) ? fields : Object.values(fields); |
| 181 | + if (fieldsArray.length === 0) { |
| 182 | + console.warn("[ResponseFieldGroup] Missing or invalid fields"); |
| 183 | + return null; |
| 184 | + } |
| 185 | + |
| 186 | + const componentMap = { |
| 187 | + expandable: Expandable, |
| 188 | + accordion: Accordion, |
| 189 | + }; |
| 190 | + const Component = componentMap[component] || Accordion; |
| 191 | + return ( |
| 192 | + <Component className={className} style={style} {...props}> |
| 193 | + {fieldsArray.map((field, index) => ( |
| 194 | + <ValueResponseField key={index} {...field} /> |
| 195 | + ))} |
| 196 | + </Component> |
| 197 | + ); |
| 198 | +}; |
| 199 | + |
| 200 | +/** |
| 201 | + * @component FunctionField |
| 202 | + * @type displays |
| 203 | + * @subniche response-fields |
| 204 | + * @status stable |
| 205 | + * @description Solidity function signature field with typed parameter pairs and optional return type. |
| 206 | + * @accepts children, className, style, ...rest |
| 207 | + * @aiDiscoverability none |
| 208 | + * @param {string} name - Function name. |
| 209 | + * @param {string[]} [params=[]] - Parameter strings in "type name" format (e.g. "bytes32 _id"). |
| 210 | + * @param {string} [returns] - Return type (e.g. "address", "uint256"). |
| 211 | + * @param {React.ReactNode} children - Description of the function. |
| 212 | + * @param {boolean} [line=true] - Show bottom border. |
| 213 | + * @param {object} [style={}] - Inline style overrides. |
| 214 | + * @param {string} [className=""] - CSS class name. |
| 215 | + */ |
| 216 | +const FunctionField = ({ |
| 217 | + name, |
| 218 | + params = [], |
| 219 | + returns, |
| 220 | + children, |
| 221 | + line = true, |
| 222 | + style = {}, |
| 223 | + className = '', |
| 224 | + ...rest |
| 225 | +}) => { |
| 226 | + const paramsFormatted = params.length > 0 |
| 227 | + ? params.map(p => { |
| 228 | + const parts = p.split(/\s+/) |
| 229 | + const type = parts[0] |
| 230 | + const pName = parts.slice(1).join(' ') |
| 231 | + return pName ? `${pName}: ${type}` : type |
| 232 | + }).join(', ') |
| 233 | + : '' |
| 234 | + |
| 235 | + const returnType = returns ? `returns: ${returns}` : 'returns: void' |
| 236 | + |
| 237 | + const post = params.length > 0 |
| 238 | + ? [ |
| 239 | + <span> |
| 240 | + <span style={{ color: "white" }}>params: </span> |
| 241 | + <span style={{ color: "var(--lp-color-arbitrum, #3ea6f8)" }}>({paramsFormatted})</span> |
| 242 | + </span>, |
| 243 | + ] |
| 244 | + : null |
| 245 | + |
| 246 | + return ( |
| 247 | + <div className={className} style={style} {...rest}> |
| 248 | + <ResponseField name={name} type={returnType} post={post}> |
| 249 | + {children} |
| 250 | + </ResponseField> |
| 251 | + </div> |
| 252 | + ) |
| 253 | +} |
| 254 | + |
| 255 | +export { |
| 256 | + ValueResponseField, |
| 257 | + CustomResponseField, |
| 258 | + ResponseFieldExpandable, |
| 259 | + ResponseFieldAccordion, |
| 260 | + ResponseFieldGroup, |
| 261 | + FunctionField, |
| 262 | +}; |
0 commit comments