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';
|
2023-06-09 02:38:04 -07:00
|
|
|
import {
|
2023-07-01 19:10:05 -07:00
|
|
|
InternetProviderLyricSearchResponse,
|
|
|
|
|
LyricSearchQuery,
|
|
|
|
|
LyricSource,
|
2023-06-09 02:38:04 -07:00
|
|
|
} 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,
|
2023-07-23 12:23:18 +00:00
|
|
|
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
|
|
|
};
|