2022-05-29 18:42:58 +02:00
|
|
|
import {
|
2022-11-29 20:32:24 +09:00
|
|
|
ActionIcon,
|
2022-11-30 09:15:03 +09:00
|
|
|
Autocomplete,
|
2022-11-29 20:32:24 +09:00
|
|
|
Box,
|
|
|
|
|
createStyles,
|
|
|
|
|
Divider,
|
|
|
|
|
Kbd,
|
|
|
|
|
Menu,
|
|
|
|
|
Popover,
|
|
|
|
|
ScrollArea,
|
|
|
|
|
TextInput,
|
|
|
|
|
Tooltip,
|
|
|
|
|
} from '@mantine/core';
|
|
|
|
|
import { IconSearch, IconBrandYoutube, IconDownload, IconMovie } from '@tabler/icons';
|
2022-11-30 09:15:03 +09:00
|
|
|
import React, { forwardRef, useEffect, useRef, useState } from 'react';
|
2022-11-29 20:32:24 +09:00
|
|
|
import { useDebouncedValue, useHotkeys } from '@mantine/hooks';
|
|
|
|
|
import { showNotification } from '@mantine/notifications';
|
2022-08-22 09:50:54 +02:00
|
|
|
import { useTranslation } from 'next-i18next';
|
2022-05-29 21:39:57 +02:00
|
|
|
import axios from 'axios';
|
2022-07-23 22:22:55 +02:00
|
|
|
import { IModule } from '../ModuleTypes';
|
2022-11-29 20:32:24 +09:00
|
|
|
import { useConfig } from '../../tools/state';
|
2022-07-24 23:18:01 +02:00
|
|
|
import { OverseerrModule } from '../overseerr';
|
2022-11-29 20:32:24 +09:00
|
|
|
import Tip from '../../components/layout/Tip';
|
2022-08-08 13:45:54 +02:00
|
|
|
import { OverseerrMediaDisplay } from '../common';
|
2022-11-30 09:15:03 +09:00
|
|
|
import SmallServiceItem from '../../components/AppShelf/SmallServiceItem';
|
2022-05-16 15:56:01 +02:00
|
|
|
|
2022-05-17 02:04:44 +02:00
|
|
|
export const SearchModule: IModule = {
|
2022-08-24 20:13:53 +02:00
|
|
|
title: 'Search',
|
2022-11-29 20:32:24 +09:00
|
|
|
icon: IconSearch,
|
|
|
|
|
component: SearchModuleComponent,
|
2022-08-25 11:07:25 +02:00
|
|
|
id: 'search',
|
2022-05-17 02:04:44 +02:00
|
|
|
};
|
|
|
|
|
|
2022-11-29 20:32:24 +09:00
|
|
|
interface ItemProps extends React.ComponentPropsWithoutRef<'div'> {
|
|
|
|
|
label: string;
|
|
|
|
|
disabled: boolean;
|
|
|
|
|
value: string;
|
|
|
|
|
description: string;
|
|
|
|
|
icon: React.ReactNode;
|
|
|
|
|
url: string;
|
|
|
|
|
shortcut: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const useStyles = createStyles((theme) => ({
|
|
|
|
|
item: {
|
|
|
|
|
'&[data-hovered]': {
|
|
|
|
|
backgroundColor: theme.colors[theme.primaryColor][theme.fn.primaryShade()],
|
|
|
|
|
color: theme.white,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
2022-11-29 21:06:11 +09:00
|
|
|
export function SearchModuleComponent() {
|
2022-07-24 23:18:01 +02:00
|
|
|
const { config } = useConfig();
|
2022-11-29 20:32:24 +09:00
|
|
|
const { t } = useTranslation('modules/search');
|
|
|
|
|
const [searchQuery, setSearchQuery] = useState('');
|
|
|
|
|
const [debounced, cancel] = useDebouncedValue(searchQuery, 250);
|
2022-08-25 11:07:25 +02:00
|
|
|
const isOverseerrEnabled = config.modules?.[OverseerrModule.id]?.enabled ?? false;
|
2022-08-09 13:35:59 +02:00
|
|
|
const OverseerrService = config.services.find(
|
|
|
|
|
(service) => service.type === 'Overseerr' || service.type === 'Jellyseerr'
|
|
|
|
|
);
|
2022-07-24 23:18:01 +02:00
|
|
|
|
2022-11-29 20:32:24 +09:00
|
|
|
const searchEnginesList: ItemProps[] = [
|
|
|
|
|
{
|
|
|
|
|
icon: <IconSearch />,
|
|
|
|
|
disabled: false,
|
|
|
|
|
label: t('searchEngines.search.name'),
|
|
|
|
|
value: 'search',
|
|
|
|
|
description: t('searchEngines.search.description'),
|
2022-11-30 00:42:10 +09:00
|
|
|
url: config.settings.searchUrl,
|
2022-11-29 20:32:24 +09:00
|
|
|
shortcut: 's',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
icon: <IconDownload />,
|
|
|
|
|
disabled: false,
|
|
|
|
|
label: t('searchEngines.torrents.name'),
|
|
|
|
|
value: 'torrents',
|
|
|
|
|
description: t('searchEngines.torrents.description'),
|
|
|
|
|
url: 'https://www.torrentdownloads.me/search/?search=',
|
|
|
|
|
shortcut: 't',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
icon: <IconBrandYoutube />,
|
|
|
|
|
disabled: false,
|
|
|
|
|
label: t('searchEngines.youtube.name'),
|
|
|
|
|
value: 'youtube',
|
|
|
|
|
description: t('searchEngines.youtube.description'),
|
|
|
|
|
url: 'https://www.youtube.com/results?search_query=',
|
|
|
|
|
shortcut: 'y',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
icon: <IconMovie />,
|
|
|
|
|
disabled: !(isOverseerrEnabled === true && OverseerrService !== undefined),
|
|
|
|
|
label: t('searchEngines.overseerr.name'),
|
|
|
|
|
value: 'overseerr',
|
|
|
|
|
description: t('searchEngines.overseerr.description'),
|
|
|
|
|
url: `${OverseerrService?.url}search?query=`,
|
|
|
|
|
shortcut: 'm',
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
const [selectedSearchEngine, setSearchEngine] = useState<ItemProps>(searchEnginesList[0]);
|
2022-11-30 09:15:03 +09:00
|
|
|
const matchingServices = config.services.filter((service) => {
|
|
|
|
|
if (searchQuery === '' || searchQuery === undefined) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return service.name.toLowerCase().includes(searchQuery.toLowerCase());
|
|
|
|
|
});
|
|
|
|
|
const autocompleteData = matchingServices.map((service) => ({
|
|
|
|
|
label: service.name,
|
|
|
|
|
value: service.name,
|
|
|
|
|
icon: service.icon,
|
|
|
|
|
url: service.openedUrl ?? service.url,
|
|
|
|
|
}));
|
|
|
|
|
const AutoCompleteItem = forwardRef<HTMLDivElement, any>(
|
|
|
|
|
({ label, value, icon, url, ...others }: any, ref) => (
|
|
|
|
|
<div ref={ref} {...others}>
|
|
|
|
|
<SmallServiceItem service={{ label, value, icon, url }} />
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
);
|
2022-11-30 00:42:10 +09:00
|
|
|
useEffect(() => {
|
|
|
|
|
// Refresh the default search engine every time the config for it changes #521
|
|
|
|
|
setSearchEngine(searchEnginesList[0]);
|
|
|
|
|
}, [config.settings.searchUrl]);
|
2022-11-29 20:32:24 +09:00
|
|
|
const textInput = useRef<HTMLInputElement>(null);
|
|
|
|
|
useHotkeys([['mod+K', () => textInput.current && textInput.current.focus()]]);
|
|
|
|
|
const { classes } = useStyles();
|
|
|
|
|
const openInNewTab = config.settings.searchNewTab ? '_blank' : '_self';
|
2022-07-24 23:18:01 +02:00
|
|
|
const [OverseerrResults, setOverseerrResults] = useState<any[]>([]);
|
|
|
|
|
const [opened, setOpened] = useState(false);
|
|
|
|
|
|
2022-05-29 21:39:57 +02:00
|
|
|
useEffect(() => {
|
2022-11-29 20:32:24 +09:00
|
|
|
if (debounced !== '' && selectedSearchEngine.value === 'overseerr' && searchQuery.length > 3) {
|
|
|
|
|
axios.get(`/api/modules/overseerr?query=${searchQuery}`).then((res) => {
|
|
|
|
|
setOverseerrResults(res.data.results ?? []);
|
2022-07-24 23:18:01 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, [debounced]);
|
2022-05-22 20:42:10 +02:00
|
|
|
|
2022-11-29 20:32:24 +09:00
|
|
|
const isModuleEnabled = config.modules?.[SearchModule.id]?.enabled ?? false;
|
2022-07-24 23:18:01 +02:00
|
|
|
if (!isModuleEnabled) {
|
2022-04-27 20:10:51 +02:00
|
|
|
return null;
|
|
|
|
|
}
|
2022-11-29 20:32:24 +09:00
|
|
|
//TODO: Fix the bug where clicking anything inside the Modal to ask for a movie
|
|
|
|
|
// will close it (Because it closes the underlying Popover)
|
2022-04-27 14:14:10 +02:00
|
|
|
return (
|
2022-11-29 20:32:24 +09:00
|
|
|
<Box style={{ width: '100%', maxWidth: 400, minWidth: 300 }}>
|
2022-07-24 23:18:01 +02:00
|
|
|
<Popover
|
2022-11-29 20:32:24 +09:00
|
|
|
opened={OverseerrResults.length > 0 && opened && searchQuery.length > 3}
|
2022-07-24 23:18:01 +02:00
|
|
|
position="bottom"
|
2022-08-07 17:20:34 +02:00
|
|
|
withinPortal
|
|
|
|
|
shadow="md"
|
2022-06-06 20:02:21 +02:00
|
|
|
radius="md"
|
2022-08-08 13:45:54 +02:00
|
|
|
zIndex={100}
|
|
|
|
|
transition="pop-top-right"
|
2022-08-07 17:20:34 +02:00
|
|
|
>
|
|
|
|
|
<Popover.Target>
|
2022-11-30 09:15:03 +09:00
|
|
|
<Autocomplete
|
2022-11-29 20:32:24 +09:00
|
|
|
ref={textInput}
|
2022-08-07 17:20:34 +02:00
|
|
|
onFocusCapture={() => setOpened(true)}
|
2022-07-24 23:18:01 +02:00
|
|
|
autoFocus
|
2022-11-29 20:32:24 +09:00
|
|
|
rightSection={<SearchModuleMenu />}
|
|
|
|
|
placeholder={t(`searchEngines.${selectedSearchEngine.value}.description`)}
|
|
|
|
|
value={searchQuery}
|
2022-11-30 09:15:03 +09:00
|
|
|
onChange={(currentString) => tryMatchSearchEngine(currentString, setSearchQuery)}
|
|
|
|
|
itemComponent={AutoCompleteItem}
|
|
|
|
|
data={autocompleteData}
|
|
|
|
|
onItemSubmit={(item) => {
|
|
|
|
|
setOpened(false);
|
|
|
|
|
if (item.url) {
|
|
|
|
|
setSearchQuery('');
|
|
|
|
|
window.open(item.openedUrl ? item.openedUrl : item.url, openInNewTab);
|
|
|
|
|
}
|
|
|
|
|
}}
|
2022-11-29 20:32:24 +09:00
|
|
|
// Replace %s if it is in selectedSearchEngine.url with searchQuery, otherwise append searchQuery at the end of it
|
|
|
|
|
onKeyDown={(event) => {
|
2022-11-30 09:15:03 +09:00
|
|
|
if (
|
|
|
|
|
event.key === 'Enter' &&
|
|
|
|
|
searchQuery.length > 0 &&
|
|
|
|
|
autocompleteData.length === 0
|
|
|
|
|
) {
|
2022-11-29 20:32:24 +09:00
|
|
|
if (selectedSearchEngine.url.includes('%s')) {
|
|
|
|
|
window.open(selectedSearchEngine.url.replace('%s', searchQuery), openInNewTab);
|
|
|
|
|
} else {
|
|
|
|
|
window.open(selectedSearchEngine.url + searchQuery, openInNewTab);
|
|
|
|
|
}
|
2022-08-24 18:44:11 +02:00
|
|
|
}
|
|
|
|
|
}}
|
2022-11-30 09:15:03 +09:00
|
|
|
radius="md"
|
|
|
|
|
size="md"
|
2022-07-24 23:18:01 +02:00
|
|
|
/>
|
2022-08-07 17:20:34 +02:00
|
|
|
</Popover.Target>
|
2022-08-09 13:35:59 +02:00
|
|
|
<Popover.Dropdown>
|
2022-11-29 20:32:24 +09:00
|
|
|
<div>
|
|
|
|
|
<ScrollArea style={{ height: 400, width: 420 }} offsetScrollbars>
|
2022-08-09 13:35:59 +02:00
|
|
|
{OverseerrResults.slice(0, 5).map((result, index) => (
|
|
|
|
|
<React.Fragment key={index}>
|
|
|
|
|
<OverseerrMediaDisplay key={result.id} media={result} />
|
|
|
|
|
{index < OverseerrResults.length - 1 && <Divider variant="dashed" my="xl" />}
|
|
|
|
|
</React.Fragment>
|
|
|
|
|
))}
|
|
|
|
|
</ScrollArea>
|
|
|
|
|
</div>
|
2022-08-07 17:20:34 +02:00
|
|
|
</Popover.Dropdown>
|
2022-07-24 23:18:01 +02:00
|
|
|
</Popover>
|
2022-11-29 20:32:24 +09:00
|
|
|
</Box>
|
2022-04-27 14:14:10 +02:00
|
|
|
);
|
2022-11-29 20:32:24 +09:00
|
|
|
|
|
|
|
|
function tryMatchSearchEngine(query: string, setSearchQuery: (value: string) => void) {
|
|
|
|
|
const foundSearchEngine = searchEnginesList.find(
|
|
|
|
|
(engine) => query.includes(`!${engine.shortcut}`) && !engine.disabled
|
|
|
|
|
);
|
|
|
|
|
if (foundSearchEngine) {
|
|
|
|
|
setSearchQuery(query.replace(`!${foundSearchEngine.shortcut}`, ''));
|
|
|
|
|
changeSearchEngine(foundSearchEngine);
|
|
|
|
|
} else {
|
|
|
|
|
setSearchQuery(query);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function SearchModuleMenu() {
|
|
|
|
|
return (
|
|
|
|
|
<Menu shadow="md" width={200} withinPortal classNames={classes}>
|
|
|
|
|
<Menu.Target>
|
|
|
|
|
<ActionIcon>{selectedSearchEngine.icon}</ActionIcon>
|
|
|
|
|
</Menu.Target>
|
|
|
|
|
|
|
|
|
|
<Menu.Dropdown>
|
|
|
|
|
{searchEnginesList.map((item) => (
|
|
|
|
|
<Tooltip
|
|
|
|
|
multiline
|
|
|
|
|
label={item.description}
|
|
|
|
|
withinPortal
|
|
|
|
|
width={200}
|
|
|
|
|
position="left"
|
|
|
|
|
key={item.value}
|
|
|
|
|
>
|
|
|
|
|
<Menu.Item
|
|
|
|
|
key={item.value}
|
|
|
|
|
icon={item.icon}
|
|
|
|
|
rightSection={<Kbd>!{item.shortcut}</Kbd>}
|
|
|
|
|
disabled={item.disabled}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
changeSearchEngine(item);
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{item.label}
|
|
|
|
|
</Menu.Item>
|
|
|
|
|
</Tooltip>
|
|
|
|
|
))}
|
|
|
|
|
<Menu.Divider />
|
|
|
|
|
<Menu.Label>
|
|
|
|
|
<Tip>
|
|
|
|
|
{t('tip')} <Kbd>mod+k</Kbd>{' '}
|
|
|
|
|
</Tip>
|
|
|
|
|
</Menu.Label>
|
|
|
|
|
</Menu.Dropdown>
|
|
|
|
|
</Menu>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function changeSearchEngine(item: ItemProps) {
|
|
|
|
|
setSearchEngine(item);
|
|
|
|
|
showNotification({
|
|
|
|
|
radius: 'lg',
|
|
|
|
|
disallowClose: true,
|
|
|
|
|
id: 'spotlight',
|
|
|
|
|
autoClose: 1000,
|
|
|
|
|
icon: <ActionIcon size="sm">{item.icon}</ActionIcon>,
|
|
|
|
|
message: t('switchedSearchEngine', { searchEngine: t(`searchEngines.${item.value}.name`) }),
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-04-27 14:14:10 +02:00
|
|
|
}
|