feishin/src/renderer/features/player/hooks/use-handle-playqueue-add.ts

209 lines
6.8 KiB
TypeScript
Raw Normal View History

2022-12-31 19:26:58 -08:00
import { useCallback } from 'react';
2022-12-20 04:11:06 -08:00
import { useQueryClient } from '@tanstack/react-query';
import { api } from '/@/renderer/api/index';
import { queryKeys } from '/@/renderer/api/query-keys';
import { useCurrentServer, usePlayerControls, usePlayerStore } from '/@/renderer/store';
2022-12-25 01:55:00 -08:00
import { usePlayerType } from '/@/renderer/store/settings.store';
2023-01-05 21:59:07 -08:00
import { PlayQueueAddOptions, Play, PlaybackType } from '/@/renderer/types';
import { toast } from '/@/renderer/components/toast/index';
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';
2023-05-19 22:23:10 -07:00
import { LibraryItem, SongListSort, SortOrder, Song } 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;
const utils = isElectron() ? window.electron.utils : null;
const mpris = isElectron() && utils?.isLinux() ? window.electron.mpris : 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();
const server = useCurrentServer();
const { play } = usePlayerControls();
2022-12-20 04:11:06 -08:00
2022-12-31 19:26:58 -08:00
const handlePlayQueueAdd = useCallback(
async (options: PlayQueueAddOptions) => {
if (!server) return toast.error({ message: 'No server selected', type: 'error' });
let songs = null;
2023-01-07 03:26:18 -08:00
// const itemCount = options.byItemType?.id?.length || 0;
// const fetchId = itemCount > 1 ? nanoid() : null;
2022-12-31 19:26:58 -08:00
if (options.byItemType) {
2023-05-19 22:23:10 -07:00
let songsList: any;
2022-12-31 19:26:58 -08:00
let queryFilter: any;
let queryKey: any;
2023-01-07 03:26:18 -08:00
2022-12-31 20:07:44 -08:00
if (options.byItemType.type === LibraryItem.PLAYLIST) {
2023-01-07 03:26:18 -08:00
// if (fetchId) {
// toast.success({
// autoClose: false,
// id: fetchId,
// loading: true,
// message: `This may take a while...`,
// title: `Adding ${itemCount} albums to the queue`,
// });
// }
2022-12-31 19:26:58 -08:00
queryFilter = {
2022-12-31 20:07:44 -08:00
id: options.byItemType?.id || [],
sortBy: 'id',
2022-12-31 19:26:58 -08:00
sortOrder: SortOrder.ASC,
startIndex: 0,
};
2022-12-31 20:07:44 -08:00
queryKey = queryKeys.playlists.songList(
server?.id,
options.byItemType?.id?.[0] || '',
queryFilter,
);
} else if (options.byItemType.type === LibraryItem.ALBUM) {
2023-01-07 03:26:18 -08:00
// if (fetchId) {
// toast.success({
// autoClose: false,
// id: fetchId,
// loading: true,
// message: `This may take a while...`,
// title: `Adding ${itemCount} albums to the queue`,
// });
// }
2022-12-31 19:26:58 -08:00
queryFilter = {
2022-12-31 20:07:44 -08:00
albumIds: options.byItemType?.id || [],
2022-12-31 19:26:58 -08:00
sortBy: SongListSort.ALBUM,
sortOrder: SortOrder.ASC,
startIndex: 0,
};
queryKey = queryKeys.songs.list(server?.id, queryFilter);
2022-12-31 20:07:44 -08:00
} else if (options.byItemType.type === LibraryItem.ALBUM_ARTIST) {
2023-01-07 03:26:18 -08:00
// if (fetchId) {
// toast.success({
// autoClose: false,
// id: fetchId,
// loading: true,
// message: `This may take a while...`,
// title: `Adding ${itemCount} album artists to the queue`,
// });
// }
2022-12-31 19:26:58 -08:00
queryFilter = {
artistIds: options.byItemType?.id || [],
2023-01-07 03:26:18 -08:00
sortBy: SongListSort.ALBUM_ARTIST,
2022-12-31 19:26:58 -08:00
sortOrder: SortOrder.ASC,
startIndex: 0,
};
2023-01-07 03:26:18 -08:00
queryKey = queryKeys.songs.list(server?.id, queryFilter);
} else if (options.byItemType.type === LibraryItem.SONG) {
2023-05-19 22:23:10 -07:00
queryFilter = { id: options.byItemType.id };
queryKey = queryKeys.songs.detail(server?.id, queryFilter);
2022-12-31 19:26:58 -08:00
}
try {
2022-12-31 20:07:44 -08:00
if (options.byItemType?.type === LibraryItem.PLAYLIST) {
2023-01-07 03:26:18 -08:00
songsList = await queryClient.fetchQuery(
queryKey,
async ({ signal }) =>
api.controller.getPlaylistSongList({
apiClientProps: {
server,
signal,
},
2023-01-07 03:26:18 -08:00
query: queryFilter,
}),
{
cacheTime: 1000 * 60,
staleTime: 1000 * 60,
},
2022-12-31 20:07:44 -08:00
);
2023-05-19 22:23:10 -07:00
} else if (options.byItemType?.type === LibraryItem.SONG) {
const song = (await queryClient.fetchQuery(queryKey, async ({ signal }) =>
api.controller.getSongDetail({
apiClientProps: {
server,
signal,
},
query: queryFilter,
}),
)) as Song;
if (song) {
songsList = { items: [song], startIndex: 0, totalRecordCount: 1 };
}
2022-12-31 20:07:44 -08:00
} else {
2023-01-07 03:26:18 -08:00
songsList = await queryClient.fetchQuery(
queryKey,
async ({ signal }) =>
api.controller.getSongList({
apiClientProps: {
server,
signal,
},
2023-01-07 03:26:18 -08:00
query: queryFilter,
}),
{
cacheTime: 1000 * 60,
staleTime: 1000 * 60,
},
2022-12-31 20:07:44 -08:00
);
}
2022-12-31 19:26:58 -08:00
} catch (err: any) {
return toast.error({
message: err.message,
title: 'Play queue add failed',
});
}
if (!songsList) return toast.warn({ message: 'No songs found' });
2023-05-19 22:23:10 -07:00
songs = songsList.items?.map((song: Song) => ({ ...song, uniqueId: nanoid() }));
2022-12-31 19:26:58 -08:00
} else if (options.byData) {
songs = options.byData.map((song) => ({ ...song, uniqueId: nanoid() }));
2022-12-30 21:02:17 -08:00
}
2022-12-20 04:11:06 -08:00
2022-12-31 19:26:58 -08:00
if (!songs) return toast.warn({ message: 'No songs found' });
2022-12-20 04:11:06 -08:00
2022-12-31 19:26:58 -08:00
const playerData = usePlayerStore.getState().actions.addToQueue(songs, options.play);
2022-12-20 04:11:06 -08:00
if (playerType === PlaybackType.LOCAL) {
if (options.play === Play.NEXT || options.play === Play.LAST) {
2022-12-31 19:26:58 -08:00
mpvPlayer.setQueueNext(playerData);
}
2022-12-20 04:11:06 -08:00
if (options.play === Play.NOW) {
2022-12-31 19:26:58 -08:00
mpvPlayer.setQueue(playerData);
mpvPlayer.play();
}
}
2022-12-28 15:31:04 -08:00
play();
mpris?.updateSong({
currentTime: usePlayerStore.getState().current.time,
repeat: usePlayerStore.getState().repeat,
shuffle: usePlayerStore.getState().shuffle,
song: playerData.current.song,
status: 'Playing',
});
2023-01-07 03:26:18 -08:00
// if (fetchId) {
// toast.update({
// autoClose: 1000,
// id: fetchId,
// message: '',
// title: `Added ${songs.length} items to the queue`,
// });
// // toast.hide(fetchId);
// } else {
// toast.success({
// // message: 'Success',
// title: `Added ${songs.length} items to the queue`,
// });
// }
2022-12-31 19:26:58 -08:00
return null;
},
[play, playerType, queryClient, server],
2022-12-31 19:26:58 -08:00
);
2022-12-20 04:11:06 -08:00
return handlePlayQueueAdd;
};