@@ -25,7 +25,12 @@ import store, {
2525 isInteractionOnHold ,
2626 MEDIA_TYPE_TELEPHONY_LOWER ,
2727} from '@webex/cc-store' ;
28- import { TIMER_LABEL_CONSULTING , TIMER_LABEL_CONSULT_REQUESTED , TIMER_LABEL_CONSULT_ON_HOLD , TIMER_LABEL_WRAP_UP } from './Utils/constants' ;
28+ import {
29+ TIMER_LABEL_CONSULTING ,
30+ TIMER_LABEL_CONSULT_REQUESTED ,
31+ TIMER_LABEL_CONSULT_ON_HOLD ,
32+ TIMER_LABEL_WRAP_UP ,
33+ } from './Utils/constants' ;
2934import { calculateStateTimerData , calculateConsultTimerData , findLatestConsultMedia } from './Utils/timer-utils' ;
3035import { useHoldTimer } from './Utils/useHoldTimer' ;
3136import { OutdialAniEntriesResponse } from '@webex/contact-center/dist/types/services/config/types' ;
@@ -334,8 +339,7 @@ export const useCallControl = (props: useCallControlProps) => {
334339 useEffect ( ( ) => {
335340 // During conference, the call is never on hold
336341 const isInConference =
337- controls ?. main ?. exitConference ?. isVisible ||
338- currentTask ?. data ?. interaction ?. state === 'conference' ;
342+ controls ?. main ?. exitConference ?. isVisible || currentTask ?. data ?. interaction ?. state === 'conference' ;
339343 if ( isInConference ) {
340344 setIsHeld ( false ) ;
341345 return ;
@@ -368,6 +372,9 @@ export const useCallControl = (props: useCallControlProps) => {
368372
369373 const { interaction} = currentTask . data ;
370374 const myAgentId = store . cc . agentConfig ?. agentId ;
375+ const currentDestination = store . lastConsultDestination ;
376+ const destinationType = currentDestination ?. destinationType ;
377+ const destinationId = currentDestination ?. to ;
371378
372379 // For Entry Point or Dial Number consults, check if destination agent has joined
373380 if ( lastTargetType === TARGET_TYPE . ENTRY_POINT || lastTargetType === TARGET_TYPE . DIAL_NUMBER ) {
@@ -396,8 +403,10 @@ export const useCallControl = (props: useCallControlProps) => {
396403 // eslint-disable-next-line @typescript-eslint/no-explicit-any
397404 const participant = interaction . participants [ consultParticipantId ] as any ;
398405 const phoneNumber = participant . dn || participant . id ;
406+ const matchesCurrentDestination =
407+ ! destinationId || participant . epId === destinationId || participant . id === destinationId ;
399408
400- if ( phoneNumber && phoneNumber !== consultAgentName ) {
409+ if ( phoneNumber && matchesCurrentDestination ) {
401410 setConsultAgentName ( phoneNumber ) ;
402411 logger . info ( `${ lastTargetType } consult ringing - showing phone number: ${ phoneNumber } ` , {
403412 module : 'widget-cc-task#helper.ts' ,
@@ -418,7 +427,12 @@ export const useCallControl = (props: useCallControlProps) => {
418427 // Find the agent participant in consult media who is not the current agent
419428 const consultParticipantId = consultMedia . participants ?. find ( ( participantId : string ) => {
420429 const participant = interaction . participants [ participantId ] ;
421- return participant && participant . id !== myAgentId && participant . pType === 'Agent' ;
430+ const matchesDestination =
431+ destinationType !== 'agent' ||
432+ ! destinationId ||
433+ participantId === destinationId ||
434+ participant ?. id === destinationId ;
435+ return participant && participant . id !== myAgentId && participant . pType === 'Agent' && matchesDestination ;
422436 } ) ;
423437
424438 if ( consultParticipantId && interaction . participants [ consultParticipantId ] ) {
@@ -430,45 +444,12 @@ export const useCallControl = (props: useCallControlProps) => {
430444 } ) ;
431445 }
432446 } else {
433- // Fallback: Use old logic if consult media not found
434- const otherAgents = Object . values ( interaction . participants || { } ) . filter (
435- ( participant ) => participant . pType === 'Agent' && participant . id !== myAgentId
436- ) ;
437-
438- // In a conference with multiple agents, find the agent currently being consulted
439- // Priority: 1) consultState="consulting" 2) most recent consultTimestamp
440- let foundAgent : { id : string ; name : string } | null = null ;
441-
442- if ( otherAgents . length > 0 ) {
443- // eslint-disable-next-line @typescript-eslint/no-explicit-any
444- const consultingAgent = otherAgents . find ( ( agent : any ) => agent . consultState === 'consulting' ) ;
445-
446- if ( consultingAgent ) {
447- foundAgent = {
448- id : consultingAgent . id ,
449- name : consultingAgent . name ,
450- } ;
451- } else {
452- // Fallback: Find agent with most recent consultTimestamp
453- // eslint-disable-next-line @typescript-eslint/no-explicit-any
454- const agentWithMostRecentTimestamp = otherAgents . reduce ( ( latest : any , current : any ) => {
455- const currentTimestamp = current . consultTimestamp || current . joinTimestamp || 0 ;
456- const latestTimestamp = latest ? latest . consultTimestamp || latest . joinTimestamp || 0 : 0 ;
457- return currentTimestamp >= latestTimestamp ? current : latest ;
458- } , null ) ;
459-
460- if ( agentWithMostRecentTimestamp ) {
461- foundAgent = {
462- id : agentWithMostRecentTimestamp . id ,
463- name : agentWithMostRecentTimestamp . name ,
464- } ;
465- }
466- }
467- }
468-
469- if ( foundAgent ) {
470- setConsultAgentName ( foundAgent . name ) ;
471- logger . info ( `Consulting agent detected (fallback): ${ foundAgent . name } ${ foundAgent . id } ` , {
447+ // When consult media is temporarily missing, trust the current consult
448+ // destination instead of broad participant fallbacks that can be stale.
449+ if ( destinationType === 'agent' && destinationId && interaction . participants ?. [ destinationId ] ) {
450+ const targetedAgent = interaction . participants [ destinationId ] ;
451+ setConsultAgentName ( targetedAgent . name || targetedAgent . id ) ;
452+ logger . info ( `Consulting agent detected (destination): ${ targetedAgent . name } ${ targetedAgent . id } ` , {
472453 module : 'widget-cc-task#helper.ts' ,
473454 method : 'useCallControl#extractConsultingAgent' ,
474455 } ) ;
@@ -481,7 +462,7 @@ export const useCallControl = (props: useCallControlProps) => {
481462 method : 'extractConsultingAgent' ,
482463 } ) ;
483464 }
484- } , [ currentTask , logger , lastTargetType , consultAgentName , setConsultAgentName ] ) ;
465+ } , [ currentTask , logger , lastTargetType ] ) ;
485466
486467 // Extract main call timestamp whenever currentTask changes
487468 useEffect ( ( ) => {
@@ -893,6 +874,10 @@ export const useCallControl = (props: useCallControlProps) => {
893874 holdParticipants : ! allowParticipantsToInteract ,
894875 } ;
895876
877+ // Update target type at source before consult starts so extraction logic
878+ // does not use a stale previous consult target type.
879+ setLastTargetType ( destinationType as TargetType ) ;
880+
896881 store . setLastConsultDestination ( { to : consultDestination , destinationType} ) ;
897882
898883 if ( destinationType === 'queue' ) {
@@ -969,7 +954,7 @@ export const useCallControl = (props: useCallControlProps) => {
969954 let recoveredDestinationType : DestinationType = 'agent' as DestinationType ;
970955 if ( consultMedia ?. participants ) {
971956 for ( const pid of consultMedia . participants ) {
972- const p = interaction ?. participants ?. [ pid ] as any ;
957+ const p = interaction ?. participants ?. [ pid ] as { id ?: string ; pType ?: string ; epId ?: string } | undefined ;
973958 if ( ! p || p . id === myAgentId ) continue ;
974959 if ( p . pType === 'Agent' ) {
975960 recoveredTo = pid ;
@@ -1100,9 +1085,14 @@ export const useCallControl = (props: useCallControlProps) => {
11001085 setStateTimerTimestamp ( stateTimerData . timestamp ) ;
11011086 }
11021087 } , [
1103- currentTask , controls , agentId ,
1104- participantIsWrapUp , participantWrapUpTimestamp , participantLastUpdated ,
1105- participantCurrentState , interactionState ,
1088+ currentTask ,
1089+ controls ,
1090+ agentId ,
1091+ participantIsWrapUp ,
1092+ participantWrapUpTimestamp ,
1093+ participantLastUpdated ,
1094+ participantCurrentState ,
1095+ interactionState ,
11061096 ] ) ;
11071097
11081098 // Calculate consult timer label and timestamp.
@@ -1132,8 +1122,7 @@ export const useCallControl = (props: useCallControlProps) => {
11321122 } , [ currentTask , controls , agentId , consultMediaIsHold , consultMediaId , participantConsultState ] ) ;
11331123
11341124 const isInConferenceState =
1135- controls ?. main ?. exitConference ?. isVisible ||
1136- currentTask ?. data ?. interaction ?. state === 'conference' ;
1125+ controls ?. main ?. exitConference ?. isVisible || currentTask ?. data ?. interaction ?. state === 'conference' ;
11371126 const effectiveIsHeld = isInConferenceState ? false : isHeld ;
11381127
11391128 return {
0 commit comments