2022-05-12 02:45:43 +08:00
|
|
|
import { fetchPlaylist } from '@/web/api/playlist'
|
|
|
|
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 {
|
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-05-12 02:45:43 +08:00
|
|
|
import { useQuery } from 'react-query'
|
2022-03-13 14:40:38 +08:00
|
|
|
|
|
|
|
const fetch = (params: FetchPlaylistParams, noCache?: boolean) => {
|
|
|
|
return fetchPlaylist(params, !!noCache)
|
|
|
|
}
|
|
|
|
|
2022-06-06 01:00:25 +08:00
|
|
|
export const fetchFromCache = (id: number): FetchPlaylistResponse | undefined =>
|
|
|
|
window.ipcRenderer?.sendSync(IpcChannels.GetApiCacheSync, {
|
|
|
|
api: APIs.Playlist,
|
|
|
|
query: { id },
|
|
|
|
})
|
|
|
|
|
2022-03-13 14:40:38 +08:00
|
|
|
export default function usePlaylist(
|
|
|
|
params: FetchPlaylistParams,
|
|
|
|
noCache?: boolean
|
|
|
|
) {
|
|
|
|
return useQuery(
|
2022-04-16 22:39:51 +08:00
|
|
|
[PlaylistApiNames.FetchPlaylist, params],
|
2022-03-13 14:40:38 +08:00
|
|
|
() => fetch(params, noCache),
|
|
|
|
{
|
|
|
|
enabled: !!(params.id && params.id > 0 && !isNaN(Number(params.id))),
|
2022-03-21 02:03:25 +08:00
|
|
|
refetchOnWindowFocus: true,
|
2022-06-06 01:00:25 +08:00
|
|
|
placeholderData: () => fetchFromCache(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(
|
2022-04-16 22:39:51 +08:00
|
|
|
[PlaylistApiNames.FetchPlaylist, params],
|
2022-04-02 18:46:08 +08:00
|
|
|
() => fetch(params),
|
|
|
|
{
|
|
|
|
staleTime: 3600000,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-03-13 14:40:38 +08:00
|
|
|
export async function prefetchPlaylist(params: FetchPlaylistParams) {
|
2022-06-06 01:00:25 +08:00
|
|
|
if (fetchFromCache(params.id)) return
|
2022-03-13 14:40:38 +08:00
|
|
|
await reactQueryClient.prefetchQuery(
|
2022-04-16 22:39:51 +08:00
|
|
|
[PlaylistApiNames.FetchPlaylist, params],
|
2022-03-13 14:40:38 +08:00
|
|
|
() => fetch(params),
|
|
|
|
{
|
|
|
|
staleTime: 3600000,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|