Skip to content

Commit

Permalink
fix: check fcwidget loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuzhy-Deriv committed Nov 22, 2024
1 parent 0264d9d commit 0f5c4d7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/javascript/app/hooks/useFreshChat.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { useEffect } from 'react';
import { useEffect, useState } from 'react';
import { useScript } from 'usehooks-ts';

const useFreshChat = (token, flag) => {
const freshchat_script = 'https://static.deriv.com/scripts/freshchat/freshchat-1.0.1.js';
const script_status = useScript(flag ? freshchat_script : null);
const [is_ready, setIsReady] = useState(false);

useEffect(() => {
const checkFcWidget = (intervalId) => {
if (typeof window !== 'undefined') {
if (window.fcWidget?.isInitialized() === true) {
if (window.fcWidget?.isInitialized() === true && !is_ready) {
setIsReady(true);
clearInterval(intervalId);
}
}
Expand All @@ -30,6 +32,11 @@ const useFreshChat = (token, flag) => {

if (flag) initFreshChat();
}, [flag, script_status, token]);

return {
is_ready,
widget: window.fcWidget,
};
};

export default useFreshChat;
5 changes: 4 additions & 1 deletion src/javascript/app/pages/livechat.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ReactDOM from 'react-dom';
import useFreshChat from '../hooks/useFreshChat';
import useInterComChat from '../hooks/useInterComChat';
import useGrowthbookGetFeatureValue from '../hooks/useGrowthbookGetFeatureValue';
import { getElementById } from '../../_common/common_functions';

const LiveChat = ({ cs_live_chat }) => {

Expand All @@ -24,10 +25,12 @@ const LiveChat = ({ cs_live_chat }) => {
featureFlag: 'enable_intercom_st',
});

useFreshChat(token, isFreshChatEnabled);
const freshChat = useFreshChat(token, isFreshChatEnabled);
useInterComChat(token, isICEnabled);

if (!isFreshChatEnabled && !isICEnabled && !cs_live_chat) return null;
getElementById('livechat').style.display =
isFreshChatEnabled && !freshChat?.is_ready ? 'none' : 'flex';

return (
<React.Fragment>
Expand Down

0 comments on commit 0f5c4d7

Please sign in to comment.