movie-web/__old/VideoContext.tsx

99 lines
2.3 KiB
TypeScript
Raw Normal View History

import { MWStreamQuality, MWStreamType } from "@/backend/helpers/streams";
2023-01-08 13:15:32 +01:00
import React, {
createContext,
MutableRefObject,
2023-01-08 15:37:16 +01:00
useContext,
2023-01-08 13:15:32 +01:00
useEffect,
useReducer,
} from "react";
2023-01-08 15:37:16 +01:00
import {
initialPlayerState,
2023-01-10 19:53:55 +01:00
PlayerContext,
2023-01-08 15:37:16 +01:00
useVideoPlayer,
} from "./hooks/useVideoPlayer";
2023-01-08 13:15:32 +01:00
interface VideoPlayerContextType {
2023-01-08 15:37:16 +01:00
source: string | null;
sourceType: MWStreamType;
quality: MWStreamQuality;
2023-01-10 19:53:55 +01:00
state: PlayerContext;
2023-01-08 13:15:32 +01:00
}
2023-01-08 15:37:16 +01:00
const initial: VideoPlayerContextType = {
2023-01-08 13:15:32 +01:00
source: null,
sourceType: MWStreamType.MP4,
quality: MWStreamQuality.QUNKNOWN,
2023-01-08 15:37:16 +01:00
state: initialPlayerState,
};
2023-01-08 13:15:32 +01:00
type VideoPlayerContextAction =
| {
type: "SET_SOURCE";
url: string;
sourceType: MWStreamType;
quality: MWStreamQuality;
}
2023-01-08 13:15:32 +01:00
| {
type: "UPDATE_PLAYER";
2023-01-10 19:53:55 +01:00
state: PlayerContext;
2023-01-08 13:15:32 +01:00
};
function videoPlayerContextReducer(
original: VideoPlayerContextType,
action: VideoPlayerContextAction
): VideoPlayerContextType {
const video = { ...original };
if (action.type === "SET_SOURCE") {
video.source = action.url;
2023-01-10 19:53:55 +01:00
video.sourceType = action.sourceType;
video.quality = action.quality;
2023-01-08 13:15:32 +01:00
return video;
}
if (action.type === "UPDATE_PLAYER") {
2023-01-08 15:37:16 +01:00
video.state = action.state;
2023-01-08 13:15:32 +01:00
return video;
}
return original;
}
2023-01-08 15:37:16 +01:00
export const VideoPlayerContext =
createContext<VideoPlayerContextType>(initial);
2023-01-08 13:15:32 +01:00
export const VideoPlayerDispatchContext = createContext<
React.Dispatch<VideoPlayerContextAction>
>(null as any);
export function VideoPlayerContextProvider(props: {
children: React.ReactNode;
player: MutableRefObject<HTMLVideoElement | null>;
2023-01-08 16:23:42 +01:00
wrapper: MutableRefObject<HTMLDivElement | null>;
2023-01-08 13:15:32 +01:00
}) {
2023-01-08 16:23:42 +01:00
const { playerState } = useVideoPlayer(props.player, props.wrapper);
2023-01-08 13:15:32 +01:00
const [videoData, dispatch] = useReducer<typeof videoPlayerContextReducer>(
videoPlayerContextReducer,
2023-01-08 15:37:16 +01:00
initial
2023-01-08 13:15:32 +01:00
);
useEffect(() => {
dispatch({
type: "UPDATE_PLAYER",
2023-01-08 15:37:16 +01:00
state: playerState,
2023-01-08 13:15:32 +01:00
});
2023-01-08 15:37:16 +01:00
}, [playerState]);
2023-01-08 13:15:32 +01:00
return (
<VideoPlayerContext.Provider value={videoData}>
<VideoPlayerDispatchContext.Provider value={dispatch}>
{props.children}
</VideoPlayerDispatchContext.Provider>
</VideoPlayerContext.Provider>
);
}
2023-01-08 15:37:16 +01:00
export function useVideoPlayerState() {
const { state } = useContext(VideoPlayerContext);
return {
videoState: state,
};
}