Delay loading support chat (#10234)

There are a lot of requests being done in parallel when you load the
page, and the support chat is responsible for some of them. I don't
think it's critical to have it upon loading so delaying it by 2 seconds
This commit is contained in:
Félix Malfait
2025-02-16 12:58:20 +01:00
committed by GitHub
parent f795d90702
commit 1dca3694f7
2 changed files with 14 additions and 5 deletions

View File

@ -53,6 +53,9 @@ export const Default: Story = {
expect(await canvas.findByText('Support')).toBeInTheDocument();
await userEvent.click(canvas.getByText('Support'));
// Add delay to account for the timeout in useSupportChat
await new Promise((resolve) => setTimeout(resolve, 600));
expect(await canvas.findByText('Documentation')).toBeInTheDocument();
expect(await canvas.findByText('Talk to us')).toBeInTheDocument();
},

View File

@ -12,15 +12,18 @@ const insertScript = ({
src,
innerHTML,
onLoad,
defer = false,
}: {
src?: string;
innerHTML?: string;
onLoad?: (...args: any[]) => void;
defer?: boolean;
}) => {
const script = document.createElement('script');
if (isNonEmptyString(src)) script.src = src;
if (isNonEmptyString(innerHTML)) script.innerHTML = innerHTML;
if (isDefined(onLoad)) script.onload = onLoad;
script.defer = defer;
document.body.appendChild(script);
};
@ -50,6 +53,7 @@ export const useSupportChat = () => {
insertScript({
src: url,
defer: true,
onLoad: () => {
window.FrontChat?.('init', {
chatId,
@ -76,11 +80,13 @@ export const useSupportChat = () => {
isDefined(currentWorkspaceMember) &&
!isFrontChatLoaded
) {
configureFront(
supportChat.supportFrontChatId,
currentUser,
currentWorkspaceMember,
);
setTimeout(() => {
configureFront(
supportChat.supportFrontChatId as string,
currentUser,
currentWorkspaceMember,
);
}, 500);
}
}, [
configureFront,