mirror of
https://github.com/austinried/subtracks.git
synced 2025-12-29 17:39:27 +01:00
store client in state, populate with active server
This commit is contained in:
parent
8b17ebe9c2
commit
6d30168ea0
@ -31,36 +31,33 @@ const selectors = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const useArtistInfo = (id: string) => {
|
export const useArtistInfo = (id: string) => {
|
||||||
const server = useStore(selectSettings.activeServer)
|
|
||||||
const artistInfo = useStore(useCallback((state: Store) => state.artistInfo[id], [id]))
|
const artistInfo = useStore(useCallback((state: Store) => state.artistInfo[id], [id]))
|
||||||
const fetchArtistInfo = useStore(selectors.fetchArtistInfo)
|
const fetchArtistInfo = useStore(selectors.fetchArtistInfo)
|
||||||
|
|
||||||
if (server && !artistInfo) {
|
if (!artistInfo) {
|
||||||
fetchArtistInfo(server, id)
|
fetchArtistInfo(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
return artistInfo
|
return artistInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useAlbumWithSongs = (id: string) => {
|
export const useAlbumWithSongs = (id: string) => {
|
||||||
const server = useStore(selectSettings.activeServer)
|
|
||||||
const album = useStore(useCallback((state: Store) => state.albums[id], [id]))
|
const album = useStore(useCallback((state: Store) => state.albums[id], [id]))
|
||||||
const fetchAlbum = useStore(selectors.fetchAlbum)
|
const fetchAlbum = useStore(selectors.fetchAlbum)
|
||||||
|
|
||||||
if (server && !album) {
|
if (!album) {
|
||||||
fetchAlbum(server, id)
|
fetchAlbum(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
return album
|
return album
|
||||||
}
|
}
|
||||||
|
|
||||||
export const usePlaylistWithSongs = (id: string) => {
|
export const usePlaylistWithSongs = (id: string) => {
|
||||||
const server = useStore(selectSettings.activeServer)
|
|
||||||
const playlist = useStore(useCallback((state: Store) => state.playlists[id], [id]))
|
const playlist = useStore(useCallback((state: Store) => state.playlists[id], [id]))
|
||||||
const fetchPlaylist = useStore(selectors.fetchPlaylist)
|
const fetchPlaylist = useStore(selectors.fetchPlaylist)
|
||||||
|
|
||||||
if (server && !playlist) {
|
if (!playlist) {
|
||||||
fetchPlaylist(server, id)
|
fetchPlaylist(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
return playlist
|
return playlist
|
||||||
|
|||||||
@ -10,9 +10,7 @@ import {
|
|||||||
PlaylistWithSongs,
|
PlaylistWithSongs,
|
||||||
SearchResults,
|
SearchResults,
|
||||||
} from '@app/models/music'
|
} from '@app/models/music'
|
||||||
import { Server } from '@app/models/settings'
|
|
||||||
import { Store } from '@app/state/store'
|
import { Store } from '@app/state/store'
|
||||||
import { SubsonicApiClient } from '@app/subsonic/api'
|
|
||||||
import produce from 'immer'
|
import produce from 'immer'
|
||||||
import { atom } from 'jotai'
|
import { atom } from 'jotai'
|
||||||
import { GetState, SetState } from 'zustand'
|
import { GetState, SetState } from 'zustand'
|
||||||
@ -25,12 +23,12 @@ export type MusicSlice = {
|
|||||||
albumsCache: string[]
|
albumsCache: string[]
|
||||||
playlists: { [id: string]: PlaylistWithSongs | undefined }
|
playlists: { [id: string]: PlaylistWithSongs | undefined }
|
||||||
playlistsCache: string[]
|
playlistsCache: string[]
|
||||||
fetchArtistInfo: (server: Server, id: string) => Promise<ArtistInfo | undefined>
|
fetchArtistInfo: (id: string) => Promise<ArtistInfo | undefined>
|
||||||
fetchAlbum: (server: Server, id: string) => Promise<AlbumWithSongs | undefined>
|
fetchAlbum: (id: string) => Promise<AlbumWithSongs | undefined>
|
||||||
fetchPlaylist: (server: Server, id: string) => Promise<PlaylistWithSongs | undefined>
|
fetchPlaylist: (id: string) => Promise<PlaylistWithSongs | undefined>
|
||||||
}
|
}
|
||||||
|
|
||||||
export const createMusicSlice = (set: SetState<Store>, _get: GetState<Store>): MusicSlice => ({
|
export const createMusicSlice = (set: SetState<Store>, get: GetState<Store>): MusicSlice => ({
|
||||||
cacheSize: 100,
|
cacheSize: 100,
|
||||||
artistInfo: {},
|
artistInfo: {},
|
||||||
artistInfoCache: [],
|
artistInfoCache: [],
|
||||||
@ -38,8 +36,11 @@ export const createMusicSlice = (set: SetState<Store>, _get: GetState<Store>): M
|
|||||||
albumsCache: [],
|
albumsCache: [],
|
||||||
playlists: {},
|
playlists: {},
|
||||||
playlistsCache: [],
|
playlistsCache: [],
|
||||||
fetchArtistInfo: async (server, id) => {
|
fetchArtistInfo: async id => {
|
||||||
const client = new SubsonicApiClient(server)
|
const client = get().client
|
||||||
|
if (!client) {
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const [artistResponse, artistInfoResponse] = await Promise.all([
|
const [artistResponse, artistInfoResponse] = await Promise.all([
|
||||||
@ -68,8 +69,11 @@ export const createMusicSlice = (set: SetState<Store>, _get: GetState<Store>): M
|
|||||||
return undefined
|
return undefined
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
fetchAlbum: async (server, id) => {
|
fetchAlbum: async id => {
|
||||||
const client = new SubsonicApiClient(server)
|
const client = get().client
|
||||||
|
if (!client) {
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await client.getAlbum({ id })
|
const response = await client.getAlbum({ id })
|
||||||
@ -89,8 +93,11 @@ export const createMusicSlice = (set: SetState<Store>, _get: GetState<Store>): M
|
|||||||
return undefined
|
return undefined
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
fetchPlaylist: async (server, id) => {
|
fetchPlaylist: async id => {
|
||||||
const client = new SubsonicApiClient(server)
|
const client = get().client
|
||||||
|
if (!client) {
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await client.getPlaylist({ id })
|
const response = await client.getPlaylist({ id })
|
||||||
|
|||||||
@ -1,35 +1,55 @@
|
|||||||
import { AppSettings, Server } from '@app/models/settings'
|
import { AppSettings, Server } from '@app/models/settings'
|
||||||
import { Store } from '@app/state/store'
|
import { Store } from '@app/state/store'
|
||||||
|
import { SubsonicApiClient } from '@app/subsonic/api'
|
||||||
import produce from 'immer'
|
import produce from 'immer'
|
||||||
import { GetState, SetState } from 'zustand'
|
import { GetState, SetState } from 'zustand'
|
||||||
|
|
||||||
export type SettingsSlice = {
|
export type SettingsSlice = {
|
||||||
settings: AppSettings
|
settings: AppSettings
|
||||||
|
client?: SubsonicApiClient
|
||||||
|
createClient: () => void
|
||||||
setActiveServer: (id?: string) => void
|
setActiveServer: (id?: string) => void
|
||||||
setServers: (servers: Server[]) => void
|
setServers: (servers: Server[]) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
export const createSettingsSlice = (set: SetState<Store>, _get: GetState<Store>): SettingsSlice => ({
|
export const createSettingsSlice = (set: SetState<Store>, get: GetState<Store>): SettingsSlice => ({
|
||||||
settings: {
|
settings: {
|
||||||
servers: [],
|
servers: [],
|
||||||
home: {
|
home: {
|
||||||
lists: ['recent', 'random', 'frequent', 'starred'],
|
lists: ['recent', 'random', 'frequent', 'starred'],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
setActiveServer: id =>
|
createClient: () => {
|
||||||
|
const server = get().settings.servers.find(s => s.id === get().settings.activeServer)
|
||||||
|
if (!server) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
set(
|
set(
|
||||||
produce<Store>(state => {
|
produce<Store>(state => {
|
||||||
if (!state.settings.servers.find(s => s.id === id)) {
|
state.client = new SubsonicApiClient(server)
|
||||||
console.log('could not find')
|
}),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
setActiveServer: id => {
|
||||||
|
const servers = get().settings.servers
|
||||||
|
const currentActiveServerId = get().settings.activeServer
|
||||||
|
|
||||||
|
if (!servers.find(s => s.id === id)) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (state.settings.activeServer === id) {
|
if (currentActiveServerId === id) {
|
||||||
console.log('already active')
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
set(
|
||||||
|
produce<Store>(state => {
|
||||||
state.settings.activeServer = id
|
state.settings.activeServer = id
|
||||||
}),
|
}),
|
||||||
),
|
)
|
||||||
|
|
||||||
|
get().createClient()
|
||||||
|
},
|
||||||
setServers: servers =>
|
setServers: servers =>
|
||||||
set(
|
set(
|
||||||
produce<Store>(state => {
|
produce<Store>(state => {
|
||||||
|
|||||||
@ -34,9 +34,11 @@ export const useStore = create<Store>(
|
|||||||
name: '@appStore',
|
name: '@appStore',
|
||||||
getStorage: () => storage,
|
getStorage: () => storage,
|
||||||
whitelist: ['settings'],
|
whitelist: ['settings'],
|
||||||
// onRehydrateStorage: state => {
|
onRehydrateStorage: _preState => {
|
||||||
// return (state, error) => {}
|
return (postState, _error) => {
|
||||||
// },
|
postState?.createClient()
|
||||||
|
}
|
||||||
|
},
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user