2023-01-15 16:51:55 +01:00
|
|
|
import { useGoBack } from "@/hooks/useGoBack";
|
2023-01-24 18:12:37 +01:00
|
|
|
import { useVolumeControl } from "@/hooks/useVolumeToggle";
|
2023-01-10 19:53:55 +01:00
|
|
|
import { forwardRef, useContext, useEffect, useRef } from "react";
|
2023-02-03 15:20:26 +01:00
|
|
|
import { VideoErrorBoundary } from "./parts/VideoErrorBoundary";
|
2023-01-24 18:12:37 +01:00
|
|
|
import {
|
|
|
|
useVideoPlayerState,
|
|
|
|
VideoPlayerContext,
|
|
|
|
VideoPlayerContextProvider,
|
2023-02-03 15:20:26 +01:00
|
|
|
} from "./VideoContext";
|
2023-01-08 13:15:32 +01:00
|
|
|
|
2023-01-08 22:29:38 +01:00
|
|
|
export interface VideoPlayerProps {
|
2023-01-08 20:36:46 +01:00
|
|
|
autoPlay?: boolean;
|
2023-01-08 13:15:32 +01:00
|
|
|
children?: React.ReactNode;
|
|
|
|
}
|
|
|
|
|
2023-01-08 20:36:46 +01:00
|
|
|
const VideoPlayerInternals = forwardRef<
|
|
|
|
HTMLVideoElement,
|
|
|
|
{ autoPlay: boolean }
|
|
|
|
>((props, ref) => {
|
2023-01-08 13:15:32 +01:00
|
|
|
const video = useContext(VideoPlayerContext);
|
2023-01-24 18:12:37 +01:00
|
|
|
const didInitialize = useRef<{ source: string | null } | null>(null);
|
|
|
|
const { videoState } = useVideoPlayerState();
|
|
|
|
const { toggleVolume } = useVolumeControl();
|
2023-01-08 13:15:32 +01:00
|
|
|
|
2023-01-10 19:53:55 +01:00
|
|
|
useEffect(() => {
|
2023-01-24 18:12:37 +01:00
|
|
|
const value = { source: video.source };
|
|
|
|
const hasChanged = value.source !== didInitialize.current?.source;
|
|
|
|
if (!hasChanged) return;
|
2023-01-10 19:53:55 +01:00
|
|
|
if (!video.state.hasInitialized || !video.source) return;
|
|
|
|
video.state.initPlayer(video.source, video.sourceType);
|
2023-01-24 18:12:37 +01:00
|
|
|
didInitialize.current = value;
|
2023-01-10 19:53:55 +01:00
|
|
|
}, [didInitialize, video]);
|
|
|
|
|
|
|
|
// muted attribute is required for safari, as they cant change the volume itself
|
2023-01-08 13:15:32 +01:00
|
|
|
return (
|
2023-01-08 20:36:46 +01:00
|
|
|
<video
|
|
|
|
ref={ref}
|
|
|
|
autoPlay={props.autoPlay}
|
2023-01-10 19:53:55 +01:00
|
|
|
muted={video.state.volume === 0}
|
2023-01-08 20:36:46 +01:00
|
|
|
playsInline
|
|
|
|
className="h-full w-full"
|
2023-01-10 19:53:55 +01:00
|
|
|
/>
|
2023-01-08 13:15:32 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
export function VideoPlayer(props: VideoPlayerProps) {
|
|
|
|
const playerRef = useRef<HTMLVideoElement | null>(null);
|
2023-01-08 16:23:42 +01:00
|
|
|
const playerWrapperRef = useRef<HTMLDivElement | null>(null);
|
2023-01-15 16:51:55 +01:00
|
|
|
const goBack = useGoBack();
|
|
|
|
|
|
|
|
// TODO move error boundary to only decorated, <VideoPlayer /> shouldn't have styling
|
2023-01-08 13:15:32 +01:00
|
|
|
|
|
|
|
return (
|
2023-01-08 16:23:42 +01:00
|
|
|
<VideoPlayerContextProvider player={playerRef} wrapper={playerWrapperRef}>
|
|
|
|
<div
|
2023-01-24 18:12:37 +01:00
|
|
|
className="is-video-player relative h-full w-full select-none overflow-hidden bg-black [border-left:env(safe-area-inset-left)_solid_transparent] [border-right:env(safe-area-inset-right)_solid_transparent]"
|
2023-01-08 16:23:42 +01:00
|
|
|
ref={playerWrapperRef}
|
|
|
|
>
|
2023-01-15 16:51:55 +01:00
|
|
|
<VideoErrorBoundary onGoBack={goBack}>
|
|
|
|
<VideoPlayerInternals
|
|
|
|
autoPlay={props.autoPlay ?? false}
|
|
|
|
ref={playerRef}
|
|
|
|
/>
|
|
|
|
<div className="absolute inset-0">{props.children}</div>
|
|
|
|
</VideoErrorBoundary>
|
2023-01-08 16:23:42 +01:00
|
|
|
</div>
|
2023-01-08 13:15:32 +01:00
|
|
|
</VideoPlayerContextProvider>
|
|
|
|
);
|
|
|
|
}
|