import React, { useEffect, useMemo, useRef, useState } from 'react' import { useHostAvatars } from 'contexts/HostAvatarsContext' 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 { updateLastAvatarOffset } = useHostAvatars() const messageContainer = useRef(null) const image = useRef(null) const [isTyping, setIsTyping] = useState(true) const url = useMemo( () => parseVariables({ text: step.content?.url, variables: typebot.variables }), [typebot.variables] ) useEffect(() => { showContentAfterMediaLoad() }, []) 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(() => { sendAvatarOffset() onTransitionEnd() }, showAnimationDuration) } const sendAvatarOffset = () => { if (!messageContainer.current) return const containerDimensions = messageContainer.current.getBoundingClientRect() updateLastAvatarOffset(containerDimensions.top + containerDimensions.height) } return (
{isTyping ? : <>}
) }