import React, { useEffect, useMemo, useRef, useState } from 'react' import { useHostAvatars } from 'contexts/HostAvatarsContext' import { useTypebot } from 'contexts/TypebotContext' import { Variable, VideoBubbleContent, VideoBubbleContentType, VideoBubbleStep, } from 'models' import { TypingContent } from './TypingContent' import { parseVariables } from 'services/variable' type Props = { step: VideoBubbleStep onTransitionEnd: () => void } export const showAnimationDuration = 400 export const mediaLoadingFallbackTimeout = 5000 export const VideoBubble = ({ step, onTransitionEnd }: Props) => { const { typebot } = useTypebot() const { updateLastAvatarOffset } = useHostAvatars() const messageContainer = useRef(null) const [isTyping, setIsTyping] = useState(true) useEffect(() => { showContentAfterMediaLoad() // eslint-disable-next-line react-hooks/exhaustive-deps }, []) const showContentAfterMediaLoad = () => { setTimeout(() => { setIsTyping(false) onTypingEnd() }, 1000) } 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 ? : <>}
) } const VideoContent = ({ content, isTyping, variables, }: { content?: VideoBubbleContent isTyping: boolean variables: Variable[] }) => { const url = useMemo( () => parseVariables({ text: content?.url, variables: variables }), // eslint-disable-next-line react-hooks/exhaustive-deps [variables] ) if (!content?.type) return <> switch (content.type) { case VideoBubbleContentType.URL: const isSafariBrowser = window.navigator.vendor.match(/apple/i) return ( ) case VideoBubbleContentType.VIMEO: case VideoBubbleContentType.YOUTUBE: { const baseUrl = content.type === VideoBubbleContentType.VIMEO ? 'https://player.vimeo.com/video' : 'https://www.youtube.com/embed' return (