Accept string of ids by itemtype in playqueue add
This commit is contained in:
parent
694969cf41
commit
9836d548a6
2 changed files with 48 additions and 49 deletions
|
@ -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;
|
||||
|
|
|
@ -159,7 +159,7 @@ export enum TableColumn {
|
|||
export type PlayQueueAddOptions = {
|
||||
byData?: QueueSong[];
|
||||
byItemType?: {
|
||||
id: string;
|
||||
id: string[];
|
||||
type: LibraryItem;
|
||||
};
|
||||
play: Play;
|
||||
|
|
Reference in a new issue