@@ -16,6 +16,7 @@ import Smooch from 'smooch';
16
16
import { useChatStatus } from '../hooks' ;
17
17
import { HELP_CENTER_STORE } from '../stores' ;
18
18
import { getClientId , getZendeskConversations } from './utils' ;
19
+ import type { ZendeskMessage } from '@automattic/odie-client' ;
19
20
20
21
const destroy = ( ) => {
21
22
Smooch . destroy ( ) ;
@@ -64,7 +65,7 @@ const HelpCenterSmooch: React.FC< { enableAuth: boolean } > = ( { enableAuth } )
64
65
const getUnreadNotifications = useGetUnreadConversations ( ) ;
65
66
66
67
const getUnreadListener = useCallback (
67
- ( message : unknown , data : { conversation : { id : string } } ) => {
68
+ ( message : ZendeskMessage , data : { conversation : { id : string } } ) => {
68
69
if ( isHelpCenterShown ) {
69
70
return ;
70
71
}
@@ -74,6 +75,18 @@ const HelpCenterSmooch: React.FC< { enableAuth: boolean } > = ( { enableAuth } )
74
75
[ isHelpCenterShown ]
75
76
) ;
76
77
78
+ const clientIdListener = useCallback (
79
+ ( message : ZendeskMessage ) => {
80
+ if ( message ?. source ?. type === 'web' && message . source ?. id ) {
81
+ setZendeskClientId ( message . source ?. id ) ;
82
+ // Unregister the listener after setting the client ID
83
+ // @ts -expect-error -- 'off' is not part of the def.
84
+ Smooch ?. off ?.( 'message:sent' , clientIdListener ) ;
85
+ }
86
+ } ,
87
+ [ setZendeskClientId ]
88
+ ) ;
89
+
77
90
// Initialize Smooch which communicates with Zendesk
78
91
useEffect ( ( ) => {
79
92
if ( isMessagingScriptLoaded && authData ?. isLoggedIn ) {
@@ -110,11 +123,14 @@ const HelpCenterSmooch: React.FC< { enableAuth: boolean } > = ( { enableAuth } )
110
123
getUnreadNotifications ( allConversations ) ;
111
124
setZendeskClientId ( getClientId ( allConversations ) ) ;
112
125
Smooch . on ( 'message:received' , getUnreadListener ) ;
126
+ Smooch . on ( 'message:sent' , clientIdListener ) ;
113
127
}
114
128
115
129
return ( ) => {
116
130
// @ts -expect-error -- 'off' is not part of the def.
117
131
Smooch ?. off ?.( 'message:received' , getUnreadListener ) ;
132
+ // @ts -expect-error -- 'off' is not part of the def.
133
+ Smooch ?. off ?.( 'message:sent' , clientIdListener ) ;
118
134
} ;
119
135
} , [ getUnreadListener , isChatLoaded , getUnreadNotifications , setZendeskClientId ] ) ;
120
136
0 commit comments