2021-12-22 14:59:07 +01:00
|
|
|
import React, { useEffect, useRef, useState } from 'react'
|
2021-12-29 10:22:26 +01:00
|
|
|
import { Answer, PublicTypebot } from '..'
|
2021-12-22 14:59:07 +01:00
|
|
|
|
|
|
|
import { Block } from '..'
|
|
|
|
import { ChatBlock } from './ChatBlock/ChatBlock'
|
2021-12-23 09:37:42 +01:00
|
|
|
import { useFrame } from 'react-frame-component'
|
|
|
|
import { setCssVariablesValue } from '../services/theme'
|
2021-12-29 10:22:26 +01:00
|
|
|
import { useAnswers } from '../contexts/AnswersContext'
|
|
|
|
import { deepEqual } from 'fast-equals'
|
2021-12-22 14:59:07 +01:00
|
|
|
|
2021-12-29 10:22:26 +01:00
|
|
|
type Props = {
|
|
|
|
typebot: PublicTypebot
|
|
|
|
onNewBlockVisible: (blockId: string) => void
|
|
|
|
onAnswersUpdate: (answers: Answer[]) => void
|
|
|
|
onCompleted: () => void
|
|
|
|
}
|
2021-12-22 14:59:07 +01:00
|
|
|
export const ConversationContainer = ({
|
|
|
|
typebot,
|
2021-12-29 10:22:26 +01:00
|
|
|
onNewBlockVisible,
|
|
|
|
onAnswersUpdate,
|
|
|
|
onCompleted,
|
|
|
|
}: Props) => {
|
2021-12-23 09:37:42 +01:00
|
|
|
const { document: frameDocument } = useFrame()
|
2021-12-22 14:59:07 +01:00
|
|
|
const [displayedBlocks, setDisplayedBlocks] = useState<Block[]>([])
|
2021-12-29 10:22:26 +01:00
|
|
|
const [localAnswers, setLocalAnswers] = useState<Answer[]>([])
|
|
|
|
const { answers } = useAnswers()
|
2021-12-22 14:59:07 +01:00
|
|
|
const bottomAnchor = useRef<HTMLDivElement | null>(null)
|
|
|
|
|
2021-12-29 10:22:26 +01:00
|
|
|
const displayNextBlock = (blockId?: string) => {
|
|
|
|
if (!blockId) return onCompleted()
|
2021-12-22 14:59:07 +01:00
|
|
|
const nextBlock = typebot.blocks.find((b) => b.id === blockId)
|
2021-12-29 10:22:26 +01:00
|
|
|
if (!nextBlock) return onCompleted()
|
|
|
|
onNewBlockVisible(blockId)
|
2021-12-22 14:59:07 +01:00
|
|
|
setDisplayedBlocks([...displayedBlocks, nextBlock])
|
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const firstBlockId = typebot.startBlock.steps[0].target?.blockId
|
|
|
|
if (firstBlockId) displayNextBlock(firstBlockId)
|
|
|
|
}, [])
|
|
|
|
|
2021-12-23 09:37:42 +01:00
|
|
|
useEffect(() => {
|
|
|
|
setCssVariablesValue(typebot.theme, frameDocument.body.style)
|
|
|
|
}, [typebot.theme, frameDocument])
|
|
|
|
|
2021-12-29 10:22:26 +01:00
|
|
|
useEffect(() => {
|
|
|
|
if (deepEqual(localAnswers, answers)) return
|
|
|
|
setLocalAnswers(answers)
|
|
|
|
onAnswersUpdate(answers)
|
|
|
|
}, [answers])
|
|
|
|
|
2021-12-22 14:59:07 +01:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className="overflow-y-scroll w-full lg:w-3/4 min-h-full rounded lg:px-5 px-3 pt-10 relative scrollable-container typebot-chat-view"
|
|
|
|
id="scrollable-container"
|
|
|
|
>
|
|
|
|
{displayedBlocks.map((block, idx) => (
|
|
|
|
<ChatBlock
|
|
|
|
key={block.id + idx}
|
|
|
|
block={block}
|
|
|
|
onBlockEnd={displayNextBlock}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
{/* We use a block to simulate padding because it makes iOS scroll flicker */}
|
2021-12-29 10:22:26 +01:00
|
|
|
<div className="w-full" ref={bottomAnchor} />
|
2021-12-22 14:59:07 +01:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|