Skip to content
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
5 changes: 4 additions & 1 deletion packages/help-center/src/data/use-support-status.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { keepPreviousData, useQuery } from '@tanstack/react-query';
import apiFetch from '@wordpress/api-fetch';
import wpcomRequest, { canAccessWpcomApis } from 'wpcom-proxy-request';
import { useHelpCenterContext } from '../contexts/HelpCenterContext';
import { SupportStatus } from '../types';

// Bump me to invalidate the cache.
Expand All @@ -12,13 +13,15 @@ interface APIFetchOptions {
}

export function useSupportStatus( enabled = true ) {
const { currentUser } = useHelpCenterContext();

return useQuery< SupportStatus, Error >( {
queryKey: [ 'support-status', VERSION ],
queryFn: async () =>
canAccessWpcomApis()
? await wpcomRequest( { path: '/help/support-status', apiNamespace: 'wpcom/v2' } )
: await apiFetch( { path: 'help-center/support-status', global: true } as APIFetchOptions ),
enabled,
enabled: enabled && !! currentUser?.ID,
refetchOnWindowFocus: false,
placeholderData: keepPreviousData,
staleTime: 180000, // 3mins.
Expand Down
1 change: 1 addition & 0 deletions packages/odie-client/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ export { useOdieAssistantContext } from './context';
export type { Conversations, OdieConversation, OdieMessage, SupportInteraction } from './types';
export type { ZendeskConversation, ZendeskMessage } from '@automattic/zendesk-client';
export { useManagedOdieChat } from './data/use-managed-odie-chat';
export { convertOdieChatToOdieConversation } from './utils/chat-utils';
14 changes: 12 additions & 2 deletions packages/odie-client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export type CurrentUser = {
display_name: string;
avatar_URL?: string;
email?: string;
id?: number;
ID?: number;
};

export type Source = {
Expand Down Expand Up @@ -159,6 +159,7 @@ export type Message = {
created_at?: string;
feedbackOptions?: MessageAction[];
metadata?: Record< string, any >;
ts?: number;
payload?: string;
/**
* Timestamp of the message.
Expand Down Expand Up @@ -215,6 +216,7 @@ type Metadata = {
createdAt: number;
supportInteractionId: string;
status: InteractionStatus;
botSlug?: string;
};

export type OdieConversation = {
Expand All @@ -224,13 +226,21 @@ export type OdieConversation = {
metadata?: Metadata;
};

export type LoggedOutOdieConversation = OdieConversation & {
metadata: {
sessionId: string;
} & Metadata;
};

export type SupportInteractionDraft = {
bot_slug: OdieAllowedBots;
event_external_id: string;
event_source: SupportProvider;
};

export type Conversations = Array< OdieConversation | ZendeskConversation >;
export type Conversations = Array<
OdieConversation | LoggedOutOdieConversation | ZendeskConversation
>;

export type SupportInteractionUser = {
user_id: string;
Expand Down
30 changes: 29 additions & 1 deletion packages/odie-client/src/utils/chat-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getTimestamp } from './get-timestamp';
import type { Chat } from '../types';
import type { Chat, OdieChat, OdieMessage, Message, LoggedOutOdieConversation } from '../types';

const MAX_ESCALATION_ATTEMPT_TIME = 3 * 24 * 60 * 60 * 1000; // three days

Expand Down Expand Up @@ -30,3 +30,31 @@ export const hasRecentEscalationAttempt = ( chat: Chat ) => {

return false;
};

function convertMessageToOdieMessage( message: Message ): OdieMessage {
return {
received: message.ts || 0,
role: message.role,
text: message.content as string,
};
}

export const convertOdieChatToOdieConversation = (
odieChat: OdieChat,
sessionId: string,
botSlug: string
): LoggedOutOdieConversation => {
return {
id: odieChat.odieId?.toString() || '',
messages: odieChat.messages.map( ( message ) => convertMessageToOdieMessage( message ) ),
createdAt: odieChat.messages[ 0 ].ts || 0,
metadata: {
odieChatId: odieChat.odieId || 0,
createdAt: odieChat.messages[ 0 ].ts || 0,
supportInteractionId: '',
status: 'open',
botSlug,
sessionId,
},
};
};
19 changes: 19 additions & 0 deletions packages/wpcom-proxy-request/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,25 @@ export function requestAllBlogsAccess() {
return request( { metaAPI: { accessAllUsersBlogs: true } } );
}

/**
* Set localStorage item in the proxy iframe.
* @param {string} key - The key to set.
* @param {string} value - The value to set.
* @returns {Promise} - A promise that resolves when the item is set.
*/
export function setCrossOriginStorageItem( key, value ) {
return request( { metaAPI: { setCrossOriginStorageItem: { key, value } } } );
}

/**
* Get localStorage item in the proxy iframe.
* @param {string} key - The key to get.
* @returns {Promise} - A promise that resolves when the item is set.
*/
export function getCrossOriginStorageItem( key ) {
return request( { metaAPI: { getCrossOriginStorageItem: { key } } } );
}

/**
* Calls the `postMessage()` function on the <iframe>.
* @param {Object} params
Expand Down
9 changes: 9 additions & 0 deletions packages/wpcom-proxy-request/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export interface WpcomRequestParams {
query?: string | Record< string, string | number >;
metaAPI?: {
accessAllUsersBlogs?: boolean;
setCrossOriginStorageItem?: { key: string; value: string };
getCrossOriginStorageItem?: { key: string };
};
signal?: AbortSignal;
apiNamespace?: string;
Expand All @@ -27,6 +29,13 @@ export function canAccessWpcomApis(): boolean;

export function requestAllBlogsAccess(): ReturnType< typeof request >;

export function setCrossOriginStorageItem(
key: string,
value: string
): ReturnType< typeof request >;

export function getCrossOriginStorageItem( key: string ): ReturnType< typeof request >;

export default function request(
params: WpcomRequestParams,
callback: ( err: unknown, body: unknown, headers: unknown ) => void
Expand Down