import { Icon, Icons } from "@/components/Icon"; import { makePercentage, makePercentageString, useProgressBar, } from "@/hooks/useProgressBar"; 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 [storedVolume, setStoredVolume] = useState(1); 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(() => { if (videoState.volume > 0) { videoState.setVolume(0); setStoredVolume(videoState.volume); } else { videoState.setVolume(storedVolume > 0 ? storedVolume : 1); } }, [videoState, setStoredVolume, storedVolume]); 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} />
); }