2
0

(chatwoot) Unmount Typebot embed bubble when opening chatwoot

Closes #1007
This commit is contained in:
Baptiste Arnaud
2023-11-06 16:57:05 +01:00
parent b2b82c48e1
commit eed562b47a
11 changed files with 40 additions and 6 deletions

View File

@ -29,6 +29,7 @@ const parseChatwootOpenCode = ({
typebotId,
}: ChatwootOptions & { typebotId: string; resultId: string }) => {
const openChatwoot = `${parseSetUserCode(user, resultId)}
if(window.Typebot?.unmount) window.Typebot.unmount();
window.$chatwoot.setCustomAttributes({
typebot_result_url: "${
env.NEXTAUTH_URL

View File

@ -1,6 +1,6 @@
{
"name": "@typebot.io/js",
"version": "0.2.13",
"version": "0.2.14",
"description": "Javascript library to display typebots on your website",
"type": "module",
"main": "dist/index.js",

View File

@ -31,6 +31,7 @@ export const Bubble = (props: BubbleProps) => {
'theme',
'autoShowDelay',
])
const [isMounted, setIsMounted] = createSignal(true)
const [prefilledVariables, setPrefilledVariables] = createSignal(
// eslint-disable-next-line solid/reactivity
botProps.prefilledVariables
@ -91,6 +92,7 @@ export const Bubble = (props: BubbleProps) => {
...existingPrefilledVariables,
...data.variables,
}))
if (data.command === 'unmount') unmount()
}
const openBot = () => {
@ -126,8 +128,17 @@ export const Bubble = (props: BubbleProps) => {
setIsPreviewMessageDisplayed(false)
}
const unmount = () => {
if (isBotOpened()) {
closeBot()
setTimeout(() => {
setIsMounted(false)
}, 200)
} else setIsMounted(false)
}
return (
<>
<Show when={isMounted()}>
<style>{styles}</style>
<Show when={isPreviewMessageDisplayed()}>
<PreviewMessage
@ -175,6 +186,6 @@ export const Bubble = (props: BubbleProps) => {
/>
</Show>
</div>
</>
</Show>
)
}

View File

@ -4,7 +4,7 @@ export type CommandData = {
isFromTypebot: boolean
} & (
| {
command: 'open' | 'toggle' | 'close' | 'hidePreviewMessage'
command: 'open' | 'toggle' | 'close' | 'hidePreviewMessage' | 'unmount'
}
| ShowMessageCommandData
| SetPrefilledVariablesCommandData

View File

@ -5,3 +5,4 @@ export * from './setPrefilledVariables'
export * from './showPreviewMessage'
export * from './toggle'
export * from './setInputValue'
export * from './unmount'

View File

@ -0,0 +1,9 @@
import { CommandData } from '../types'
export const unmount = () => {
const message: CommandData = {
isFromTypebot: true,
command: 'unmount',
}
window.postMessage(message)
}

View File

@ -85,6 +85,7 @@ export const Popup = (props: PopupProps) => {
...existingPrefilledVariables,
...data.variables,
}))
if (data.command === 'unmount') closeBot()
}
const openBot = () => {

View File

@ -1,5 +1,6 @@
import styles from '../../../assets/index.css'
import { Bot, BotProps } from '@/components/Bot'
import { CommandData } from '@/features/commands/types'
import { createSignal, onCleanup, onMount, Show } from 'solid-js'
const hostElementCss = `
@ -27,9 +28,16 @@ export const Standard = (
})
onMount(() => {
window.addEventListener('message', processIncomingEvent)
botLauncherObserver.observe(element)
})
const processIncomingEvent = (event: MessageEvent<CommandData>) => {
const { data } = event
if (!data.isFromTypebot) return
if (data.command === 'unmount') setIsBotDisplayed(false)
}
onCleanup(() => {
botLauncherObserver.disconnect()
})

View File

@ -10,6 +10,7 @@ import {
showPreviewMessage,
toggle,
setInputValue,
unmount,
} from './features/commands'
export const initStandard = (props: BotProps & { id?: string }) => {
@ -43,6 +44,7 @@ type Typebot = {
showPreviewMessage: typeof showPreviewMessage
toggle: typeof toggle
setInputValue: typeof setInputValue
unmount: typeof unmount
}
declare const window:
@ -62,6 +64,7 @@ export const parseTypebot = () => ({
showPreviewMessage,
toggle,
setInputValue,
unmount,
})
export const injectTypebotInWindow = (typebot: Typebot) => {

View File

@ -1,6 +1,6 @@
{
"name": "@typebot.io/nextjs",
"version": "0.2.13",
"version": "0.2.14",
"description": "Convenient library to display typebots on your Next.js website",
"main": "dist/index.js",
"types": "dist/index.d.ts",

View File

@ -1,6 +1,6 @@
{
"name": "@typebot.io/react",
"version": "0.2.13",
"version": "0.2.14",
"description": "Convenient library to display typebots on your React app",
"main": "dist/index.js",
"types": "dist/index.d.ts",