2022-12-22 17:02:34 +01:00
|
|
|
import { LiteBadge } from './LiteBadge'
|
2023-01-16 12:13:21 +01:00
|
|
|
import { createEffect, createSignal, onCleanup, onMount, Show } from 'solid-js'
|
2023-01-25 11:27:47 +01:00
|
|
|
import { injectCustomHeadCode, isNotEmpty } from 'utils'
|
2022-12-22 17:02:34 +01:00
|
|
|
import { getInitialChatReplyQuery } from '@/queries/getInitialChatReplyQuery'
|
|
|
|
import { ConversationContainer } from './ConversationContainer'
|
|
|
|
import { setIsMobile } from '@/utils/isMobileSignal'
|
2023-02-21 08:28:36 +01:00
|
|
|
import { BotContext, InitialChatReply, OutgoingLog } from '@/types'
|
2023-01-16 12:13:21 +01:00
|
|
|
import { ErrorMessage } from './ErrorMessage'
|
|
|
|
import {
|
|
|
|
getExistingResultIdFromSession,
|
|
|
|
setResultInSession,
|
|
|
|
} from '@/utils/sessionStorage'
|
|
|
|
import { setCssVariablesValue } from '@/utils/setCssVariablesValue'
|
2022-12-22 17:02:34 +01:00
|
|
|
|
2023-02-21 08:28:36 +01:00
|
|
|
export type BotProps = {
|
2023-02-23 07:48:11 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2023-02-21 08:28:36 +01:00
|
|
|
typebot: string | any
|
|
|
|
isPreview?: boolean
|
|
|
|
resultId?: string
|
|
|
|
startGroupId?: string
|
|
|
|
prefilledVariables?: Record<string, unknown>
|
2022-12-22 17:02:34 +01:00
|
|
|
apiHost?: string
|
2023-01-16 12:13:21 +01:00
|
|
|
onNewInputBlock?: (ids: { id: string; groupId: string }) => void
|
|
|
|
onAnswer?: (answer: { message: string; blockId: string }) => void
|
|
|
|
onInit?: () => void
|
|
|
|
onEnd?: () => void
|
2023-02-21 08:28:36 +01:00
|
|
|
onNewLogs?: (logs: OutgoingLog[]) => void
|
2022-12-22 17:02:34 +01:00
|
|
|
}
|
|
|
|
|
2023-01-25 11:27:47 +01:00
|
|
|
export const Bot = (props: BotProps & { class?: string }) => {
|
2022-12-22 17:02:34 +01:00
|
|
|
const [initialChatReply, setInitialChatReply] = createSignal<
|
|
|
|
InitialChatReply | undefined
|
2023-01-16 12:13:21 +01:00
|
|
|
>()
|
2023-01-25 11:27:47 +01:00
|
|
|
const [customCss, setCustomCss] = createSignal('')
|
|
|
|
const [isInitialized, setIsInitialized] = createSignal(false)
|
|
|
|
const [error, setError] = createSignal<Error | undefined>()
|
2022-12-22 17:02:34 +01:00
|
|
|
|
2023-01-16 12:13:21 +01:00
|
|
|
const initializeBot = async () => {
|
2023-01-25 11:27:47 +01:00
|
|
|
setIsInitialized(true)
|
2023-01-16 12:13:21 +01:00
|
|
|
const urlParams = new URLSearchParams(location.search)
|
|
|
|
props.onInit?.()
|
|
|
|
const prefilledVariables: { [key: string]: string } = {}
|
|
|
|
urlParams.forEach((value, key) => {
|
|
|
|
prefilledVariables[key] = value
|
|
|
|
})
|
|
|
|
const { data, error } = await getInitialChatReplyQuery({
|
|
|
|
typebot: props.typebot,
|
|
|
|
apiHost: props.apiHost,
|
|
|
|
isPreview: props.isPreview ?? false,
|
|
|
|
resultId: isNotEmpty(props.resultId)
|
|
|
|
? props.resultId
|
|
|
|
: getExistingResultIdFromSession(),
|
|
|
|
startGroupId: props.startGroupId,
|
|
|
|
prefilledVariables: {
|
|
|
|
...prefilledVariables,
|
|
|
|
...props.prefilledVariables,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if (error && 'code' in error && typeof error.code === 'string') {
|
|
|
|
if (['BAD_REQUEST', 'FORBIDDEN'].includes(error.code))
|
|
|
|
setError(new Error('This bot is now closed.'))
|
2023-01-25 11:27:47 +01:00
|
|
|
if (error.code === 'NOT_FOUND')
|
|
|
|
setError(new Error("The bot you're looking for doesn't exist."))
|
2023-01-16 12:13:21 +01:00
|
|
|
return
|
2022-12-22 17:02:34 +01:00
|
|
|
}
|
2023-01-16 12:13:21 +01:00
|
|
|
|
2023-01-25 11:27:47 +01:00
|
|
|
if (!data) return setError(new Error("Error! Couldn't initiate the chat."))
|
2023-01-16 12:13:21 +01:00
|
|
|
|
|
|
|
if (data.resultId) setResultInSession(data.resultId)
|
|
|
|
setInitialChatReply(data)
|
2023-01-25 11:27:47 +01:00
|
|
|
setCustomCss(data.typebot.theme.customCss ?? '')
|
2023-01-16 12:13:21 +01:00
|
|
|
|
|
|
|
if (data.input?.id && props.onNewInputBlock)
|
|
|
|
props.onNewInputBlock({
|
|
|
|
id: data.input.id,
|
|
|
|
groupId: data.input.groupId,
|
|
|
|
})
|
2023-01-25 11:27:47 +01:00
|
|
|
if (data.logs) props.onNewLogs?.(data.logs)
|
2023-01-16 12:13:21 +01:00
|
|
|
const customHeadCode = data.typebot.settings.metadata.customHeadCode
|
|
|
|
if (customHeadCode) injectCustomHeadCode(customHeadCode)
|
|
|
|
}
|
|
|
|
|
2023-01-25 11:27:47 +01:00
|
|
|
createEffect(() => {
|
|
|
|
if (!props.typebot || isInitialized()) return
|
2023-01-16 12:13:21 +01:00
|
|
|
initializeBot().then()
|
2022-12-22 17:02:34 +01:00
|
|
|
})
|
|
|
|
|
2023-01-25 11:27:47 +01:00
|
|
|
createEffect(() => {
|
|
|
|
if (typeof props.typebot === 'string') return
|
|
|
|
setCustomCss(props.typebot.theme.customCss ?? '')
|
|
|
|
})
|
|
|
|
|
|
|
|
onCleanup(() => {
|
|
|
|
setIsInitialized(false)
|
|
|
|
})
|
|
|
|
|
2022-12-22 17:02:34 +01:00
|
|
|
return (
|
2023-01-16 12:13:21 +01:00
|
|
|
<>
|
2023-01-25 11:27:47 +01:00
|
|
|
<style>{customCss()}</style>
|
2023-01-16 12:13:21 +01:00
|
|
|
<Show when={error()} keyed>
|
|
|
|
{(error) => <ErrorMessage error={error} />}
|
|
|
|
</Show>
|
2022-12-22 17:02:34 +01:00
|
|
|
<Show when={initialChatReply()} keyed>
|
|
|
|
{(initialChatReply) => (
|
|
|
|
<BotContent
|
2023-01-25 11:27:47 +01:00
|
|
|
class={props.class}
|
2023-01-16 12:13:21 +01:00
|
|
|
initialChatReply={{
|
|
|
|
...initialChatReply,
|
|
|
|
typebot: {
|
|
|
|
...initialChatReply.typebot,
|
|
|
|
settings:
|
|
|
|
typeof props.typebot === 'string'
|
|
|
|
? initialChatReply.typebot?.settings
|
|
|
|
: props.typebot?.settings,
|
|
|
|
theme:
|
|
|
|
typeof props.typebot === 'string'
|
|
|
|
? initialChatReply.typebot?.theme
|
|
|
|
: props.typebot?.theme,
|
|
|
|
},
|
|
|
|
}}
|
2022-12-22 17:02:34 +01:00
|
|
|
context={{
|
|
|
|
apiHost: props.apiHost,
|
2023-01-25 11:27:47 +01:00
|
|
|
isPreview:
|
|
|
|
typeof props.typebot !== 'string' || (props.isPreview ?? false),
|
2023-01-16 12:13:21 +01:00
|
|
|
typebotId: initialChatReply.typebot.id,
|
2022-12-22 17:02:34 +01:00
|
|
|
resultId: initialChatReply.resultId,
|
|
|
|
}}
|
2023-01-16 12:13:21 +01:00
|
|
|
onNewInputBlock={props.onNewInputBlock}
|
2023-01-25 11:27:47 +01:00
|
|
|
onNewLogs={props.onNewLogs}
|
2023-01-16 12:13:21 +01:00
|
|
|
onAnswer={props.onAnswer}
|
|
|
|
onEnd={props.onEnd}
|
2022-12-22 17:02:34 +01:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</Show>
|
2023-01-16 12:13:21 +01:00
|
|
|
</>
|
2022-12-22 17:02:34 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
type BotContentProps = {
|
|
|
|
initialChatReply: InitialChatReply
|
|
|
|
context: BotContext
|
2023-01-25 11:27:47 +01:00
|
|
|
class?: string
|
|
|
|
onNewInputBlock?: (block: { id: string; groupId: string }) => void
|
2023-01-16 12:13:21 +01:00
|
|
|
onAnswer?: (answer: { message: string; blockId: string }) => void
|
|
|
|
onEnd?: () => void
|
2023-02-21 08:28:36 +01:00
|
|
|
onNewLogs?: (logs: OutgoingLog[]) => void
|
2022-12-22 17:02:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const BotContent = (props: BotContentProps) => {
|
|
|
|
let botContainer: HTMLDivElement | undefined
|
|
|
|
|
|
|
|
const resizeObserver = new ResizeObserver((entries) => {
|
|
|
|
return setIsMobile(entries[0].target.clientWidth < 400)
|
|
|
|
})
|
|
|
|
|
|
|
|
const injectCustomFont = () => {
|
|
|
|
const font = document.createElement('link')
|
|
|
|
font.href = `https://fonts.googleapis.com/css2?family=${
|
|
|
|
props.initialChatReply.typebot?.theme?.general?.font ?? 'Open Sans'
|
2023-02-20 17:40:51 +01:00
|
|
|
}:ital,wght@0,300;0,400;0,600;1,300;1,400;1,600&display=swap');')`
|
2022-12-22 17:02:34 +01:00
|
|
|
font.rel = 'stylesheet'
|
|
|
|
document.head.appendChild(font)
|
|
|
|
}
|
|
|
|
|
|
|
|
onMount(() => {
|
|
|
|
injectCustomFont()
|
2023-01-16 12:13:21 +01:00
|
|
|
if (!botContainer) return
|
|
|
|
resizeObserver.observe(botContainer)
|
|
|
|
})
|
|
|
|
|
|
|
|
createEffect(() => {
|
|
|
|
if (!botContainer) return
|
|
|
|
setCssVariablesValue(props.initialChatReply.typebot.theme, botContainer)
|
2022-12-22 17:02:34 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
onCleanup(() => {
|
2023-01-16 12:13:21 +01:00
|
|
|
if (!botContainer) return
|
|
|
|
resizeObserver.unobserve(botContainer)
|
2022-12-22 17:02:34 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
return (
|
2023-01-16 12:13:21 +01:00
|
|
|
<div
|
|
|
|
ref={botContainer}
|
2023-01-25 11:27:47 +01:00
|
|
|
class={
|
|
|
|
'relative flex w-full h-full text-base overflow-hidden bg-cover flex-col items-center typebot-container ' +
|
|
|
|
props.class
|
|
|
|
}
|
2023-01-16 12:13:21 +01:00
|
|
|
>
|
|
|
|
<div class="flex w-full h-full justify-center">
|
|
|
|
<ConversationContainer
|
|
|
|
context={props.context}
|
|
|
|
initialChatReply={props.initialChatReply}
|
|
|
|
onNewInputBlock={props.onNewInputBlock}
|
|
|
|
onAnswer={props.onAnswer}
|
|
|
|
onEnd={props.onEnd}
|
2023-01-25 11:27:47 +01:00
|
|
|
onNewLogs={props.onNewLogs}
|
2023-01-16 12:13:21 +01:00
|
|
|
/>
|
2022-12-22 17:02:34 +01:00
|
|
|
</div>
|
2023-01-16 12:13:21 +01:00
|
|
|
<Show
|
|
|
|
when={props.initialChatReply.typebot.settings.general.isBrandingEnabled}
|
|
|
|
>
|
|
|
|
<LiteBadge botContainer={botContainer} />
|
|
|
|
</Show>
|
|
|
|
</div>
|
2022-12-22 17:02:34 +01:00
|
|
|
)
|
|
|
|
}
|