YesPlayMusic/packages/web/api/hooks/useArtistAlbums.ts

36 lines
1.1 KiB
TypeScript
Raw Normal View History

2022-05-12 02:45:43 +08:00
import { fetchArtistAlbums } from '@/web/api/artist'
2022-04-16 21:14:03 +08:00
import { IpcChannels } from '@/shared/IpcChannels'
import { APIs } from '@/shared/CacheAPIs'
2022-10-28 20:29:04 +08:00
import { FetchArtistAlbumsParams, ArtistApiNames } from '@/shared/api/Artist'
2022-08-03 23:48:39 +08:00
import { useQuery } from '@tanstack/react-query'
2022-10-28 20:29:04 +08:00
import reactQueryClient from '@/web/utils/reactQueryClient'
2022-03-13 14:40:38 +08:00
2022-08-03 23:48:39 +08:00
export default function useArtistAlbums(params: FetchArtistAlbumsParams) {
2022-10-28 20:29:04 +08:00
const key = [ArtistApiNames.FetchArtistAlbums, params]
2022-03-13 14:40:38 +08:00
return useQuery(
2022-10-28 20:29:04 +08:00
key,
2022-03-13 14:40:38 +08:00
async () => {
2022-10-28 20:29:04 +08:00
// fetch from cache as placeholder
window.ipcRenderer
?.invoke(IpcChannels.GetApiCache, {
2022-04-16 21:14:03 +08:00
api: APIs.ArtistAlbum,
2022-03-30 00:53:05 +08:00
query: {
id: params.id,
},
2022-10-28 20:29:04 +08:00
})
.then(cache => {
const existsQueryData = reactQueryClient.getQueryData(key)
if (!existsQueryData && cache) {
reactQueryClient.setQueryData(key, cache)
}
})
return fetchArtistAlbums(params)
},
{
enabled: !!params.id && params.id !== 0,
staleTime: 3600000,
2022-03-13 14:40:38 +08:00
}
)
}