group album tracks as they appear (in queue)

+ move handling disc logic to the album store
This commit is contained in:
geoffrey45
2022-08-02 23:46:13 +03:00
parent 3dcb8ed2ef
commit ef2926f18f
5 changed files with 39 additions and 34 deletions
+22 -3
View File
@@ -17,10 +17,24 @@ function sortTracks(tracks: Track[]) {
});
}
interface Discs {
[key: string]: Track[];
}
function createDiscs(tracks: Track[]): Discs {
return tracks.reduce((group, track) => {
const { discnumber } = track;
group[discnumber] = group[discnumber] ?? [];
group[discnumber].push(track);
return group;
}, {} as Discs);
}
export default defineStore("album", {
state: () => ({
info: <AlbumInfo>{},
tracks: <Track[]>[],
discs: <Discs>{},
artists: <Artist[]>[],
bio: null,
}),
@@ -31,11 +45,16 @@ export default defineStore("album", {
* @param hash title of the album
*/
async fetchTracksAndArtists(hash: string) {
const tracks = await getAlbumTracks(hash, useNotifStore);
this.tracks = [];
const album = await getAlbumTracks(hash, useNotifStore);
const artists = await getAlbumArtists(hash);
this.tracks = sortTracks(tracks.tracks);
this.info = tracks.info;
this.discs = createDiscs(sortTracks(album.tracks));
Object.keys(this.discs).forEach((disc) => {
this.tracks.push(...this.discs[disc]);
});
this.info = album.info;
this.artists = artists;
},
/**