Skip to content

Commit

Permalink
Infer zd client id by listening to sent messages (#98550)
Browse files Browse the repository at this point in the history
  • Loading branch information
heavyweight authored Jan 17, 2025
1 parent 3f1ca30 commit da5ee03
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
18 changes: 17 additions & 1 deletion packages/help-center/src/components/help-center-smooch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Smooch from 'smooch';
import { useChatStatus } from '../hooks';
import { HELP_CENTER_STORE } from '../stores';
import { getClientId, getZendeskConversations } from './utils';
import type { ZendeskMessage } from '@automattic/odie-client';

const destroy = () => {
Smooch.destroy();
Expand Down Expand Up @@ -64,7 +65,7 @@ const HelpCenterSmooch: React.FC< { enableAuth: boolean } > = ( { enableAuth } )
const getUnreadNotifications = useGetUnreadConversations();

const getUnreadListener = useCallback(
( message: unknown, data: { conversation: { id: string } } ) => {
( message: ZendeskMessage, data: { conversation: { id: string } } ) => {
if ( isHelpCenterShown ) {
return;
}
Expand All @@ -74,6 +75,18 @@ const HelpCenterSmooch: React.FC< { enableAuth: boolean } > = ( { enableAuth } )
[ isHelpCenterShown ]
);

const clientIdListener = useCallback(
( message: ZendeskMessage ) => {
if ( message?.source?.type === 'web' && message.source?.id ) {
setZendeskClientId( message.source?.id );
// Unregister the listener after setting the client ID
// @ts-expect-error -- 'off' is not part of the def.
Smooch?.off?.( 'message:sent', clientIdListener );
}
},
[ setZendeskClientId ]
);

// Initialize Smooch which communicates with Zendesk
useEffect( () => {
if ( isMessagingScriptLoaded && authData?.isLoggedIn ) {
Expand Down Expand Up @@ -110,11 +123,14 @@ const HelpCenterSmooch: React.FC< { enableAuth: boolean } > = ( { enableAuth } )
getUnreadNotifications( allConversations );
setZendeskClientId( getClientId( allConversations ) );
Smooch.on( 'message:received', getUnreadListener );
Smooch.on( 'message:sent', clientIdListener );
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { recordTracksEvent } from '@automattic/calypso-analytics';
import { Gravatar } from '@automattic/components';
import { getRelativeTimeString, useLocale } from '@automattic/i18n-utils';
import { type ZendeskMessage } from '@automattic/odie-client';
import { HumanAvatar } from '@automattic/odie-client/src/assets';
import { useGetSupportInteractionById } from '@automattic/odie-client/src/data/use-get-support-interaction-by-id';
import { useDispatch as useDataStoreDispatch } from '@wordpress/data';
Expand All @@ -11,6 +10,7 @@ import clsx from 'clsx';
import { Link } from 'react-router-dom';
import { useHelpCenterContext } from '../contexts/HelpCenterContext';
import { HELP_CENTER_STORE } from '../stores';
import type { ZendeskMessage } from '@automattic/odie-client';

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

Expand Down

0 comments on commit da5ee03

Please sign in to comment.