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 05f09cc5..8859470f 100644 --- a/src/renderer/features/player/hooks/use-handle-playqueue-add.ts +++ b/src/renderer/features/player/hooks/use-handle-playqueue-add.ts @@ -11,6 +11,7 @@ import { PlayQueueAddOptions, LibraryItem, Play, PlaybackType } from '/@/rendere import { toast } from '/@/renderer/components/toast'; import isElectron from 'is-electron'; import { nanoid } from 'nanoid/non-secure'; +import { SongListSort, SortOrder } from '/@/renderer/api/types'; const mpvPlayer = isElectron() ? window.electron.mpvPlayer : null; @@ -22,31 +23,48 @@ export const useHandlePlayQueueAdd = () => { const handlePlayQueueAdd = async (options: PlayQueueAddOptions) => { if (!server) return toast.error({ message: 'No server selected', type: 'error' }); + let songs = null; if (options.byItemType) { - let songs = null; - if (options.byItemType.type === LibraryItem.ALBUM) { - const albumDetail = await queryClient.fetchQuery( - queryKeys.albums.detail(server?.id, { id: options.byItemType.id }), - async ({ signal }) => - api.controller.getAlbumDetail({ - query: { id: options.byItemType!.id }, - server, - signal, - }), + // const albumDetail = await queryClient.fetchQuery( + // queryKeys.albums.detail(server?.id, { id: options.byItemType.id }), + // async ({ signal }) => + // api.controller.getAlbumDetail({ + // query: { id: options.byItemType!.id }, + // server, + // signal, + // }), + // ); + + // if (!albumDetail) return null; + + const queryFilter = { + albumIds: options.byItemType?.id || [], + sortBy: SongListSort.ALBUM, + sortOrder: SortOrder.ASC, + startIndex: 0, + }; + + const queryKey = queryKeys.songs.list(server?.id, queryFilter); + const songsList = await queryClient.fetchQuery(queryKey, async ({ signal }) => + api.controller.getSongList({ + query: queryFilter, + server, + signal, + }), ); - if (!albumDetail) return null; + if (!songsList) return toast.warn({ message: 'Error occurred while fetching' }); switch (server?.type) { case 'jellyfin': - songs = albumDetail.songs?.map((song) => + songs = songsList.items?.map((song) => jfNormalize.song(song as JFSong, server, deviceId), ); break; case 'navidrome': - songs = albumDetail.songs?.map((song) => + songs = songsList.items?.map((song) => ndNormalize.song(song as NDSong, server, deviceId), ); break; @@ -54,48 +72,29 @@ export const useHandlePlayQueueAdd = () => { break; } } - - if (!songs) return toast.warn({ message: 'No songs found' }); - - const playerData = usePlayerStore.getState().actions.addToQueue(songs, options.play); - - if (options.play === Play.NEXT || options.play === Play.LAST) { - if (playerType === PlaybackType.LOCAL) { - mpvPlayer.setQueueNext(playerData); - } - } - - if (options.play === Play.NOW) { - if (playerType === PlaybackType.LOCAL) { - mpvPlayer.setQueue(playerData); - mpvPlayer.play(); - } - - usePlayerStore.getState().actions.play(); - } } if (options.byData) { - const songsWithNewUniqueId = options.byData.map((song) => ({ ...song, uniqueId: nanoid() })); + songs = options.byData.map((song) => ({ ...song, uniqueId: nanoid() })); + } - const playerData = usePlayerStore - .getState() - .actions.addToQueue(songsWithNewUniqueId, options.play); + if (!songs) return toast.warn({ message: 'No songs found' }); - if (options.play === Play.NEXT || options.play === Play.LAST) { - if (playerType === PlaybackType.LOCAL) { - mpvPlayer.setQueueNext(playerData); - } + const playerData = usePlayerStore.getState().actions.addToQueue(songs, options.play); + + if (options.play === Play.NEXT || options.play === Play.LAST) { + if (playerType === PlaybackType.LOCAL) { + mpvPlayer.setQueueNext(playerData); + } + } + + if (options.play === Play.NOW) { + if (playerType === PlaybackType.LOCAL) { + mpvPlayer.setQueue(playerData); + mpvPlayer.play(); } - if (options.play === Play.NOW) { - if (playerType === PlaybackType.LOCAL) { - mpvPlayer.setQueue(playerData); - mpvPlayer.play(); - } - - usePlayerStore.getState().actions.play(); - } + usePlayerStore.getState().actions.play(); } return null; diff --git a/src/renderer/types.ts b/src/renderer/types.ts index a26dce4a..fb5a4d9e 100644 --- a/src/renderer/types.ts +++ b/src/renderer/types.ts @@ -159,7 +159,7 @@ export enum TableColumn { export type PlayQueueAddOptions = { byData?: QueueSong[]; byItemType?: { - id: string; + id: string[]; type: LibraryItem; }; play: Play;