2
0

🚸 (videoBubble) Reparse variable video URL to correctly detect provider

This commit is contained in:
Baptiste Arnaud
2023-10-03 11:44:51 +02:00
parent a53d128fb0
commit a7b784b446
4 changed files with 55 additions and 35 deletions

View File

@ -0,0 +1,20 @@
import { VideoBubbleContentType } from '@typebot.io/schemas/features/blocks/bubbles/video/enums'
const vimeoRegex = /vimeo\.com\/(\d+)/
const youtubeRegex = /youtube\.com\/(watch\?v=|shorts\/)(\w+)|youtu\.be\/(\w+)/
export const parseVideoUrl = (
url: string
): { type: VideoBubbleContentType; url: string; id?: string } => {
if (vimeoRegex.test(url)) {
const id = url.match(vimeoRegex)?.at(1)
if (!id) return { type: VideoBubbleContentType.URL, url }
return { type: VideoBubbleContentType.VIMEO, url, id }
}
if (youtubeRegex.test(url)) {
const id = url.match(youtubeRegex)?.at(2) ?? url.match(youtubeRegex)?.at(3)
if (!id) return { type: VideoBubbleContentType.URL, url }
return { type: VideoBubbleContentType.YOUTUBE, url, id }
}
return { type: VideoBubbleContentType.URL, url }
}