From 97a738b3a3ba81333b1217ca1796f7778bbd2b85 Mon Sep 17 00:00:00 2001 From: Dustin Kroger Date: Fri, 17 Jan 2025 15:21:56 +0100 Subject: [PATCH] feat(container): refacor livechat component ref: MANAGER-15587 Signed-off-by: Dustin Kroger --- .../container/src/components/LiveChat.tsx | 95 +++++++--- .../container/src/components/VirtualAgent.tsx | 75 ++++---- .../livechat/LiveChat.component.tsx | 173 ++++++++++++++++++ .../src/components/livechat/LiveChat.spec.tsx | 0 .../components/livechat/liveChat.constants.ts | 3 + .../apps/container/src/container/index.tsx | 6 +- .../UsefulLinks/useUsefulLinks.ts | 1 - .../src/core/container/ContainerProvider.tsx | 1 + 8 files changed, 290 insertions(+), 64 deletions(-) create mode 100644 packages/manager/apps/container/src/components/livechat/LiveChat.component.tsx create mode 100644 packages/manager/apps/container/src/components/livechat/LiveChat.spec.tsx create mode 100644 packages/manager/apps/container/src/components/livechat/liveChat.constants.ts diff --git a/packages/manager/apps/container/src/components/LiveChat.tsx b/packages/manager/apps/container/src/components/LiveChat.tsx index 6385566292ae..2b282f03c706 100644 --- a/packages/manager/apps/container/src/components/LiveChat.tsx +++ b/packages/manager/apps/container/src/components/LiveChat.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useEffect, useState } from 'react'; import VirtualAgent from './VirtualAgent'; interface LiveChatProps { @@ -11,6 +11,8 @@ interface LiveChatProps { onReduce?: () => void; } +type LiveChatType = 'Adrielly' | 'SNOW'; + const getCustomerLevel = (level: string) => { switch (level) { case 'standard': @@ -31,27 +33,78 @@ const getCustomerLevel = (level: string) => { const LiveChat: React.FC> = ( { language, open, reduced, subsidiary, supportLevel, ...rest }: LiveChatProps, ): JSX.Element => { + + const [chatType, setChatType] = useState('Adrielly'); + + console.log('lc','open', open, 'reduced', reduced); + const customerLevel = getCustomerLevel(supportLevel); - const url = `https://chat.ovh.com/system/templates/liveChat-manager/${customerLevel}/${subsidiary}_${language}/docs/index2.html`; - - return ( - - ); + // const url = `https://chat.ovh.com/system/templates/liveChat-manager/${customerLevel}/${subsidiary}_${language}/docs/index2.html`; + const url = `https://chat.ovh.com/system/templates/pre-prod/prepa_prod/STD/FR_fr/docs/index2.html`; + + + useEffect(() => { + if (open) { + setChatType('Adrielly'); + } + }, [open]) + + useEffect(() => { + + const livechatMessageEventHandler = (ev: MessageEvent<{ event: string, queue: string }>) => { + + if (ev.origin !== 'https://chat.ovh.com') return; + ev.stopPropagation(); + + if (typeof ev.data !== 'object' || ev.data.event !== 'open_agent_chat') return + setChatType('SNOW'); + + } + + window.addEventListener('message', livechatMessageEventHandler) + return () => window.removeEventListener('message', livechatMessageEventHandler); + }, []) + + if (chatType === 'Adrielly') { + return (<> + + ) + } + else if (chatType === 'SNOW') { + return (<> + + ) + } }; export default LiveChat; diff --git a/packages/manager/apps/container/src/components/VirtualAgent.tsx b/packages/manager/apps/container/src/components/VirtualAgent.tsx index e0ef1f49c62c..bea40ed0a778 100644 --- a/packages/manager/apps/container/src/components/VirtualAgent.tsx +++ b/packages/manager/apps/container/src/components/VirtualAgent.tsx @@ -9,6 +9,7 @@ import dialogPolyfill from 'dialog-polyfill'; import { useMediaQuery } from 'react-responsive'; import styles from './virtualAgentStyles.module.scss'; +import useContainer from '@/core/container'; interface VirtualAgentProps { name: string; @@ -41,9 +42,15 @@ const VirtualAgent: React.FC> = ( buttonIcon = 'oui-icon-chat', customStyles, onClose, - onStart, - onReduce, } = props; + + const { + chatbotOpen, + chatbotReduced, + setChatbotOpen, + setChatbotReduced, + } = useContainer(); + const dialog = useRef(null); const mainFrame = useRef(null); const [started, setStarted] = useState(agentStarted); @@ -58,8 +65,8 @@ const VirtualAgent: React.FC> = ( }); const toggle = () => { - setReduced(!reduced); - if (onReduce) onReduce(); + setChatbotReduced(!chatbotReduced); + // if (onReduce) onReduce(); }; const stop = () => { setStarted(false); @@ -69,8 +76,7 @@ const VirtualAgent: React.FC> = ( const start = (startedValue: boolean, reducedValue: boolean) => { setStarted(startedValue); - setReduced(reducedValue); - if (onStart) onStart(); + setChatbotReduced(reducedValue); }; const setCustomStyleOnMobile = ( @@ -89,18 +95,18 @@ const VirtualAgent: React.FC> = ( useEffect(() => { dialogPolyfill.registerDialog(dialog.current); - if (useStorage) { - if (storage) { - start( - storage === 'started' || storage === 'reduced', - storage === 'reduced', - ); - } else { - start(agentStarted, !agentStarted); - } - } else { - start(agentStarted, !agentStarted); - } + // if (useStorage) { + // if (storage) { + // start( + // storage === 'started' || storage === 'reduced', + // storage === 'reduced', + // ); + // } else { + // start(agentStarted, !agentStarted); + // } + // } else { + start(agentStarted, false); + //} }, []); useEffect(() => { @@ -110,14 +116,7 @@ const VirtualAgent: React.FC> = ( }, [agentStarted]); useEffect(() => { - if (!agentReduced) { - setReduced(false); - if (onReduce) onReduce(); - } - }, [agentReduced]); - - useEffect(() => { - if (started && !reduced) { + if (started && !chatbotReduced) { dialog.current.show(); // This next line is for the focus when clicking with the keyboard // The native dialog API seem to focus right with the mouse click but not with the keyboard @@ -128,16 +127,16 @@ const VirtualAgent: React.FC> = ( } else { if (dialog?.current?.open) dialog.current.close(); - if (started && reduced) setStorage('reduced'); + if (started && chatbotReduced) setStorage('reduced'); else removeStorage(); } - }, [reduced, started]); + }, [chatbotReduced, started]); return (
> = ( className={`${styles.dialog_content} d-flex h-100 flex-column`} >
- {reduced && ( + {chatbotReduced && (