YesPlayMusic/src/renderer/hooks/usePlaylist.ts

55 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-04-09 00:28:37 +08:00
import { fetchPlaylist } from '@/renderer/api/playlist'
2022-04-16 21:14:03 +08:00
import reactQueryClient from '@/renderer/utils/reactQueryClient'
import { IpcChannels } from '@/shared/IpcChannels'
import { APIs } from '@/shared/CacheAPIs'
import {
2022-04-09 00:28:37 +08:00
FetchPlaylistParams,
2022-04-16 21:14:03 +08:00
PlaylistApiNames,
2022-04-09 00:28:37 +08:00
FetchPlaylistResponse,
2022-04-16 21:14:03 +08:00
} from '@/shared/api/Playlists'
2022-03-13 14:40:38 +08:00
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 =>
2022-04-09 00:28:37 +08:00
window.ipcRenderer?.sendSync(IpcChannels.GetApiCacheSync, {
2022-04-16 21:14:03 +08:00
api: APIs.Playlist,
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 fetchPlaylistWithReactQuery(params: FetchPlaylistParams) {
return reactQueryClient.fetchQuery(
[PlaylistApiNames.FETCH_PLAYLIST, params],
() => fetch(params),
{
staleTime: 3600000,
}
)
}
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,
}
)
}