index.tsx
Overview
index.tsx defines the SingleDebugSheet React functional component. This component provides a user interface to debug a single "agent" or "component" by fetching and displaying its input form parameters and the debug results in JSON format. The UI enables users to modify input parameters, submit them for debugging, and view detailed JSON output, including error states if present. It also offers convenient features like JSON viewing with collapse and copy-to-clipboard functionality.
The component is typically used as a modal sheet (overlay panel) within a larger application that manages or interacts with agents/components requiring runtime debugging and testing.
Detailed Explanation
Component: SingleDebugSheet
Purpose
Renders a sheet/modal for debugging a single component by:
Fetching the component's input form structure.
Allowing the user to modify inputs.
Submitting inputs to trigger a debug request.
Displaying the debug result JSON with error highlighting.
Providing copy-to-clipboard support for the JSON output.
Props
SingleDebugSheet accepts the following props:
Prop Name | Type | Description |
|---|---|---|
|
| The unique identifier for the component to debug. |
|
| Controls visibility of the debug sheet modal. |
| () => void (from | Callback to close/hide the modal. |
Imports and Dependencies
UI components:
Sheet,SheetContent,SheetHeader(custom UI sheet components)CopyToClipboardfor copying JSON output textHooks:
useDebugSingle(to run debug requests),useFetchInputForm(fetch input form data)Helpers:
transferInputsArrayToObject,buildBeginInputListFromObject(transform input formats)Icons: X (close icon)
react18-json-viewfor JSON renderingDebugContentcomponent renders the form UI for input parametersuseTranslation for i18n text
Internal Logic
Fetching input form:
const inputForm = useFetchInputForm(componentId);Fetches the input form definition for the given
componentId.Fetching debug data:
const { debugSingle, data, loading } = useDebugSingle();Provides a
debugSinglefunction to invoke debugging, anddata/loadingstate for the result.Building input parameter list:
const list = useMemo(() => buildBeginInputListFromObject(inputForm), [inputForm]);Transforms the input form object into a list suitable for
DebugContent.Handling form submission:
const onOk = useCallback((nextValues: any[]) => { if (componentId) { debugSingle({ component_id: componentId, params: transferInputsArrayToObject(nextValues), }); } }, [componentId, debugSingle]);On submit, transforms the array of inputs back to object format and triggers debug.
Rendering:
Sheet UI container controlled by
visibleSheetHeader includes modal title and close icon
DebugContent renders the input form UI, with submission and loading states
Conditional rendering of JSON debug output using
JsonViewStyling changes depending on presence of
_ERRORproperty indataCopy to clipboard button for JSON content
Return Value
Returns a React element that renders the debug modal sheet UI.
Usage Example
import SingleDebugSheet from './index';
function MyDebugPage() {
const [visible, setVisible] = useState(false);
const componentId = "component-123";
return (
<>
<button onClick={() => setVisible(true)}>Open Debug Sheet</button>
<SingleDebugSheet
componentId={componentId}
visible={visible}
hideModal={() => setVisible(false)}
/>
</>
);
}
Important Implementation Details and Algorithms
Input transformation: Uses
buildBeginInputListFromObjectto convert the input form object into a list suitable for parameter editing, andtransferInputsArrayToObjectto convert the edited list back to an object for submission. This ensures compatibility between form UI and backend API.Conditional styling: The JSON output container applies an error border style if the debug result contains an
_ERRORfield, signaling a failure state.Performance optimization: Uses
useMemoanduseCallbackhooks to memoize derived data and callback functions, preventing unnecessary re-renders.Third-party JSON viewer: Integrates
react18-json-viewfor a rich, collapsible, and readable JSON display with a large collapse string length to avoid automatic collapsing.
Interaction With Other Parts of the System
Hooks (
useDebugSingle,useFetchInputForm): These hooks likely connect to global state managers or API layers to fetch input form metadata and perform debug requests respectively.DebugContentcomponent: Renders the dynamic form UI for input parameters, interacting with this component through props likeparameters,okcallback, and loading state.Utilities (
transferInputsArrayToObject,buildBeginInputListFromObject): Shared helper functions that transform input data formats between UI and backend API.UI components (
Sheet,SheetContent,SheetHeader): Provide modal sheet structure and styling consistent with the application design system.CopyToClipboard: Enables copying debug JSON results to the user's clipboard.
i18n: Uses
react-i18nextfor translated UI text.
Mermaid Component Diagram
componentDiagram
component SingleDebugSheet {
+componentId?: string
+visible: boolean
+hideModal(): void
--uses-->
Hook useFetchInputForm
Hook useDebugSingle
Component DebugContent
Component Sheet
Component CopyToClipboard
Component JsonView
}
SingleDebugSheet --> Sheet : wraps UI
Sheet --> SheetContent : contains content
SheetContent --> SheetHeader : contains header with close icon
SheetContent --> DebugContent : renders input form
SheetContent --> JsonView : renders JSON output
SheetContent --> CopyToClipboard : copy JSON text
Summary
The index.tsx file implements a reusable debugging UI modal for inspecting and testing single components or agents by fetching their input forms, allowing parameter edits, running debug requests, and viewing detailed JSON results. It is tightly integrated with the application's hooks, utilities, and UI components to provide a smooth and user-friendly debugging experience.