Skip to content

Commit 75c967a

Browse files
alshakeroescapemanuele
authored andcommitted
Progress
1 parent 4fc3843 commit 75c967a

File tree

8 files changed

+33
-31
lines changed

8 files changed

+33
-31
lines changed

packages/data-stores/src/help-center/actions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export const setIsMinimized = function* ( minimized: boolean ) {
9595
};
9696

9797
export const setLoggedOutOdieChat = (
98-
session: { odieId: number; sessionId: string } | undefined
98+
session: { odieId: number; sessionId: string; botSlug: string } | undefined
9999
) =>
100100
( {
101101
type: 'HELP_CENTER_SET_LOGGED_OUT_ODIE_CHAT',

packages/data-stores/src/help-center/reducer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const helpCenterRouterHistory: Reducer<
5858
};
5959

6060
const loggedOutOdieChat: Reducer<
61-
{ odieId: number; sessionId: string } | undefined,
61+
{ odieId: number; sessionId: string; botSlug: string } | undefined,
6262
HelpCenterAction
6363
> = ( state = undefined, action ) => {
6464
switch ( action.type ) {

packages/help-center/src/contexts/HelpCenterContext.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { ODIE_NEW_INTERACTIONS_BOT_SLUG } from '@automattic/odie-client/src/constants';
22
import { useContext, createContext } from '@wordpress/element';
3-
import { useNewInteractionsBotConfig } from '../hooks/use-new-interaction-bot-config';
43
import type { CurrentUser, HelpCenterSite } from '@automattic/data-stores';
54

65
export type HelpCenterRequiredInformation = {
@@ -87,11 +86,10 @@ export const HelpCenterRequiredContextProvider: React.FC< {
8786
value: Partial< HelpCenterRequiredInformation > &
8887
Pick< HelpCenterRequiredInformation, 'currentUser' | 'sectionName' >;
8988
} > = function ( { children, value } ) {
90-
const newInteractionsBotConfig = useNewInteractionsBotConfig( value.currentUser );
9189
return (
9290
<HelpCenterRequiredContext.Provider
9391
value={ {
94-
...Object.assign( {}, defaultContext, newInteractionsBotConfig, value ),
92+
...Object.assign( {}, defaultContext, value ),
9593
} }
9694
>
9795
{ children }

packages/help-center/src/hooks/use-get-history-chats.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ export const useGetHistoryChats = (): UseGetHistoryChatsResult => {
142142

143143
const loggedOutChat = useOdieChat(
144144
loggedOutSession ? loggedOutSession.odieId : null,
145-
loggedOutSession ? loggedOutSession.sessionId : null
145+
loggedOutSession ? loggedOutSession.sessionId : null,
146+
loggedOutSession ? loggedOutSession.botSlug : undefined
146147
);
147148

148149
useEffect( () => {

packages/help-center/src/hooks/use-new-interaction-bot-config.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

packages/odie-client/src/data/use-odie-chat.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ import type { OdieChat, ReturnedChat } from '../types';
1111
* Get the ODIE chat and manage the cache to save on API calls.
1212
* @param chatId - The chat ID to fetch
1313
*/
14-
export const useOdieChat = ( chatId: number | null, sessionId: string | undefined | null ) => {
15-
const { version, currentUser } = useOdieAssistantContext();
14+
export const useOdieChat = (
15+
chatId: number | null,
16+
sessionId: string | undefined | null,
17+
defaultBotSlug?: string | null
18+
) => {
19+
const { version } = useOdieAssistantContext();
1620
const { data: supportInteraction } = useCurrentSupportInteraction();
1721

1822
// Hover `ODIE_DEFAULT_BOT_SLUG_LEGACY` for more information.
19-
let botSlug = supportInteraction?.bot_slug || ODIE_DEFAULT_BOT_SLUG_LEGACY;
20-
21-
if ( ! currentUser?.ID ) {
22-
botSlug = 'wpcom-chat-loggedout';
23-
}
23+
const botSlug = supportInteraction?.bot_slug || defaultBotSlug || ODIE_DEFAULT_BOT_SLUG_LEGACY;
2424

2525
return useQuery< OdieChat, Error >( {
2626
queryKey: [ 'odie-chat', botSlug, chatId, version, sessionId ],

packages/odie-client/src/data/use-send-odie-message.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,18 @@ const HELP_CENTER_STORE = HelpCenter.register();
2626

2727
function getBotSlug(
2828
supportInteraction: SupportInteraction | undefined,
29-
newInteractionsBotSlug: string
29+
newInteractionsBotSlug: string,
30+
currentUserId?: number
3031
): string {
3132
if ( supportInteraction ) {
3233
// Legacy support interactions have their botSlug set to `''`. We need to use the legacy bot slug for them.
3334
return supportInteraction.bot_slug || ODIE_DEFAULT_BOT_SLUG_LEGACY;
3435
}
3536

37+
if ( ! currentUserId ) {
38+
return 'wpcom-chat-loggedout';
39+
}
40+
3641
// When the interaction is undefined, it means we're sending the first message to Odie, which is done before the interaction is created.
3742
// In this case, we use the new interactions bot slug.
3843
return newInteractionsBotSlug;
@@ -129,12 +134,13 @@ export const useSendOdieMessage = ( signal: AbortSignal ) => {
129134
);
130135

131136
const updateLoggedOutSession = useCallback(
132-
( chatId: string, sessionId: string ) => {
137+
( chatId: string, sessionId: string, botSlug: string ) => {
133138
const params = new URLSearchParams( location.search );
134139
params.set( 'chatId', chatId );
135140
params.set( 'sessionId', sessionId );
141+
params.set( 'botSlug', botSlug );
136142
navigate( `${ location.pathname }?${ params.toString() }`, { replace: true } );
137-
setLoggedOutOdieChat( { odieId: chatId, sessionId } );
143+
setLoggedOutOdieChat( { odieId: chatId, sessionId, botSlug } );
138144
},
139145
[ location.pathname, location.search, navigate, setLoggedOutOdieChat ]
140146
);
@@ -224,7 +230,11 @@ export const useSendOdieMessage = ( signal: AbortSignal ) => {
224230

225231
return useMutation< ReturnedChat, Error, Message >( {
226232
mutationFn: async ( message: Message ): Promise< ReturnedChat > => {
227-
const botSlug = getBotSlug( currentSupportInteraction, newInteractionsBotSlug );
233+
const botSlug = getBotSlug(
234+
currentSupportInteraction,
235+
newInteractionsBotSlug,
236+
currentUser?.ID
237+
);
228238
const chatIdSegment = odieId ? `/${ odieId }` : '';
229239
const session_id = sessionId;
230240

@@ -308,8 +318,10 @@ export const useSendOdieMessage = ( signal: AbortSignal ) => {
308318
},
309319
} );
310320
} else if ( ! isLoggedIn ) {
321+
const botSlug = getBotSlug( currentSupportInteraction, newInteractionsBotSlug );
322+
311323
// If the user is not logged in, we don't need to create a new support interaction.
312-
updateLoggedOutSession( chatId.toString(), returnedChat.session_id );
324+
updateLoggedOutSession( chatId.toString(), returnedChat.session_id, botSlug );
313325
}
314326
} catch ( error ) {
315327
trackEvent( 'error_updating_support_interaction', {

packages/odie-client/src/hooks/use-get-combined-chat.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,13 @@ export const useGetCombinedChat = (
5656
const params = new URLSearchParams( location.search );
5757
const loggedOutOdieChatId = params.get( 'chatId' );
5858
const loggedOutOdieSessionId = params.get( 'sessionId' );
59+
const loggedOutOdieBotSlug = params.get( 'botSlug' );
5960

6061
const odieId = isLoggedIn
6162
? getOdieIdFromInteraction( currentSupportInteraction )
6263
: loggedOutOdieChatId;
63-
6464
const sessionId = isLoggedIn ? undefined : loggedOutOdieSessionId;
65+
const botSlug = isLoggedIn ? undefined : loggedOutOdieBotSlug;
6566

6667
const { isChatLoaded, connectionStatus } = useSelect( ( select ) => {
6768
const store = select( HELP_CENTER_STORE ) as HelpCenterSelect;
@@ -79,7 +80,8 @@ export const useGetCombinedChat = (
7980
const getZendeskConversation = useGetZendeskConversation();
8081
const { data: odieChat, isFetching: isOdieChatLoading } = useOdieChat(
8182
Number( odieId ),
82-
sessionId
83+
sessionId,
84+
botSlug
8385
);
8486
const { startNewInteraction } = useManageSupportInteraction();
8587
const isUploadingUnsentMessages = useIsMutating( {

0 commit comments

Comments
 (0)