Skip to content

Commit

Permalink
⚡ (chatwoot) Unmount Typebot embed bubble when opening chatwoot
Browse files Browse the repository at this point in the history
Closes #1007
  • Loading branch information
baptisteArno committed Nov 6, 2023
1 parent b2b82c4 commit eed562b
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/embeds/js/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
15 changes: 13 additions & 2 deletions packages/embeds/js/src/features/bubble/components/Bubble.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -91,6 +92,7 @@ export const Bubble = (props: BubbleProps) => {
...existingPrefilledVariables,
...data.variables,
}))
if (data.command === 'unmount') unmount()
}

const openBot = () => {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -175,6 +186,6 @@ export const Bubble = (props: BubbleProps) => {
/>
</Show>
</div>
</>
</Show>
)
}
2 changes: 1 addition & 1 deletion packages/embeds/js/src/features/commands/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export type CommandData = {
isFromTypebot: boolean
} & (
| {
command: 'open' | 'toggle' | 'close' | 'hidePreviewMessage'
command: 'open' | 'toggle' | 'close' | 'hidePreviewMessage' | 'unmount'
}
| ShowMessageCommandData
| SetPrefilledVariablesCommandData
Expand Down
1 change: 1 addition & 0 deletions packages/embeds/js/src/features/commands/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './setPrefilledVariables'
export * from './showPreviewMessage'
export * from './toggle'
export * from './setInputValue'
export * from './unmount'
9 changes: 9 additions & 0 deletions packages/embeds/js/src/features/commands/utils/unmount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { CommandData } from '../types'

export const unmount = () => {
const message: CommandData = {
isFromTypebot: true,
command: 'unmount',
}
window.postMessage(message)
}
1 change: 1 addition & 0 deletions packages/embeds/js/src/features/popup/components/Popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export const Popup = (props: PopupProps) => {
...existingPrefilledVariables,
...data.variables,
}))
if (data.command === 'unmount') closeBot()
}

const openBot = () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 = `
Expand Down Expand Up @@ -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()
})
Expand Down
3 changes: 3 additions & 0 deletions packages/embeds/js/src/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
showPreviewMessage,
toggle,
setInputValue,
unmount,
} from './features/commands'

export const initStandard = (props: BotProps & { id?: string }) => {
Expand Down Expand Up @@ -43,6 +44,7 @@ type Typebot = {
showPreviewMessage: typeof showPreviewMessage
toggle: typeof toggle
setInputValue: typeof setInputValue
unmount: typeof unmount
}

declare const window:
Expand All @@ -62,6 +64,7 @@ export const parseTypebot = () => ({
showPreviewMessage,
toggle,
setInputValue,
unmount,
})

export const injectTypebotInWindow = (typebot: Typebot) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/embeds/nextjs/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/embeds/react/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

3 comments on commit eed562b

@vercel
Copy link

@vercel vercel bot commented on eed562b Nov 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on eed562b Nov 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

builder-v2 – ./apps/builder

app.typebot.io
builder-v2-git-main-typebot-io.vercel.app
builder-v2-typebot-io.vercel.app

@vercel
Copy link

@vercel vercel bot commented on eed562b Nov 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

viewer-v2 – ./apps/viewer

bot.a6t-you.com
bot.artiweb.app
bot.devitus.com
bot.reeplai.com
bot.scayver.com
bot.tc-mail.com
carspecs.lam.ee
chat.borang.net
chat.lalmon.com
chat.sureb4.com
conversawpp.com
eventhub.com.au
feiraododia.com
fitness.riku.ai
games.klujo.com
iludogaming.com
ironcitygym.com
localove.online
marksteward.org
mikestuward.org
projektelco.com
proscale.com.br
ren.hartawan.my
sellmycarbr.com
sniperclass.com
svhmapp.mprs.in
tanjakoil.store
typebot.aloe.do
web.ofilipi.com
acordo-certo.com
app-liberado.pro
ask.pemantau.org
batepapo.digital
bot.contakit.com
bot.imovfast.com
bot.lafabri.link
bot.piccinato.co
chat.sifucrm.com
chat.syncwin.com
chatonlineja.com
clo.closeer.work
cockroach.cr8.ai
demo.chatjer.com
desafioem21d.com
digitando.online
faqs.nigerias.io
feiraodehoje.com
georgemarttt.com
go.chatbotcv.com
haymanevents.com
kw.wpwakanda.com
bot.dsignagency.com
bot.enthrallart.com
bot.eventhub.com.au
bot.febredojogo.com
bot.gravityatoms.in
bot.jepierre.com.br
bot.jogodoandre.com
bot.jogomoderno.com
bot.ltmidias.com.br
bot.viralsangat.com
bot.winglabs.com.br
bt.scadiagro.com.br
capitaldigital.live
carsalesenquiry.com
casahackeada.online
chat.jubahkonvo.com
chat.marius.digital
chat.mosdent.com.tr
chat.rahsiakami.com
chat.sr7digital.com
chatbot.matthesv.de
chatbot.repplai.com
chatwebandreia.site
co.onewebcenter.com
cr.onewebcenter.com
danielmentality.com
demo.botscientis.us
demo.newaiguide.com
demo.wemakebots.xyz
feiralimpanomes.com
finalizarseguro.app
go.onewebcenter.com
gv.onewebcenter.com
hrbot.robomotion.io
inearephones.cr8.ai
joaomigowski.com.br
kbsub.wpwakanda.com
limitenahora.com.br
live.botscientis.us
lojasbloombelle.com
mentoria.omelhor.vc
messengerbet.online
noticiariododia.com
nutrisamirbayde.com
online.onlinmey.com
portaldosganhos.com
premi.thegymgame.it
query.forgetsql.com
quest.wpwakanda.com

Please sign in to comment.