feishin/src/renderer/features/lyrics/queries/lyric-search-query.ts

30 lines
1 KiB
TypeScript
Raw Normal View History

2023-06-08 03:40:34 -07:00
import { useQuery } from '@tanstack/react-query';
import isElectron from 'is-electron';
import { queryKeys } from '/@/renderer/api/query-keys';
import {
2023-07-01 19:10:05 -07:00
InternetProviderLyricSearchResponse,
LyricSearchQuery,
LyricSource,
} from '/@/renderer/api/types';
2023-06-08 03:40:34 -07:00
import { QueryHookArgs } from '/@/renderer/lib/react-query';
const lyricsIpc = isElectron() ? window.electron.lyrics : null;
export const useLyricSearch = (args: Omit<QueryHookArgs<LyricSearchQuery>, 'serverId'>) => {
2023-07-01 19:10:05 -07:00
const { options, query } = args;
2023-06-08 03:40:34 -07:00
2023-07-01 19:10:05 -07:00
return useQuery<Record<LyricSource, InternetProviderLyricSearchResponse[]>>({
cacheTime: 1000 * 60 * 1,
enabled: !!query.artist || !!query.name,
queryFn: () => {
if (lyricsIpc) {
return lyricsIpc.searchRemoteLyrics(query);
}
return {} as Record<LyricSource, InternetProviderLyricSearchResponse[]>;
},
2023-07-01 19:10:05 -07:00
queryKey: queryKeys.songs.lyricsSearch(query),
staleTime: 1000 * 60 * 1,
...options,
});
2023-06-08 03:40:34 -07:00
};