import React from "react"; import { useTime } from "../context/time-context"; import { FaPlay, FaPause, FaBackward, FaForward, FaUndoAlt, FaArrowDown, FaArrowUp, } from "react-icons/fa"; import { debounce } from "@/utils/debounce"; const PlaybackBar: React.FC = () => { const { duration, isPlaying, setIsPlaying, currentTime, setCurrentTime } = useTime(); const sliderActiveRef = React.useRef(false); const wasPlayingRef = React.useRef(false); const [sliderValue, setSliderValue] = React.useState(currentTime); // Only update sliderValue from context if not dragging React.useEffect(() => { if (!sliderActiveRef.current) { setSliderValue(currentTime); } }, [currentTime]); const updateTime = debounce((t: number) => { setCurrentTime(t); }, 200); const handleSliderChange = (e: React.ChangeEvent) => { const t = Number(e.target.value); setSliderValue(t); updateTime(t); }; const handleSliderMouseDown = () => { sliderActiveRef.current = true; wasPlayingRef.current = isPlaying; setIsPlaying(false); }; const handleSliderMouseUp = () => { sliderActiveRef.current = false; setCurrentTime(sliderValue); // Snap to final value if (wasPlayingRef.current) { setIsPlaying(true); } // If it was paused before, keep it paused }; return (
{Math.floor(sliderValue)} / {Math.floor(duration)}

Space {" "} to pause/unpause

/ {" "} to previous/next episode

); }; export default PlaybackBar;