Require only single query for search
This commit is contained in:
parent
e56350c1c2
commit
255a131f3b
5 changed files with 22 additions and 9 deletions
|
@ -64,11 +64,18 @@ export async function getSearchResults(
|
||||||
params: LyricSearchQuery,
|
params: LyricSearchQuery,
|
||||||
): Promise<InternetProviderLyricSearchResponse[] | null> {
|
): Promise<InternetProviderLyricSearchResponse[] | null> {
|
||||||
let result: AxiosResponse<GeniusSearchResponse>;
|
let result: AxiosResponse<GeniusSearchResponse>;
|
||||||
|
|
||||||
|
const searchQuery = [params.artist, params.name].join(' ');
|
||||||
|
|
||||||
|
if (!searchQuery) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
result = await axios.get(SEARCH_URL, {
|
result = await axios.get(SEARCH_URL, {
|
||||||
params: {
|
params: {
|
||||||
per_page: '5',
|
per_page: '5',
|
||||||
q: `${params.artist} ${params.name}`,
|
q: searchQuery,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
|
@ -21,12 +21,19 @@ export async function getSearchResults(
|
||||||
params: LyricSearchQuery,
|
params: LyricSearchQuery,
|
||||||
): Promise<InternetProviderLyricSearchResponse[] | null> {
|
): Promise<InternetProviderLyricSearchResponse[] | null> {
|
||||||
let result: AxiosResponse<any, any>;
|
let result: AxiosResponse<any, any>;
|
||||||
|
|
||||||
|
const searchQuery = [params.artist, params.name].join(' ');
|
||||||
|
|
||||||
|
if (!searchQuery) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
result = await axios.get(SEARCH_URL, {
|
result = await axios.get(SEARCH_URL, {
|
||||||
params: {
|
params: {
|
||||||
limit: 5,
|
limit: 5,
|
||||||
offset: 0,
|
offset: 0,
|
||||||
s: `${params.artist} ${params.name}`,
|
s: searchQuery,
|
||||||
type: '1',
|
type: '1',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
|
@ -1062,8 +1062,8 @@ export const instanceOfCancellationError = (error: any) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
export type LyricSearchQuery = {
|
export type LyricSearchQuery = {
|
||||||
artist: string;
|
artist?: string;
|
||||||
name: string;
|
name?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type LyricGetQuery = {
|
export type LyricGetQuery = {
|
||||||
|
|
|
@ -74,8 +74,7 @@ export const LyricsSearchForm = ({ artist, name, onSearchOverride }: LyricSearch
|
||||||
const [debouncedArtist] = useDebouncedValue(form.values.artist, 500);
|
const [debouncedArtist] = useDebouncedValue(form.values.artist, 500);
|
||||||
const [debouncedName] = useDebouncedValue(form.values.name, 500);
|
const [debouncedName] = useDebouncedValue(form.values.name, 500);
|
||||||
|
|
||||||
const { data, isLoading } = useLyricSearch({
|
const { data, isInitialLoading } = useLyricSearch({
|
||||||
options: { enabled: Boolean(form.values.artist && form.values.name) },
|
|
||||||
query: { artist: debouncedArtist, name: debouncedName },
|
query: { artist: debouncedArtist, name: debouncedName },
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -91,7 +90,7 @@ export const LyricsSearchForm = ({ artist, name, onSearchOverride }: LyricSearch
|
||||||
}, [data]);
|
}, [data]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Stack>
|
<Stack h={400}>
|
||||||
<form>
|
<form>
|
||||||
<Group grow>
|
<Group grow>
|
||||||
<TextInput
|
<TextInput
|
||||||
|
@ -108,12 +107,11 @@ export const LyricsSearchForm = ({ artist, name, onSearchOverride }: LyricSearch
|
||||||
</Group>
|
</Group>
|
||||||
</form>
|
</form>
|
||||||
<Divider />
|
<Divider />
|
||||||
{isLoading ? (
|
{isInitialLoading ? (
|
||||||
<Spinner container />
|
<Spinner container />
|
||||||
) : (
|
) : (
|
||||||
<ScrollArea
|
<ScrollArea
|
||||||
offsetScrollbars
|
offsetScrollbars
|
||||||
h={350}
|
|
||||||
pr="1rem"
|
pr="1rem"
|
||||||
>
|
>
|
||||||
<Stack spacing="md">
|
<Stack spacing="md">
|
||||||
|
|
|
@ -15,6 +15,7 @@ export const useLyricSearch = (args: Omit<QueryHookArgs<LyricSearchQuery>, 'serv
|
||||||
|
|
||||||
return useQuery<Record<LyricSource, InternetProviderLyricSearchResponse[]>>({
|
return useQuery<Record<LyricSource, InternetProviderLyricSearchResponse[]>>({
|
||||||
cacheTime: 1000 * 60 * 1,
|
cacheTime: 1000 * 60 * 1,
|
||||||
|
enabled: !!query.artist || !!query.name,
|
||||||
queryFn: () => lyricsIpc?.searchRemoteLyrics(query),
|
queryFn: () => lyricsIpc?.searchRemoteLyrics(query),
|
||||||
queryKey: queryKeys.songs.lyricsSearch(query),
|
queryKey: queryKeys.songs.lyricsSearch(query),
|
||||||
staleTime: 1000 * 60 * 1,
|
staleTime: 1000 * 60 * 1,
|
||||||
|
|
Reference in a new issue