create-agent-modal.tsx
Overview
create-agent-modal.tsx defines a React functional component CreateAgentModal that provides a user interface modal dialog for creating a new "agent" entity by entering its name. The component integrates with Ant Design's Modal and Form components to handle form state, validation, and submission. It also supports internationalization using a translation hook.
This modal primarily serves as a controlled input dialog that:
Displays a form with a single input field for the agent's name.
Validates that the name is provided before submission.
Handles submission either via the modal's OK button or pressing Enter.
Shows loading state feedback during asynchronous operations triggered by submission.
Emits the entered name back to the parent component through a callback.
Detailed Explanation
Component: CreateAgentModal
Purpose
CreateAgentModal is designed to be rendered inside a modal manager system. It encapsulates the UI and logic needed to prompt the user for a new agent's name and notify the parent component when creation is confirmed.
Props (IProps)
Prop Name | Type | Description |
|---|---|---|
|
| Controls modal visibility. |
| Callback to close/hide the modal. | |
|
| Indicates whether a submission action is in progress, used to disable UI elements and show loading spinner. |
| Function called with the validated agent name when the user confirms creation. | |
() => void (optional) | Not used internally but inherited from modal manager props. |
Note: The interface IProps extends from IModalManagerChildrenProps but excludes the showModal property.
Internal Types
FieldType: An interface defining form field shape, currently with an optional
namestring property.
Internal State and Hooks
const [form] = Form.useForm();
Ant Design form instance to manage form data and validation.const { t } = useTranslate('common');
Translation function for internationalized strings scoped to the "common" namespace.
Key Methods
handleOk:
An async function that triggers form validation withform.validateFields(). If validation passes, it extracts thenamefield and calls theonOkcallback with it.
Returns the result ofonOk(potentially a Promise).handleKeyDown:
Event handler for keyboard input in the name field. If Enter is pressed without Shift, it triggershandleOkto submit the form immediately.
JSX Structure
The modal dialog wrapper with:title: localized string for "Create Graph".open: controlled byvisible.onOk: triggershandleOk.onCancel: triggershideModalto close modal.okButtonProps: passesloadingto disable and show spinner on OK button.confirmLoading: passesloadingto show loading indicator during submission.
Ant Design form configured for:Label and wrapper col layout.
Max width styling.
Auto-complete off.
Controlled by
forminstance.
<Form.Item>
Form field for "name" with:Label localized as "Name".
Required validation rule with localized placeholder message.
field with
onKeyDownhandler for Enter key submission.
Usage Example
import React, { useState } from 'react';
import CreateAgentModal from './create-agent-modal';
const ParentComponent = () => {
const [modalVisible, setModalVisible] = useState(false);
const [loading, setLoading] = useState(false);
const handleCreateAgent = async (name: string) => {
setLoading(true);
try {
// API call or logic to create agent with 'name'
console.log('Creating agent:', name);
// After success:
setModalVisible(false);
} catch (err) {
// Handle errors
} finally {
setLoading(false);
}
};
return (
<>
<button onClick={() => setModalVisible(true)}>New Agent</button>
<CreateAgentModal
visible={modalVisible}
hideModal={() => setModalVisible(false)}
loading={loading}
onOk={handleCreateAgent}
/>
</>
);
};
Important Implementation Details
Form Validation
Uses Ant Design'sform.validateFields()to ensure user input meets required constraints before submission. This prevents empty names from being accepted.Keyboard Accessibility
The form supports submitting via the Enter key on the input field (without Shift), improving usability for keyboard users.Loading State Handling
The component disables the OK button and shows a loading spinner while the parent component signals a submission is in progress via theloadingprop.Internationalization (i18n)
Texts such as modal title, field label, and validation messages are localized through a translation hookuseTranslatescoped to the 'common' namespace. This allows easy language switching.TypeScript Generics in Form.Item
The use of<Form.Item<FieldType>>provides type safety for the form fields, ensuring that the fieldnamematches the expected string type when validated.
Interaction with Other System Parts
Modal Manager
CreateAgentModalis designed to be used within a modal manager system, which controls the visibility and lifecycle of modals. It receives props likevisibleandhideModalfrom this system.Parent Components
Parent components provide theonOkcallback to receive the entered agent name and handle creation logic, such as API requests or state updates.Translation System
The component uses theuseTranslatehook, which is part of the app's internationalization infrastructure, to render localized UI strings.UI Library Integration
Utilizes Ant Design (antd) components for consistent styling and built-in form/modal functionality.
Visual Diagram
componentDiagram
direction LR
component CreateAgentModal {
+visible: boolean
+hideModal(): void
+loading: boolean
+onOk(name: string): void
-form: FormInstance
-handleOk(): Promise<void>
-handleKeyDown(e: KeyboardEvent): void
}
component Modal {
+title: string
+open: boolean
+onOk(): void
+onCancel(): void
+okButtonProps: object
+confirmLoading: boolean
}
component Form {
+name: string
+labelCol: object
+wrapperCol: object
+style: object
+autoComplete: string
+form: FormInstance
}
component FormItem {
+label: string
+name: string
+rules: array
}
component Input {
+onKeyDown(e: KeyboardEvent): void
}
CreateAgentModal --> Modal : renders >
Modal --> Form : contains >
Form --> FormItem : contains >
FormItem --> Input : contains >
Summary
The create-agent-modal.tsx file implements a reusable, internationalized modal dialog component for creating agents by entering a name. It leverages Ant Design's modal and form components for UI and validation, supports keyboard submission, and communicates with parent components through typed callbacks. This component fits within a modal management and translation system, forming a critical part of the user interface for agent creation workflows.