2
0
Files
bot/packages/bot-engine/src/components/ChatGroup/ChatBlock/bubbles/EmbedBubble.tsx

78 lines
2.1 KiB
TypeScript
Raw Normal View History

import React, { useEffect, useMemo, useRef, useState } from 'react'
2022-06-11 07:27:38 +02:00
import { EmbedBubbleBlock } from 'models'
2022-03-23 12:01:35 +01:00
import { TypingContent } from './TypingContent'
import { parseVariables } from 'services/variable'
import { useTypebot } from 'contexts/TypebotContext'
2022-03-23 12:01:35 +01:00
type Props = {
2022-06-11 07:27:38 +02:00
block: EmbedBubbleBlock
2022-03-23 12:01:35 +01:00
onTransitionEnd: () => void
}
export const showAnimationDuration = 400
2022-06-11 07:27:38 +02:00
export const EmbedBubble = ({ block, onTransitionEnd }: Props) => {
const { typebot, isLoading } = useTypebot()
2022-03-23 12:01:35 +01:00
const messageContainer = useRef<HTMLDivElement | null>(null)
const [isTyping, setIsTyping] = useState(true)
const url = useMemo(
2022-06-11 07:27:38 +02:00
() => parseVariables(typebot.variables)(block.content?.url),
[block.content?.url, typebot.variables]
)
2022-03-23 12:01:35 +01:00
useEffect(() => {
if (!isTyping || isLoading) return
2022-03-23 12:01:35 +01:00
showContentAfterMediaLoad()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isLoading])
2022-03-23 12:01:35 +01:00
const showContentAfterMediaLoad = () => {
setTimeout(() => {
setIsTyping(false)
onTypingEnd()
}, 1000)
}
const onTypingEnd = () => {
setIsTyping(false)
setTimeout(() => {
onTransitionEnd()
}, showAnimationDuration)
}
return (
<div className="flex flex-col w-full" 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 w-full'
}
>
<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>
<iframe
id="embed-bubble-content"
src={url}
2022-03-23 12:01:35 +01:00
className={
'w-full z-20 p-4 content-opacity ' +
(isTyping ? 'opacity-0' : 'opacity-100')
}
style={{
2022-06-11 07:27:38 +02:00
height: isTyping ? '2rem' : block.content.height,
2022-03-23 12:01:35 +01:00
borderRadius: '15px',
}}
/>
</div>
</div>
</div>
)
}