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, typebotId,
}: ChatwootOptions & { typebotId: string; resultId: string }) => { }: ChatwootOptions & { typebotId: string; resultId: string }) => {
const openChatwoot = `${parseSetUserCode(user, resultId)} const openChatwoot = `${parseSetUserCode(user, resultId)}
if(window.Typebot?.unmount) window.Typebot.unmount();
window.$chatwoot.setCustomAttributes({ window.$chatwoot.setCustomAttributes({
typebot_result_url: "${ typebot_result_url: "${
env.NEXTAUTH_URL env.NEXTAUTH_URL

View File

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

View File

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

View File

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

View File

@@ -5,3 +5,4 @@ export * from './setPrefilledVariables'
export * from './showPreviewMessage' export * from './showPreviewMessage'
export * from './toggle' export * from './toggle'
export * from './setInputValue' 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, ...existingPrefilledVariables,
...data.variables, ...data.variables,
})) }))
if (data.command === 'unmount') closeBot()
} }
const openBot = () => { const openBot = () => {

View File

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

View File

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

View File

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

View File

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