import React, { useEffect, useRef, useState } from 'react' import { ChatBlock } from './ChatBlock/ChatBlock' import { useFrame } from 'react-frame-component' import { setCssVariablesValue } from '../services/theme' import { useAnswers } from '../contexts/AnswersContext' import { deepEqual } from 'fast-equals' import { Answer, Block, PublicTypebot, Target } from 'models' type Props = { typebot: PublicTypebot onNewBlockVisible: (blockId: string) => void onNewAnswer: (answer: Answer) => void onCompleted: () => void } export const ConversationContainer = ({ typebot, onNewBlockVisible, onNewAnswer, onCompleted, }: Props) => { const { document: frameDocument } = useFrame() const [displayedBlocks, setDisplayedBlocks] = useState< { block: Block; startStepId?: string }[] >([]) const [localAnswer, setLocalAnswer] = useState() const { answers } = useAnswers() const bottomAnchor = useRef(null) const displayNextBlock = (target?: Target) => { if (!target) return onCompleted() const nextBlock = { block: typebot.blocks.byId[target.blockId], startStepId: target.stepId, } if (!nextBlock) return onCompleted() onNewBlockVisible(target.blockId) setDisplayedBlocks([...displayedBlocks, nextBlock]) } useEffect(() => { const blocks = typebot.blocks const firstTarget = typebot.steps.byId[blocks.byId[blocks.allIds[0]].stepIds[0]].target if (firstTarget) displayNextBlock(firstTarget) }, []) useEffect(() => { setCssVariablesValue(typebot.theme, frameDocument.body.style) }, [typebot.theme, frameDocument]) useEffect(() => { const answer = [...answers].pop() if (!answer || deepEqual(localAnswer, answer)) return setLocalAnswer(answer) onNewAnswer(answer) }, [answers]) return (
{displayedBlocks.map((displayedBlock, idx) => ( ))} {/* We use a block to simulate padding because it makes iOS scroll flicker */}
) }