From 6747fbb701e3e2f21790b6ba7c1e2d51a5a0728a Mon Sep 17 00:00:00 2001 From: jeffvli Date: Sat, 20 May 2023 20:47:07 -0700 Subject: [PATCH] Add initialSongId prop as alternative to initialIndex --- .../player/hooks/use-handle-playqueue-add.ts | 17 +++++++++++++---- src/renderer/types.ts | 3 ++- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/renderer/features/player/hooks/use-handle-playqueue-add.ts b/src/renderer/features/player/hooks/use-handle-playqueue-add.ts index e5d145a5..1f1735ae 100644 --- a/src/renderer/features/player/hooks/use-handle-playqueue-add.ts +++ b/src/renderer/features/player/hooks/use-handle-playqueue-add.ts @@ -29,8 +29,9 @@ export const useHandlePlayQueueAdd = () => { const handlePlayQueueAdd = useCallback( async (options: PlayQueueAddOptions) => { if (!server) return toast.error({ message: 'No server selected', type: 'error' }); - const { initialIndex, playType, byData, byItemType } = options; + const { initialIndex, initialSongId, playType, byData, byItemType } = options; let songs: QueueSong[] | null = null; + let initialSongIndex = 0; if (byItemType) { let songList: SongListResponse | undefined; @@ -38,13 +39,13 @@ export const useHandlePlayQueueAdd = () => { try { if (itemType === LibraryItem.PLAYLIST) { - songList = await getPlaylistSongsById({ id, queryClient, server }); + songList = await getPlaylistSongsById({ id: id?.[0], queryClient, server }); } else if (itemType === LibraryItem.ALBUM) { songList = await getAlbumSongsById({ id, queryClient, server }); } else if (itemType === LibraryItem.ALBUM_ARTIST) { songList = await getAlbumArtistSongsById({ id, queryClient, server }); } else { - songList = await getSongById({ id, queryClient, server }); + songList = await getSongById({ id: id?.[0], queryClient, server }); } } catch (err: any) { return toast.error({ @@ -60,7 +61,15 @@ export const useHandlePlayQueueAdd = () => { if (!songs) return toast.warn({ message: 'No songs found' }); - const playerData = addToQueue({ initialIndex: initialIndex || 0, playType, songs }); + // const index = initialIndex || initial songs.findIndex((song) => song.id === initialSongId); + + if (initialIndex) { + initialSongIndex = initialIndex; + } else if (initialSongId) { + initialSongIndex = songs.findIndex((song) => song.id === initialSongId); + } + + const playerData = addToQueue({ initialIndex: initialSongIndex, playType, songs }); if (playerType === PlaybackType.LOCAL) { mpvPlayer?.volume(usePlayerStore.getState().volume); diff --git a/src/renderer/types.ts b/src/renderer/types.ts index 2323869e..3df22f90 100644 --- a/src/renderer/types.ts +++ b/src/renderer/types.ts @@ -151,10 +151,11 @@ export enum TableColumn { export type PlayQueueAddOptions = { byData?: QueueSong[]; byItemType?: { - id: string[] | any; + id: string[]; type: LibraryItem; }; initialIndex?: number; + initialSongId?: string; playType: Play; };