1- import { useEffect , useRef } from "react" ;
1+ import { useEffect , useRef , useState } from "react" ;
22import { Box , BoxProps , useTheme } from "@mui/material" ;
33import { useQuery } from "@tanstack/react-query" ;
44
@@ -20,7 +20,7 @@ const colorBuffering = "#00000050";
2020export function Waveform ( { height } : { height ?: number } ) {
2121 const theme = useTheme ( ) ;
2222 const containerRef = useRef < HTMLDivElement > ( null ) ;
23- const wavesurferRef = useRef < WaveSurfer | null > ( null ) ;
23+ const [ wavesurfer , setWavesurfer ] = useState < WaveSurfer | null > ( null ) ;
2424 const loadingRegion = useRef < Region | null > ( null ) ;
2525 const { currentAudio, currentItem, buffered, currentTime } = useAudioContext ( ) ;
2626
@@ -44,7 +44,7 @@ export function Waveform({ height }: { height?: number }) {
4444 plugins : [ regions ] ,
4545 backend : "MediaElement" ,
4646 } ) ;
47- wavesurferRef . current = wavesurfer ;
47+ setWavesurfer ( wavesurfer ) ;
4848 loadingRegion . current = regions . addRegion ( {
4949 start : 0 ,
5050 end : currentItem ?. length ,
@@ -55,7 +55,7 @@ export function Waveform({ height }: { height?: number }) {
5555
5656 return ( ) => {
5757 wavesurfer . destroy ( ) ;
58- wavesurferRef . current = null ;
58+ setWavesurfer ( null ) ;
5959 loadingRegion . current = null ;
6060 } ;
6161 } , [ currentItem , peaks , currentAudio , theme , height ] ) ;
@@ -64,15 +64,15 @@ export function Waveform({ height }: { height?: number }) {
6464
6565 // Register events for the wavesurfer instance
6666 useEffect ( ( ) => {
67- if ( ! wavesurferRef . current || ! currentAudio ) return ;
68- const wavesurfer = wavesurferRef . current ;
67+ if ( ! wavesurfer || ! currentAudio ) return ;
6968
7069 const onClick = ( percentage : number ) => {
7170 const time = wavesurfer . getDuration ( ) * percentage ;
7271 currentAudio . currentTime = time ;
7372 } ;
7473 const onDragStart = ( percentage : number ) => {
7574 dragging . current = true ;
75+ console . log ( "dragging started" , percentage , currentAudio ) ;
7676 const time = wavesurfer . getDuration ( ) * percentage ;
7777 wavesurfer . setTime ( time ) ;
7878 } ;
@@ -94,19 +94,18 @@ export function Waveform({ height }: { height?: number }) {
9494 wavesurfer . un ( "dragstart" , onDragStart ) ;
9595 wavesurfer . un ( "dragend" , onDragEnd ) ;
9696 } ;
97- } , [ currentAudio ] ) ;
97+ } , [ currentAudio , wavesurfer ] ) ;
9898
9999 // Update current time of audio
100100 useEffect ( ( ) => {
101- if ( ! wavesurferRef . current || ! currentAudio || dragging . current ) return ;
102- const wavesurfer = wavesurferRef . current ;
101+ if ( ! wavesurfer || ! currentAudio || dragging . current ) return ;
103102 wavesurfer . setTime ( currentTime ) ;
104- } , [ currentAudio , currentTime ] ) ;
103+ } , [ currentAudio , currentTime , wavesurfer ] ) ;
105104
106105 // Audio buffering region
107106 useEffect ( ( ) => {
108- if ( ! loadingRegion . current || ! wavesurferRef . current ) return ;
109- const dur = currentItem ?. length || wavesurferRef . current . getDuration ( ) ;
107+ if ( ! loadingRegion . current || ! wavesurfer ) return ;
108+ const dur = currentItem ?. length || wavesurfer . getDuration ( ) ;
110109 const loaded =
111110 buffered && buffered . length > 0 ? buffered . end ( buffered . length - 1 ) : 0 ;
112111 const complete = loaded >= dur ;
@@ -127,7 +126,7 @@ export function Waveform({ height }: { height?: number }) {
127126 resize : false ,
128127 } ) ;
129128 }
130- } , [ buffered , currentItem ?. length ] ) ;
129+ } , [ buffered , currentItem ?. length , wavesurfer ] ) ;
131130
132131 if ( isPending || ! peaks ) {
133132 return (
0 commit comments