mirror of
https://github.com/austinried/subtracks.git
synced 2026-02-10 15:02:42 +01:00
don't reset parts manually, do it all at once
This commit is contained in:
@@ -20,6 +20,7 @@ import {
|
||||
SubsonicResponse,
|
||||
} from '@app/subsonic/responses'
|
||||
import produce from 'immer'
|
||||
import { WritableDraft } from 'immer/dist/types/types-external'
|
||||
import merge from 'lodash.merge'
|
||||
import pick from 'lodash.pick'
|
||||
import { GetState, SetState } from 'zustand'
|
||||
@@ -175,13 +176,12 @@ export type LibrarySlice = {
|
||||
songs: ById<Song>
|
||||
}
|
||||
|
||||
resetLibrary: () => void
|
||||
resetLibrary: (state?: WritableDraft<Store>) => void
|
||||
|
||||
fetchLibraryArtists: () => Promise<void>
|
||||
fetchLibraryArtist: (id: string) => Promise<void>
|
||||
fetchLibraryArtistInfo: (artistId: string) => Promise<void>
|
||||
fetchLibraryArtistTopSongs: (artistName: string) => Promise<void>
|
||||
resetLibraryArtists: () => void
|
||||
|
||||
fetchLibraryAlbum: (id: string) => Promise<void>
|
||||
|
||||
@@ -205,6 +205,10 @@ function mergeById<T extends { [id: string]: unknown }>(object: T, source: T): v
|
||||
merge(object, source)
|
||||
}
|
||||
|
||||
export function mapById<T>(object: ById<T>, ids: string[]): T[] {
|
||||
return ids.map(id => object[id]).filter(a => a !== undefined)
|
||||
}
|
||||
|
||||
const defaultEntities = () => ({
|
||||
artists: {},
|
||||
artistAlbums: {},
|
||||
@@ -225,7 +229,11 @@ const defaultEntities = () => ({
|
||||
export const createLibrarySlice = (set: SetState<Store>, get: GetState<Store>): LibrarySlice => ({
|
||||
entities: defaultEntities(),
|
||||
|
||||
resetLibrary: () => {
|
||||
resetLibrary: state => {
|
||||
if (state) {
|
||||
state.entities = defaultEntities()
|
||||
return
|
||||
}
|
||||
set(store => {
|
||||
store.entities = defaultEntities()
|
||||
})
|
||||
@@ -281,15 +289,6 @@ export const createLibrarySlice = (set: SetState<Store>, get: GetState<Store>):
|
||||
)
|
||||
},
|
||||
|
||||
resetLibraryArtists: () => {
|
||||
set(
|
||||
produce<LibrarySlice>(state => {
|
||||
state.entities.artists = {}
|
||||
state.entities.artistAlbums = {}
|
||||
}),
|
||||
)
|
||||
},
|
||||
|
||||
fetchLibraryArtistInfo: async id => {
|
||||
const client = get().client
|
||||
if (!client) {
|
||||
|
||||
@@ -115,10 +115,9 @@ export const createSettingsSlice = (set: SetState<Store>, get: GetState<Store>):
|
||||
produce<Store>(state => {
|
||||
state.settings.activeServer = newActiveServer.id
|
||||
state.client = new SubsonicApiClient(newActiveServer)
|
||||
get().resetLibrary(state)
|
||||
}),
|
||||
)
|
||||
|
||||
get().resetLibrary()
|
||||
},
|
||||
|
||||
getActiveServer: () => get().settings.servers.find(s => s.id === get().settings.activeServer),
|
||||
|
||||
Reference in New Issue
Block a user