-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix 6428/chat open on clicking dropdown (#6429)
This issue was caused due to last comomit on issue by me only fix_6127/support_button_updated I had fixed the chat opening problem , in this commit --------- Co-authored-by: Charles Bochet <[email protected]>
- Loading branch information
1 parent
6bc7622
commit 0fd3c8b
Showing
3 changed files
with
97 additions
and
100 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
95 changes: 95 additions & 0 deletions
95
packages/twenty-front/src/modules/support/hooks/useSupportChat.ts
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,95 @@ | ||
import { currentUserState } from '@/auth/states/currentUserState'; | ||
import { currentWorkspaceMemberState } from '@/auth/states/currentWorkspaceMemberState'; | ||
import { supportChatState } from '@/client-config/states/supportChatState'; | ||
import { useIsPrefetchLoading } from '@/prefetch/hooks/useIsPrefetchLoading'; | ||
import { isNonEmptyString } from '@sniptt/guards'; | ||
import { useCallback, useEffect, useState } from 'react'; | ||
import { useRecoilValue } from 'recoil'; | ||
import { isDefined } from 'twenty-ui'; | ||
import { User, WorkspaceMember } from '~/generated-metadata/graphql'; | ||
|
||
const insertScript = ({ | ||
src, | ||
innerHTML, | ||
onLoad, | ||
}: { | ||
src?: string; | ||
innerHTML?: string; | ||
onLoad?: (...args: any[]) => void; | ||
}) => { | ||
const script = document.createElement('script'); | ||
if (isNonEmptyString(src)) script.src = src; | ||
if (isNonEmptyString(innerHTML)) script.innerHTML = innerHTML; | ||
if (isDefined(onLoad)) script.onload = onLoad; | ||
document.body.appendChild(script); | ||
}; | ||
|
||
export const useSupportChat = () => { | ||
const currentUser = useRecoilValue(currentUserState); | ||
const currentWorkspaceMember = useRecoilValue(currentWorkspaceMemberState); | ||
const supportChat = useRecoilValue(supportChatState); | ||
const [isFrontChatLoaded, setIsFrontChatLoaded] = useState(false); | ||
const loading = useIsPrefetchLoading(); | ||
|
||
const configureFront = useCallback( | ||
( | ||
chatId: string, | ||
currentUser: Pick<User, 'email' | 'supportUserHash'>, | ||
currentWorkspaceMember: Pick<WorkspaceMember, 'name'>, | ||
) => { | ||
const url = 'https://chat-assets.frontapp.com/v1/chat.bundle.js'; | ||
let script = document.querySelector(`script[src="${url}"]`); | ||
|
||
// This function only gets called when front chat is not loaded | ||
// If the script is already defined, but front chat is not loaded | ||
// then there was an error loading the script; reload the script | ||
if (isDefined(script)) { | ||
script.parentNode?.removeChild(script); | ||
script = null; | ||
} | ||
|
||
insertScript({ | ||
src: url, | ||
onLoad: () => { | ||
window.FrontChat?.('init', { | ||
chatId, | ||
useDefaultLauncher: false, | ||
email: currentUser.email, | ||
name: | ||
currentWorkspaceMember.name.firstName + | ||
' ' + | ||
currentWorkspaceMember.name.lastName, | ||
userHash: currentUser?.supportUserHash, | ||
}); | ||
setIsFrontChatLoaded(true); | ||
}, | ||
}); | ||
}, | ||
[], | ||
); | ||
|
||
useEffect(() => { | ||
if ( | ||
supportChat?.supportDriver === 'front' && | ||
isNonEmptyString(supportChat.supportFrontChatId) && | ||
isNonEmptyString(currentUser?.email) && | ||
isDefined(currentWorkspaceMember) && | ||
!isFrontChatLoaded | ||
) { | ||
configureFront( | ||
supportChat.supportFrontChatId, | ||
currentUser, | ||
currentWorkspaceMember, | ||
); | ||
} | ||
}, [ | ||
configureFront, | ||
currentUser, | ||
isFrontChatLoaded, | ||
supportChat?.supportDriver, | ||
supportChat.supportFrontChatId, | ||
currentWorkspaceMember, | ||
]); | ||
|
||
return { loading, isFrontChatLoaded }; | ||
}; |
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