-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSpotifyDisplay.tsx
More file actions
327 lines (307 loc) · 9.5 KB
/
Copy pathSpotifyDisplay.tsx
File metadata and controls
327 lines (307 loc) · 9.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
'use client'
// File: app/components/dashboard/SpotifyDisplay.tsx
import { useSpotifyAuth } from '@/hooks/useSpotifyAuth'
import useSpotifyWebPlayback from '@/hooks/useSpotifyWebPlayback'
import { useDashboardRegistration } from '@/hooks/useDashboardRegistration'
import { clampVolume } from '@/utils/audioManager'
import { useWebSocket } from '@/context/WebSocketContext'
import { useSpotifyCommand } from '@/hooks/useSpotifyCommand'
import PauseIcon from '@mui/icons-material/Pause'
import PlayArrowIcon from '@mui/icons-material/PlayArrow'
import SkipNextIcon from '@mui/icons-material/SkipNext'
import SkipPreviousIcon from '@mui/icons-material/SkipPrevious'
import Box from '@mui/material/Box'
import Button from '@mui/material/Button'
import IconButton from '@mui/material/IconButton'
import Typography from '@mui/material/Typography'
import { useCallback, useEffect, useState } from 'react'
import { signOut } from 'next-auth/react'
import AuthButton from './AuthButton'
import VolumeSlider from './shared/VolumeSlider'
import SpotifyDeviceSelector from './SpotifyDeviceSelector'
import DeviceRecommendation from './Spotify/DeviceRecommendation'
import { useSpotifyVolume } from '@/hooks/useSpotifyVolume'
const SpotifyDisplay = () => {
const { isLoggedIn } = useSpotifyAuth()
const { spotifyData, connectionStatus } = useWebSocket()
const { execute: executeSpotify } = useSpotifyCommand()
const [selectedDeviceId, setSelectedDeviceId] = useState<string>('')
const [deviceMenuAnchor, setDeviceMenuAnchor] = useState<null | HTMLElement>(
null
)
const hasActiveDevice =
!!selectedDeviceId ||
spotifyData.devices?.some((device) => device.is_active)
const handleLogout = async () => {
await signOut({ redirect: false })
window.location.reload()
}
const { player, isReady, deviceId } = useSpotifyWebPlayback()
// Enable remote Spotify control from controllers
useDashboardRegistration(player)
const sendVolumeCommand = useCallback(
(volume: number) => {
if (connectionStatus !== 'Connected') return
const targetDeviceId =
selectedDeviceId ||
spotifyData.devices?.find((device) => device.is_active)?.id
if (!targetDeviceId) return
const sanitized = clampVolume(volume)
executeSpotify('SET_VOLUME', {
volume: sanitized,
deviceId: targetDeviceId,
})
},
[connectionStatus, selectedDeviceId, executeSpotify, spotifyData.devices]
)
const {
displayVolume,
isMuted,
handleVolumeChange,
handleVolumeChangeCommitted,
handleToggleMute,
} = useSpotifyVolume(spotifyData.playback.volume_percent, sendVolumeCommand)
// Effect to auto-select the active device
useEffect(() => {
const devices = spotifyData.devices || []
if (devices.length === 0) {
if (selectedDeviceId !== '') {
// eslint-disable-next-line react-hooks/set-state-in-effect
setSelectedDeviceId('')
}
return
}
const activeDevice = devices.find((device) => device.is_active)
if (!selectedDeviceId && activeDevice) {
setSelectedDeviceId(activeDevice.id)
return
}
if (
selectedDeviceId &&
!devices.some((device) => device.id === selectedDeviceId)
) {
setSelectedDeviceId(activeDevice?.id ?? '')
}
}, [spotifyData.devices, selectedDeviceId])
const handlePlayPauseToggle = () => {
if (spotifyData.playback.is_playing) {
executeSpotify('PAUSE')
} else {
executeSpotify('PLAY')
}
}
const handleDeviceSelect = (deviceId: string) => {
setSelectedDeviceId(deviceId)
setDeviceMenuAnchor(null)
executeSpotify('TRANSFER_PLAYBACK', { deviceId })
}
if (!isLoggedIn) {
return (
<Box
data-testid="spotify-auth-container"
sx={{
backgroundColor: 'grey.900',
color: 'common.white',
px: 3,
py: 1.5,
borderRadius: 2,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
position: 'fixed',
bottom: 56,
left: 0,
right: 0,
zIndex: 1100,
boxShadow: 3,
width: '100%',
minHeight: '64px',
}}
>
<AuthButton providerId="spotify" providerName="Spotify" />
</Box>
)
}
if (isLoggedIn) {
const isWaiting = spotifyData.playback.track.name === 'Awaiting Login...'
const displayTrackName = isWaiting
? 'No Active Playback'
: spotifyData.playback.track.name
const displayArtist = isWaiting
? ''
: `— ${spotifyData.playback.track.artist}`
return (
<Box
data-testid="spotify-display-container"
aria-label={`Now playing: ${displayTrackName} ${displayArtist}, Status: ${
spotifyData.playback.is_playing ? 'Playing' : 'Paused'
}${isReady ? ', Browser player ready' : ''}`}
sx={{
backgroundColor: 'grey.900',
color: 'common.white',
px: { xs: 2, sm: 3 },
py: 1.5,
borderRadius: 2,
display: 'grid',
gridTemplateColumns: '1fr auto 1fr',
alignItems: 'center',
position: 'fixed',
bottom: 56,
left: 0,
right: 0,
zIndex: 1100,
boxShadow: 3,
width: '100%',
minHeight: '64px',
}}
>
<Box
sx={{
display: 'flex',
alignItems: 'center',
justifySelf: 'start',
gap: 2,
minHeight: '32px',
}}
>
<Typography
variant="body2"
sx={{ fontWeight: 600 }}
data-testid="spotify-now-playing"
>
{displayTrackName} {displayArtist}
</Typography>
{!isReady && (
<Typography
variant="caption"
sx={{
opacity: 0.8,
backgroundColor: 'info.main',
color: 'common.white',
px: 1,
py: 0.5,
borderRadius: 1,
whiteSpace: 'nowrap',
}}
>
🔄 Connecting Player...
</Typography>
)}
{isReady && deviceId && (
<Typography
variant="caption"
sx={{
opacity: 0.8,
backgroundColor: 'success.main',
color: 'common.white',
px: 1,
py: 0.5,
borderRadius: 1,
whiteSpace: 'nowrap',
}}
>
🎵 Browser Player Active
</Typography>
)}
<DeviceRecommendation />
</Box>
<Box
sx={{
display: 'flex',
alignItems: 'center',
gap: 1,
justifySelf: 'center',
}}
>
<IconButton
size="small"
onClick={() => executeSpotify('PREVIOUS')}
sx={{
color: 'common.white',
'&:hover': { backgroundColor: 'grey.800' },
}}
aria-label="Previous track"
data-testid="spotify-previous-button"
>
<SkipPreviousIcon />
</IconButton>
<IconButton
size="medium"
onClick={handlePlayPauseToggle}
sx={{
color: 'common.white',
backgroundColor: 'grey.700',
'&:hover': { backgroundColor: 'grey.600' },
}}
aria-label={spotifyData.playback.is_playing ? 'Pause' : 'Play'}
data-testid="spotify-play-pause-button"
>
{spotifyData.playback.is_playing ? (
<PauseIcon />
) : (
<PlayArrowIcon />
)}
</IconButton>
<IconButton
size="small"
onClick={() => executeSpotify('NEXT')}
sx={{
color: 'common.white',
'&:hover': { backgroundColor: 'grey.800' },
}}
aria-label="Next track"
data-testid="spotify-next-button"
>
<SkipNextIcon />
</IconButton>
</Box>
<Box
sx={{
display: 'flex',
alignItems: 'center',
justifySelf: 'end',
gap: 1,
}}
>
<VolumeSlider
volume={displayVolume}
muted={isMuted}
onVolumeChange={handleVolumeChange}
onVolumeChangeCommitted={handleVolumeChangeCommitted}
onToggleMute={handleToggleMute}
showValue={true}
disabled={!hasActiveDevice}
/>
<SpotifyDeviceSelector
availableDevices={spotifyData.devices || []}
deviceMenuAnchor={deviceMenuAnchor}
onDeviceSelect={handleDeviceSelect}
onMenuOpen={(e) => setDeviceMenuAnchor(e.currentTarget)}
onMenuClose={() => setDeviceMenuAnchor(null)}
/>
<Button
variant="outlined"
size="small"
onClick={handleLogout}
data-testid="spotify-logout-button"
sx={{
color: 'common.white',
borderColor: 'grey.600',
'&:hover': {
borderColor: 'grey.500',
backgroundColor: 'grey.800',
},
minWidth: 'auto',
px: 1.5,
fontSize: '0.75rem',
}}
>
Logout
</Button>
</Box>
</Box>
)
}
return null
}
export default SpotifyDisplay