agent-template-modal.tsx
Overview
agent-template-modal.tsx defines a React functional component named AgentTemplateModal, which provides a user interface modal dialog for creating new "agents" (or flows) based on existing flow templates. This modal presents a list of flow templates in a card grid format, allowing users to preview and select a template to create a new agent. Once a template is selected, a secondary modal (CreateAgentModal) is displayed to capture additional input (such as naming the new agent) before finalizing the creation process.
This component integrates localization, safe HTML rendering, and modal state management. It is designed to be part of a larger workflow/agent management system where users manage flow-based processes or automations.
Detailed Explanation
Component: AgentTemplateModal
Purpose
To display a modal window that lists available flow templates for the user to select from, and to handle the creation of new agents based on the selected template.
Props (IProps)
Prop Name | Type | Description |
|---|---|---|
|
| Controls the visibility of the modal. |
| Callback to hide/close the modal. | |
|
| Indicates loading state, typically during agent creation. |
| [(name: string, templateId: string) => void \ | Promise](/projects/311/72190) |
() => void (optional) | Optional method to show the modal externally. | |
| Array of flow templates to display in the list. Each template includes metadata such as title, description, and avatar. |
Internal State
selectedId: string (from
useSelectItem)
Tracks which template card the user is currently interacting with (hovered/selected).checkedId: string
Stores the ID of the currently chosen template for creating a new agent.Modal visibility state for the nested
CreateAgentModalis managed via useSetModalState() hook, providingvisible,showModal, andhideModalfunctions.
Hooks Used
useTranslate('common')
Provides translation functiontscoped to the 'common' namespace.useSelectItem('')
Custom hook to manage selection state of list items. ProvidesselectedIdandhandleItemClick(id: string).useSetModalState()
Custom hook to manage modal visibility states (open, close, toggle).useCallbackand useState from React for memoizing callbacks and managing local state.
Rendered UI Elements
Modal (Ant Design):
Main modal container with title, width, cancel handler, and custom CSS classes.List (Ant Design):
A grid list of cards, each representing a flow template.Card (Ant Design):
Each card shows:An avatar rendered by
GraphAvatar(likely a component that displays a graphical representation or icon).The template title with ellipsis and tooltip.
The template description rendered as sanitized HTML using
DOMPurifyto prevent XSS.A conditional "Use Template" button shown only when the card is selected (hovered).
CreateAgentModal:
Nested modal shown when the user clicks "Use Template", allowing the user to input additional details (e.g., name) before creating the agent.
Key Methods
handleOk(name: string): Promise
Invokes theonOkprop callback with the user-provided name and the currently checked template ID. Memoized withuseCallbackto avoid unnecessary re-renders.onShowCreatingModal(id: string): () => void
Returns a function that shows theCreateAgentModaland sets the chosen template ID (checkedId). Used as the click handler for the "Use Template" button.
Usage Example
<AgentTemplateModal
visible={isModalVisible}
hideModal={() => setModalVisible(false)}
loading={isCreating}
onOk={async (name, templateId) => {
await createAgent(name, templateId);
setModalVisible(false);
}}
templateList={flowTemplates}
/>
Important Implementation Details
Sanitizing Template Descriptions:
Template descriptions may contain HTML. To prevent XSS attacks, the descriptions are sanitized usingDOMPurifybefore being rendered withdangerouslySetInnerHTML.Selection Handling:
Template cards use mouse enter/leave events to manage the "selected" state. When a card is hovered, its ID is passed tohandleItemClickto updateselectedId. This controls the visibility of the "Use Template" button on that card.Modal State Management:
The component uses a custom hookuseSetModalStateto control the visibility of the nestedCreateAgentModalseparately from the main modal.Asynchronous Handling:
ThehandleOkmethod supports async operations by returningonOkwhich can be a promise, enabling integration with backend calls or other async flows.Styling:
CSS modules (index.less) provide scoped styles for the modal and its components, ensuring no leakage or conflicts with global styles.
Interaction With Other Parts of the Application
IFlowTemplateinterface:
ThetemplateListprop expects an array of flow template objects conforming to this interface, which likely includes fields such asid,title,description, andavatar.CreateAgentModalcomponent:
This nested modal is triggered when the user chooses to create an agent from a template. It captures additional input for the creation process.GraphAvatarcomponent:
Used to show a graphical representation or icon for each flow template.Hooks (
useSelectItem,useSetModalState,useTranslate):
These are common hooks used across the application for selection logic, modal visibility management, and internationalization respectively.Modal Manager:
The props extendIModalManagerChildrenProps(exceptshowModal), indicating this component integrates with a modal management system in the app.
Visual Diagram
componentDiagram
direction LR
AgentTemplateModal --|> React.Component
AgentTemplateModal --> Modal : renders
AgentTemplateModal --> List : renders template cards
List --> Card : multiple cards for templates
Card --> GraphAvatar : displays avatar
Card --> Button : "Use Template" button (conditional)
AgentTemplateModal --> CreateAgentModal : nested modal for creation input
AgentTemplateModal ..> useSelectItem : manages selection state
AgentTemplateModal ..> useSetModalState : manages nested modal visibility
AgentTemplateModal ..> useTranslate : localization
Card --> DOMPurify : sanitizes description HTML
Summary
agent-template-modal.tsx is a React component that allows users to select a flow template from a list and create a new agent based on that template. It features a clean and interactive UI using Ant Design components, integrates localization, safe HTML rendering, and modal state management hooks, and connects to other components like CreateAgentModal and GraphAvatar to provide a cohesive user experience in the agent creation workflow.