2023-01-29 18:40:26 -08:00
|
|
|
import { Box, Group, Stack } from '@mantine/core';
|
|
|
|
|
import { useForm } from '@mantine/form';
|
|
|
|
|
import { closeModal, ContextModalProps } from '@mantine/modals';
|
|
|
|
|
import { useMemo, useState } from 'react';
|
|
|
|
|
import { api } from '/@/renderer/api';
|
|
|
|
|
import { queryKeys } from '/@/renderer/api/query-keys';
|
|
|
|
|
import { PlaylistListSort, SongListQuery, SongListSort, SortOrder } from '/@/renderer/api/types';
|
|
|
|
|
import { Button, MultiSelect, Switch, toast } from '/@/renderer/components';
|
2023-08-04 02:27:04 -07:00
|
|
|
import { getGenreSongsById } from '/@/renderer/features/player';
|
2023-01-29 18:40:26 -08:00
|
|
|
import { useAddToPlaylist } from '/@/renderer/features/playlists/mutations/add-to-playlist-mutation';
|
|
|
|
|
import { usePlaylistList } from '/@/renderer/features/playlists/queries/playlist-list-query';
|
|
|
|
|
import { queryClient } from '/@/renderer/lib/react-query';
|
|
|
|
|
import { useCurrentServer } from '/@/renderer/store';
|
|
|
|
|
|
|
|
|
|
export const AddToPlaylistContextModal = ({
|
2023-07-01 19:10:05 -07:00
|
|
|
id,
|
|
|
|
|
innerProps,
|
2023-01-29 18:40:26 -08:00
|
|
|
}: ContextModalProps<{
|
2023-07-01 19:10:05 -07:00
|
|
|
albumId?: string[];
|
|
|
|
|
artistId?: string[];
|
2023-08-04 02:27:04 -07:00
|
|
|
genreId?: string[];
|
2023-07-01 19:10:05 -07:00
|
|
|
songId?: string[];
|
2023-01-29 18:40:26 -08:00
|
|
|
}>) => {
|
2023-08-04 02:27:04 -07:00
|
|
|
const { albumId, artistId, genreId, songId } = innerProps;
|
2023-07-01 19:10:05 -07:00
|
|
|
const server = useCurrentServer();
|
|
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
|
|
|
|
|
|
|
|
const addToPlaylistMutation = useAddToPlaylist({});
|
|
|
|
|
|
|
|
|
|
const playlistList = usePlaylistList({
|
|
|
|
|
query: {
|
|
|
|
|
_custom: {
|
|
|
|
|
navidrome: {
|
|
|
|
|
smart: false,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
sortBy: PlaylistListSort.NAME,
|
|
|
|
|
sortOrder: SortOrder.ASC,
|
|
|
|
|
startIndex: 0,
|
2023-04-30 22:01:52 -07:00
|
|
|
},
|
2023-07-01 19:10:05 -07:00
|
|
|
serverId: server?.id,
|
|
|
|
|
});
|
2023-01-29 18:40:26 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const playlistSelect = useMemo(() => {
|
|
|
|
|
return (
|
|
|
|
|
playlistList.data?.items?.map((playlist) => ({
|
|
|
|
|
label: playlist.name,
|
|
|
|
|
value: playlist.id,
|
|
|
|
|
})) || []
|
|
|
|
|
);
|
|
|
|
|
}, [playlistList.data]);
|
2023-01-29 18:40:26 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const form = useForm({
|
|
|
|
|
initialValues: {
|
|
|
|
|
playlistId: [],
|
|
|
|
|
skipDuplicates: true,
|
|
|
|
|
},
|
2023-04-30 22:01:52 -07:00
|
|
|
});
|
2023-01-29 18:40:26 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const getSongsByAlbum = async (albumId: string) => {
|
|
|
|
|
const query: SongListQuery = {
|
|
|
|
|
albumIds: [albumId],
|
|
|
|
|
sortBy: SongListSort.ALBUM,
|
|
|
|
|
sortOrder: SortOrder.ASC,
|
|
|
|
|
startIndex: 0,
|
|
|
|
|
};
|
2023-01-29 18:40:26 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const queryKey = queryKeys.songs.list(server?.id || '', query);
|
2023-01-29 18:40:26 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const songsRes = await queryClient.fetchQuery(queryKey, ({ signal }) => {
|
|
|
|
|
if (!server) throw new Error('No server');
|
|
|
|
|
return api.controller.getSongList({ apiClientProps: { server, signal }, query });
|
|
|
|
|
});
|
2023-01-29 18:40:26 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
return songsRes;
|
|
|
|
|
};
|
2023-01-29 18:40:26 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const getSongsByArtist = async (artistId: string) => {
|
|
|
|
|
const query: SongListQuery = {
|
|
|
|
|
artistIds: [artistId],
|
|
|
|
|
sortBy: SongListSort.ARTIST,
|
|
|
|
|
sortOrder: SortOrder.ASC,
|
|
|
|
|
startIndex: 0,
|
2023-01-29 18:40:26 -08:00
|
|
|
};
|
|
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const queryKey = queryKeys.songs.list(server?.id || '', query);
|
2023-01-29 18:40:26 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const songsRes = await queryClient.fetchQuery(queryKey, ({ signal }) => {
|
|
|
|
|
if (!server) throw new Error('No server');
|
|
|
|
|
return api.controller.getSongList({ apiClientProps: { server, signal }, query });
|
2023-04-30 22:01:52 -07:00
|
|
|
});
|
2023-01-29 18:40:26 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
return songsRes;
|
|
|
|
|
};
|
2023-01-29 18:40:26 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const isSubmitDisabled = form.values.playlistId.length === 0 || addToPlaylistMutation.isLoading;
|
|
|
|
|
|
|
|
|
|
const handleSubmit = form.onSubmit(async (values) => {
|
|
|
|
|
setIsLoading(true);
|
|
|
|
|
const allSongIds: string[] = [];
|
2023-10-17 23:11:14 +00:00
|
|
|
let totalUniquesAdded = 0;
|
2023-07-01 19:10:05 -07:00
|
|
|
|
|
|
|
|
if (albumId && albumId.length > 0) {
|
|
|
|
|
for (const id of albumId) {
|
|
|
|
|
const songs = await getSongsByAlbum(id);
|
|
|
|
|
allSongIds.push(...(songs?.items?.map((song) => song.id) || []));
|
|
|
|
|
}
|
2023-01-29 18:40:26 -08:00
|
|
|
}
|
2023-07-01 19:10:05 -07:00
|
|
|
|
|
|
|
|
if (artistId && artistId.length > 0) {
|
|
|
|
|
for (const id of artistId) {
|
|
|
|
|
const songs = await getSongsByArtist(id);
|
|
|
|
|
allSongIds.push(...(songs?.items?.map((song) => song.id) || []));
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-04 02:27:04 -07:00
|
|
|
|
|
|
|
|
if (genreId && genreId.length > 0) {
|
|
|
|
|
const songs = await getGenreSongsById({
|
|
|
|
|
id: genreId,
|
|
|
|
|
queryClient,
|
|
|
|
|
server,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
allSongIds.push(...(songs?.items?.map((song) => song.id) || []));
|
|
|
|
|
}
|
2023-07-01 19:10:05 -07:00
|
|
|
|
|
|
|
|
if (songId && songId.length > 0) {
|
|
|
|
|
allSongIds.push(...songId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const playlistId of values.playlistId) {
|
2023-10-17 23:11:14 +00:00
|
|
|
const uniqueSongIds: string[] = [];
|
|
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
if (values.skipDuplicates) {
|
|
|
|
|
const query = {
|
|
|
|
|
id: playlistId,
|
|
|
|
|
startIndex: 0,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const queryKey = queryKeys.playlists.songList(server?.id || '', playlistId, query);
|
|
|
|
|
|
|
|
|
|
const playlistSongsRes = await queryClient.fetchQuery(queryKey, ({ signal }) => {
|
|
|
|
|
if (!server) throw new Error('No server');
|
|
|
|
|
return api.controller.getPlaylistSongList({
|
|
|
|
|
apiClientProps: {
|
|
|
|
|
server,
|
|
|
|
|
signal,
|
|
|
|
|
},
|
|
|
|
|
query: { id: playlistId, startIndex: 0 },
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const playlistSongIds = playlistSongsRes?.items?.map((song) => song.id);
|
|
|
|
|
|
|
|
|
|
for (const songId of allSongIds) {
|
|
|
|
|
if (!playlistSongIds?.includes(songId)) {
|
|
|
|
|
uniqueSongIds.push(songId);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-17 23:11:14 +00:00
|
|
|
totalUniquesAdded += uniqueSongIds.length;
|
2023-07-01 19:10:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (values.skipDuplicates ? uniqueSongIds.length > 0 : allSongIds.length > 0) {
|
|
|
|
|
if (!server) return null;
|
|
|
|
|
addToPlaylistMutation.mutate(
|
|
|
|
|
{
|
|
|
|
|
body: { songId: values.skipDuplicates ? uniqueSongIds : allSongIds },
|
|
|
|
|
query: { id: playlistId },
|
|
|
|
|
serverId: server?.id,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
onError: (err) => {
|
|
|
|
|
toast.error({
|
|
|
|
|
message: `[${
|
|
|
|
|
playlistSelect.find((playlist) => playlist.value === playlistId)
|
|
|
|
|
?.label
|
|
|
|
|
}] ${err.message}`,
|
|
|
|
|
title: 'Failed to add songs to playlist',
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-17 23:11:14 +00:00
|
|
|
const addMessage =
|
|
|
|
|
values.skipDuplicates &&
|
|
|
|
|
allSongIds.length * values.playlistId.length !== totalUniquesAdded
|
|
|
|
|
? `around ${Math.floor(totalUniquesAdded / values.playlistId.length)}`
|
|
|
|
|
: allSongIds.length;
|
|
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
setIsLoading(false);
|
|
|
|
|
toast.success({
|
2023-10-17 23:11:14 +00:00
|
|
|
message: `Added ${addMessage} songs to ${values.playlistId.length} playlist(s)`,
|
2023-07-01 19:10:05 -07:00
|
|
|
});
|
|
|
|
|
closeModal(id);
|
|
|
|
|
return null;
|
2023-01-29 18:40:26 -08:00
|
|
|
});
|
2023-07-01 19:10:05 -07:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Box p="1rem">
|
|
|
|
|
<form onSubmit={handleSubmit}>
|
|
|
|
|
<Stack>
|
|
|
|
|
<MultiSelect
|
|
|
|
|
clearable
|
|
|
|
|
searchable
|
|
|
|
|
data={playlistSelect}
|
|
|
|
|
disabled={playlistList.isLoading}
|
|
|
|
|
label="Playlists"
|
|
|
|
|
size="md"
|
|
|
|
|
{...form.getInputProps('playlistId')}
|
|
|
|
|
/>
|
|
|
|
|
<Switch
|
|
|
|
|
label="Skip duplicates"
|
|
|
|
|
{...form.getInputProps('skipDuplicates', { type: 'checkbox' })}
|
|
|
|
|
/>
|
|
|
|
|
<Group position="right">
|
|
|
|
|
<Group>
|
|
|
|
|
<Button
|
|
|
|
|
disabled={addToPlaylistMutation.isLoading}
|
|
|
|
|
size="md"
|
|
|
|
|
variant="subtle"
|
|
|
|
|
onClick={() => closeModal(id)}
|
|
|
|
|
>
|
|
|
|
|
Cancel
|
|
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
disabled={isSubmitDisabled}
|
|
|
|
|
loading={isLoading}
|
|
|
|
|
size="md"
|
|
|
|
|
type="submit"
|
|
|
|
|
variant="filled"
|
|
|
|
|
>
|
|
|
|
|
Add
|
|
|
|
|
</Button>
|
|
|
|
|
</Group>
|
|
|
|
|
</Group>
|
|
|
|
|
</Stack>
|
|
|
|
|
</form>
|
|
|
|
|
</Box>
|
|
|
|
|
);
|
2023-01-29 18:40:26 -08:00
|
|
|
};
|