Files
twenty/front/src/modules/ui/navigation/navbar/components/SupportChat.tsx
Charles Bochet 6129444c5c [WIP] Whole FE migrated (#2517)
* Wip

* WIP

* Removed concole log

* Add relations to workspace init (#2511)

* Add relations to workspace init

* remove logs

* update prefill

* add missing isSystem

* comment relation fields

* Migrate v2 core models to graphql schema (#2509)

* migrate v2 core models to graphql schema

* Migrate to new workspace member schema

* Continue work

* migrated-main

* Finished accountOwner nested field integration on companies

* Introduce bug

* Fix

---------

Co-authored-by: Lucas Bordeau <bordeau.lucas@gmail.com>
Co-authored-by: Weiko <corentin@twenty.com>
2023-11-15 15:46:06 +01:00

112 lines
2.9 KiB
TypeScript

import { useCallback, useEffect, useState } from 'react';
import styled from '@emotion/styled';
import { useRecoilValue } from 'recoil';
import { currentUserState } from '@/auth/states/currentUserState';
import {
CurrentWorkspaceMember,
currentWorkspaceMemberState,
} from '@/auth/states/currentWorkspaceMemberState';
import { supportChatState } from '@/client-config/states/supportChatState';
import { IconHelpCircle } from '@/ui/display/icon';
import { Button } from '@/ui/input/button/components/Button';
import { User } from '~/generated/graphql';
const StyledButtonContainer = styled.div`
display: flex;
`;
const insertScript = ({
src,
innerHTML,
onLoad,
}: {
src?: string;
innerHTML?: string;
onLoad?: (...args: any[]) => void;
}) => {
const script = document.createElement('script');
if (src) script.src = src;
if (innerHTML) script.innerHTML = innerHTML;
if (onLoad) script.onload = onLoad;
document.body.appendChild(script);
};
const SupportChat = () => {
const currentUser = useRecoilValue(currentUserState);
const currentWorkspaceMember = useRecoilValue(currentWorkspaceMemberState);
const supportChat = useRecoilValue(supportChatState);
const [isFrontChatLoaded, setIsFrontChatLoaded] = useState(false);
const configureFront = useCallback(
(
chatId: string,
currentUser: Pick<User, 'email' | 'supportUserHash'>,
currentWorkspaceMember: Pick<
CurrentWorkspaceMember,
'firstName' | 'lastName'
>,
) => {
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:
currentWorkspaceMember.firstName +
' ' +
currentWorkspaceMember.lastName,
userHash: currentUser?.supportUserHash,
});
setIsFrontChatLoaded(true);
},
});
}
},
[],
);
useEffect(() => {
if (
supportChat?.supportDriver === 'front' &&
supportChat.supportFrontChatId &&
currentUser?.email &&
currentWorkspaceMember &&
!isFrontChatLoaded
) {
configureFront(
supportChat.supportFrontChatId,
currentUser,
currentWorkspaceMember,
);
}
}, [
configureFront,
currentUser,
isFrontChatLoaded,
supportChat?.supportDriver,
supportChat.supportFrontChatId,
currentWorkspaceMember,
]);
return isFrontChatLoaded ? (
<StyledButtonContainer>
<Button
variant={'tertiary'}
size={'small'}
title="Support"
Icon={IconHelpCircle}
onClick={() => window.FrontChat?.('show')}
/>
</StyledButtonContainer>
) : null;
};
export default SupportChat;