Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bugfix]: do not duplicate tracks when adding to multiple playlists #300

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export const AddToPlaylistContextModal = ({
const handleSubmit = form.onSubmit(async (values) => {
setIsLoading(true);
const allSongIds: string[] = [];
const uniqueSongIds: string[] = [];
let totalUniquesAdded = 0;

if (albumId && albumId.length > 0) {
for (const id of albumId) {
Expand Down Expand Up @@ -129,6 +129,8 @@ export const AddToPlaylistContextModal = ({
}

for (const playlistId of values.playlistId) {
const uniqueSongIds: string[] = [];

if (values.skipDuplicates) {
const query = {
id: playlistId,
Expand All @@ -155,6 +157,7 @@ export const AddToPlaylistContextModal = ({
uniqueSongIds.push(songId);
}
}
totalUniquesAdded += uniqueSongIds.length;
}

if (values.skipDuplicates ? uniqueSongIds.length > 0 : allSongIds.length > 0) {
Expand All @@ -180,11 +183,15 @@ export const AddToPlaylistContextModal = ({
}
}

const addMessage =
values.skipDuplicates &&
allSongIds.length * values.playlistId.length !== totalUniquesAdded
? `around ${Math.floor(totalUniquesAdded / values.playlistId.length)}`
: allSongIds.length;

setIsLoading(false);
toast.success({
message: `Added ${
values.skipDuplicates ? uniqueSongIds.length : allSongIds.length
} songs to ${values.playlistId.length} playlist(s)`,
message: `Added ${addMessage} songs to ${values.playlistId.length} playlist(s)`,
});
closeModal(id);
return null;
Expand Down