2023-11-14 15:10:00 +00:00
|
|
|
import { clsx } from 'clsx'
|
2024-02-04 11:25:21 +00:00
|
|
|
import { Match, Show, Switch, createEffect, createSignal } from 'solid-js'
|
2023-11-14 15:10:00 +00:00
|
|
|
|
|
|
|
import { useLocalize } from '../../../context/localize'
|
2023-06-10 14:10:05 +00:00
|
|
|
import { Button } from '../Button'
|
|
|
|
import { Icon } from '../Icon'
|
|
|
|
import { Popover } from '../Popover'
|
2023-11-14 15:10:00 +00:00
|
|
|
|
2023-06-10 14:10:05 +00:00
|
|
|
import styles from './VideoPlayer.module.scss'
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
videoUrl: string
|
|
|
|
title?: string
|
|
|
|
description?: string
|
|
|
|
class?: string
|
2023-07-19 11:00:58 +00:00
|
|
|
onVideoDelete?: () => void
|
|
|
|
articleView?: boolean
|
2023-06-10 14:10:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const VideoPlayer = (props: Props) => {
|
|
|
|
const { t } = useLocalize()
|
|
|
|
const [videoId, setVideoId] = createSignal<string | undefined>()
|
|
|
|
const [isVimeo, setIsVimeo] = createSignal(false)
|
|
|
|
|
|
|
|
createEffect(() => {
|
|
|
|
const isYoutube = props.videoUrl.includes('youtube.com') || props.videoUrl.includes('youtu.be')
|
|
|
|
setIsVimeo(!isYoutube)
|
|
|
|
if (isYoutube) {
|
|
|
|
if (props.videoUrl.includes('youtube.com')) {
|
2023-09-21 17:16:07 +00:00
|
|
|
const videoIdMatch = props.videoUrl.match(/watch=(\w+)/)
|
2024-02-04 09:03:15 +00:00
|
|
|
setVideoId(videoIdMatch?.[1])
|
2023-06-10 14:10:05 +00:00
|
|
|
} else {
|
|
|
|
const videoIdMatch = props.videoUrl.match(/youtu.be\/(\w+)/)
|
2024-02-04 09:03:15 +00:00
|
|
|
setVideoId(videoIdMatch?.[1])
|
2023-06-10 14:10:05 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const videoIdMatch = props.videoUrl.match(/vimeo.com\/(\d+)/)
|
2024-02-04 09:03:15 +00:00
|
|
|
setVideoId(videoIdMatch?.[1])
|
2023-06-10 14:10:05 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return (
|
2023-07-19 11:00:58 +00:00
|
|
|
<div class={clsx(styles.VideoPlayer, props.class, { [styles.articleView]: props.articleView })}>
|
|
|
|
<Show when={props.onVideoDelete}>
|
2023-06-10 14:10:05 +00:00
|
|
|
<Popover content={t('Delete')}>
|
2024-06-24 17:50:27 +00:00
|
|
|
{(triggerRef: (el: HTMLElement) => void) => (
|
2023-06-10 14:10:05 +00:00
|
|
|
<Button
|
|
|
|
ref={triggerRef}
|
|
|
|
size="S"
|
|
|
|
class={styles.deleteAction}
|
2024-06-24 17:50:27 +00:00
|
|
|
onClick={() => props.onVideoDelete?.()}
|
2023-06-10 14:10:05 +00:00
|
|
|
value={<Icon class={styles.deleteIcon} name="delete" />}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</Popover>
|
|
|
|
</Show>
|
|
|
|
<Switch>
|
|
|
|
<Match when={isVimeo()}>
|
|
|
|
<div class={styles.videoContainer}>
|
|
|
|
<iframe
|
2024-02-04 09:03:15 +00:00
|
|
|
title={props.title}
|
2023-06-10 14:10:05 +00:00
|
|
|
src={`https://player.vimeo.com/video/${videoId()}`}
|
|
|
|
width="640"
|
|
|
|
height="360"
|
|
|
|
allow="autoplay; fullscreen; picture-in-picture"
|
|
|
|
allowfullscreen
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</Match>
|
|
|
|
<Match when={!isVimeo()}>
|
|
|
|
<div class={styles.videoContainer}>
|
|
|
|
<iframe
|
2024-02-04 09:03:15 +00:00
|
|
|
title={props.title}
|
2023-06-10 14:10:05 +00:00
|
|
|
width="560"
|
|
|
|
height="315"
|
|
|
|
src={`https://www.youtube.com/embed/${videoId()}`}
|
|
|
|
allowfullscreen
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</Match>
|
|
|
|
</Switch>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|