Skip to content

Commit 094fc9c

Browse files
feat(medium): Standardize Spotify track playback command dispatch (#9546)
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: arii <342438+arii@users.noreply.github.com>
1 parent bfe1348 commit 094fc9c

File tree

9 files changed

+41
-15
lines changed

9 files changed

+41
-15
lines changed

app/client/spotify-selection/page.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,16 @@ const SpotifySelectionPage = () => {
4343
}
4444

4545
const handlePlaylistPlay = (uri: string) => {
46-
executeSpotify('PLAY', { playlistUri: uri })
46+
executeSpotify('PLAY', { contextUri: uri })
47+
}
48+
49+
const handleTrackPlay = (index: number) => {
50+
if (selectedPlaylistId) {
51+
executeSpotify('PLAY', {
52+
contextUri: `spotify:playlist:${selectedPlaylistId}`,
53+
offset: { position: index },
54+
})
55+
}
4756
}
4857

4958
return (
@@ -87,7 +96,7 @@ const SpotifySelectionPage = () => {
8796
{selectedPlaylistId && (
8897
<PlaylistDetails
8998
playlistId={selectedPlaylistId}
90-
onTrackPlay={handlePlaylistPlay}
99+
onTrackPlay={handleTrackPlay}
91100
/>
92101
)}
93102
</Container>

components/Playlist/PlaylistTracksDisplay.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ const PlaylistTracksDisplay = ({ playlistId }: PlaylistTracksDisplayProps) => {
150150
onClick={() =>
151151
isPlaying
152152
? handlePause()
153-
: handlePlayTrack(playlistUri, index)
153+
: handlePlayTrack(playlistUri, offset + index)
154154
}
155155
aria-label={isPlaying ? 'Pause' : 'Play'}
156156
>

components/Spotify/PlaylistDetails.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { SpotifyPlaylistItem as Track } from '../../types/core'
1414

1515
interface PlaylistDetailsProps {
1616
playlistId: string
17-
onTrackPlay: (trackUri: string) => void
17+
onTrackPlay: (index: number) => void
1818
}
1919

2020
const PlaylistDetails: React.FC<PlaylistDetailsProps> = ({
@@ -104,7 +104,7 @@ const PlaylistDetails: React.FC<PlaylistDetailsProps> = ({
104104
<List dense>
105105
{tracks.map((track, index) => (
106106
<ListItem key={`${track.id}-${index}`} divider disablePadding>
107-
<ListItemButton onClick={() => onTrackPlay(track.uri)}>
107+
<ListItemButton onClick={() => onTrackPlay(index)}>
108108
<MusicNote
109109
sx={{ mr: 1.5, color: 'text.secondary', fontSize: 20 }}
110110
/>

services/spotifyPlayerManager.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ export class SpotifyPlayerManager {
158158
command: SpotifyCommand,
159159
params: SpotifyCommandParameters
160160
) {
161-
const { deviceId, volume, playlistUri, contextUri, uri } = params
161+
const { deviceId, volume, playlistUri, contextUri, uri, offset } = params
162162
const effectiveContextUri = contextUri || playlistUri
163163
const sdk = this.sdk
164164

@@ -171,18 +171,26 @@ export class SpotifyPlayerManager {
171171
return sdk.player.startResumePlayback(
172172
deviceId as string,
173173
undefined,
174-
[uri]
174+
[uri],
175+
offset
175176
)
176177
}
177178
if (effectiveContextUri) {
178179
return sdk.player.startResumePlayback(
179180
deviceId as string,
180-
effectiveContextUri
181+
effectiveContextUri,
182+
undefined,
183+
offset
181184
)
182185
}
183186
return sdk.player.startResumePlayback(deviceId as string)
184187
},
185-
{ deviceId, contextUri: effectiveContextUri, uri }
188+
{
189+
deviceId,
190+
contextUri: effectiveContextUri,
191+
uri,
192+
offset: offset?.position,
193+
}
186194
)
187195
break
188196
case 'PAUSE':

tests/unit/app/client/spotify-selection/page.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ describe('SpotifySelectionPage', () => {
114114
fireEvent.click(playButton)
115115

116116
expect(executeMock).toHaveBeenCalledWith('PLAY', {
117-
playlistUri: 'spotify:playlist:mock1',
117+
contextUri: 'spotify:playlist:mock1',
118118
})
119119
})
120120
})

tests/unit/components/Spotify/PlaylistDetails.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ describe('PlaylistDetails', () => {
8585
).toBeInTheDocument()
8686

8787
fireEvent.click(screen.getByText('Track 1'))
88-
expect(onTrackPlay).toHaveBeenCalledWith('spotify:track:1')
88+
expect(onTrackPlay).toHaveBeenCalledWith(0)
8989
})
9090

9191
it('displays the track list and handles play clicks when artists is a string', async () => {
@@ -114,7 +114,7 @@ describe('PlaylistDetails', () => {
114114
).toBeInTheDocument()
115115

116116
fireEvent.click(screen.getByText('Track 1'))
117-
expect(onTrackPlay).toHaveBeenCalledWith('spotify:track:1')
117+
expect(onTrackPlay).toHaveBeenCalledWith(0)
118118
})
119119

120120
it('displays an error message when the API call fails', async () => {

tests/unit/spotifyPolling.test.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,9 @@ describe('SpotifyPolling Service', () => {
222222
await spotifyService.handleCommand('PLAY', { playlistUri })
223223
expect(mockPlayer.startResumePlayback).toHaveBeenCalledWith(
224224
undefined,
225-
playlistUri
225+
playlistUri,
226+
undefined,
227+
undefined
226228
)
227229
})
228230

@@ -231,7 +233,9 @@ describe('SpotifyPolling Service', () => {
231233
await spotifyService.handleCommand('PLAY', { contextUri })
232234
expect(mockPlayer.startResumePlayback).toHaveBeenCalledWith(
233235
undefined,
234-
contextUri
236+
contextUri,
237+
undefined,
238+
undefined
235239
)
236240
})
237241

@@ -241,7 +245,9 @@ describe('SpotifyPolling Service', () => {
241245
await spotifyService.handleCommand('PLAY', { contextUri, playlistUri })
242246
expect(mockPlayer.startResumePlayback).toHaveBeenCalledWith(
243247
undefined,
244-
contextUri
248+
contextUri,
249+
undefined,
250+
undefined
245251
)
246252
})
247253
})

types/core.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ export interface SpotifyCommandParameters {
232232
playlistUri?: string
233233
contextUri?: string
234234
uri?: string
235+
offset?: { position: number }
235236
}
236237

237238
/**

utils/socketManager.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ const handleIncomingMessage = (
435435
playlistUri?: string
436436
contextUri?: string
437437
uri?: string
438+
offset?: { position: number }
438439
} = {}
439440
if (commandMsg.deviceId)
440441
spotifyCommandParams.deviceId = commandMsg.deviceId
@@ -445,6 +446,7 @@ const handleIncomingMessage = (
445446
if (commandMsg.contextUri)
446447
spotifyCommandParams.contextUri = commandMsg.contextUri
447448
if (commandMsg.uri) spotifyCommandParams.uri = commandMsg.uri
449+
if (commandMsg.offset) spotifyCommandParams.offset = commandMsg.offset
448450

449451
spotifyService.handleCommand(commandMsg.command, spotifyCommandParams)
450452
break

0 commit comments

Comments
 (0)