import { Icon, Icons } from "@/components/Icon"; import { makePercentage, makePercentageString, useProgressBar, } from "@/hooks/useProgressBar"; import { useVolumeControl } from "@/hooks/useVolumeToggle"; import { canChangeVolume } from "@/utils/detectFeatures"; import { useCallback, useEffect, useRef, useState } from "react"; import { useVideoPlayerState } from "../VideoContext"; interface Props { className?: string; } export function VolumeControl(props: Props) { const { videoState } = useVideoPlayerState(); const ref = useRef(null); const { setStoredVolume, toggleVolume } = useVolumeControl(); const [hoveredOnce, setHoveredOnce] = useState(false); const commitVolume = useCallback( (percentage) => { videoState.setVolume(percentage); setStoredVolume(percentage); }, [videoState, setStoredVolume] ); const { dragging, dragPercentage, dragMouseDown } = useProgressBar( ref, commitVolume, true ); useEffect(() => { if (!videoState.leftControlHovering) setHoveredOnce(false); }, [videoState, setHoveredOnce]); const handleClick = useCallback(() => { toggleVolume(); }, [toggleVolume]); const handleMouseEnter = useCallback(async () => { if (await canChangeVolume()) setHoveredOnce(true); }, [setHoveredOnce]); let percentage = makePercentage(videoState.volume * 100); if (dragging) percentage = makePercentage(dragPercentage); const percentageString = makePercentageString(percentage); return (
0 ? Icons.VOLUME : Icons.VOLUME_X} />
); }