import React, { useEffect, useMemo, useRef, useState } from 'react' import { useTypebot } from 'contexts/TypebotContext' import { ImageBubbleBlock } from 'models' import { TypingContent } from './TypingContent' import { parseVariables } from 'services/variable' type Props = { block: ImageBubbleBlock onTransitionEnd: () => void } export const showAnimationDuration = 400 export const mediaLoadingFallbackTimeout = 5000 export const ImageBubble = ({ block, onTransitionEnd }: Props) => { const { typebot, isLoading } = useTypebot() const messageContainer = useRef(null) const image = useRef(null) const [isTyping, setIsTyping] = useState(true) const url = useMemo( () => parseVariables(typebot.variables)(block.content?.url), [block.content?.url, typebot.variables] ) useEffect(() => { if (!isTyping || isLoading) return showContentAfterMediaLoad() // eslint-disable-next-line react-hooks/exhaustive-deps }, [isLoading]) 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
) }