Skip to content

Commit da5ee03

Browse files
authored
Infer zd client id by listening to sent messages (#98550)
1 parent 3f1ca30 commit da5ee03

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

packages/help-center/src/components/help-center-smooch.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import Smooch from 'smooch';
1616
import { useChatStatus } from '../hooks';
1717
import { HELP_CENTER_STORE } from '../stores';
1818
import { getClientId, getZendeskConversations } from './utils';
19+
import type { ZendeskMessage } from '@automattic/odie-client';
1920

2021
const destroy = () => {
2122
Smooch.destroy();
@@ -64,7 +65,7 @@ const HelpCenterSmooch: React.FC< { enableAuth: boolean } > = ( { enableAuth } )
6465
const getUnreadNotifications = useGetUnreadConversations();
6566

6667
const getUnreadListener = useCallback(
67-
( message: unknown, data: { conversation: { id: string } } ) => {
68+
( message: ZendeskMessage, data: { conversation: { id: string } } ) => {
6869
if ( isHelpCenterShown ) {
6970
return;
7071
}
@@ -74,6 +75,18 @@ const HelpCenterSmooch: React.FC< { enableAuth: boolean } > = ( { enableAuth } )
7475
[ isHelpCenterShown ]
7576
);
7677

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+
7790
// Initialize Smooch which communicates with Zendesk
7891
useEffect( () => {
7992
if ( isMessagingScriptLoaded && authData?.isLoggedIn ) {
@@ -110,11 +123,14 @@ const HelpCenterSmooch: React.FC< { enableAuth: boolean } > = ( { enableAuth } )
110123
getUnreadNotifications( allConversations );
111124
setZendeskClientId( getClientId( allConversations ) );
112125
Smooch.on( 'message:received', getUnreadListener );
126+
Smooch.on( 'message:sent', clientIdListener );
113127
}
114128

115129
return () => {
116130
// @ts-expect-error -- 'off' is not part of the def.
117131
Smooch?.off?.( 'message:received', getUnreadListener );
132+
// @ts-expect-error -- 'off' is not part of the def.
133+
Smooch?.off?.( 'message:sent', clientIdListener );
118134
};
119135
}, [ getUnreadListener, isChatLoaded, getUnreadNotifications, setZendeskClientId ] );
120136

packages/help-center/src/components/help-center-support-chat-message.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { recordTracksEvent } from '@automattic/calypso-analytics';
22
import { Gravatar } from '@automattic/components';
33
import { getRelativeTimeString, useLocale } from '@automattic/i18n-utils';
4-
import { type ZendeskMessage } from '@automattic/odie-client';
54
import { HumanAvatar } from '@automattic/odie-client/src/assets';
65
import { useGetSupportInteractionById } from '@automattic/odie-client/src/data/use-get-support-interaction-by-id';
76
import { useDispatch as useDataStoreDispatch } from '@wordpress/data';
@@ -11,6 +10,7 @@ import clsx from 'clsx';
1110
import { Link } from 'react-router-dom';
1211
import { useHelpCenterContext } from '../contexts/HelpCenterContext';
1312
import { HELP_CENTER_STORE } from '../stores';
13+
import type { ZendeskMessage } from '@automattic/odie-client';
1414

1515
import './help-center-support-chat-message.scss';
1616

0 commit comments

Comments
 (0)