49 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-05-12 02:45:43 +08:00
import { fetchLyric } from '@/web/api/track'
import reactQueryClient from '@/web/utils/reactQueryClient'
2022-10-28 20:29:04 +08:00
import { FetchLyricParams, TrackApiNames } from '@/shared/api/Track'
2022-04-16 21:14:03 +08:00
import { APIs } from '@/shared/CacheAPIs'
import { IpcChannels } from '@/shared/IpcChannels'
2022-08-03 23:48:39 +08:00
import { useQuery } from '@tanstack/react-query'
2022-04-04 17:51:07 +08:00
export default function useLyric(params: FetchLyricParams) {
2022-10-28 20:29:04 +08:00
const key = [TrackApiNames.FetchLyric, params]
2022-04-04 17:51:07 +08:00
return useQuery(
2022-10-28 20:29:04 +08:00
key,
async () => {
// fetch from cache as initial data
const cache = window.ipcRenderer?.invoke(IpcChannels.GetApiCache, {
api: APIs.Lyric,
query: {
id: params.id,
},
})
if (cache) return cache
2022-04-04 17:51:07 +08:00
return fetchLyric(params)
},
{
enabled: !!params.id && params.id !== 0,
refetchInterval: false,
2022-06-14 23:23:34 +08:00
refetchOnWindowFocus: false,
2022-04-04 17:51:07 +08:00
staleTime: Infinity,
}
)
}
2022-10-28 20:29:04 +08:00
export function fetchLyricWithReactQuery(params: FetchLyricParams) {
2022-04-04 17:51:07 +08:00
return reactQueryClient.fetchQuery(
[TrackApiNames.FetchLyric, params],
2022-04-04 17:51:07 +08:00
() => {
return fetchLyric(params)
},
{
retry: 4,
retryDelay: (retryCount: number) => {
return retryCount * 500
},
staleTime: Infinity,
}
)
}