From ba3a2b711e45050b679c2fed1c2c520cbd59aec1 Mon Sep 17 00:00:00 2001 From: Baptiste Arnaud Date: Wed, 12 Jul 2023 12:00:23 +0200 Subject: [PATCH] :bug: (bot) Fix audio and video autoplay when loading take some time --- packages/embeds/js/package.json | 2 +- .../bubbles/audio/components/AudioBubble.tsx | 29 ++-- .../bubbles/video/components/VideoBubble.tsx | 129 +++++++++--------- packages/embeds/react/package.json | 2 +- 4 files changed, 85 insertions(+), 77 deletions(-) diff --git a/packages/embeds/js/package.json b/packages/embeds/js/package.json index 3071bfc51..e09713ec3 100644 --- a/packages/embeds/js/package.json +++ b/packages/embeds/js/package.json @@ -1,6 +1,6 @@ { "name": "@typebot.io/js", - "version": "0.0.74", + "version": "0.0.75", "description": "Javascript library to display typebots on your website", "type": "module", "main": "dist/index.js", diff --git a/packages/embeds/js/src/features/blocks/bubbles/audio/components/AudioBubble.tsx b/packages/embeds/js/src/features/blocks/bubbles/audio/components/AudioBubble.tsx index e3d0e65a6..efc375416 100644 --- a/packages/embeds/js/src/features/blocks/bubbles/audio/components/AudioBubble.tsx +++ b/packages/embeds/js/src/features/blocks/bubbles/audio/components/AudioBubble.tsx @@ -8,26 +8,34 @@ type Props = { } const showAnimationDuration = 400 -const typingDuration = 500 +const defaultTypingDuration = 5000 let typingTimeout: NodeJS.Timeout export const AudioBubble = (props: Props) => { let ref: HTMLDivElement | undefined + let audioElement: HTMLAudioElement | undefined const [isTyping, setIsTyping] = createSignal(true) + const autoPlay = () => { + if (audioElement) + audioElement + .play() + .catch((e) => console.warn('Could not autoplay the audio:', e)) + props.onTransitionEnd(ref?.offsetTop) + } + onMount(() => { typingTimeout = setTimeout(() => { setIsTyping(false) - setTimeout(() => { - const audioElement = ref?.querySelector('audio') - if (audioElement) - audioElement - .play() - .catch((e) => console.warn('Could not autoplay the audio:', e)) - props.onTransitionEnd(ref?.offsetTop) - }, showAnimationDuration) - }, typingDuration) + setTimeout(autoPlay, showAnimationDuration) + }, defaultTypingDuration) + if (audioElement) + audioElement.oncanplay = () => { + clearTimeout(typingTimeout) + setIsTyping(false) + setTimeout(autoPlay, showAnimationDuration) + } }) onCleanup(() => { @@ -48,6 +56,7 @@ export const AudioBubble = (props: Props) => { {isTyping() && }