(props: JsonTextBoxProps)
| 509 | }; |
| 510 | |
| 511 | function JsonTextBox(props: JsonTextBoxProps) { |
| 512 | const [text, setText] = useState( |
| 513 | // Just format the text once at the start to prevent formatting mid-editing |
| 514 | textToJson(JSON.stringify(props.action[props.title])), |
| 515 | ); |
| 516 | useEffect(() => { |
| 517 | setText(textToJson(JSON.stringify(props.action[props.title]))); |
| 518 | }, [props.action]); |
| 519 | |
| 520 | return ( |
| 521 | <> |
| 522 | <div className="w-full px-6 py-0.5 flex flex-row justify-between place-items-start overflow-hidden"> |
| 523 | <div className="flex flex-col w-32"> |
| 524 | <div |
| 525 | className={classNames( |
| 526 | "font-bold text-lg mt-4", |
| 527 | props.disabled ? "text-gray-500" : "text-gray-100", |
| 528 | )} |
| 529 | > |
| 530 | {props.title !== "keys_to_keep" |
| 531 | ? props.title.charAt(0).toUpperCase() + |
| 532 | props.title.slice(1).replace(/_/g, " ") |
| 533 | : "Include these keys in response"} |
| 534 | </div> |
| 535 | {props.title === "keys_to_keep" && ( |
| 536 | <div |
| 537 | className={classNames( |
| 538 | "mt-1 text-sm", |
| 539 | props.disabled ? "text-gray-600" : "text-gray-400", |
| 540 | )} |
| 541 | > |
| 542 | Comma-separated list of keys |
| 543 | </div> |
| 544 | )} |
| 545 | </div> |
| 546 | <AutoGrowingTextArea |
| 547 | className={classNames( |
| 548 | "border border-gray-700 flex-1 px-4 py-3 font-mono text-sm rounded whitespace-pre-wrap resize-none overflow-hidden", |
| 549 | props.disabled |
| 550 | ? "bg-gray-700 text-gray-800" |
| 551 | : "bg-gray-50 text-black", |
| 552 | )} |
| 553 | onChange={(e) => { |
| 554 | setText(e.target.value); |
| 555 | props.setValidJSON(isJsonString(e.target.value)); |
| 556 | }} |
| 557 | value={text} |
| 558 | placeholder={"{}"} |
| 559 | onBlur={(e) => { |
| 560 | try { |
| 561 | const textarea = e.target as HTMLTextAreaElement; |
| 562 | textarea.value = textToJson(textarea.value); |
| 563 | const newAction = props.action; |
| 564 | newAction[props.title] = JSON.parse(e.target.value); |
| 565 | props.setLocalAction(newAction); |
| 566 | props.setValidJSON(true); |
| 567 | } catch (error) { |
| 568 | props.setValidJSON(false); |
nothing calls this directly
no test coverage detected