Skip to content

Commit 935ff2f

Browse files
alshakeroheydemoura
authored andcommitted
Create a new data-store field to persist logged out sessions (#107997)
1 parent c44daaa commit 935ff2f

File tree

5 files changed

+56
-5
lines changed

5 files changed

+56
-5
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Location } from 'history';
55
import { default as wpcomRequestPromise, canAccessWpcomApis } from 'wpcom-proxy-request';
66
import { GeneratorReturnType } from '../mapped-types';
77
import { SiteDetails } from '../site';
8-
import { isE2ETest } from '../utils';
8+
import { isE2ETest, isLoggedInHCUser } from '../utils';
99
import { STORE_KEY } from './constants';
1010
import type { HelpCenterOptions, HelpCenterSelect, HelpCenterShowOptions } from './types';
1111
import type { APIFetchOptions } from '../shared-types';
@@ -16,6 +16,10 @@ import type { APIFetchOptions } from '../shared-types';
1616
* @param isMinimized - Whether the help center is minimized.
1717
*/
1818
export const saveOpenState = ( isShown: boolean | undefined, isMinimized: boolean | undefined ) => {
19+
if ( ! isLoggedInHCUser() ) {
20+
return null;
21+
}
22+
1923
const saveState: Record< string, boolean | null > = {};
2024

2125
if ( typeof isShown === 'boolean' ) {
@@ -90,6 +94,14 @@ export const setIsMinimized = function* ( minimized: boolean ) {
9094
} as const;
9195
};
9296

97+
export const setLoggedOutOdieChat = (
98+
session: { odieId: number; sessionId: string; botSlug: string } | undefined
99+
) =>
100+
( {
101+
type: 'HELP_CENTER_SET_LOGGED_OUT_ODIE_CHAT',
102+
session,
103+
} ) as const;
104+
93105
export const setIsChatLoaded = ( isChatLoaded: boolean ) =>
94106
( {
95107
type: 'HELP_CENTER_SET_IS_CHAT_LOADED',
@@ -286,6 +298,7 @@ export type HelpCenterAction =
286298
| typeof setSubject
287299
| typeof resetStore
288300
| typeof setMessage
301+
| typeof setLoggedOutOdieChat
289302
| typeof setContextTerm
290303
| typeof setUserDeclaredSite
291304
| typeof setUserDeclaredSiteUrl
@@ -294,6 +307,7 @@ export type HelpCenterAction =
294307
| typeof setIsChatLoaded
295308
| typeof setAreSoundNotificationsEnabled
296309
| typeof setZendeskClientId
310+
| typeof setLoggedOutOdieChat
297311
| typeof setSupportTypingStatus
298312
| typeof setZendeskConnectionStatus
299313
| typeof setNavigateToRoute

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { registerStore } from '@wordpress/data';
22
import { controls } from '@wordpress/data-controls';
33
import { registerPlugins } from '../plugins';
4-
import { isE2ETest, isInSupportSession } from '../utils';
4+
import { isE2ETest, isInSupportSession, isLoggedInHCUser } from '../utils';
55
import { controls as wpcomRequestControls } from '../wpcom-request-controls';
66
import * as actions from './actions';
77
import { STORE_KEY } from './constants';
@@ -13,7 +13,7 @@ export type { State };
1313
let isRegistered = false;
1414

1515
export function register(): typeof STORE_KEY {
16-
const enabledPersistedOpenState = ! isE2ETest() && ! isInSupportSession();
16+
const enabledPersistedOpenState = ! isE2ETest() && ! isInSupportSession() && isLoggedInHCUser();
1717

1818
registerPlugins();
1919

@@ -23,7 +23,16 @@ export function register(): typeof STORE_KEY {
2323
reducer,
2424
controls: { ...controls, ...wpcomRequestControls },
2525
selectors,
26-
persist: [ 'message', 'userDeclaredSite', 'userDeclaredSiteUrl', 'subject' ],
26+
persist: [
27+
'message',
28+
'userDeclaredSite',
29+
'userDeclaredSiteUrl',
30+
'subject',
31+
'loggedOutOdieChat',
32+
...( ! isLoggedInHCUser()
33+
? [ 'helpCenterRouterHistory', 'helpCenterMinimized', 'showHelpCenter' ]
34+
: [] ),
35+
],
2736
// Don't persist the open state for e2e users, because parallel tests will start interfering with each other.
2837
resolvers: enabledPersistedOpenState ? { isHelpCenterShown } : undefined,
2938
} );

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ const helpCenterRouterHistory: Reducer<
5757
return state;
5858
};
5959

60+
const loggedOutOdieChat: Reducer<
61+
{ odieId: number; sessionId: string; botSlug: string } | undefined,
62+
HelpCenterAction
63+
> = ( state = undefined, action ) => {
64+
switch ( action.type ) {
65+
case 'HELP_CENTER_SET_LOGGED_OUT_ODIE_CHAT':
66+
return action.session;
67+
}
68+
return state;
69+
};
70+
6071
const showMessagingWidget: Reducer< boolean | undefined, HelpCenterAction > = ( state, action ) => {
6172
switch ( action.type ) {
6273
case 'HELP_CENTER_SET_SHOW_MESSAGING_WIDGET':
@@ -216,6 +227,7 @@ const reducer = combineReducers( {
216227
odieInitialPromptText,
217228
odieBotNameSlug,
218229
helpCenterRouterHistory,
230+
loggedOutOdieChat,
219231
hasPremiumSupport,
220232
contextTerm,
221233
helpCenterOptions,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export const getUnreadCount = ( state: State ) => state.unreadCount;
1111
export const getZendeskConnectionStatus = ( state: State ) => state.zendeskConnectionStatus;
1212
export const getIsMinimized = ( state: State ) => state.isMinimized;
1313
export const getIsChatLoaded = ( state: State ) => state.isChatLoaded;
14+
export const getLoggedOutOdieChat = ( state: State ) => state.loggedOutOdieChat;
1415
export const getAreSoundNotificationsEnabled = ( state: State ) =>
1516
state.areSoundNotificationsEnabled;
1617
export const getZendeskClientId = ( state: State ) => state.zendeskClientId;

packages/data-stores/src/utils.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
* Utility functions shared across data stores
33
*/
44

5-
declare const helpCenterData: { isProxied: boolean; isSU: boolean; isSSP: boolean } | undefined;
5+
declare const helpCenterData:
6+
| { isProxied: boolean; isSU: boolean; isSSP: boolean; currentUser: { ID: number } }
7+
| undefined;
68
declare const isSupportSession: boolean;
79
declare const isSSP: boolean;
810

@@ -30,3 +32,16 @@ export const isInSupportSession = () => {
3032
}
3133
return false;
3234
};
35+
36+
/**
37+
* Check if the user is logged in in a synchronous way. Works in wp-admin and Calypso.
38+
* @returns True if the user is logged in, false otherwise.
39+
*/
40+
export const isLoggedInHCUser = () => {
41+
return (
42+
// Calypso
43+
( typeof window !== 'undefined' && !! window.currentUser?.ID ) ||
44+
// wp-admin and Gutenberg
45+
( typeof helpCenterData !== 'undefined' && !! helpCenterData?.currentUser?.ID )
46+
);
47+
};

0 commit comments

Comments
 (0)