2022-12-20 04:11:06 -08:00
|
|
|
import { useQueryClient } from '@tanstack/react-query';
|
|
|
|
|
import { api } from '/@/renderer/api/index';
|
|
|
|
|
import { jfNormalize } from '/@/renderer/api/jellyfin.api';
|
|
|
|
|
import { JFSong } from '/@/renderer/api/jellyfin.types';
|
|
|
|
|
import { ndNormalize } from '/@/renderer/api/navidrome.api';
|
|
|
|
|
import { NDSong } from '/@/renderer/api/navidrome.types';
|
|
|
|
|
import { queryKeys } from '/@/renderer/api/query-keys';
|
|
|
|
|
import { useAuthStore, usePlayerStore } from '/@/renderer/store';
|
2022-12-25 01:55:00 -08:00
|
|
|
import { usePlayerType } from '/@/renderer/store/settings.store';
|
2022-12-20 04:11:06 -08:00
|
|
|
import { PlayQueueAddOptions, LibraryItem, Play, PlaybackType } from '/@/renderer/types';
|
|
|
|
|
import { toast } from '/@/renderer/components/toast';
|
2022-12-25 01:25:46 -08:00
|
|
|
import isElectron from 'is-electron';
|
2022-12-28 15:31:04 -08:00
|
|
|
import { nanoid } from 'nanoid/non-secure';
|
2022-12-28 19:19:05 -08:00
|
|
|
import { SongListSort, SortOrder } from '/@/renderer/api/types';
|
2022-12-20 04:11:06 -08:00
|
|
|
|
2022-12-25 01:25:46 -08:00
|
|
|
const mpvPlayer = isElectron() ? window.electron.mpvPlayer : null;
|
2022-12-20 04:11:06 -08:00
|
|
|
|
|
|
|
|
export const useHandlePlayQueueAdd = () => {
|
|
|
|
|
const queryClient = useQueryClient();
|
2022-12-25 01:55:00 -08:00
|
|
|
const playerType = usePlayerType();
|
2022-12-20 04:11:06 -08:00
|
|
|
const deviceId = useAuthStore.getState().deviceId;
|
|
|
|
|
const server = useAuthStore.getState().currentServer;
|
|
|
|
|
|
|
|
|
|
const handlePlayQueueAdd = async (options: PlayQueueAddOptions) => {
|
|
|
|
|
if (!server) return toast.error({ message: 'No server selected', type: 'error' });
|
2022-12-28 19:19:05 -08:00
|
|
|
let songs = null;
|
2022-12-20 04:11:06 -08:00
|
|
|
|
|
|
|
|
if (options.byItemType) {
|
|
|
|
|
if (options.byItemType.type === LibraryItem.ALBUM) {
|
2022-12-28 19:19:05 -08:00
|
|
|
// 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);
|
2022-12-29 18:29:24 -08:00
|
|
|
let songsList;
|
|
|
|
|
try {
|
|
|
|
|
songsList = await queryClient.fetchQuery(queryKey, async ({ signal }) =>
|
|
|
|
|
api.controller.getSongList({
|
|
|
|
|
query: queryFilter,
|
|
|
|
|
server,
|
|
|
|
|
signal,
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
} catch (err: any) {
|
|
|
|
|
return toast.error({
|
|
|
|
|
message: err.message,
|
|
|
|
|
title: 'Play queue add failed',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!songsList) return toast.warn({ message: 'No songs found' });
|
2022-12-20 04:11:06 -08:00
|
|
|
|
|
|
|
|
switch (server?.type) {
|
|
|
|
|
case 'jellyfin':
|
2022-12-28 19:19:05 -08:00
|
|
|
songs = songsList.items?.map((song) =>
|
2022-12-20 04:11:06 -08:00
|
|
|
jfNormalize.song(song as JFSong, server, deviceId),
|
|
|
|
|
);
|
|
|
|
|
break;
|
|
|
|
|
case 'navidrome':
|
2022-12-28 19:19:05 -08:00
|
|
|
songs = songsList.items?.map((song) =>
|
2022-12-20 04:11:06 -08:00
|
|
|
ndNormalize.song(song as NDSong, server, deviceId),
|
|
|
|
|
);
|
|
|
|
|
break;
|
|
|
|
|
case 'subsonic':
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-28 19:19:05 -08:00
|
|
|
}
|
2022-12-20 04:11:06 -08:00
|
|
|
|
2022-12-28 19:19:05 -08:00
|
|
|
if (options.byData) {
|
|
|
|
|
songs = options.byData.map((song) => ({ ...song, uniqueId: nanoid() }));
|
|
|
|
|
}
|
2022-12-20 04:11:06 -08:00
|
|
|
|
2022-12-28 19:19:05 -08:00
|
|
|
if (!songs) return toast.warn({ message: 'No songs found' });
|
2022-12-20 04:11:06 -08:00
|
|
|
|
2022-12-28 19:19:05 -08:00
|
|
|
const playerData = usePlayerStore.getState().actions.addToQueue(songs, options.play);
|
2022-12-20 04:11:06 -08:00
|
|
|
|
2022-12-28 19:19:05 -08:00
|
|
|
if (options.play === Play.NEXT || options.play === Play.LAST) {
|
|
|
|
|
if (playerType === PlaybackType.LOCAL) {
|
|
|
|
|
mpvPlayer.setQueueNext(playerData);
|
2022-12-20 04:11:06 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-28 19:19:05 -08:00
|
|
|
if (options.play === Play.NOW) {
|
|
|
|
|
if (playerType === PlaybackType.LOCAL) {
|
|
|
|
|
mpvPlayer.setQueue(playerData);
|
|
|
|
|
mpvPlayer.play();
|
2022-12-28 15:31:04 -08:00
|
|
|
}
|
|
|
|
|
|
2022-12-28 19:19:05 -08:00
|
|
|
usePlayerStore.getState().actions.play();
|
2022-12-28 15:31:04 -08:00
|
|
|
}
|
|
|
|
|
|
2022-12-20 04:11:06 -08:00
|
|
|
return null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return handlePlayQueueAdd;
|
|
|
|
|
};
|