From 3c3eb3610875b4283c5a36cca7884610b81f53fc Mon Sep 17 00:00:00 2001 From: Kosta Date: Thu, 6 Feb 2025 12:58:21 +0100 Subject: [PATCH] Fix issues when conversation can't be found (#99337) --- .../src/data/use-get-zendesk-conversation.ts | 16 +---- .../src/hooks/use-get-combined-chat.ts | 59 ++++++++++++------- 2 files changed, 40 insertions(+), 35 deletions(-) diff --git a/packages/odie-client/src/data/use-get-zendesk-conversation.ts b/packages/odie-client/src/data/use-get-zendesk-conversation.ts index da099334d4af9..385b376f4c95c 100644 --- a/packages/odie-client/src/data/use-get-zendesk-conversation.ts +++ b/packages/odie-client/src/data/use-get-zendesk-conversation.ts @@ -1,6 +1,5 @@ import { useCallback } from 'react'; import Smooch from 'smooch'; -import { useOdieAssistantContext } from '../context'; import { zendeskMessageConverter } from '../utils'; import { useGetUnreadConversations } from './use-get-unread-conversations'; import type { ZendeskMessage } from '../types'; @@ -23,7 +22,6 @@ const parseResponse = ( conversation: Conversation ) => { */ export const useGetZendeskConversation = () => { const getUnreadNotifications = useGetUnreadConversations(); - const { trackEvent } = useOdieAssistantContext(); return useCallback( ( { @@ -46,19 +44,11 @@ export const useGetZendeskConversation = () => { return Number( conversation.metadata[ 'odieChatId' ] ) === Number( chatId ); } - return false; + throw new Error(); } ); if ( ! conversation ) { - // Conversation id was passed but the conversion was not found. Something went wrong. - if ( conversationId ) { - trackEvent( 'zendesk_conversation_not_found', { - conversationId, - chatId, - conversationsCount: conversations?.length ?? null, - } ); - } - return null; + throw new Error(); } // We need to ensure that more than one message is loaded @@ -70,6 +60,6 @@ export const useGetZendeskConversation = () => { } ) .catch( () => parseResponse( conversation ) ); }, - [ getUnreadNotifications, trackEvent ] + [ getUnreadNotifications ] ); }; diff --git a/packages/odie-client/src/hooks/use-get-combined-chat.ts b/packages/odie-client/src/hooks/use-get-combined-chat.ts index 7ae08496a38f9..4fcba1979188a 100644 --- a/packages/odie-client/src/hooks/use-get-combined-chat.ts +++ b/packages/odie-client/src/hooks/use-get-combined-chat.ts @@ -2,9 +2,10 @@ import { HelpCenterSelect } from '@automattic/data-stores'; import { HELP_CENTER_STORE } from '@automattic/help-center/src/stores'; import { useSelect } from '@wordpress/data'; import { useState, useEffect } from '@wordpress/element'; +import { v4 as uuidv4 } from 'uuid'; import { ODIE_TRANSFER_MESSAGE } from '../constants'; -import { emptyChat } from '../context'; -import { useGetZendeskConversation, useOdieChat } from '../data'; +import { emptyChat, useOdieAssistantContext } from '../context'; +import { useGetZendeskConversation, useManageSupportInteraction, useOdieChat } from '../data'; import type { Chat, Message } from '../types'; /** @@ -39,8 +40,9 @@ export const useGetCombinedChat = ( canConnectToZendesk: boolean ) => { const [ mainChatState, setMainChatState ] = useState< Chat >( emptyChat ); const getZendeskConversation = useGetZendeskConversation(); - const { data: odieChat, isLoading: isOdieChatLoading } = useOdieChat( Number( odieId ) ); + const { startNewInteraction } = useManageSupportInteraction(); + const { trackEvent } = useOdieAssistantContext(); useEffect( () => { if ( odieId && odieChat && ! conversationId ) { @@ -53,25 +55,38 @@ export const useGetCombinedChat = ( canConnectToZendesk: boolean ) => { } ); } else if ( conversationId && canConnectToZendesk ) { if ( isChatLoaded ) { - getZendeskConversation( { - chatId: odieChat?.odieId, - conversationId: conversationId.toString(), - } )?.then( ( conversation ) => { - if ( conversation ) { - setMainChatState( { - ...( odieChat ? odieChat : {} ), - supportInteractionId: currentSupportInteraction!.uuid, - conversationId: conversation.id, - messages: [ - ...( odieChat ? odieChat.messages : [] ), - ...( odieChat ? ODIE_TRANSFER_MESSAGE : [] ), - ...( conversation.messages as Message[] ), - ], - provider: 'zendesk', - status: currentSupportInteraction?.status === 'closed' ? 'closed' : 'loaded', - } ); - } - } ); + try { + getZendeskConversation( { + chatId: odieChat?.odieId, + conversationId: conversationId.toString(), + } )?.then( ( conversation ) => { + if ( conversation ) { + setMainChatState( { + ...( odieChat ? odieChat : {} ), + supportInteractionId: currentSupportInteraction!.uuid, + conversationId: conversation.id, + messages: [ + ...( odieChat ? odieChat.messages : [] ), + ...( odieChat ? ODIE_TRANSFER_MESSAGE : [] ), + ...( conversation.messages as Message[] ), + ], + provider: 'zendesk', + status: currentSupportInteraction?.status === 'closed' ? 'closed' : 'loaded', + } ); + } + } ); + } catch ( error ) { + // Conversation id was passed but the conversion was not found. Something went wrong. + trackEvent( 'zendesk_conversation_not_found', { + conversationId, + odieId, + } ); + + startNewInteraction( { + event_source: 'help-center', + event_external_id: uuidv4(), + } ); + } } } else if ( currentSupportInteraction ) { setMainChatState( ( prevChat ) => ( {