mirror of
https://github.com/austinried/subtracks.git
synced 2025-12-28 01:19:27 +01:00
87 lines
2.1 KiB
TypeScript
87 lines
2.1 KiB
TypeScript
import { selectMusic } from '@app/state/music'
|
|
import { Store, useStore } from '@app/state/store'
|
|
import { useCallback } from 'react'
|
|
|
|
export const useArtistInfo = (id: string) => {
|
|
const artistInfo = useStore(useCallback((state: Store) => state.artistInfo[id], [id]))
|
|
const fetchArtistInfo = useStore(selectMusic.fetchArtistInfo)
|
|
|
|
if (!artistInfo) {
|
|
fetchArtistInfo(id)
|
|
return undefined
|
|
}
|
|
|
|
return artistInfo
|
|
}
|
|
|
|
export const useAlbumWithSongs = (id: string) => {
|
|
const album = useStore(useCallback((state: Store) => state.albumsWithSongs[id], [id]))
|
|
const fetchAlbum = useStore(selectMusic.fetchAlbumWithSongs)
|
|
|
|
if (!album) {
|
|
fetchAlbum(id)
|
|
}
|
|
|
|
return album
|
|
}
|
|
|
|
export const usePlaylistWithSongs = (id: string) => {
|
|
const playlist = useStore(useCallback((state: Store) => state.playlistsWithSongs[id], [id]))
|
|
const fetchPlaylist = useStore(selectMusic.fetchPlaylistWithSongs)
|
|
|
|
if (!playlist) {
|
|
fetchPlaylist(id)
|
|
}
|
|
|
|
return playlist
|
|
}
|
|
|
|
export const useStarred = (id: string, type: string) => {
|
|
return useStore(
|
|
useCallback(
|
|
(state: Store) => {
|
|
switch (type) {
|
|
case 'song':
|
|
return state.starredSongs[id]
|
|
case 'album':
|
|
return state.starredAlbums[id]
|
|
case 'artist':
|
|
return state.starredArtists[id]
|
|
default:
|
|
return false
|
|
}
|
|
},
|
|
[type, id],
|
|
),
|
|
)
|
|
}
|
|
|
|
export const useCoverArtFile = (coverArt: string = '-1') => {
|
|
const file = useStore(useCallback((state: Store) => state.cachedCoverArt[coverArt], [coverArt]))
|
|
const cacheCoverArt = useStore(selectMusic.cacheCoverArt)
|
|
|
|
if (!file) {
|
|
cacheCoverArt(coverArt)
|
|
return undefined
|
|
}
|
|
|
|
return file
|
|
}
|
|
|
|
export const useArtistCoverArtFile = (artistId: string) => {
|
|
const artistInfo = useArtistInfo(artistId)
|
|
const file = useStore(useCallback((state: Store) => state.cachedArtistArt[artistId], [artistId]))
|
|
const cacheArtistArt = useStore(selectMusic.cacheArtistArt)
|
|
|
|
if (!artistInfo) {
|
|
return undefined
|
|
}
|
|
|
|
if (!file) {
|
|
cacheArtistArt(artistId, artistInfo.largeImageUrl)
|
|
return undefined
|
|
}
|
|
|
|
return file
|
|
}
|