-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #135 from shafin-deriv/shafin/BOT-2392/chore-chat-app
chore: implement live chat and fresh chat
- Loading branch information
Showing
19 changed files
with
526 additions
and
112 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { useEffect } from 'react'; | ||
import { observer } from 'mobx-react-lite'; | ||
import { V2GetActiveToken } from '@/external/bot-skeleton/services/api/appId'; | ||
import { LegacyLiveChatOutlineIcon } from '@deriv/quill-icons/Legacy'; | ||
import { localize } from '@deriv-com/translations'; | ||
import { Tooltip, useDevice } from '@deriv-com/ui'; | ||
import useFreshChat from './useFreshchat'; | ||
import useIsLiveChatWidgetAvailable from './useIsLiveChatWidgetAvailable'; | ||
|
||
const Livechat = observer(() => { | ||
const { isDesktop } = useDevice(); | ||
|
||
const token = V2GetActiveToken() ?? null; | ||
|
||
const { is_livechat_available } = useIsLiveChatWidgetAvailable(); | ||
const { isReady, featureFlagValue, widget } = useFreshChat(token); | ||
|
||
useEffect(() => { | ||
window.enable_freshworks_live_chat = !!featureFlagValue; | ||
}, [featureFlagValue]); | ||
|
||
const isFreshchatEnabledButNotReady = featureFlagValue && !isReady; | ||
const isNeitherChatNorLiveChatAvailable = !is_livechat_available && !featureFlagValue; | ||
|
||
if (isFreshchatEnabledButNotReady || isNeitherChatNorLiveChatAvailable) { | ||
return null; | ||
} | ||
|
||
// Quick fix for making sure livechat won't popup if feature flag is late to enable. | ||
// We will add a refactor after this | ||
setInterval(() => { | ||
if (featureFlagValue) { | ||
window.LiveChatWidget?.call('destroy'); | ||
} | ||
}, 10); | ||
|
||
const liveChatClickHandler = () => { | ||
featureFlagValue ? widget.open() : window.LiveChatWidget?.call('maximize'); | ||
}; | ||
|
||
if (isDesktop) | ||
return ( | ||
<div onKeyDown={liveChatClickHandler} onClick={liveChatClickHandler}> | ||
<Tooltip as='button' className='app-footer__icon' tooltipContent={localize('Live chat')}> | ||
<LegacyLiveChatOutlineIcon iconSize='xs' /> | ||
</Tooltip> | ||
</div> | ||
); | ||
|
||
// For mobile sidebar we only need the component to be rendered. | ||
// Design is handled from use-mobile-menu-config.tsx | ||
return <LegacyLiveChatOutlineIcon iconSize='xs' className='mobile-menu__content__items--right-margin' />; | ||
}); | ||
|
||
export default Livechat; |
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,45 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { useScript } from 'usehooks-ts'; | ||
import useGrowthbookGetFeatureValue from '@/hooks/growthbook/useGrowthbookGetFeatureValue'; | ||
|
||
const useFreshChat = (token: string | null) => { | ||
const scriptStatus = useScript('https://static.deriv.com/scripts/freshchat/v1.0.2.js'); | ||
const [isReady, setIsReady] = useState(false); | ||
const { featureFlagValue, isGBLoaded } = useGrowthbookGetFeatureValue({ | ||
featureFlag: 'enable_freshchat', | ||
}); | ||
|
||
useEffect(() => { | ||
const checkFcWidget = (intervalId: NodeJS.Timeout) => { | ||
if (typeof window !== 'undefined') { | ||
if (window.fcWidget?.isInitialized() == true && !isReady) { | ||
setIsReady(true); | ||
clearInterval(intervalId); | ||
} | ||
} | ||
}; | ||
|
||
const initFreshChat = async () => { | ||
if (scriptStatus === 'ready' && window.FreshChat && window.fcSettings) { | ||
window.FreshChat.initialize({ | ||
token, | ||
hideButton: true, | ||
}); | ||
|
||
const intervalId = setInterval(() => checkFcWidget(intervalId), 500); | ||
|
||
return () => clearInterval(intervalId); | ||
} | ||
}; | ||
|
||
featureFlagValue && isGBLoaded && initFreshChat(); | ||
}, [featureFlagValue, isGBLoaded, isReady, scriptStatus, token]); | ||
|
||
return { | ||
isReady, | ||
widget: window.fcWidget, | ||
featureFlagValue, | ||
}; | ||
}; | ||
|
||
export default useFreshChat; |
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,17 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
const useIsLiveChatWidgetAvailable = () => { | ||
const [is_livechat_available, setIsLivechatAvailable] = useState(false); | ||
|
||
useEffect(() => { | ||
window.LiveChatWidget?.on('ready', data => { | ||
if (data.state.availability === 'online') setIsLivechatAvailable(true); | ||
}); | ||
}, []); | ||
|
||
return { | ||
is_livechat_available, | ||
}; | ||
}; | ||
|
||
export default useIsLiveChatWidgetAvailable; |
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,92 @@ | ||
import { useEffect } from 'react'; | ||
import Cookies from 'js-cookie'; | ||
import useRemoteConfig from '@/hooks/growthbook/useRemoteConfig'; | ||
import { URLUtils } from '@deriv-com/utils'; | ||
|
||
type TLiveChatClientInformation = { | ||
is_client_store_initialized: boolean; | ||
is_logged_in: boolean; | ||
loginid?: string; | ||
landing_company_shortcode?: string; | ||
currency?: string; | ||
residence?: string; | ||
email?: string; | ||
first_name?: string; | ||
last_name?: string; | ||
}; | ||
|
||
const useLiveChat = (client_information: TLiveChatClientInformation) => { | ||
const { | ||
is_client_store_initialized, | ||
landing_company_shortcode = ' ', | ||
currency = ' ', | ||
email = ' ', | ||
is_logged_in = ' ', | ||
loginid = ' ', | ||
residence = ' ', | ||
last_name = ' ', | ||
first_name = ' ', | ||
} = client_information; | ||
|
||
const url_query_string = window.location.search; | ||
const url_params = new URLSearchParams(url_query_string); | ||
const reset_password = URLUtils.getQueryParameter('action') === 'reset_password'; | ||
const should_disable_livechat = url_params.get('code') && reset_password; | ||
|
||
const { data } = useRemoteConfig(true); | ||
const { cs_chat_livechat } = data; | ||
|
||
useEffect(() => { | ||
if (is_client_store_initialized && cs_chat_livechat) { | ||
window.LiveChatWidget?.init(); | ||
} | ||
}, [is_client_store_initialized, cs_chat_livechat]); | ||
|
||
useEffect(() => { | ||
if (!should_disable_livechat && is_client_store_initialized) { | ||
window.LiveChatWidget?.on('ready', data => { | ||
//hide red widget on responsive | ||
if (data.state.visibility === 'minimized') { | ||
window.LiveChatWidget?.call('hide'); | ||
} | ||
const utm_data = JSON.parse(Cookies.get('utm_data') || '{}'); | ||
const { utm_source, utm_medium, utm_campaign } = utm_data; | ||
|
||
const session_variables = { | ||
is_logged_in: String(is_logged_in), | ||
utm_source: utm_source || ' ', | ||
utm_medium: utm_medium || ' ', | ||
utm_campaign: utm_campaign || ' ', | ||
loginid: is_logged_in ? loginid : ' ', | ||
landing_company_shortcode: is_logged_in ? landing_company_shortcode : ' ', | ||
currency: is_logged_in ? currency : ' ', | ||
residence: is_logged_in ? residence : ' ', | ||
email: is_logged_in ? email : ' ', | ||
}; | ||
|
||
window.LiveChatWidget?.call('set_session_variables', session_variables); | ||
|
||
if (is_logged_in) { | ||
window.LiveChatWidget?.call('set_customer_email', email); | ||
window.LiveChatWidget?.call('set_customer_name', `${first_name} ${last_name}`); | ||
} else { | ||
window.LiveChatWidget?.call('set_customer_email', ' '); | ||
window.LiveChatWidget?.call('set_customer_name', ' '); | ||
} | ||
}); | ||
} | ||
}, [ | ||
email, | ||
should_disable_livechat, | ||
loginid, | ||
is_logged_in, | ||
landing_company_shortcode, | ||
is_client_store_initialized, | ||
currency, | ||
first_name, | ||
last_name, | ||
residence, | ||
]); | ||
}; | ||
|
||
export default useLiveChat; |
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
Oops, something went wrong.