2022-01-14 07:49:24 +01:00
|
|
|
import React, { useEffect, useMemo, useRef, useState } from 'react'
|
|
|
|
import { useTypebot } from 'contexts/TypebotContext'
|
2022-01-20 16:14:47 +01:00
|
|
|
import { ImageBubbleStep } from 'models'
|
2021-12-22 14:59:07 +01:00
|
|
|
import { TypingContent } from './TypingContent'
|
2022-01-14 07:49:24 +01:00
|
|
|
import { parseVariables } from 'services/variable'
|
2021-12-22 14:59:07 +01:00
|
|
|
|
2022-01-20 16:14:47 +01:00
|
|
|
type Props = {
|
|
|
|
step: ImageBubbleStep
|
2021-12-22 14:59:07 +01:00
|
|
|
onTransitionEnd: () => void
|
|
|
|
}
|
|
|
|
|
|
|
|
export const showAnimationDuration = 400
|
|
|
|
|
|
|
|
export const mediaLoadingFallbackTimeout = 5000
|
|
|
|
|
2022-01-20 16:14:47 +01:00
|
|
|
export const ImageBubble = ({ step, onTransitionEnd }: Props) => {
|
2021-12-23 13:49:24 +01:00
|
|
|
const { typebot } = useTypebot()
|
2021-12-22 14:59:07 +01:00
|
|
|
const messageContainer = useRef<HTMLDivElement | null>(null)
|
2022-01-20 16:14:47 +01:00
|
|
|
const image = useRef<HTMLImageElement | null>(null)
|
2021-12-22 14:59:07 +01:00
|
|
|
const [isTyping, setIsTyping] = useState(true)
|
|
|
|
|
2022-01-20 16:14:47 +01:00
|
|
|
const url = useMemo(
|
2022-02-07 18:06:37 +01:00
|
|
|
() => parseVariables(typebot.variables)(step.content?.url),
|
2022-01-25 18:19:37 +01:00
|
|
|
[step.content?.url, typebot.variables]
|
2022-01-14 07:49:24 +01:00
|
|
|
)
|
|
|
|
|
2021-12-22 14:59:07 +01:00
|
|
|
useEffect(() => {
|
2022-01-20 16:14:47 +01:00
|
|
|
showContentAfterMediaLoad()
|
2022-01-25 18:19:37 +01:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2021-12-22 14:59:07 +01:00
|
|
|
}, [])
|
|
|
|
|
2022-01-20 16:14:47 +01:00
|
|
|
const showContentAfterMediaLoad = () => {
|
|
|
|
if (!image.current) return
|
|
|
|
const timeout = setTimeout(() => {
|
|
|
|
setIsTyping(false)
|
|
|
|
onTypingEnd()
|
|
|
|
}, mediaLoadingFallbackTimeout)
|
|
|
|
image.current.onload = () => {
|
|
|
|
clearTimeout(timeout)
|
|
|
|
setIsTyping(false)
|
|
|
|
onTypingEnd()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-22 14:59:07 +01:00
|
|
|
const onTypingEnd = () => {
|
|
|
|
setIsTyping(false)
|
|
|
|
setTimeout(() => {
|
|
|
|
onTransitionEnd()
|
|
|
|
}, showAnimationDuration)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="flex flex-col" ref={messageContainer}>
|
|
|
|
<div className="flex mb-2 w-full lg:w-11/12 items-center">
|
|
|
|
<div className={'flex relative z-10 items-start typebot-host-bubble'}>
|
|
|
|
<div
|
|
|
|
className="flex items-center absolute px-4 py-2 rounded-lg bubble-typing z-10 "
|
|
|
|
style={{
|
|
|
|
width: isTyping ? '4rem' : '100%',
|
|
|
|
height: isTyping ? '2rem' : '100%',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{isTyping ? <TypingContent /> : <></>}
|
|
|
|
</div>
|
2022-01-20 16:14:47 +01:00
|
|
|
<img
|
|
|
|
ref={image}
|
|
|
|
src={url}
|
|
|
|
className={
|
|
|
|
'p-4 content-opacity z-10 w-auto ' +
|
|
|
|
(isTyping ? 'opacity-0' : 'opacity-100')
|
|
|
|
}
|
|
|
|
style={{
|
|
|
|
maxHeight: '32rem',
|
|
|
|
height: isTyping ? '2rem' : 'auto',
|
|
|
|
maxWidth: '100%',
|
|
|
|
}}
|
2022-01-25 18:19:37 +01:00
|
|
|
alt="Bubble image"
|
2022-01-20 16:14:47 +01:00
|
|
|
/>
|
2021-12-22 14:59:07 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|