'use client' import { cx, VariantProps } from 'class-variance-authority' import { useRef, useState, VideoHTMLAttributes } from 'react' import { Lang, languages } from '@scandic-hotels/common/constants/language' import { FocalPoint } from '@scandic-hotels/common/utils/imageVault' import { useIntl } from 'react-intl' import { VideoPlayerButton } from './Button' import { variants } from './variants' import styles from './videoPlayer.module.css' interface Caption { src: string srcLang: Lang isDefault: boolean } interface VideoPlayerProps extends VariantProps { src: string className?: string captions?: Caption[] focalPoint?: FocalPoint autoPlay?: boolean } export function VideoPlayer({ src, captions, focalPoint = { x: 50, y: 50 }, className, variant = 'inline', autoPlay, }: VideoPlayerProps) { const intl = useIntl() const videoRef = useRef(null) const [isActivated, setIsActivated] = useState( (variant === 'hero' && (autoPlay ?? true)) || !!autoPlay ) const [isPlaying, setIsPlaying] = useState(autoPlay ?? false) const [isMuted, setIsMuted] = useState(true) const defaultProps = getVideoPropsByVariant(variant, isActivated, autoPlay) const classNames = variants({ className, variant, }) function togglePlay() { const videoElement = videoRef.current if (videoElement) { if (variant === 'hero') { if (videoElement.paused) { videoElement.play() } else { videoElement.pause() } } else { setIsActivated(true) videoElement.play() } } } function handleMuteToggle() { const videoElement = videoRef.current if (videoElement) { const currentlyMuted = videoElement.muted videoElement.muted = !currentlyMuted setIsMuted(!currentlyMuted) } } function handleVolumeChangeEvent(event: React.UIEvent) { if (event.currentTarget.muted && !isMuted) { setIsMuted(true) } else if (!event.currentTarget.muted && isMuted) { setIsMuted(false) } } const showPlayButton = variant === 'hero' || (variant === 'inline' && !isActivated) const showMuteButton = variant === 'inline' && isActivated return (
{showPlayButton ? ( ) : null} {showMuteButton ? ( ) : null}
) } function getVideoPropsByVariant( variant: VideoPlayerProps['variant'], isActive: boolean, autoPlay?: boolean ): VideoHTMLAttributes { switch (variant) { case 'hero': return { controls: false, controlsList: 'nodownload nofullscreen noremoteplayback', autoPlay: autoPlay ?? true, muted: true, loop: true, } case 'inline': default: return { controls: isActive, controlsList: 'nodownload noremoteplayback', autoPlay: autoPlay ?? isActive, muted: true, loop: false, } } }