57 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-05-12 02:45:43 +08:00
import { fetchAlbum } from '@/web/api/album'
import reactQueryClient from '@/web/utils/reactQueryClient'
2022-04-16 21:14:03 +08:00
import { IpcChannels } from '@/shared/IpcChannels'
import { APIs } from '@/shared/CacheAPIs'
import {
FetchAlbumParams,
AlbumApiNames,
FetchAlbumResponse,
} from '@/shared/api/Album'
2022-05-12 02:45:43 +08:00
import { useQuery } from 'react-query'
2022-03-13 14:40:38 +08:00
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.FetchAlbum, params.id],
2022-03-13 14:40:38 +08:00
() => 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 =>
2022-04-09 00:28:37 +08:00
window.ipcRenderer?.sendSync(IpcChannels.GetApiCacheSync, {
2022-04-16 21:14:03 +08:00
api: APIs.Album,
2022-03-30 00:53:05 +08:00
query: {
id: params.id,
},
}),
2022-03-13 14:40:38 +08:00
}
)
}
2022-04-02 18:46:08 +08:00
export function fetchAlbumWithReactQuery(params: FetchAlbumParams) {
return reactQueryClient.fetchQuery(
[AlbumApiNames.FetchAlbum, params.id],
2022-04-02 18:46:08 +08:00
() => fetch(params),
{
staleTime: Infinity,
}
)
}
2022-03-13 14:40:38 +08:00
export async function prefetchAlbum(params: FetchAlbumParams) {
await reactQueryClient.prefetchQuery(
[AlbumApiNames.FetchAlbum, params.id],
2022-03-13 14:40:38 +08:00
() => fetch(params),
{
staleTime: Infinity,
}
)
}