11import React , { useState , useEffect , useRef } from "react" ;
2+ import { usePlaybackProgressConsumer } from "../../hooks/usePlaybackProgress" ;
23
34const ProgressBar = ( {
4- progress,
5- isPlaying,
6- durationMs,
5+ progress : externalProgress ,
6+ isPlaying : externalIsPlaying ,
7+ durationMs : externalDurationMs ,
78 onSeek,
89 onPlayPause,
910 onScrubbingChange,
10- onProgressUpdate,
1111} ) => {
12- const [ displayProgress , setDisplayProgress ] = useState ( progress ) ;
12+ const progressContext = usePlaybackProgressConsumer ( ) ;
13+
14+ const progress = externalProgress ?? progressContext . progressPercentage ;
15+ const isPlaying = externalIsPlaying ?? progressContext . isPlaying ;
16+ const durationMs = externalDurationMs ?? progressContext . duration ;
17+
1318 const [ isScrubbing , setIsScrubbing ] = useState ( false ) ;
1419 const [ scrubbingProgress , setScrubbingProgress ] = useState ( null ) ;
15- const animationFrameRef = useRef ( null ) ;
1620 const wasPlayingRef = useRef ( false ) ;
1721 const containerRef = useRef ( null ) ;
18- const isMountedRef = useRef ( false ) ;
19- const lastTimestampRef = useRef ( performance . now ( ) ) ;
20-
21- const progressUpdateRef = useRef ( onProgressUpdate ) ;
22- const displayProgressRef = useRef ( displayProgress ) ;
23- const scrubbingProgressRef = useRef ( scrubbingProgress ) ;
24-
25- useEffect ( ( ) => {
26- progressUpdateRef . current = onProgressUpdate ;
27- } , [ onProgressUpdate ] ) ;
28-
29- useEffect ( ( ) => {
30- displayProgressRef . current = displayProgress ;
31- } , [ displayProgress ] ) ;
32-
33- useEffect ( ( ) => {
34- scrubbingProgressRef . current = scrubbingProgress ;
35- } , [ scrubbingProgress ] ) ;
36-
37- useEffect ( ( ) => {
38- if ( progressUpdateRef . current ) {
39- progressUpdateRef . current (
40- scrubbingProgressRef . current ?? displayProgressRef . current
41- ) ;
42- }
43- } , [ displayProgress , scrubbingProgress ] ) ;
44-
45- useEffect ( ( ) => {
46- if ( ! isScrubbing && progress !== null && progress !== displayProgress ) {
47- setDisplayProgress ( progress ) ;
48- }
49- } , [ progress , isScrubbing , displayProgress ] ) ;
50-
51- useEffect ( ( ) => {
52- if ( animationFrameRef . current ) {
53- cancelAnimationFrame ( animationFrameRef . current ) ;
54- animationFrameRef . current = null ;
55- }
56-
57- if ( isPlaying && ! isScrubbing && durationMs > 0 ) {
58- lastTimestampRef . current = performance . now ( ) ;
59-
60- const animate = ( timestamp ) => {
61- if ( ! isPlaying || ! durationMs || isScrubbing ) {
62- return ;
63- }
64-
65- const deltaTime = timestamp - lastTimestampRef . current ;
66- lastTimestampRef . current = timestamp ;
67-
68- const progressIncrement = ( deltaTime / durationMs ) * 100 ;
69-
70- setDisplayProgress ( ( prevProgress ) => {
71- return Math . min ( prevProgress + progressIncrement , 100 ) ;
72- } ) ;
73-
74- animationFrameRef . current = requestAnimationFrame ( animate ) ;
75- } ;
76-
77- animationFrameRef . current = requestAnimationFrame ( animate ) ;
78- }
79-
80- return ( ) => {
81- if ( animationFrameRef . current ) {
82- cancelAnimationFrame ( animationFrameRef . current ) ;
83- animationFrameRef . current = null ;
84- }
85- } ;
86- } , [ isPlaying , isScrubbing , durationMs ] ) ;
8722
8823 const handleClick = ( ) => {
8924 setIsScrubbing ( true ) ;
@@ -102,14 +37,14 @@ const ProgressBar = ({
10237
10338 setScrubbingProgress ( ( prev ) => {
10439 const nextValue =
105- ( prev ?? displayProgress ) + ( delta > 0 ? step : - step ) ;
40+ ( prev ?? progress ) + ( delta > 0 ? step : - step ) ;
10641 return Math . max ( 0 , Math . min ( 100 , nextValue ) ) ;
10742 } ) ;
10843 } ;
10944
11045 window . addEventListener ( "wheel" , handleWheel , { passive : false } ) ;
11146 return ( ) => window . removeEventListener ( "wheel" , handleWheel ) ;
112- } , [ isScrubbing , displayProgress ] ) ;
47+ } , [ isScrubbing , progress ] ) ;
11348
11449 useEffect ( ( ) => {
11550 const handleKeyDown = ( event ) => {
@@ -124,7 +59,9 @@ const ProgressBar = ({
12459 if ( scrubbingProgress !== null ) {
12560 const seekMs = Math . floor ( ( scrubbingProgress / 100 ) * durationMs ) ;
12661 onSeek ( seekMs ) ;
127- setDisplayProgress ( scrubbingProgress ) ;
62+ if ( progressContext . updateProgress ) {
63+ progressContext . updateProgress ( seekMs ) ;
64+ }
12865 }
12966
13067 setScrubbingProgress ( null ) ;
@@ -136,7 +73,6 @@ const ProgressBar = ({
13673 setIsScrubbing ( false ) ;
13774 onScrubbingChange ( false ) ;
13875 setScrubbingProgress ( null ) ;
139- setDisplayProgress ( progress ) ;
14076 return false ;
14177 }
14278 } ;
@@ -151,23 +87,21 @@ const ProgressBar = ({
15187 onSeek ,
15288 onPlayPause ,
15389 onScrubbingChange ,
154- progress ,
90+ progressContext ,
15591 ] ) ;
15692
157- const finalProgress = scrubbingProgress ?? displayProgress ;
93+ const finalProgress = scrubbingProgress ?? progress ;
15894 const shouldShowTimestampOutside = finalProgress < 8 ;
15995
16096 return (
16197 < div
16298 ref = { containerRef }
163- className = { `relative transition-all duration-200 ease-in-out ${
164- isScrubbing ? "translate-y-8" : ""
165- } `}
99+ className = { `relative transition-all duration-200 ease-in-out ${ isScrubbing ? "translate-y-8" : ""
100+ } `}
166101 >
167102 < div
168- className = { `relative w-full bg-white/20 rounded-full overflow-hidden cursor-pointer transition-all duration-300 ${
169- isScrubbing ? "h-8" : "h-2 mt-4"
170- } `}
103+ className = { `relative w-full bg-white/20 rounded-full overflow-hidden cursor-pointer transition-all duration-300 ${ isScrubbing ? "h-8" : "h-2 mt-4"
104+ } `}
171105 onClick = { handleClick }
172106 >
173107 < div
@@ -184,11 +118,10 @@ const ProgressBar = ({
184118 } }
185119 >
186120 < span
187- className = { `text-lg font-[580] absolute ${
188- shouldShowTimestampOutside
189- ? "left-2 text-black/40"
190- : "right-full pr-2 text-black/40"
191- } `}
121+ className = { `text-lg font-[580] absolute ${ shouldShowTimestampOutside
122+ ? "left-2 text-black/40"
123+ : "right-full pr-2 text-black/40"
124+ } `}
192125 >
193126 { formatTime ( Math . floor ( ( finalProgress / 100 ) * durationMs ) ) }
194127 </ span >
0 commit comments