mirror of
https://github.com/austinried/subtracks.git
synced 2026-02-10 15:02:42 +01:00
fixed changing activeServer
This commit is contained in:
@@ -43,10 +43,13 @@ export const useUpdateArtists = () => {
|
||||
setUpdating(true)
|
||||
|
||||
const client = new SubsonicApiClient(server)
|
||||
const response = await client.getArtists()
|
||||
|
||||
setArtists(response.data.artists.map(mapArtistID3toArtist))
|
||||
setUpdating(false)
|
||||
try {
|
||||
const response = await client.getArtists()
|
||||
setArtists(response.data.artists.map(mapArtistID3toArtist))
|
||||
} finally {
|
||||
setUpdating(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,17 +86,19 @@ export const useUpdateHomeLists = () => {
|
||||
|
||||
const client = new SubsonicApiClient(server)
|
||||
|
||||
const promises: Promise<any>[] = []
|
||||
for (const type of types) {
|
||||
promises.push(
|
||||
client.getAlbumList2({ type: type as GetAlbumList2Type, size: 20 }).then(response => {
|
||||
updateHomeList({ type, albums: response.data.albums.map(mapAlbumID3toAlbumListItem) })
|
||||
}),
|
||||
)
|
||||
try {
|
||||
const promises: Promise<any>[] = []
|
||||
for (const type of types) {
|
||||
promises.push(
|
||||
client.getAlbumList2({ type: type as GetAlbumList2Type, size: 20 }).then(response => {
|
||||
updateHomeList({ type, albums: response.data.albums.map(mapAlbumID3toAlbumListItem) })
|
||||
}),
|
||||
)
|
||||
}
|
||||
await Promise.all(promises)
|
||||
} finally {
|
||||
setUpdating(false)
|
||||
}
|
||||
await Promise.all(promises)
|
||||
|
||||
setUpdating(false)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,14 +125,17 @@ export const useUpdateSearchResults = () => {
|
||||
setUpdating(true)
|
||||
|
||||
const client = new SubsonicApiClient(server)
|
||||
const response = await client.search3({ query })
|
||||
|
||||
updateList({
|
||||
artists: response.data.artists.map(mapArtistID3toArtist),
|
||||
albums: response.data.albums.map(mapAlbumID3toAlbumListItem),
|
||||
songs: response.data.songs.map(a => mapChildToSong(a, client)),
|
||||
})
|
||||
setUpdating(false)
|
||||
try {
|
||||
const response = await client.search3({ query })
|
||||
updateList({
|
||||
artists: response.data.artists.map(mapArtistID3toArtist),
|
||||
albums: response.data.albums.map(mapAlbumID3toAlbumListItem),
|
||||
songs: response.data.songs.map(a => mapChildToSong(a, client)),
|
||||
})
|
||||
} finally {
|
||||
setUpdating(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,10 +158,13 @@ export const useUpdatePlaylists = () => {
|
||||
setUpdating(true)
|
||||
|
||||
const client = new SubsonicApiClient(server)
|
||||
const response = await client.getPlaylists()
|
||||
|
||||
updateList(response.data.playlists.map(mapPlaylistListItem))
|
||||
setUpdating(false)
|
||||
try {
|
||||
const response = await client.getPlaylists()
|
||||
updateList(response.data.playlists.map(mapPlaylistListItem))
|
||||
} finally {
|
||||
setUpdating(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -165,8 +176,13 @@ export const playlistAtomFamily = atomFamily((id: string) =>
|
||||
}
|
||||
|
||||
const client = new SubsonicApiClient(server)
|
||||
const response = await client.getPlaylist({ id })
|
||||
return mapPlaylistWithSongs(response.data.playlist, client)
|
||||
|
||||
try {
|
||||
const response = await client.getPlaylist({ id })
|
||||
return mapPlaylistWithSongs(response.data.playlist, client)
|
||||
} catch {
|
||||
return undefined
|
||||
}
|
||||
}),
|
||||
)
|
||||
|
||||
@@ -189,10 +205,13 @@ export const useUpdateAlbumList = () => {
|
||||
setUpdating(true)
|
||||
|
||||
const client = new SubsonicApiClient(server)
|
||||
const response = await client.getAlbumList2({ type: 'alphabeticalByArtist', size: 500 })
|
||||
|
||||
updateList(response.data.albums.map(mapAlbumID3toAlbumListItem))
|
||||
setUpdating(false)
|
||||
try {
|
||||
const response = await client.getAlbumList2({ type: 'alphabeticalByArtist', size: 500 })
|
||||
updateList(response.data.albums.map(mapAlbumID3toAlbumListItem))
|
||||
} finally {
|
||||
setUpdating(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -204,8 +223,13 @@ export const albumAtomFamily = atomFamily((id: string) =>
|
||||
}
|
||||
|
||||
const client = new SubsonicApiClient(server)
|
||||
const response = await client.getAlbum({ id })
|
||||
return mapAlbumID3WithSongstoAlbunWithSongs(response.data.album, response.data.songs, client)
|
||||
|
||||
try {
|
||||
const response = await client.getAlbum({ id })
|
||||
return mapAlbumID3WithSongstoAlbunWithSongs(response.data.album, response.data.songs, client)
|
||||
} catch {
|
||||
return undefined
|
||||
}
|
||||
}),
|
||||
)
|
||||
|
||||
@@ -217,13 +241,17 @@ export const artistInfoAtomFamily = atomFamily((id: string) =>
|
||||
}
|
||||
|
||||
const client = new SubsonicApiClient(server)
|
||||
const [artistResponse, artistInfoResponse] = await Promise.all([
|
||||
client.getArtist({ id }),
|
||||
client.getArtistInfo2({ id }),
|
||||
])
|
||||
const topSongsResponse = await client.getTopSongs({ artist: artistResponse.data.artist.name, count: 50 })
|
||||
|
||||
return mapArtistInfo(artistResponse.data, artistInfoResponse.data.artistInfo, topSongsResponse.data.songs, client)
|
||||
try {
|
||||
const [artistResponse, artistInfoResponse] = await Promise.all([
|
||||
client.getArtist({ id }),
|
||||
client.getArtistInfo2({ id }),
|
||||
])
|
||||
const topSongsResponse = await client.getTopSongs({ artist: artistResponse.data.artist.name, count: 50 })
|
||||
return mapArtistInfo(artistResponse.data, artistInfoResponse.data.artistInfo, topSongsResponse.data.songs, client)
|
||||
} catch {
|
||||
return undefined
|
||||
}
|
||||
}),
|
||||
)
|
||||
|
||||
|
||||
54
app/state/server.ts
Normal file
54
app/state/server.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { useAtom } from 'jotai'
|
||||
import { useAtomValue, useUpdateAtom } from 'jotai/utils'
|
||||
import { useEffect } from 'react'
|
||||
import { albumListAtom, artistsAtom, homeListsAtom, playlistsAtom, searchResultsAtom } from './music'
|
||||
import { activeServerAtom } from './settings'
|
||||
import { useReset } from './trackplayer'
|
||||
|
||||
export const useSetActiveServer = () => {
|
||||
const [activeServer, setActiveServer] = useAtom(activeServerAtom)
|
||||
const setArtists = useUpdateAtom(artistsAtom)
|
||||
const setHomeLists = useUpdateAtom(homeListsAtom)
|
||||
const setSearchResults = useUpdateAtom(searchResultsAtom)
|
||||
const setPlaylists = useUpdateAtom(playlistsAtom)
|
||||
const setAlbumLists = useUpdateAtom(albumListAtom)
|
||||
const resetPlayer = useReset()
|
||||
|
||||
return async (id: string) => {
|
||||
if (id === activeServer?.id) {
|
||||
return
|
||||
}
|
||||
|
||||
await resetPlayer()
|
||||
|
||||
setArtists([])
|
||||
setHomeLists({})
|
||||
setSearchResults({ artists: [], albums: [], songs: [] })
|
||||
setPlaylists([])
|
||||
setAlbumLists([])
|
||||
|
||||
setActiveServer(id)
|
||||
}
|
||||
}
|
||||
|
||||
export const useActiveListRefresh = (list: unknown[], update: () => void) => {
|
||||
const activeServer = useAtomValue(activeServerAtom)
|
||||
|
||||
useEffect(() => {
|
||||
if (list.length === 0) {
|
||||
update()
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [activeServer])
|
||||
}
|
||||
|
||||
export const useActiveServerRefresh = (update: () => void) => {
|
||||
const activeServer = useAtomValue(activeServerAtom)
|
||||
|
||||
useEffect(() => {
|
||||
if (activeServer) {
|
||||
update()
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [activeServer])
|
||||
}
|
||||
@@ -1,8 +1,6 @@
|
||||
import { atom } from 'jotai'
|
||||
import { AppSettings, Server } from '@app/models/settings'
|
||||
import atomWithAsyncStorage from '@app/storage/atomWithAsyncStorage'
|
||||
import { useEffect } from 'react'
|
||||
import { useAtomValue } from 'jotai/utils'
|
||||
import equal from 'fast-deep-equal'
|
||||
|
||||
export const appSettingsAtom = atomWithAsyncStorage<AppSettings>('@appSettings', {
|
||||
@@ -45,15 +43,4 @@ export const serversAtom = atom<Server[], Server[]>(
|
||||
},
|
||||
)
|
||||
|
||||
export const useActiveServerRefresh = (update: () => any) => {
|
||||
const activeServer = useAtomValue(activeServerAtom)
|
||||
|
||||
useEffect(() => {
|
||||
if (activeServer) {
|
||||
update()
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [activeServer])
|
||||
}
|
||||
|
||||
export const homeListTypesAtom = atom(get => get(appSettingsAtom).home.lists)
|
||||
|
||||
Reference in New Issue
Block a user