Skip to content

Commit

Permalink
[WIP] fixup! Adjust reading the config into the new format
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucki committed Aug 27, 2024
1 parent 6479d6f commit ca09d7c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 80 deletions.
26 changes: 4 additions & 22 deletions packages/app/app/actions/downloads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import _ from 'lodash';
import { isEqual } from 'lodash';
import { store, StreamProvider } from '@nuclear/core';
import { getTrackItem } from '@nuclear/ui';
import { safeAddUuid } from './helpers';
import { rewriteTrackArtists, safeAddUuid } from './helpers';
import { Download, DownloadStatus, Track, TrackItem } from '@nuclear/ui/lib/types';
import { createStandardAction } from 'typesafe-actions';
import { Download as DownloadActionTypes } from './actionTypes';
Expand Down Expand Up @@ -179,26 +179,8 @@ export const clearFinishedDownloads = createStandardAction(DownloadActionTypes.C
*/
function getDownloadsBackwardsCompatible(): Download[] {
const downloads: Download[] = store.get('downloads');

downloads.forEach(download => {
// @ts-expect-error For backwards compatibility we're trying to parse an invalid field
if (download.track.artists || !download.track.artist) {
return;
}

// @ts-expect-error For backwards compatibility we're trying to parse an invalid field
if (download.track.artist) {
// @ts-expect-error For backwards compatibility we're trying to parse an invalid field
download.track.artists = _.isString(download.track.artist) ? [download.track.artist] : [download.track.artist.name];
}

// Assuming we have `extraArtists` on a track, we must had an `artist` which
// was already saved into `artists`, so this `track.artists` shouldn't be undefined
// @ts-expect-error For backwards compatibility we're trying to parse an invalid field
download.track.extraArtists?.forEach(artist => {
download.track.artists.push(artist);
});
return downloads.map(download => {
download.track = rewriteTrackArtists(download.track);
return download;
});

return downloads;
}
45 changes: 9 additions & 36 deletions packages/app/app/actions/favorites.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import _, { flow, omit, unionWith } from 'lodash';
import { store, Track } from '@nuclear/core';
import { store, Album, Artist, Track } from '@nuclear/core';
import { areTracksEqualByName, getTrackItem } from '@nuclear/ui';

import { safeAddUuid } from './helpers';
import { rewriteTrackArtists, safeAddUuid } from './helpers';
import { createStandardAction } from 'typesafe-actions';
import { TrackItem } from '@nuclear/ui/lib/types';

export const READ_FAVORITES = 'READ_FAVORITES';
export const ADD_FAVORITE_TRACK = 'ADD_FAVORITE_TRACK';
Expand Down Expand Up @@ -126,41 +127,13 @@ export function removeFavoriteArtist(artist) {
* `Track.artist` and `Track.extraArtists` are written into {@link Track.artists}
*/
function getFavoritesBackwardsCompatible() {
const favorites = store.get('favorites');

favorites.tracks?.forEach(track => {
if (track.artists || !track.artist) {
return;
}

if (track.artist) {
track.artists = _.isString(track.artist) ? [track.artist] : [track.artist.name];
}

// Assuming we have `extraArtists` on a track, we must had an `artist` which
// was already saved into `artists`, so this `track.artists` shouldn't be undefined
track.extraArtists?.forEach(artist => {
track.artists.push(artist);
});
});
const favorites: { albums?: Album[], artists?: Artist[], tracks?: Track[] } = store.get('favorites');

favorites.albums?.forEach(album => {
album.tracklist?.forEach(track => {
if (track.artists || !track.artist) {
return;
}

if (track.artist) {
track.artists = _.isString(track.artist) ? [track.artist] : [track.artist.name];
}

// Assuming we have `extraArtists` on a track, we must had an `artist` which
// was already saved into `artists`, so this `track.artists` shouldn't be undefined
track.extraArtists?.forEach(artist => {
track.artists.push(artist);
});
});
favorites.tracks = favorites.tracks?.map(rewriteTrackArtists);
favorites.albums = favorites.albums?.map(album => {
album.tracklist = album.tracklist?.map(rewriteTrackArtists);
return album;
});

return favorites;
return favorites as any;
}
18 changes: 18 additions & 0 deletions packages/app/app/actions/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { v4 } from 'uuid';
import _ from 'lodash';
import { createAction } from 'redux-actions';
import { PlaylistTrack, Track } from '@nuclear/core';
import { TrackItem } from '@nuclear/ui/lib/types';

type ActionsBasicType = {
[k: string]: (...payload: any) => any;
Expand All @@ -22,3 +24,19 @@ export const safeAddUuid = track => {

return clonedTrack;
};

export function rewriteTrackArtists<T extends PlaylistTrack | Track | TrackItem>(track: T): T {
const clonedTrack = _.cloneDeep(track);

// @ts-expect-error For backwards compatibility we're trying to parse an invalid field
if (clonedTrack.artists || !clonedTrack.artist) {
return clonedTrack;
}

// @ts-expect-error For backwards compatibility we're trying to parse an invalid field
clonedTrack.artists = _.isString(clonedTrack.artist) ? [clonedTrack.artist] : [clonedTrack.artist.name];
// @ts-expect-error For backwards compatibility we're trying to parse an invalid field
clonedTrack.artists = clonedTrack.artists.concat(clonedTrack.extraArtists?.map(artist));

return clonedTrack;
}
27 changes: 5 additions & 22 deletions packages/app/app/actions/playlists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { v4 } from 'uuid';
import { remote } from 'electron';
import { createAsyncAction, createStandardAction } from 'typesafe-actions';

import { store, PlaylistHelper, Playlist, PlaylistTrack, rest } from '@nuclear/core';
import { store, PlaylistHelper, Playlist, PlaylistTrack, Track, rest } from '@nuclear/core';
import { GetPlaylistsByUserIdResponseBody } from '@nuclear/core/src/rest/Nuclear/Playlists.types';
import { ErrorBody } from '@nuclear/core/src/rest/Nuclear/types';

Expand All @@ -18,6 +18,7 @@ import { success, error } from './toasts';
import { IdentityStore } from '../reducers/nuclear/identity';
import { PlaylistsStore } from '../reducers/playlists';
import { isEmpty } from 'lodash';
import { rewriteTrackArtists } from './helpers';

export const updatePlaylistsAction = createStandardAction(Playlists.UPDATE_LOCAL_PLAYLISTS)<PlaylistsStore['localPlaylists']['data']>();

Expand Down Expand Up @@ -175,26 +176,8 @@ export function addPlaylistFromFile(filePath, t) {
*/
function getPlaylistsBackwardsCompatible(): Playlist[] {
const playlists: Playlist[] = store.get('playlists');

playlists?.forEach(playlist => {
playlist.tracks?.forEach(track => {
// @ts-expect-error For backwards compatibility we're trying to parse an invalid field
if (track.artists || !track.artist) {
// New format already present, do nothing
return;
}

// @ts-expect-error For backwards compatibility we're trying to parse an invalid field
track.artists = _.isString(track.artist) ? [track.artist] : [track.artist.name];

// Assuming we have `extraArtists` on a track, we must had an `artist` which
// was already saved into `artists`, so this `track.artists` shouldn't be undefined
// @ts-expect-error For backwards compatibility we're trying to parse an invalid field
track.extraArtists?.forEach(artist => {
track.artists.push(artist);
});
});
return playlists.map(playlist => {
playlist.tracks = playlist.tracks?.map(rewriteTrackArtists);
return playlist;
});

return playlists;
}

0 comments on commit ca09d7c

Please sign in to comment.