From 19c36c6d11c9a8ed4e5218ee4be42654b7f51e59 Mon Sep 17 00:00:00 2001 From: Asharib Ali Date: Sat, 22 Feb 2025 06:15:52 +0500 Subject: [PATCH] Add Paste JSON Schema Functionality to Custom Tools (#4053) feat: add paste JSON schema functionality to custom tools - Add PasteJSONDialog component for JSON input - Add Paste JSON button to Tool Dialog - Support JSON validation and format conversion - Add example JSON template --- .../ui/src/views/tools/PasteJSONDialog.jsx | 87 +++++++++++++++++++ packages/ui/src/views/tools/ToolDialog.jsx | 30 ++++++- 2 files changed, 113 insertions(+), 4 deletions(-) create mode 100644 packages/ui/src/views/tools/PasteJSONDialog.jsx diff --git a/packages/ui/src/views/tools/PasteJSONDialog.jsx b/packages/ui/src/views/tools/PasteJSONDialog.jsx new file mode 100644 index 00000000..02778aa1 --- /dev/null +++ b/packages/ui/src/views/tools/PasteJSONDialog.jsx @@ -0,0 +1,87 @@ +import { createPortal } from 'react-dom' +import PropTypes from 'prop-types' +import { useState } from 'react' +import { Box, Button, Dialog, DialogActions, DialogContent, DialogTitle } from '@mui/material' +import { StyledButton } from '@/ui-component/button/StyledButton' +import { CodeEditor } from '@/ui-component/editor/CodeEditor' + +const PasteJSONDialog = ({ show, onCancel, onConfirm, customization }) => { + const portalElement = document.getElementById('portal') + const [jsonInput, setJsonInput] = useState('') + const [error, setError] = useState('') + + const handleConfirm = () => { + try { + const parsedJSON = JSON.parse(jsonInput) + if (!Array.isArray(parsedJSON)) throw new Error('Input must be an array of properties') + const formattedData = parsedJSON.map((item, index) => ({ + id: index + 1, + property: item.property || '', + type: item.type || 'string', + description: item.description || '', + required: item.required || false + })) + onConfirm(formattedData) + setError('') + } catch (err) { + setError('Invalid JSON format. Please check your input.') + } + } + + const exampleJSON = `[ + { + "property": "name", + "type": "string", + "description": "User's name", + "required": true + }, + { + "property": "age", + "type": "number", + "description": "User's age", + "required": false + } +]` + + const component = show ? ( + + + Paste JSON Schema + + + + + { + setJsonInput(code) + setError('') + }} + /> + {error && {error}} + + + + + + Confirm + + + + ) : null + + return createPortal(component, portalElement) +} + +PasteJSONDialog.propTypes = { + show: PropTypes.bool, + onCancel: PropTypes.func, + onConfirm: PropTypes.func, + customization: PropTypes.object +} + +export default PasteJSONDialog diff --git a/packages/ui/src/views/tools/ToolDialog.jsx b/packages/ui/src/views/tools/ToolDialog.jsx index 8be2a325..de048b63 100644 --- a/packages/ui/src/views/tools/ToolDialog.jsx +++ b/packages/ui/src/views/tools/ToolDialog.jsx @@ -14,9 +14,10 @@ import DeleteIcon from '@mui/icons-material/Delete' import ConfirmDialog from '@/ui-component/dialog/ConfirmDialog' import { CodeEditor } from '@/ui-component/editor/CodeEditor' import HowToUseFunctionDialog from './HowToUseFunctionDialog' +import PasteJSONDialog from './PasteJSONDialog' // Icons -import { IconX, IconFileDownload, IconPlus, IconTemplate } from '@tabler/icons-react' +import { IconX, IconFileDownload, IconPlus, IconTemplate, IconCode } from '@tabler/icons-react' // API import toolsApi from '@/api/tools' @@ -83,6 +84,8 @@ const ToolDialog = ({ show, dialogProps, onUseTemplate, onCancel, onConfirm, set const [exportAsTemplateDialogOpen, setExportAsTemplateDialogOpen] = useState(false) const [exportAsTemplateDialogProps, setExportAsTemplateDialogProps] = useState({}) + const [showPasteJSONDialog, setShowPasteJSONDialog] = useState(false) + const deleteItem = useCallback( (id) => () => { setTimeout(() => { @@ -409,6 +412,11 @@ const ToolDialog = ({ show, dialogProps, onUseTemplate, onCancel, onConfirm, set } } + const handlePastedJSON = (formattedData) => { + setToolSchema(formattedData) + setShowPasteJSONDialog(false) + } + const component = show ? ( {dialogProps.type !== 'TEMPLATE' && ( - + + + + )} @@ -577,6 +590,15 @@ const ToolDialog = ({ show, dialogProps, onUseTemplate, onCancel, onConfirm, set )} setShowHowToDialog(false)} /> + + {showPasteJSONDialog && ( + setShowPasteJSONDialog(false)} + onConfirm={handlePastedJSON} + customization={customization} + /> + )} ) : null