2
0
Files
bot/packages/bot-engine/src/components/ConversationContainer.tsx

73 lines
2.3 KiB
TypeScript
Raw Normal View History

import React, { useEffect, useRef, useState } from 'react'
import { ChatBlock } from './ChatBlock/ChatBlock'
2021-12-23 09:37:42 +01:00
import { useFrame } from 'react-frame-component'
import { setCssVariablesValue } from '../services/theme'
import { useAnswers } from '../contexts/AnswersContext'
import { deepEqual } from 'fast-equals'
2022-01-06 09:40:56 +01:00
import { Answer, Block, PublicTypebot } from 'models'
import { filterTable } from 'utils'
type Props = {
typebot: PublicTypebot
onNewBlockVisible: (blockId: string) => void
2021-12-30 10:24:16 +01:00
onNewAnswer: (answer: Answer) => void
onCompleted: () => void
}
export const ConversationContainer = ({
typebot,
onNewBlockVisible,
2021-12-30 10:24:16 +01:00
onNewAnswer,
onCompleted,
}: Props) => {
2021-12-23 09:37:42 +01:00
const { document: frameDocument } = useFrame()
const [displayedBlocks, setDisplayedBlocks] = useState<Block[]>([])
2021-12-30 10:24:16 +01:00
const [localAnswer, setLocalAnswer] = useState<Answer | undefined>()
const { answers } = useAnswers()
const bottomAnchor = useRef<HTMLDivElement | null>(null)
const displayNextBlock = (blockId?: string) => {
if (!blockId) return onCompleted()
2022-01-06 09:40:56 +01:00
const nextBlock = typebot.blocks.byId[blockId]
if (!nextBlock) return onCompleted()
onNewBlockVisible(blockId)
setDisplayedBlocks([...displayedBlocks, nextBlock])
}
useEffect(() => {
2022-01-06 09:40:56 +01:00
const blocks = typebot.blocks
const firstBlockId =
typebot.steps.byId[blocks.byId[blocks.allIds[0]].stepIds[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])
useEffect(() => {
2021-12-30 10:24:16 +01:00
const answer = [...answers].pop()
if (!answer || deepEqual(localAnswer, answer)) return
setLocalAnswer(answer)
onNewAnswer(answer)
}, [answers])
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}
2022-01-06 09:40:56 +01:00
steps={filterTable(block.stepIds, typebot.steps)}
onBlockEnd={displayNextBlock}
/>
))}
{/* We use a block to simulate padding because it makes iOS scroll flicker */}
<div className="w-full" ref={bottomAnchor} />
</div>
)
}