reimpl all music state into zustand

This commit is contained in:
austinried
2021-08-03 15:37:07 +09:00
parent 300d0bd1b9
commit 33dc0be02b
11 changed files with 325 additions and 294 deletions

View File

@@ -1,38 +1,13 @@
import {
mapAlbumID3toAlbumListItem,
mapArtistID3toArtist,
mapChildToSong,
mapPlaylistListItem,
} from '@app/models/music'
import {
albumListAtom,
albumListUpdatingAtom,
artistsAtom,
artistsUpdatingAtom,
homeListsUpdatingAtom,
homeListsWriteAtom,
playlistsAtom,
playlistsUpdatingAtom,
searchResultsAtom,
searchResultsUpdatingAtom,
} from '@app/state/music'
import { selectMusic } from '@app/state/music'
import { selectSettings } from '@app/state/settings'
import { Store, useStore } from '@app/state/store'
import { SubsonicApiClient } from '@app/subsonic/api'
import { GetAlbumList2Type, GetCoverArtParams } from '@app/subsonic/params'
import { useAtom } from 'jotai'
import { useUpdateAtom } from 'jotai/utils'
import { GetCoverArtParams } from '@app/subsonic/params'
import { useCallback } from 'react'
const selectors = {
fetchArtistInfo: (state: Store) => state.fetchArtistInfo,
fetchAlbum: (state: Store) => state.fetchAlbum,
fetchPlaylist: (state: Store) => state.fetchPlaylist,
}
export const useArtistInfo = (id: string) => {
const artistInfo = useStore(useCallback((state: Store) => state.artistInfo[id], [id]))
const fetchArtistInfo = useStore(selectors.fetchArtistInfo)
const fetchArtistInfo = useStore(selectMusic.fetchArtistInfo)
if (!artistInfo) {
fetchArtistInfo(id)
@@ -42,8 +17,8 @@ export const useArtistInfo = (id: string) => {
}
export const useAlbumWithSongs = (id: string) => {
const album = useStore(useCallback((state: Store) => state.albums[id], [id]))
const fetchAlbum = useStore(selectors.fetchAlbum)
const album = useStore(useCallback((state: Store) => state.albumsWithSongs[id], [id]))
const fetchAlbum = useStore(selectMusic.fetchAlbumWithSongs)
if (!album) {
fetchAlbum(id)
@@ -53,8 +28,8 @@ export const useAlbumWithSongs = (id: string) => {
}
export const usePlaylistWithSongs = (id: string) => {
const playlist = useStore(useCallback((state: Store) => state.playlists[id], [id]))
const fetchPlaylist = useStore(selectors.fetchPlaylist)
const playlist = useStore(useCallback((state: Store) => state.playlistsWithSongs[id], [id]))
const fetchPlaylist = useStore(selectMusic.fetchPlaylistWithSongs)
if (!playlist) {
fetchPlaylist(id)
@@ -63,148 +38,6 @@ export const usePlaylistWithSongs = (id: string) => {
return playlist
}
export const useUpdateArtists = () => {
const server = useStore(selectSettings.activeServer)
const [updating, setUpdating] = useAtom(artistsUpdatingAtom)
const setArtists = useUpdateAtom(artistsAtom)
if (!server) {
return () => Promise.resolve()
}
return async () => {
if (updating) {
return
}
setUpdating(true)
const client = new SubsonicApiClient(server)
try {
const response = await client.getArtists()
setArtists(response.data.artists.map(mapArtistID3toArtist))
} finally {
setUpdating(false)
}
}
}
export const useUpdateHomeLists = () => {
const server = useStore(selectSettings.activeServer)
const types = useStore(selectSettings.homeLists)
const updateHomeList = useUpdateAtom(homeListsWriteAtom)
const [updating, setUpdating] = useAtom(homeListsUpdatingAtom)
if (!server) {
return async () => {}
}
return async () => {
if (updating) {
return
}
setUpdating(true)
const client = new SubsonicApiClient(server)
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)
}
}
}
export const useUpdateSearchResults = () => {
const server = useStore(selectSettings.activeServer)
const updateList = useUpdateAtom(searchResultsAtom)
const [updating, setUpdating] = useAtom(searchResultsUpdatingAtom)
if (!server) {
return async () => {}
}
return async (query: string) => {
if (updating || query.length < 2) {
return
}
setUpdating(true)
const client = new SubsonicApiClient(server)
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)
}
}
}
export const useUpdatePlaylists = () => {
const server = useStore(selectSettings.activeServer)
const updateList = useUpdateAtom(playlistsAtom)
const [updating, setUpdating] = useAtom(playlistsUpdatingAtom)
if (!server) {
return async () => {}
}
return async () => {
if (updating) {
return
}
setUpdating(true)
const client = new SubsonicApiClient(server)
try {
const response = await client.getPlaylists()
updateList(response.data.playlists.map(mapPlaylistListItem))
} finally {
setUpdating(false)
}
}
}
export const useUpdateAlbumList = () => {
const server = useStore(selectSettings.activeServer)
const updateList = useUpdateAtom(albumListAtom)
const [updating, setUpdating] = useAtom(albumListUpdatingAtom)
if (!server) {
return async () => {}
}
return async () => {
if (updating) {
return
}
setUpdating(true)
const client = new SubsonicApiClient(server)
try {
const response = await client.getAlbumList2({ type: 'alphabeticalByArtist', size: 500 })
updateList(response.data.albums.map(mapAlbumID3toAlbumListItem))
} finally {
setUpdating(false)
}
}
}
export const useCoverArtUri = () => {
const server = useStore(selectSettings.activeServer)

View File

@@ -1,18 +1,11 @@
import { albumListAtom, artistsAtom, homeListsAtom, playlistsAtom, searchResultsAtom } from '@app/state/music'
import { selectSettings } from '@app/state/settings'
import { useStore } from '@app/state/store'
import { useReset } from '@app/state/trackplayer'
import { useUpdateAtom } from 'jotai/utils'
import { useEffect } from 'react'
export const useSwitchActiveServer = () => {
const activeServer = useStore(selectSettings.activeServer)
const setActiveServer = useStore(selectSettings.setActiveServer)
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) => {
@@ -21,13 +14,6 @@ export const useSwitchActiveServer = () => {
}
await resetPlayer()
setArtists([])
setHomeLists({})
setSearchResults({ artists: [], albums: [], songs: [] })
setPlaylists([])
setAlbumLists([])
setActiveServer(id)
}
}
@@ -43,6 +29,14 @@ export const useActiveListRefresh = (list: unknown[], update: () => void) => {
}, [activeServer])
}
export const useActiveListRefresh2 = (update: () => void) => {
const activeServer = useStore(selectSettings.activeServer)
useEffect(() => {
update()
}, [activeServer, update])
}
export const useActiveServerRefresh = (update: () => void) => {
const activeServer = useStore(selectSettings.activeServer)