You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
769 B
37 lines
769 B
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; |
|
}
|
|
|