mirror of
https://github.com/sussy-code/smov.git
synced 2026-04-20 08:02:09 +00:00
37 lines
769 B
TypeScript
37 lines
769 B
TypeScript
import {
|
|
ReactNode,
|
|
createContext,
|
|
useContext,
|
|
useEffect,
|
|
useState,
|
|
} from "react";
|
|
|
|
import { registerVideoPlayer, unregisterVideoPlayer } from "./init";
|
|
|
|
const VideoPlayerContext = createContext<string>("");
|
|
|
|
export function VideoPlayerContextProvider(props: { children: ReactNode }) {
|
|
const [id, setId] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
const vidId = registerVideoPlayer();
|
|
setId(vidId);
|
|
|
|
return () => {
|
|
unregisterVideoPlayer(vidId);
|
|
};
|
|
}, [setId]);
|
|
|
|
if (!id) return null;
|
|
|
|
return (
|
|
<VideoPlayerContext.Provider value={id}>
|
|
{props.children}
|
|
</VideoPlayerContext.Provider>
|
|
);
|
|
}
|
|
|
|
export function useVideoPlayerDescriptor(): string {
|
|
const id = useContext(VideoPlayerContext);
|
|
return id;
|
|
}
|