Skip to content

Commit

Permalink
Fix issues when conversation can't be found (#99337)
Browse files Browse the repository at this point in the history
  • Loading branch information
heavyweight authored Feb 6, 2025
1 parent e36c5aa commit 3c3eb36
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 35 deletions.
16 changes: 3 additions & 13 deletions packages/odie-client/src/data/use-get-zendesk-conversation.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -23,7 +22,6 @@ const parseResponse = ( conversation: Conversation ) => {
*/
export const useGetZendeskConversation = () => {
const getUnreadNotifications = useGetUnreadConversations();
const { trackEvent } = useOdieAssistantContext();

return useCallback(
( {
Expand All @@ -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
Expand All @@ -70,6 +60,6 @@ export const useGetZendeskConversation = () => {
} )
.catch( () => parseResponse( conversation ) );
},
[ getUnreadNotifications, trackEvent ]
[ getUnreadNotifications ]
);
};
59 changes: 37 additions & 22 deletions packages/odie-client/src/hooks/use-get-combined-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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 ) {
Expand All @@ -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 ) => ( {
Expand Down

0 comments on commit 3c3eb36

Please sign in to comment.