@@ -11,7 +11,6 @@ import Select from '@mui/material/Select'
1111import Typography from '@mui/material/Typography'
1212import { useRouter } from 'next/navigation'
1313import { useCallback , useEffect , useMemo , useRef , useState } from 'react'
14- import throttle from 'lodash.throttle'
1514import { clampVolume } from '@/utils/audioManager'
1615import { useWebSocket } from '@/context/WebSocketContext'
1716import { useSpotifyCommand } from '@/hooks/useSpotifyCommand'
@@ -33,10 +32,6 @@ const SpotifyControls = () => {
3332 const { devices = [ ] } = spotifyData
3433 const lastSentVolumeRef = useRef < string | null > ( null )
3534 const [ selectedDeviceId , setSelectedDeviceId ] = useState < string > ( '' )
36- const [ isSliding , setIsSliding ] = useState ( false )
37- const prevActiveIdRef = useRef < string | undefined > ( undefined )
38- const lastVolumeSyncTimeRef = useRef < number > ( 0 )
39- const hasPendingSendRef = useRef < boolean > ( false )
4035 const [ optimisticIsPlaying , setOptimisticIsPlaying ] = useState <
4136 boolean | null
4237 > ( null )
@@ -79,27 +74,6 @@ const SpotifyControls = () => {
7974 [ connectionStatus , resolveTargetDeviceId , executeSpotify ]
8075 )
8176
82- const throttledSendVolume = useMemo (
83- ( ) =>
84- throttle ( ( val : number ) => {
85- sendVolumeCommand ( val )
86- } , 200 ) ,
87- [ sendVolumeCommand ]
88- )
89-
90- useEffect ( ( ) => {
91- return ( ) => {
92- throttledSendVolume . cancel ( )
93- }
94- } , [ throttledSendVolume ] )
95-
96- const handleThrottledVolumeChange = useCallback (
97- ( val : number ) => {
98- throttledSendVolume ( val )
99- } ,
100- [ throttledSendVolume ]
101- )
102-
10377 const {
10478 displayVolume,
10579 isMuted,
@@ -112,14 +86,6 @@ const SpotifyControls = () => {
11286 sendVolumeCommand
11387 )
11488
115- const handleVolumeSlide = useCallback (
116- ( val : number ) => {
117- handleVolumeChange ( val )
118- handleThrottledVolumeChange ( val )
119- } ,
120- [ handleVolumeChange , handleThrottledVolumeChange ]
121- )
122-
12389 const handleTrackSelect = ( uri : string ) => {
12490 const targetDeviceId = resolveTargetDeviceId ( )
12591 executeSpotify ( 'PLAY' , {
@@ -212,14 +178,12 @@ const SpotifyControls = () => {
212178 command === 'NEXT' ||
213179 command === 'PREVIOUS'
214180 ) {
215- // Optimistic UI update for Play/Pause
216181 if ( command === 'PLAY' ) {
217182 setOptimisticIsPlaying ( true )
218183 } else if ( command === 'PAUSE' ) {
219184 setOptimisticIsPlaying ( false )
220185 }
221186
222- // Clear existing timer if any
223187 if ( playbackGraceTimerRef . current ) {
224188 clearTimeout ( playbackGraceTimerRef . current )
225189 }
@@ -235,7 +199,6 @@ const SpotifyControls = () => {
235199 [ sendSpotifyCommand ]
236200 )
237201
238- // Cleanup timers on unmount
239202 useEffect ( ( ) => {
240203 return ( ) => {
241204 if ( playbackGraceTimerRef . current ) {
@@ -244,55 +207,6 @@ const SpotifyControls = () => {
244207 }
245208 } , [ ] )
246209
247- const handleVolumeChange = useCallback (
248- ( val : number ) => {
249- setIsSliding ( true )
250- setVolume ( val )
251- if ( connectionStatus !== 'Connected' ) {
252- const now = Date . now ( )
253- // Throttle warning to once every 3 seconds to avoid spam during sliding
254- if ( now - lastWarningTimeRef . current > 3000 ) {
255- showWarning ( 'Changes not saved: Offline' )
256- lastWarningTimeRef . current = now
257- }
258- }
259- } ,
260- [ connectionStatus , showWarning , setVolume ]
261- )
262-
263- const sendVolumeCommand = useCallback (
264- ( value : number ) => {
265- if ( connectionStatus !== 'Connected' ) return
266- const targetDeviceId = resolveTargetDeviceId ( )
267-
268- // Prevent sending volume command if no device is targeted
269- if ( ! targetDeviceId ) return
270-
271- const sanitized = clampVolume ( value )
272- const messageKey = `${ targetDeviceId } :${ sanitized } `
273- if ( lastSentVolumeRef . current === messageKey ) return
274-
275- hasPendingSendRef . current = true
276- lastVolumeSyncTimeRef . current = Date . now ( )
277-
278- executeSpotify ( 'SET_VOLUME' , {
279- volume : sanitized ,
280- deviceId : targetDeviceId ,
281- } )
282-
283- lastSentVolumeRef . current = messageKey
284- } ,
285- [ connectionStatus , resolveTargetDeviceId , executeSpotify ]
286- )
287-
288- const handleVolumeChangeCommitted = useCallback (
289- ( val : number ) => {
290- setIsSliding ( false )
291- sendVolumeCommand ( val )
292- } ,
293- [ sendVolumeCommand ]
294- )
295-
296210 useEffect ( ( ) => {
297211 if ( connectionStatus !== 'Connected' ) {
298212 lastSentVolumeRef . current = null
@@ -367,7 +281,7 @@ const SpotifyControls = () => {
367281 < VolumeSlider
368282 volume = { displayVolume }
369283 muted = { isMuted }
370- onVolumeChange = { handleVolumeSlide }
284+ onVolumeChange = { handleVolumeChange }
371285 onVolumeChangeCommitted = { handleVolumeChangeCommitted }
372286 onToggleMute = { handleToggleMute }
373287 showValue
0 commit comments