2023-01-22 19:26:08 +01:00
|
|
|
import { useEffect, useRef } from "react";
|
2023-01-21 23:45:26 +01:00
|
|
|
import { useVideoPlayerState } from "../VideoContext";
|
|
|
|
|
|
|
|
interface ShowControlProps {
|
|
|
|
series?: {
|
2023-01-22 19:26:08 +01:00
|
|
|
episodeId: string;
|
|
|
|
seasonId: string;
|
2023-01-21 23:45:26 +01:00
|
|
|
};
|
2023-01-22 19:26:08 +01:00
|
|
|
onSelect?: (state: { episodeId?: string; seasonId?: string }) => void;
|
2023-01-21 23:45:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function ShowControl(props: ShowControlProps) {
|
|
|
|
const { videoState } = useVideoPlayerState();
|
2023-01-22 19:26:08 +01:00
|
|
|
const lastState = useRef<{
|
|
|
|
episodeId?: string;
|
|
|
|
seasonId?: string;
|
|
|
|
} | null>({
|
|
|
|
episodeId: props.series?.episodeId,
|
|
|
|
seasonId: props.series?.seasonId,
|
|
|
|
});
|
2023-01-21 23:45:26 +01:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
videoState.setShowData({
|
|
|
|
current: props.series,
|
|
|
|
isSeries: !!props.series,
|
|
|
|
});
|
|
|
|
// we only want it to run when props change, not when videoState changes
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, [props]);
|
|
|
|
|
2023-01-22 19:26:08 +01:00
|
|
|
useEffect(() => {
|
|
|
|
const currentState = {
|
|
|
|
episodeId: videoState.seasonData.current?.episodeId,
|
|
|
|
seasonId: videoState.seasonData.current?.seasonId,
|
|
|
|
};
|
|
|
|
if (
|
|
|
|
currentState.episodeId !== lastState.current?.episodeId ||
|
|
|
|
currentState.seasonId !== lastState.current?.seasonId
|
|
|
|
) {
|
|
|
|
lastState.current = currentState;
|
|
|
|
props.onSelect?.(currentState);
|
|
|
|
}
|
|
|
|
}, [videoState, props]);
|
|
|
|
|
2023-01-21 23:45:26 +01:00
|
|
|
return null;
|
|
|
|
}
|