11"use client" ;
22
3- import React , { useState , useEffect } from "react" ;
3+ import React from "react" ;
4+ import { useState } from "react" ;
45import { useTranslation } from "react-i18next" ;
56import { Button , Col , Flex , Tooltip , Divider , Table , theme , App } from "antd" ;
67import { ExclamationCircleOutlined } from "@ant-design/icons" ;
@@ -17,58 +18,35 @@ import {
1718 exportAgent ,
1819 updateToolConfig ,
1920} from "@/services/agentConfigService" ;
21+ import { useAgentConfigStore } from "@/stores/agentConfigStore" ;
22+ import { useSaveGuard } from "@/hooks/agent/useSaveGuard" ;
2023import { clearAgentNewMark } from "@/services/agentConfigService" ;
2124import log from "@/lib/logger" ;
22- import { clearAgentAndSync } from "@/lib/agentNewUtils" ;
2325
2426interface AgentListProps {
2527 agentList : Agent [ ] ;
26- currentAgentId : number | null ;
27- hasUnsavedChanges : boolean ;
28- onSelectAgent : ( agent : Agent ) => void ;
29- onAgentDeleted ?: ( agentId : number ) => void ;
3028}
3129
3230export default function AgentList ( {
3331 agentList,
34- currentAgentId,
35- hasUnsavedChanges,
36- onSelectAgent,
37- onAgentDeleted,
3832} : AgentListProps ) {
3933 const { t } = useTranslation ( ) ;
4034 const { token } = theme . useToken ( ) ;
4135 const { message } = App . useApp ( ) ;
4236 const confirm = useConfirmModal ( ) ;
4337 const queryClient = useQueryClient ( ) ;
4438
45- // Note: rely on agent.is_new from agentList (single source of truth).
46- // Clear NEW mark when agent is selected (sync with selection visual feedback)
47- useEffect ( ( ) => {
48- if ( currentAgentId ) {
49- const agentId = String ( currentAgentId ) ;
50- const agent = agentList . find ( a => String ( a . id ) === agentId ) ;
51- if ( agent ?. is_new === true ) {
52- ( async ( ) => {
53- try {
54- const res = await clearAgentAndSync ( agentId , queryClient ) ;
55- if ( ! res ?. success ) {
56- log . warn ( "Failed to clear NEW mark for agent:" , agentId , res ) ;
57- }
58- } catch ( err ) {
59- log . error ( "Error clearing NEW mark:" , err ) ;
60- }
61- } ) ( ) ;
62- }
63- }
64- } , [ currentAgentId , agentList ] ) ;
65-
6639 // Call relationship modal state
6740 const [ callRelationshipModalVisible , setCallRelationshipModalVisible ] =
6841 useState ( false ) ;
6942 const [ selectedAgentForRelationship , setSelectedAgentForRelationship ] =
7043 useState < Agent | null > ( null ) ;
7144
45+ // Get state from store
46+ const currentAgentId = useAgentConfigStore ( ( state ) => state . currentAgentId ) ;
47+ const setCurrentAgent = useAgentConfigStore ( ( state ) => state . setCurrentAgent ) ;
48+ const hasUnsavedChanges = useAgentConfigStore ( ( state ) => state . hasUnsavedChanges ) ;
49+
7250 // Mutations
7351 const updateAgentMutation = useMutation ( {
7452 mutationFn : ( payload : any ) => updateAgentInfo ( payload ) ,
@@ -78,6 +56,9 @@ export default function AgentList({
7856 mutationFn : ( agentId : number ) => deleteAgent ( agentId ) ,
7957 } ) ;
8058
59+ // Unsaved changes guard
60+ const checkUnsavedChanges = useSaveGuard ( ) ;
61+
8162 // Handle view call relationship
8263 const handleViewCallRelationship = ( agent : Agent ) => {
8364 setSelectedAgentForRelationship ( agent ) ;
@@ -89,6 +70,53 @@ export default function AgentList({
8970 setSelectedAgentForRelationship ( null ) ;
9071 } ;
9172
73+ // Handle select agent
74+ const handleSelectAgent = async ( agent : Agent ) => {
75+ // Clear NEW mark when agent is selected for editing (only if marked as new)
76+ if ( agent . is_new === true ) {
77+ try {
78+ const res = await clearAgentNewMark ( agent . id ) ;
79+ if ( res ?. success ) {
80+ log . warn ( "Failed to clear NEW mark on select:" , res ) ;
81+ queryClient . invalidateQueries ( { queryKey : [ "agents" ] } ) ;
82+ }
83+ } catch ( err ) {
84+ log . error ( "Failed to clear NEW mark on select:" , err ) ;
85+ }
86+ }
87+
88+ // If already selected, deselect it
89+ if (
90+ currentAgentId !== null &&
91+ String ( currentAgentId ) === String ( agent . id )
92+ ) {
93+ const canDeselect = await checkUnsavedChanges . saveWithModal ( ) ;
94+ if ( canDeselect ) {
95+ setCurrentAgent ( null ) ;
96+ }
97+ return ;
98+ }
99+
100+ // Check if can switch (has unsaved changes)
101+ const canSwitch = await checkUnsavedChanges . saveWithModal ( ) ;
102+ if ( ! canSwitch ) {
103+ return ;
104+ }
105+
106+ // Load agent detail and set as current
107+ try {
108+ const result = await searchAgentInfo ( Number ( agent . id ) ) ;
109+ if ( result . success && result . data ) {
110+ setCurrentAgent ( result . data ) ;
111+ } else {
112+ message . error ( result . message || t ( "agentConfig.agents.detailsLoadFailed" ) ) ;
113+ }
114+ } catch ( error ) {
115+ log . error ( "Failed to load agent detail:" , error ) ;
116+ message . error ( t ( "agentConfig.agents.detailsLoadFailed" ) ) ;
117+ }
118+ } ;
119+
92120 // Handle export agent
93121 const handleExportAgent = async ( agent : Agent ) => {
94122 try {
@@ -244,12 +272,12 @@ export default function AgentList({
244272 } )
245273 ) ;
246274
247- // Notify parent component if this was the current agent
275+ // Clear current agent if this was the selected agent
248276 if (
249277 currentAgentId !== null &&
250278 String ( currentAgentId ) === String ( agent . id )
251279 ) {
252- onAgentDeleted ?. ( Number ( agent . id ) ) ;
280+ setCurrentAgent ( null ) ;
253281 }
254282
255283 // Refresh agent list
@@ -287,22 +315,23 @@ export default function AgentList({
287315 pagination = { false }
288316 showHeader = { false }
289317 rowClassName = { ( agent : any ) => {
318+ const isSelected =
319+ currentAgentId !== null &&
320+ String ( currentAgentId ) === String ( agent . id ) ;
290321 return `py-3 px-4 transition-colors border-gray-200 h-[80px] ${
291322 agent . is_available === false
292323 ? "opacity-60 cursor-not-allowed"
293324 : "hover:bg-gray-50 cursor-pointer"
294325 } ${
295- currentAgentId !== null &&
296- String ( currentAgentId ) === String ( agent . id )
297- ? "bg-blue-50 selected-row pl-3"
326+ isSelected ? "bg-blue-50 selected-row pl-3"
298327 : ""
299328 } `;
300329 } }
301330 onRow = { ( agent : any ) => ( {
302331 onClick : ( e : any ) => {
303332 e . preventDefault ( ) ;
304333 e . stopPropagation ( ) ;
305- onSelectAgent ( agent ) ;
334+ handleSelectAgent ( agent ) ;
306335 } ,
307336 } ) }
308337 columns = { [
0 commit comments