YesPlayMusic/src/renderer/hooks/usePlaylist.ts

40 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-21 02:03:25 +08:00
refetchOnWindowFocus: true,
2022-03-30 00:53:05 +08:00
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) {
await reactQueryClient.prefetchQuery(
[PlaylistApiNames.FETCH_PLAYLIST, params],
() => fetch(params),
{
staleTime: 3600000,
}
)
}