-
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.
Add Jetpack and Akismet zendesk chat widgets to checkout (#76561)
* Add Jetpack and Akismet zendesk chat widgets to checkout * Update jpAkismet to akismet * Add missing config var to staging * Make display logic more clear
- Loading branch information
1 parent
4ce5fc7
commit 70621b8
Showing
8 changed files
with
158 additions
and
98 deletions.
There are no files selected for viewing
99 changes: 24 additions & 75 deletions
99
client/components/jetpack/jetpack-presales-chat-widget/index.tsx
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 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,68 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { addQueryArgs } from '@wordpress/url'; | ||
import type { SetStateAction, Dispatch } from 'react'; | ||
|
||
type PresalesChatResponse = { | ||
is_available: boolean; | ||
}; | ||
|
||
//the API is rate limited if we hit the limit we'll back off and retry | ||
async function fetchWithRetry( | ||
url: string, | ||
options: RequestInit, | ||
retries = 3, | ||
delay = 30000 | ||
): Promise< Response > { | ||
try { | ||
const response = await fetch( url, options ); | ||
|
||
if ( response.status === 429 && retries > 0 ) { | ||
await new Promise( ( resolve ) => setTimeout( resolve, delay ) ); | ||
return await fetchWithRetry( url, options, retries - 1, delay * 2 ); | ||
} | ||
|
||
return response; | ||
} catch ( error ) { | ||
if ( retries > 0 ) { | ||
await new Promise( ( resolve ) => setTimeout( resolve, delay ) ); | ||
return await fetchWithRetry( url, options, retries - 1, delay * 2 ); | ||
} | ||
throw error; | ||
} | ||
} | ||
|
||
export function useJpPresalesAvailabilityQuery( setError?: Dispatch< SetStateAction< boolean > > ) { | ||
//adding a safeguard to ensure if there's an unkown error with the widget it won't crash the whole app | ||
try { | ||
return useQuery< boolean, Error >( | ||
[ 'presales-availability' ], | ||
async () => { | ||
const url = 'https://public-api.wordpress.com/wpcom/v2/presales/chat'; | ||
const queryObject = { | ||
group: 'jp_presales', | ||
}; | ||
|
||
const response = await fetchWithRetry( | ||
addQueryArgs( url, queryObject as Record< string, string > ), | ||
{ | ||
credentials: 'same-origin', | ||
mode: 'cors', | ||
} | ||
); | ||
|
||
if ( ! response.ok ) { | ||
throw new Error( `API request failed with status ${ response.status }` ); | ||
} | ||
|
||
const data: PresalesChatResponse = await response.json(); | ||
return data.is_available; | ||
}, | ||
{ | ||
meta: { persist: false }, | ||
} | ||
); | ||
} catch ( error ) { | ||
setError && setError( true ); | ||
return { data: false }; | ||
} | ||
} |
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 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 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 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 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 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
@CodeyGuyDylan, while working on #77024, I noticed that the presales availability is being queried always here. I've fixed this a bit in 3f18f83, but it needs more work in this context. Basically, you can pass
enabled
touseQuery
to skip fetching it - for example, if we know the user is not in JP checkout :) Could you please look into it and fix it in this file as well? 🙏