import React, { useEffect, useMemo, useRef, useState } from 'react' import { useTypebot } from 'contexts/TypebotContext' import { ImageBubbleStep } from 'models' import { TypingContent } from './TypingContent' import { parseVariables } from 'services/variable' type Props = { step: ImageBubbleStep onTransitionEnd: () => void } export const showAnimationDuration = 400 export const mediaLoadingFallbackTimeout = 5000 export const ImageBubble = ({ step, onTransitionEnd }: Props) => { const { typebot } = useTypebot() const messageContainer = useRef(null) const image = useRef(null) const [isTyping, setIsTyping] = useState(true) const url = useMemo( () => parseVariables(typebot.variables)(step.content?.url), [step.content?.url, typebot.variables] ) useEffect(() => { showContentAfterMediaLoad() // eslint-disable-next-line react-hooks/exhaustive-deps }, []) const showContentAfterMediaLoad = () => { if (!image.current) return const timeout = setTimeout(() => { setIsTyping(false) onTypingEnd() }, mediaLoadingFallbackTimeout) image.current.onload = () => { clearTimeout(timeout) setIsTyping(false) onTypingEnd() } } const onTypingEnd = () => { setIsTyping(false) setTimeout(() => { onTransitionEnd() }, showAnimationDuration) } return (
{isTyping ? : <>}
Bubble image
) }