feishin/src/renderer/features/lyrics/components/lyrics-search-form.tsx

155 lines
3.8 KiB
TypeScript
Raw Normal View History

2023-06-08 03:40:58 -07:00
import { useMemo } from 'react';
import { Divider, Group, Stack } from '@mantine/core';
2023-06-08 03:40:58 -07:00
import { useForm } from '@mantine/form';
import { useDebouncedValue } from '@mantine/hooks';
import { openModal } from '@mantine/modals';
import styled from 'styled-components';
import {
InternetProviderLyricSearchResponse,
LyricSource,
LyricsOverride,
} from '../../../api/types';
2023-06-08 03:40:58 -07:00
import { useLyricSearch } from '../queries/lyric-search-query';
import { Badge, ScrollArea, Spinner, Text, TextInput } from '/@/renderer/components';
const SearchItem = styled.button`
all: unset;
box-sizing: border-box !important;
padding: 0.5rem;
border-radius: 5px;
cursor: pointer;
2023-06-08 03:40:58 -07:00
&:hover,
&:focus-visible {
color: var(--btn-default-fg-hover);
background: var(--btn-default-bg-hover);
}
`;
interface SearchResultProps {
artist?: string;
name?: string;
onClick?: () => void;
2023-06-08 03:40:58 -07:00
source?: string;
}
const SearchResult = ({ name, artist, source, onClick }: SearchResultProps) => {
2023-06-08 03:40:58 -07:00
return (
<SearchItem onClick={onClick}>
2023-06-08 03:40:58 -07:00
<Group
noWrap
position="apart"
>
<Stack
maw="65%"
spacing={0}
>
<Text
size="md"
weight={600}
>
{name}
</Text>
2023-06-08 03:40:58 -07:00
<Text $secondary>{artist}</Text>
</Stack>
<Badge size="lg">{source}</Badge>
</Group>
</SearchItem>
);
};
interface LyricSearchFormProps {
artist?: string;
name?: string;
onSearchOverride?: (params: LyricsOverride) => void;
2023-06-08 03:40:58 -07:00
}
export const LyricsSearchForm = ({ artist, name, onSearchOverride }: LyricSearchFormProps) => {
2023-06-08 03:40:58 -07:00
const form = useForm({
initialValues: {
artist: artist || '',
name: name || '',
},
});
const [debouncedArtist] = useDebouncedValue(form.values.artist, 500);
const [debouncedName] = useDebouncedValue(form.values.name, 500);
2023-06-09 04:08:33 -07:00
const { data, isInitialLoading } = useLyricSearch({
2023-06-08 03:40:58 -07:00
query: { artist: debouncedArtist, name: debouncedName },
});
const searchResults = useMemo(() => {
if (!data) return [];
const results: InternetProviderLyricSearchResponse[] = [];
Object.keys(data).forEach((key) => {
(data[key as keyof typeof data] || []).forEach((result) => results.push(result));
});
return results;
}, [data]);
return (
2023-06-09 04:08:33 -07:00
<Stack h={400}>
2023-06-08 03:40:58 -07:00
<form>
<Group grow>
<TextInput
data-autofocus
required
label="Name"
{...form.getInputProps('name')}
/>
<TextInput
required
label="Artist"
{...form.getInputProps('artist')}
/>
</Group>
</form>
<Divider />
2023-06-09 04:08:33 -07:00
{isInitialLoading ? (
2023-06-08 03:40:58 -07:00
<Spinner container />
) : (
<ScrollArea
offsetScrollbars
pr="1rem"
>
<Stack spacing="md">
{searchResults.map((result) => (
<SearchResult
key={`${result.source}-${result.id}`}
artist={result.artist}
name={result.name}
source={result.source}
onClick={() => {
onSearchOverride?.({
artist: result.artist,
id: result.id,
name: result.name,
remote: true,
source: result.source as LyricSource,
});
}}
2023-06-08 03:40:58 -07:00
/>
))}
</Stack>
</ScrollArea>
)}
</Stack>
);
};
export const openLyricSearchModal = ({ artist, name, onSearchOverride }: LyricSearchFormProps) => {
2023-06-08 03:40:58 -07:00
openModal({
children: (
<LyricsSearchForm
artist={artist}
name={name}
onSearchOverride={onSearchOverride}
2023-06-08 03:40:58 -07:00
/>
),
size: 'lg',
title: 'Search for lyrics',
});
};