feishin/src/renderer/features/search/components/command-palette.tsx

279 lines
12 KiB
TypeScript
Raw Normal View History

2023-05-18 02:10:34 -07:00
/* eslint-disable react/no-unknown-property */
2023-05-21 21:01:23 -07:00
import { useCallback, useState, Fragment, useRef } from 'react';
import { ActionIcon, Group, Kbd, ScrollArea } from '@mantine/core';
2023-05-19 00:21:36 -07:00
import { useDisclosure, useDebouncedValue } from '@mantine/hooks';
2023-05-21 21:01:23 -07:00
import { RiSearchLine, RiCloseFill } from 'react-icons/ri';
2023-05-19 00:21:36 -07:00
import { generatePath, useNavigate } from 'react-router';
2023-05-18 02:10:34 -07:00
import styled from 'styled-components';
import { GoToCommands } from './go-to-commands';
import { Command, CommandPalettePages } from '/@/renderer/features/search/components/command';
2023-05-21 21:01:23 -07:00
import { Button, Modal, Paper, Spinner, TextInput } from '/@/renderer/components';
2023-05-18 02:10:34 -07:00
import { HomeCommands } from './home-commands';
2023-05-19 00:21:36 -07:00
import { ServerCommands } from '/@/renderer/features/search/components/server-commands';
import { useSearch } from '/@/renderer/features/search/queries/search-query';
import { useCurrentServer } from '/@/renderer/store';
import { AppRoute } from '/@/renderer/router/routes';
import { LibraryCommandItem } from '/@/renderer/features/search/components/library-command-item';
import { LibraryItem } from '/@/renderer/api/types';
import { usePlayQueueAdd } from '/@/renderer/features/player';
2023-05-18 02:10:34 -07:00
interface CommandPaletteProps {
2023-07-01 19:10:05 -07:00
modalProps: typeof useDisclosure['arguments'];
2023-05-18 02:10:34 -07:00
}
const CustomModal = styled(Modal)`
2023-07-01 19:10:05 -07:00
& .mantine-Modal-header {
display: none;
}
2023-05-18 02:10:34 -07:00
`;
export const CommandPalette = ({ modalProps }: CommandPaletteProps) => {
2023-07-01 19:10:05 -07:00
const navigate = useNavigate();
const server = useCurrentServer();
const [value, setValue] = useState('');
const [query, setQuery] = useState('');
const [debouncedQuery] = useDebouncedValue(query, 400);
const [pages, setPages] = useState<CommandPalettePages[]>([CommandPalettePages.HOME]);
const activePage = pages[pages.length - 1];
const isHome = activePage === CommandPalettePages.HOME;
const searchInputRef = useRef<HTMLInputElement>(null);
2023-05-18 02:10:34 -07:00
2023-07-01 19:10:05 -07:00
const popPage = useCallback(() => {
setPages((pages) => {
const x = [...pages];
x.splice(-1, 1);
return x;
});
}, []);
2023-05-18 02:10:34 -07:00
2023-07-01 19:10:05 -07:00
const { data, isLoading } = useSearch({
options: { enabled: isHome && debouncedQuery !== '' && query !== '' },
query: {
albumArtistLimit: 4,
albumArtistStartIndex: 0,
albumLimit: 4,
albumStartIndex: 0,
query: debouncedQuery,
songLimit: 4,
songStartIndex: 0,
},
serverId: server?.id,
});
2023-05-19 00:21:36 -07:00
2023-07-01 19:10:05 -07:00
const showAlbumGroup = isHome && Boolean(query && data && data?.albums?.length > 0);
const showArtistGroup = isHome && Boolean(query && data && data?.albumArtists?.length > 0);
const showTrackGroup = isHome && Boolean(query && data && data?.songs?.length > 0);
2023-05-19 00:21:36 -07:00
2023-07-01 19:10:05 -07:00
const handlePlayQueueAdd = usePlayQueueAdd();
2023-05-19 00:21:36 -07:00
2023-07-01 19:10:05 -07:00
return (
<CustomModal
{...modalProps}
centered
handlers={{
...modalProps.handlers,
close: () => {
if (isHome) {
modalProps.handlers.close();
setQuery('');
} else {
popPage();
}
},
toggle: () => {
if (isHome) {
modalProps.handlers.toggle();
setQuery('');
} else {
popPage();
}
},
}}
scrollAreaComponent={ScrollArea.Autosize}
size="lg"
>
<Group
mb="1rem"
spacing="sm"
2023-05-19 00:35:14 -07:00
>
2023-07-01 19:10:05 -07:00
{pages.map((page, index) => (
<Fragment key={page}>
{index > 0 && ' > '}
<Button
compact
disabled
variant="default"
>
{page?.toLocaleUpperCase()}
</Button>
</Fragment>
))}
</Group>
<Command
filter={(value, search) => {
if (value.includes(search)) return 1;
if (value.includes('search')) return 1;
return 0;
}}
label="Global Command Menu"
value={value}
onValueChange={setValue}
2023-05-21 21:01:23 -07:00
>
2023-07-01 19:10:05 -07:00
<TextInput
ref={searchInputRef}
data-autofocus
icon={<RiSearchLine />}
rightSection={
<ActionIcon
onClick={() => {
setQuery('');
searchInputRef.current?.focus();
}}
>
<RiCloseFill />
</ActionIcon>
2023-05-19 00:21:36 -07:00
}
2023-07-01 19:10:05 -07:00
size="lg"
value={query}
onChange={(e) => setQuery(e.currentTarget.value)}
/>
<Command.Separator />
<Command.List>
<Command.Empty>No results found.</Command.Empty>
{showAlbumGroup && (
<Command.Group heading="Albums">
{data?.albums?.map((album) => (
<Command.Item
key={`search-album-${album.id}`}
value={`search-${album.id}`}
onSelect={() => {
navigate(
generatePath(AppRoute.LIBRARY_ALBUMS_DETAIL, {
albumId: album.id,
}),
);
modalProps.handlers.close();
setQuery('');
}}
>
<LibraryCommandItem
handlePlayQueueAdd={handlePlayQueueAdd}
id={album.id}
imageUrl={album.imageUrl}
itemType={LibraryItem.ALBUM}
subtitle={album.albumArtists
.map((artist) => artist.name)
.join(', ')}
title={album.name}
/>
</Command.Item>
))}
</Command.Group>
)}
{showArtistGroup && (
<Command.Group heading="Artists">
{data?.albumArtists.map((artist) => (
<Command.Item
key={`artist-${artist.id}`}
value={`search-${artist.id}`}
onSelect={() => {
navigate(
generatePath(AppRoute.LIBRARY_ALBUM_ARTISTS_DETAIL, {
albumArtistId: artist.id,
}),
);
modalProps.handlers.close();
setQuery('');
}}
>
<LibraryCommandItem
handlePlayQueueAdd={handlePlayQueueAdd}
id={artist.id}
imageUrl={artist.imageUrl}
itemType={LibraryItem.ALBUM_ARTIST}
subtitle={
(artist?.albumCount || 0) > 0
? `${artist.albumCount} albums`
: undefined
}
title={artist.name}
/>
</Command.Item>
))}
</Command.Group>
)}
{showTrackGroup && (
<Command.Group heading="Tracks">
{data?.songs.map((song) => (
<Command.Item
key={`artist-${song.id}`}
value={`search-${song.id}`}
onSelect={() => {
navigate(
generatePath(AppRoute.LIBRARY_ALBUMS_DETAIL, {
albumId: song.albumId,
}),
);
modalProps.handlers.close();
setQuery('');
}}
>
<LibraryCommandItem
handlePlayQueueAdd={handlePlayQueueAdd}
id={song.id}
imageUrl={song.imageUrl}
itemType={LibraryItem.SONG}
subtitle={song.artists
.map((artist) => artist.name)
.join(', ')}
title={song.name}
/>
</Command.Item>
))}
</Command.Group>
)}
{activePage === CommandPalettePages.HOME && (
<HomeCommands
handleClose={modalProps.handlers.close}
pages={pages}
query={query}
setPages={setPages}
setQuery={setQuery}
/>
)}
{activePage === CommandPalettePages.GO_TO && (
<GoToCommands
handleClose={modalProps.handlers.close}
setPages={setPages}
setQuery={setQuery}
/>
)}
{activePage === CommandPalettePages.MANAGE_SERVERS && (
<ServerCommands
handleClose={modalProps.handlers.close}
setPages={setPages}
setQuery={setQuery}
/>
)}
</Command.List>
</Command>
<Paper
mt="0.5rem"
p="0.5rem"
>
<Group position="apart">
<Command.Loading>
{isHome && isLoading && query !== '' && <Spinner />}
</Command.Loading>
<Group spacing="sm">
<Kbd size="md">ESC</Kbd>
<Kbd size="md"></Kbd>
<Kbd size="md"></Kbd>
<Kbd size="md"></Kbd>
</Group>
</Group>
</Paper>
</CustomModal>
);
2023-05-18 02:10:34 -07:00
};