Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Odie] Send site_id and scroll to the beggining of the message. #88711

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useEffect, useRef } from '@wordpress/element';
import { useCallback, useState } from 'react';
import { useSelector } from 'react-redux';
import { Route, Routes, useLocation, useNavigate } from 'react-router-dom';
import { getSectionName } from 'calypso/state/ui/selectors';
import { getSectionName, getSelectedSiteId } from 'calypso/state/ui/selectors';
/**
* Internal Dependencies
*/
Expand Down Expand Up @@ -39,6 +39,7 @@ const HelpCenterContent: React.FC< { isRelative?: boolean; currentRoute?: string
isMinimized: store.getIsMinimized(),
};
}, [] );
const selectedSiteId = useSelector( getSelectedSiteId );

useEffect( () => {
recordTracksEvent( 'calypso_helpcenter_page_open', {
Expand Down Expand Up @@ -105,6 +106,7 @@ const HelpCenterContent: React.FC< { isRelative?: boolean; currentRoute?: string
initialUserMessage={ searchTerm }
logger={ trackEvent }
loggerEventNamePrefix="calypso_odie"
selectedSiteId={ selectedSiteId }
extraContactOptions={
<HelpCenterContactPage
hideHeaders
Expand Down
4 changes: 4 additions & 0 deletions packages/odie-client/src/context/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type OdieAssistantContextInterface = {
lastNudge: Nudge | null;
odieClientId: string;
sendNudge: ( nudge: Nudge ) => void;
selectedSiteId?: number | null;
setChat: ( chat: Chat ) => void;
setIsLoadingChat: ( isLoadingChat: boolean ) => void;
setMessageLikedStatus: ( message: Message, liked: boolean ) => void;
Expand Down Expand Up @@ -92,6 +93,7 @@ type OdieAssistantProviderProps = {
extraContactOptions?: ReactNode;
logger?: ( message: string, properties: Record< string, unknown > ) => void;
loggerEventNamePrefix?: string;
selectedSiteId?: number | null;
version?: string | null;
children?: ReactNode;
} & PropsWithChildren;
Expand All @@ -105,6 +107,7 @@ const OdieAssistantProvider: FC< OdieAssistantProviderProps > = ( {
enabled = true,
logger,
loggerEventNamePrefix,
selectedSiteId,
version = null,
children,
} ) => {
Expand Down Expand Up @@ -227,6 +230,7 @@ const OdieAssistantProvider: FC< OdieAssistantProviderProps > = ( {
isVisible,
lastNudge,
odieClientId,
selectedSiteId,
sendNudge: setLastNudge,
setChat,
setIsLoadingChat: noop,
Expand Down
4 changes: 2 additions & 2 deletions packages/odie-client/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export const OdieAssistant: React.FC = () => {
if ( lastMessageElement?.target ) {
lastMessageElement.target.scrollIntoView( {
behavior: 'auto',
block: 'end',
inline: 'end',
block: 'start',
inline: 'nearest',
} );
}
} );
Expand Down
36 changes: 28 additions & 8 deletions packages/odie-client/src/query/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const buildSendChatMessage = async (
message: Message,
botNameSlug: OdieAllowedBots,
chat_id?: number | null,
version?: string | null
version?: string | null,
selectedSiteId?: number | null
) => {
const baseApiPath = '/help-center/odie/chat/';
const wpcomBaseApiPath = '/odie/chat/';
Expand All @@ -30,19 +31,24 @@ const buildSendChatMessage = async (
: `${ wpcomBaseApiPath }${ botNameSlug }`;

return canAccessWpcomApis()
? odieWpcomSendSupportMessage( message, wpcomApiPathWithIds, version )
? odieWpcomSendSupportMessage( message, wpcomApiPathWithIds, version, selectedSiteId )
: apiFetch( {
path: apiPathWithIds,
method: 'POST',
data: { message, version },
data: { message, version, context: { selectedSiteId } },
} );
};

function odieWpcomSendSupportMessage( message: Message, path: string, version?: string | null ) {
function odieWpcomSendSupportMessage(
message: Message,
path: string,
version?: string | null,
selectedSiteId?: number | null
) {
return wpcom.req.post( {
path,
apiNamespace: 'wpcom/v2',
body: { message: message.content, version },
body: { message: message.content, version, context: { selectedSiteId } },
} );
}

Expand Down Expand Up @@ -70,8 +76,16 @@ export const useOdieSendMessage = (): UseMutationResult<
{ message: Message },
{ internal_message_id: string }
> => {
const { chat, botNameSlug, setIsLoading, addMessage, updateMessage, odieClientId, version } =
useOdieAssistantContext();
const {
chat,
botNameSlug,
setIsLoading,
addMessage,
updateMessage,
odieClientId,
selectedSiteId,
version,
} = useOdieAssistantContext();
const queryClient = useQueryClient();
const userMessage = useRef< Message | null >( null );

Expand All @@ -83,7 +97,13 @@ export const useOdieSendMessage = (): UseMutationResult<
>( {
mutationFn: ( { message }: { message: Message } ) => {
broadcastOdieMessage( message, odieClientId );
return buildSendChatMessage( { ...message }, botNameSlug, chat.chat_id, version );
return buildSendChatMessage(
{ ...message },
botNameSlug,
chat.chat_id,
version,
selectedSiteId
);
},
onMutate: ( { message } ) => {
const internal_message_id = uuid();
Expand Down
Loading