Browse Source
Co-authored-by: Jip Frijlink <JipFr@users.noreply.github.com> Co-authored-by: James Hawkins <jhawki2005@gmail.com> Co-authored-by: William Oldham <wegg7250@gmail.com>pull/143/head
3 changed files with 60 additions and 9 deletions
@ -1,12 +1,60 @@ |
|||||||
import { Icons } from "@/components/Icon"; |
import { Icons } from "@/components/Icon"; |
||||||
import { VideoPlayerIconButton } from "@/video/components/parts/VideoPlayerIconButton"; |
import { VideoPlayerIconButton } from "@/video/components/parts/VideoPlayerIconButton"; |
||||||
|
import { useVideoPlayerDescriptor } from "@/video/state/hooks"; |
||||||
|
import { useMisc } from "@/video/state/logic/misc"; |
||||||
|
import { useCallback, useEffect, useRef, useState } from "react"; |
||||||
|
|
||||||
interface Props { |
interface Props { |
||||||
className?: string; |
className?: string; |
||||||
} |
} |
||||||
|
|
||||||
export function ChromecastAction(props: Props) { |
export function ChromecastAction(props: Props) { |
||||||
|
const [hidden, setHidden] = useState(false); |
||||||
|
const descriptor = useVideoPlayerDescriptor(); |
||||||
|
const misc = useMisc(descriptor); |
||||||
|
const isCasting = misc.isCasting; |
||||||
|
const ref = useRef<HTMLDivElement>(null); |
||||||
|
|
||||||
|
const setButtonVisibility = useCallback( |
||||||
|
(tag: HTMLElement) => { |
||||||
|
const isVisible = (tag.getAttribute("style") ?? "").includes("inline"); |
||||||
|
setHidden(!isVisible); |
||||||
|
}, |
||||||
|
[setHidden] |
||||||
|
); |
||||||
|
|
||||||
|
useEffect(() => { |
||||||
|
const tag = ref.current?.querySelector<HTMLElement>("google-cast-launcher"); |
||||||
|
if (!tag) return; |
||||||
|
|
||||||
|
const observer = new MutationObserver(() => { |
||||||
|
setButtonVisibility(tag); |
||||||
|
}); |
||||||
|
|
||||||
|
observer.observe(tag, { attributes: true, attributeFilter: ["style"] }); |
||||||
|
setButtonVisibility(tag); |
||||||
|
|
||||||
|
return () => { |
||||||
|
observer.disconnect(); |
||||||
|
}; |
||||||
|
}, [setButtonVisibility]); |
||||||
|
|
||||||
return ( |
return ( |
||||||
<VideoPlayerIconButton className={props.className} icon={Icons.CASTING} /> |
<VideoPlayerIconButton |
||||||
|
ref={ref} |
||||||
|
className={[ |
||||||
|
props.className ?? "", |
||||||
|
"google-cast-button", |
||||||
|
isCasting ? "casting" : "", |
||||||
|
hidden ? "hidden" : "", |
||||||
|
].join(" ")} |
||||||
|
icon={Icons.CASTING} |
||||||
|
onClick={(e) => { |
||||||
|
const castButton = e.currentTarget.querySelector( |
||||||
|
"google-cast-launcher" |
||||||
|
); |
||||||
|
if (castButton) (castButton as HTMLDivElement).click(); |
||||||
|
}} |
||||||
|
/> |
||||||
); |
); |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue