2022-03-13 14:40:38 +08:00
|
|
|
import { fetchAlbum } from '@/api/album'
|
|
|
|
import { AlbumApiNames } from '@/api/album'
|
2022-03-19 17:03:29 +08:00
|
|
|
import type { FetchAlbumParams, FetchAlbumResponse } from '@/api/album'
|
2022-03-13 14:40:38 +08:00
|
|
|
import reactQueryClient from '@/utils/reactQueryClient'
|
|
|
|
|
|
|
|
const fetch = async (params: FetchAlbumParams, noCache?: boolean) => {
|
|
|
|
const album = await fetchAlbum(params, !!noCache)
|
|
|
|
if (album?.album?.songs) {
|
|
|
|
album.album.songs = album.songs
|
|
|
|
}
|
|
|
|
return album
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function useAlbum(params: FetchAlbumParams, noCache?: boolean) {
|
|
|
|
return useQuery(
|
|
|
|
[AlbumApiNames.FETCH_ALBUM, params.id],
|
|
|
|
() => fetch(params, noCache),
|
|
|
|
{
|
|
|
|
enabled: !!params.id,
|
2022-03-19 17:03:29 +08:00
|
|
|
staleTime: 24 * 60 * 60 * 1000, // 24 hours
|
2022-03-30 00:53:05 +08:00
|
|
|
placeholderData: (): FetchAlbumResponse =>
|
|
|
|
window.ipcRenderer?.sendSync('getApiCacheSync', {
|
|
|
|
api: 'album',
|
|
|
|
query: {
|
|
|
|
id: params.id,
|
|
|
|
},
|
|
|
|
}),
|
2022-03-13 14:40:38 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function prefetchAlbum(params: FetchAlbumParams) {
|
|
|
|
await reactQueryClient.prefetchQuery(
|
|
|
|
[AlbumApiNames.FETCH_ALBUM, params.id],
|
|
|
|
() => fetch(params),
|
|
|
|
{
|
|
|
|
staleTime: Infinity,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|