Fix front plugin not initialize with user email, first name and last name (#1506)

Fix front plugin not working
This commit is contained in:
Charles Bochet
2023-09-07 22:40:09 +02:00
committed by GitHub
parent 392c8052b2
commit 2eb1ea5462

View File

@ -1,4 +1,4 @@
import { useEffect, useState } from 'react'; import { useCallback, useEffect, useState } from 'react';
import { useTheme } from '@emotion/react'; import { useTheme } from '@emotion/react';
import styled from '@emotion/styled'; import styled from '@emotion/styled';
import { useRecoilValue } from 'recoil'; import { useRecoilValue } from 'recoil';
@ -7,6 +7,7 @@ import { currentUserState } from '@/auth/states/currentUserState';
import { supportChatState } from '@/client-config/states/supportChatState'; import { supportChatState } from '@/client-config/states/supportChatState';
import { Button } from '@/ui/button/components/Button'; import { Button } from '@/ui/button/components/Button';
import { IconHelpCircle } from '@/ui/icon'; import { IconHelpCircle } from '@/ui/icon';
import { User } from '~/generated/graphql';
const StyledButtonContainer = styled.div` const StyledButtonContainer = styled.div`
display: flex; display: flex;
@ -28,62 +29,56 @@ function insertScript({
document.body.appendChild(script); document.body.appendChild(script);
} }
function configureFront(chatId: string) {
const url = 'https://chat-assets.frontapp.com/v1/chat.bundle.js';
// check if Front Chat script is already loaded
const script = document.querySelector(`script[src="${url}"]`);
if (!script) {
// insert script and initialize Front Chat when it loads
insertScript({
src: url,
onLoad: () => {
window.FrontChat?.('init', {
chatId,
useDefaultLauncher: false,
});
},
});
}
}
export default function SupportChat() { export default function SupportChat() {
const theme = useTheme(); const theme = useTheme();
const currentUser = useRecoilValue(currentUserState); const currentUser = useRecoilValue(currentUserState);
const supportChat = useRecoilValue(supportChatState); const supportChat = useRecoilValue(supportChatState);
const [isFrontChatLoaded, setIsFrontChatLoaded] = useState(false); const [isFrontChatLoaded, setIsFrontChatLoaded] = useState(false);
const configureFront = useCallback(
(
chatId: string,
currentUser: Pick<User, 'email' | 'displayName' | 'supportUserHash'>,
) => {
const url = 'https://chat-assets.frontapp.com/v1/chat.bundle.js';
const script = document.querySelector(`script[src="${url}"]`);
if (!script) {
insertScript({
src: url,
onLoad: () => {
window.FrontChat?.('init', {
chatId,
useDefaultLauncher: false,
email: currentUser.email,
name: currentUser.displayName,
userHash: currentUser?.supportUserHash,
});
setIsFrontChatLoaded(true);
},
});
}
},
[],
);
useEffect(() => { useEffect(() => {
if ( if (
supportChat?.supportDriver === 'front' && supportChat?.supportDriver === 'front' &&
supportChat.supportFrontChatId && supportChat.supportFrontChatId &&
currentUser?.email &&
!isFrontChatLoaded !isFrontChatLoaded
) { ) {
configureFront(supportChat.supportFrontChatId); configureFront(supportChat.supportFrontChatId, currentUser);
setIsFrontChatLoaded(true);
}
if (currentUser?.email && isFrontChatLoaded) {
window.FrontChat?.('identity', {
email: currentUser.email,
name: currentUser.displayName,
userHash: currentUser?.supportUserHash,
});
} }
}, [ }, [
currentUser?.displayName, configureFront,
currentUser?.email, currentUser,
currentUser?.supportUserHash,
isFrontChatLoaded, isFrontChatLoaded,
supportChat?.supportDriver, supportChat?.supportDriver,
supportChat.supportFrontChatId, supportChat.supportFrontChatId,
]); ]);
function handleSupportClick() {
if (supportChat?.supportDriver === 'front') {
window.FrontChat?.('show');
}
}
return isFrontChatLoaded ? ( return isFrontChatLoaded ? (
<StyledButtonContainer> <StyledButtonContainer>
<Button <Button
@ -91,7 +86,7 @@ export default function SupportChat() {
size={'small'} size={'small'}
title="Support" title="Support"
icon={<IconHelpCircle size={theme.icon.size.md} />} icon={<IconHelpCircle size={theme.icon.size.md} />}
onClick={handleSupportClick} onClick={() => window.FrontChat?.('show')}
/> />
</StyledButtonContainer> </StyledButtonContainer>
) : null; ) : null;