@@ -42,37 +42,26 @@ export default function AgentList({
4242 const confirm = useConfirmModal ( ) ;
4343 const queryClient = useQueryClient ( ) ;
4444
45- // Local selected agent ID for optimistic UI update (immediate visual feedback)
46- const [ locallySelectedAgentId , setLocallySelectedAgentId ] = useState < string | null > ( null ) ;
47-
48- // Track cleared NEW marks to avoid redundant API calls and enable optimistic UI update
49- const clearedNewMarkIds = React . useRef < Set < string > > ( new Set ( ) ) ;
50-
51- // Handle agent selection and clear NEW mark if needed
52- const handleAgentSelect = async ( agent : Agent ) => {
53- // Optimistic update: immediately set selected state for UI
54- setLocallySelectedAgentId ( String ( agent . id ) ) ;
55-
56- // Optimistic update: immediately mark as cleared for NEW label
57- if ( agent . is_new === true ) {
58- clearedNewMarkIds . current . add ( String ( agent . id ) ) ;
59- }
60-
61- // Only clear NEW mark if agent is marked as new and hasn't been cleared yet
62- if ( agent . is_new === true && ! clearedNewMarkIds . current . has ( String ( agent . id ) ) ) {
63- try {
64- const res = await clearAgentAndSync ( String ( agent . id ) , queryClient ) ;
65- if ( res ?. success ) {
66- clearedNewMarkIds . current . add ( String ( agent . id ) ) ;
67- } else {
68- log . warn ( "Failed to clear NEW mark for agent:" , agent . id , res ) ;
69- }
70- } catch ( err ) {
71- log . error ( "Error clearing NEW mark:" , err ) ;
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+ } ) ( ) ;
7262 }
7363 }
74- onSelectAgent ( agent ) ;
75- } ;
64+ } , [ currentAgentId , agentList ] ) ;
7665
7766 // Call relationship modal state
7867 const [ callRelationshipModalVisible , setCallRelationshipModalVisible ] =
@@ -303,9 +292,8 @@ export default function AgentList({
303292 ? "opacity-60 cursor-not-allowed"
304293 : "hover:bg-gray-50 cursor-pointer"
305294 } ${
306- // Use local state for immediate visual feedback
307- locallySelectedAgentId !== null &&
308- String ( locallySelectedAgentId ) === String ( agent . id )
295+ currentAgentId !== null &&
296+ String ( currentAgentId ) === String ( agent . id )
309297 ? "bg-blue-50 selected-row pl-3"
310298 : ""
311299 } `;
@@ -314,7 +302,7 @@ export default function AgentList({
314302 onClick : ( e : any ) => {
315303 e . preventDefault ( ) ;
316304 e . stopPropagation ( ) ;
317- handleAgentSelect ( agent ) ;
305+ onSelectAgent ( agent ) ;
318306 } ,
319307 } ) }
320308 columns = { [
@@ -324,12 +312,10 @@ export default function AgentList({
324312 const isAvailable = agent . is_available !== false ;
325313 const displayName = agent . display_name || "" ;
326314 const name = agent . name || "" ;
327- // Use local state for immediate visual feedback
328315 const isSelected =
329- locallySelectedAgentId !== null &&
330- String ( locallySelectedAgentId ) === String ( agent . id ) ;
331- // Optimistic update: use clearedNewMarkIds to hide NEW immediately after click
332- const isNew = ( agent . is_new || false ) && ! clearedNewMarkIds . current . has ( String ( agent . id ) ) ;
316+ currentAgentId !== null &&
317+ String ( currentAgentId ) === String ( agent . id ) ;
318+ const isNew = agent . is_new || false ;
333319
334320 return (
335321 < Flex
0 commit comments