41 lines
1.1 KiB
TypeScript
Raw Normal View History

2022-03-13 14:40:38 +08:00
import { fetchPlaylist } from '@/api/playlist'
import { PlaylistApiNames } from '@/api/playlist'
2022-03-19 17:03:29 +08:00
import type { FetchPlaylistParams, FetchPlaylistResponse } from '@/api/playlist'
2022-03-13 14:40:38 +08:00
import reactQueryClient from '@/utils/reactQueryClient'
const fetch = (params: FetchPlaylistParams, noCache?: boolean) => {
return fetchPlaylist(params, !!noCache)
}
export default function usePlaylist(
params: FetchPlaylistParams,
noCache?: boolean
) {
return useQuery(
[PlaylistApiNames.FETCH_PLAYLIST, params],
() => fetch(params, noCache),
{
enabled: !!(params.id && params.id > 0 && !isNaN(Number(params.id))),
2022-03-19 17:03:29 +08:00
staleTime: 60 * 60 * 1000, // 1 hour
placeholderData: (): FetchPlaylistResponse | undefined =>
window.ipcRenderer.sendSync('getApiCacheSync', {
api: 'playlist/detail',
query: {
id: params.id,
},
}),
2022-03-13 14:40:38 +08:00
}
)
}
export async function prefetchPlaylist(params: FetchPlaylistParams) {
console.log('prefetchAlbum', params)
await reactQueryClient.prefetchQuery(
[PlaylistApiNames.FETCH_PLAYLIST, params],
() => fetch(params),
{
staleTime: 3600000,
}
)
}