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 { toast } from '/@/renderer/components/toast';
|
||||||
import isElectron from 'is-electron';
|
import isElectron from 'is-electron';
|
||||||
import { nanoid } from 'nanoid/non-secure';
|
import { nanoid } from 'nanoid/non-secure';
|
||||||
|
import { SongListSort, SortOrder } from '/@/renderer/api/types';
|
||||||
|
|
||||||
const mpvPlayer = isElectron() ? window.electron.mpvPlayer : null;
|
const mpvPlayer = isElectron() ? window.electron.mpvPlayer : null;
|
||||||
|
|
||||||
|
@ -22,31 +23,48 @@ export const useHandlePlayQueueAdd = () => {
|
||||||
|
|
||||||
const handlePlayQueueAdd = async (options: PlayQueueAddOptions) => {
|
const handlePlayQueueAdd = async (options: PlayQueueAddOptions) => {
|
||||||
if (!server) return toast.error({ message: 'No server selected', type: 'error' });
|
if (!server) return toast.error({ message: 'No server selected', type: 'error' });
|
||||||
|
let songs = null;
|
||||||
|
|
||||||
if (options.byItemType) {
|
if (options.byItemType) {
|
||||||
let songs = null;
|
|
||||||
|
|
||||||
if (options.byItemType.type === LibraryItem.ALBUM) {
|
if (options.byItemType.type === LibraryItem.ALBUM) {
|
||||||
const albumDetail = await queryClient.fetchQuery(
|
// const albumDetail = await queryClient.fetchQuery(
|
||||||
queryKeys.albums.detail(server?.id, { id: options.byItemType.id }),
|
// queryKeys.albums.detail(server?.id, { id: options.byItemType.id }),
|
||||||
async ({ signal }) =>
|
// async ({ signal }) =>
|
||||||
api.controller.getAlbumDetail({
|
// api.controller.getAlbumDetail({
|
||||||
query: { id: options.byItemType!.id },
|
// query: { id: options.byItemType!.id },
|
||||||
server,
|
// server,
|
||||||
signal,
|
// 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) {
|
switch (server?.type) {
|
||||||
case 'jellyfin':
|
case 'jellyfin':
|
||||||
songs = albumDetail.songs?.map((song) =>
|
songs = songsList.items?.map((song) =>
|
||||||
jfNormalize.song(song as JFSong, server, deviceId),
|
jfNormalize.song(song as JFSong, server, deviceId),
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
case 'navidrome':
|
case 'navidrome':
|
||||||
songs = albumDetail.songs?.map((song) =>
|
songs = songsList.items?.map((song) =>
|
||||||
ndNormalize.song(song as NDSong, server, deviceId),
|
ndNormalize.song(song as NDSong, server, deviceId),
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
|
@ -54,48 +72,29 @@ export const useHandlePlayQueueAdd = () => {
|
||||||
break;
|
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) {
|
if (options.byData) {
|
||||||
const songsWithNewUniqueId = options.byData.map((song) => ({ ...song, uniqueId: nanoid() }));
|
songs = options.byData.map((song) => ({ ...song, uniqueId: nanoid() }));
|
||||||
|
}
|
||||||
|
|
||||||
const playerData = usePlayerStore
|
if (!songs) return toast.warn({ message: 'No songs found' });
|
||||||
.getState()
|
|
||||||
.actions.addToQueue(songsWithNewUniqueId, options.play);
|
|
||||||
|
|
||||||
if (options.play === Play.NEXT || options.play === Play.LAST) {
|
const playerData = usePlayerStore.getState().actions.addToQueue(songs, options.play);
|
||||||
if (playerType === PlaybackType.LOCAL) {
|
|
||||||
mpvPlayer.setQueueNext(playerData);
|
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) {
|
usePlayerStore.getState().actions.play();
|
||||||
if (playerType === PlaybackType.LOCAL) {
|
|
||||||
mpvPlayer.setQueue(playerData);
|
|
||||||
mpvPlayer.play();
|
|
||||||
}
|
|
||||||
|
|
||||||
usePlayerStore.getState().actions.play();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|
|
@ -159,7 +159,7 @@ export enum TableColumn {
|
||||||
export type PlayQueueAddOptions = {
|
export type PlayQueueAddOptions = {
|
||||||
byData?: QueueSong[];
|
byData?: QueueSong[];
|
||||||
byItemType?: {
|
byItemType?: {
|
||||||
id: string;
|
id: string[];
|
||||||
type: LibraryItem;
|
type: LibraryItem;
|
||||||
};
|
};
|
||||||
play: Play;
|
play: Play;
|
||||||
|
|
Reference in a new issue