use-rename-agent.ts
Overview
The use-rename-agent.ts file defines a custom React hook named useRenameAgent designed to manage the state and logic required for renaming an "agent" entity within an application. This hook encapsulates modal visibility control, form state management, and asynchronous update requests related to renaming an agent.
In essence, this hook provides a reusable abstraction for UI components that need to display a modal dialog to rename an agent, handle user input, perform backend update calls, and manage loading and error states seamlessly.
Detailed Explanation
useRenameAgent Hook
Purpose
To provide a clean and reusable interface to rename an agent.
To manage modal visibility state for the rename dialog.
To handle asynchronous update operations with loading state.
To initialize the modal with the selected agent's current data.
Internal State and Hooks
agent(state): Stores the current agent object (IFlow) being renamed.agentRenameVisible(boolean): Controls whether the rename modal is visible.hideAgentRenameModal(function): Callback to hide the rename modal.showAgentRenameModal(function): Callback to show the rename modal.updateAgentSetting(function): Async function to update agent data in the backend.loading(boolean): Indicates whether the update operation is in progress.
Functions / Methods
onAgentRenameOk
const onAgentRenameOk = useCallback(
async (name: string) => { ... },
[updateAgentSetting, agent, hideAgentRenameModal]
);
Description: Called when the user confirms the rename action. It sends an update request with the new agent name.
Parameters:
name: string— The new name/title for the agent.
Returns:
Promise<void>Behavior:
Uses
pickfromlodashto extract immutable fields (id,avatar,description,permission) from the currentagent.Calls
updateAgentSettingwith the extracted fields plus the updatedtitlefield.If the update returns
0(indicating success), it hides the rename modal.
Usage Example:
await onAgentRenameOk('New Agent Name');
handleShowAgentRenameModal
const handleShowAgentRenameModal = useCallback(
async (record: IFlow) => { ... },
[showAgentRenameModal]
);
Description: Prepares and shows the rename modal with the selected agent's data.
Parameters:
record: IFlow— The agent object to rename.
Returns:
Promise<void>Behavior:
Sets the internal
agentstate with therecordparameter.Calls
showAgentRenameModalto display the modal.
Usage Example:
await handleShowAgentRenameModal(selectedAgent);
Returned Object
The hook returns an object containing key state variables and handler functions for use in UI components:
Property | Type | Description |
|---|---|---|
|
| Indicates if the rename update request is in progress. |
|
|
|
|
| Handler to call when confirming the rename. |
|
| Controls the visibility of the rename modal. |
|
| Function to hide the rename modal. |
|
| Function to show the rename modal with a given agent. |
Important Implementation Details
State Management: Uses React's
useStatefor local state and custom hooks for modal visibility and API interaction.Modal Visibility: Controlled via
useSetModalStatehook which abstracts modal open/close logic.API Update: Uses
useUpdateAgentSettinghook which exposes anupdateAgentSettingasync function and aloadingstate.Partial Update: Only updates a subset of agent properties (
id,avatar,description,permission) plus the newtitleto avoid overwriting unrelated data.Asynchronous Flow: Both
onAgentRenameOkandhandleShowAgentRenameModalare asynchronous callbacks to accommodate async update calls and possible future side effects.Immutability: Uses
pickfromlodashto avoid mutating the original agent object.
Interaction with Other Parts of the System
useSetModalState(from '@/hooks/common-hooks'): Manages modal visibility state and providesshowModalandhideModalfunctions.useUpdateAgentSetting(from '@/hooks/use-agent-request'): Handles API requests to update agent settings on the backend, exposingupdateAgentSettingandloading.IFlowInterface (from '@/interfaces/database/agent'): Defines the structure of the agent object used throughout the hook.UI Components: This hook is intended to be used inside React components that render agent rename modals/dialogs, providing them with controlled state and handlers.
Usage Example
import React from 'react';
import { useRenameAgent } from './use-rename-agent';
const AgentRenameModal = () => {
const {
agentRenameVisible,
hideAgentRenameModal,
showAgentRenameModal,
initialAgentName,
onAgentRenameOk,
agentRenameLoading,
} = useRenameAgent();
// Example usage: show modal for a specific agent
// showAgentRenameModal(agent);
return (
<>
{agentRenameVisible && (
<Modal
title="Rename Agent"
visible={agentRenameVisible}
onCancel={hideAgentRenameModal}
onOk={() => onAgentRenameOk(newAgentName)}
confirmLoading={agentRenameLoading}
>
<Input defaultValue={initialAgentName} onChange={e => setNewAgentName(e.target.value)} />
</Modal>
)}
</>
);
};
Mermaid Diagram
classDiagram
class useRenameAgent {
- agent: IFlow
- agentRenameVisible: boolean
- updateAgentSetting: function
- loading: boolean
+ onAgentRenameOk(name: string): Promise<void>
+ handleShowAgentRenameModal(record: IFlow): Promise<void>
+ hideAgentRenameModal(): void
+ showAgentRenameModal(): void
+ agentRenameLoading: boolean
+ initialAgentName: string | undefined
}
useRenameAgent ..> IFlow : uses
useRenameAgent ..> useSetModalState : uses
useRenameAgent ..> useUpdateAgentSetting : uses
Summary
The use-rename-agent.ts file provides a specialized hook useRenameAgent for managing the renaming of agent entities within a React application. It abstracts away modal management, state handling, and API interactions, making it easy for UI components to integrate rename functionality with minimal boilerplate. The hook ensures data immutability and clean asynchronous operations, fitting seamlessly into a modular React architecture.