-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Help Center: Refactor Zendesk Messaging hooks (#77499)
This is a much needed follow-up to #77024. This re-organizes the code for Zendesk Messaging so that it can be interacted with using clearly defined hooks. - Split out the code that was concentrated in Help Center into easier to re-use hooks. - Introduce a usePresalesChat that uses those to simplify loading presales chat.
- Loading branch information
Showing
30 changed files
with
427 additions
and
548 deletions.
There are no files selected for viewing
92 changes: 0 additions & 92 deletions
92
client/components/jetpack/jetpack-presales-chat-widget/index.tsx
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { useMessagingAvailability, useZendeskMessaging } from '@automattic/help-center/src/hooks'; | ||
import { useIsEnglishLocale } from '@automattic/i18n-utils'; | ||
import { useSelector } from 'react-redux'; | ||
import { isUserLoggedIn } from 'calypso/state/current-user/selectors'; | ||
import type { ZendeskConfigName } from '@automattic/help-center/src/hooks/use-zendesk-messaging'; | ||
|
||
export type KeyType = 'akismet' | 'jpAgency' | 'jpCheckout' | 'jpGeneral' | 'wpcom'; | ||
|
||
function getConfigName( keyType: KeyType ): ZendeskConfigName { | ||
switch ( keyType ) { | ||
case 'akismet': | ||
return 'zendesk_presales_chat_key_akismet'; | ||
case 'jpAgency': | ||
return 'zendesk_presales_chat_key_jp_agency_dashboard'; | ||
case 'jpCheckout': | ||
return 'zendesk_presales_chat_key_jp_checkout'; | ||
case 'jpGeneral': | ||
case 'wpcom': | ||
default: | ||
return 'zendesk_presales_chat_key'; | ||
} | ||
} | ||
|
||
function getGroupName( keyType: KeyType ) { | ||
switch ( keyType ) { | ||
case 'akismet': | ||
case 'jpAgency': | ||
case 'jpCheckout': | ||
case 'jpGeneral': | ||
return 'jp_presales'; | ||
case 'wpcom': | ||
default: | ||
return 'wpcom_presales'; | ||
} | ||
} | ||
|
||
export function usePresalesChat( keyType: KeyType, enabled = true ) { | ||
const isEnglishLocale = useIsEnglishLocale(); | ||
const isEligibleForPresalesChat = enabled && isEnglishLocale; | ||
|
||
const group = getGroupName( keyType ); | ||
const { data: chatAvailability, isInitialLoading: isLoadingAvailability } = | ||
useMessagingAvailability( group, isEligibleForPresalesChat ); | ||
const isPresalesChatAvailable = Boolean( chatAvailability?.is_available ); | ||
|
||
const isLoggedIn = useSelector( isUserLoggedIn ); | ||
const zendeskKeyName = getConfigName( keyType ); | ||
useZendeskMessaging( | ||
zendeskKeyName, | ||
isEligibleForPresalesChat && isPresalesChatAvailable, | ||
isLoggedIn | ||
); | ||
|
||
return { | ||
isChatActive: isPresalesChatAvailable && isEligibleForPresalesChat, | ||
isLoading: isLoadingAvailability, | ||
isPresalesChatAvailable, | ||
}; | ||
} |
Oops, something went wrong.