42 lines
1.1 KiB
TypeScript
Raw Normal View History

2022-04-09 00:28:37 +08:00
import { TrackApiNames, fetchLyric } from '@/renderer/api/track'
import type { FetchLyricParams, FetchLyricResponse } from '@/renderer/api/track'
import reactQueryClient from '@/renderer/utils/reactQueryClient'
import { IpcChannels } from '@/main/IpcChannelsName'
2022-04-04 17:51:07 +08:00
export default function useLyric(params: FetchLyricParams) {
return useQuery(
[TrackApiNames.FETCH_LYRIC, params],
() => {
return fetchLyric(params)
},
{
enabled: !!params.id && params.id !== 0,
refetchInterval: false,
staleTime: Infinity,
initialData: (): FetchLyricResponse | undefined =>
2022-04-09 00:28:37 +08:00
window.ipcRenderer?.sendSync(IpcChannels.GetApiCacheSync, {
2022-04-04 17:51:07 +08:00
api: 'lyric',
query: {
id: params.id,
},
}),
}
)
}
export function fetchTracksWithReactQuery(params: FetchLyricParams) {
return reactQueryClient.fetchQuery(
[TrackApiNames.FETCH_LYRIC, params],
() => {
return fetchLyric(params)
},
{
retry: 4,
retryDelay: (retryCount: number) => {
return retryCount * 500
},
staleTime: Infinity,
}
)
}