2022-08-07 17:20:34 +02:00
|
|
|
import { Kbd, createStyles, Autocomplete, Popover, ScrollArea, Divider } from '@mantine/core';
|
2022-08-09 13:35:59 +02:00
|
|
|
import { useClickOutside, useDebouncedValue, useHotkeys } from '@mantine/hooks';
|
2022-07-26 00:51:55 +02:00
|
|
|
import { useForm } from '@mantine/form';
|
2022-08-24 18:44:11 +02:00
|
|
|
import React, { forwardRef, useEffect, useRef, useState } from 'react';
|
2022-05-29 18:42:58 +02:00
|
|
|
import {
|
|
|
|
|
IconSearch as Search,
|
|
|
|
|
IconBrandYoutube as BrandYoutube,
|
|
|
|
|
IconDownload as Download,
|
2022-07-24 23:18:01 +02:00
|
|
|
IconMovie,
|
2022-05-29 18:42:58 +02:00
|
|
|
} from '@tabler/icons';
|
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-24 23:18:01 +02:00
|
|
|
import { showNotification } from '@mantine/notifications';
|
2022-07-23 22:22:55 +02:00
|
|
|
import { useConfig } from '../../tools/state';
|
|
|
|
|
import { IModule } from '../ModuleTypes';
|
2022-07-24 23:18:01 +02:00
|
|
|
import { OverseerrModule } from '../overseerr';
|
2022-08-08 13:45:54 +02:00
|
|
|
import { OverseerrMediaDisplay } from '../common';
|
2022-08-24 18:44:11 +02:00
|
|
|
import SmallServiceItem from '../../components/AppShelf/SmallServiceItem';
|
2022-04-27 14:14:10 +02:00
|
|
|
|
2022-05-16 15:56:01 +02:00
|
|
|
const useStyles = createStyles((theme) => ({
|
|
|
|
|
hide: {
|
|
|
|
|
[theme.fn.smallerThan('sm')]: {
|
|
|
|
|
display: 'none',
|
|
|
|
|
},
|
|
|
|
|
display: 'flex',
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
2022-05-17 02:04:44 +02:00
|
|
|
export const SearchModule: IModule = {
|
2022-08-24 20:13:53 +02:00
|
|
|
title: 'Search',
|
2022-05-17 02:04:44 +02:00
|
|
|
icon: Search,
|
|
|
|
|
component: SearchBar,
|
2022-08-25 11:07:25 +02:00
|
|
|
id: 'search',
|
2022-05-17 02:04:44 +02:00
|
|
|
};
|
|
|
|
|
|
2022-04-27 14:14:10 +02:00
|
|
|
export default function SearchBar(props: any) {
|
2022-07-24 23:18:01 +02:00
|
|
|
const { classes, cx } = useStyles();
|
|
|
|
|
// Config
|
|
|
|
|
const { config } = useConfig();
|
2022-08-25 11:07:25 +02:00
|
|
|
const isModuleEnabled = config.modules?.[SearchModule.id]?.enabled ?? false;
|
|
|
|
|
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-05-23 12:38:10 +02:00
|
|
|
const queryUrl = config.settings.searchUrl ?? 'https://www.google.com/search?q=';
|
2022-07-24 23:18:01 +02:00
|
|
|
|
|
|
|
|
const [OverseerrResults, setOverseerrResults] = useState<any[]>([]);
|
2022-07-24 23:48:48 +02:00
|
|
|
const [loading, setLoading] = useState<boolean>(false);
|
2022-07-24 23:18:01 +02:00
|
|
|
const [icon, setIcon] = useState(<Search />);
|
|
|
|
|
const [results, setResults] = useState<any[]>([]);
|
|
|
|
|
const [opened, setOpened] = useState(false);
|
2022-08-09 13:35:59 +02:00
|
|
|
const ref = useClickOutside(() => setOpened(false));
|
2022-07-24 23:18:01 +02:00
|
|
|
|
2022-05-17 01:23:19 +02:00
|
|
|
const textInput = useRef<HTMLInputElement>();
|
2022-07-24 23:18:01 +02:00
|
|
|
useHotkeys([['ctrl+K', () => textInput.current && textInput.current.focus()]]);
|
|
|
|
|
|
2022-05-29 21:39:57 +02:00
|
|
|
const form = useForm({
|
|
|
|
|
initialValues: {
|
|
|
|
|
query: '',
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
const [debounced, cancel] = useDebouncedValue(form.values.query, 250);
|
2022-08-25 11:07:25 +02:00
|
|
|
const { t } = useTranslation('modules/search');
|
2022-07-24 23:18:01 +02:00
|
|
|
|
2022-05-29 21:39:57 +02:00
|
|
|
useEffect(() => {
|
2022-07-24 23:18:01 +02:00
|
|
|
if (OverseerrService === undefined && isOverseerrEnabled) {
|
|
|
|
|
showNotification({
|
|
|
|
|
title: 'Overseerr integration',
|
2022-08-09 13:35:59 +02:00
|
|
|
message:
|
|
|
|
|
'Module enabled but no service is configured with the type "Overseerr" / "Jellyseerr"',
|
2022-07-24 23:18:01 +02:00
|
|
|
color: 'red',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, [OverseerrService, isOverseerrEnabled]);
|
2022-04-27 23:18:57 +02:00
|
|
|
|
2022-07-24 23:18:01 +02:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (
|
|
|
|
|
form.values.query !== debounced ||
|
|
|
|
|
form.values.query === '' ||
|
|
|
|
|
(form.values.query.startsWith('!') && !form.values.query.startsWith('!os'))
|
|
|
|
|
) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (form.values.query.startsWith('!os')) {
|
|
|
|
|
axios
|
2022-08-09 15:06:00 +02:00
|
|
|
.get(`/api/modules/overseerr?query=${form.values.query.replace('!os', '').trim()}`)
|
2022-07-24 23:18:01 +02:00
|
|
|
.then((res) => {
|
|
|
|
|
setOverseerrResults(res.data.results ?? []);
|
2022-07-24 23:48:48 +02:00
|
|
|
setLoading(false);
|
2022-07-24 23:18:01 +02:00
|
|
|
});
|
2022-07-24 23:48:48 +02:00
|
|
|
setLoading(true);
|
2022-07-24 23:18:01 +02:00
|
|
|
} else {
|
|
|
|
|
setOverseerrResults([]);
|
|
|
|
|
axios
|
|
|
|
|
.get(`/api/modules/search?q=${form.values.query}`)
|
|
|
|
|
.then((res) => setResults(res.data ?? []));
|
|
|
|
|
}
|
|
|
|
|
}, [debounced]);
|
2022-05-22 20:42:10 +02:00
|
|
|
|
2022-07-24 23:18:01 +02:00
|
|
|
if (!isModuleEnabled) {
|
2022-04-27 20:10:51 +02:00
|
|
|
return null;
|
|
|
|
|
}
|
2022-08-24 18:44:11 +02:00
|
|
|
// Match all the services that contain the query in their name if the query is not empty
|
|
|
|
|
const matchingServices = config.services.filter((service) => {
|
|
|
|
|
if (form.values.query === '' || form.values.query === undefined) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return service.name.toLowerCase().includes(form.values.query.toLowerCase());
|
|
|
|
|
});
|
|
|
|
|
const autocompleteData = matchingServices.map((service) => ({
|
|
|
|
|
label: service.name,
|
|
|
|
|
value: service.name,
|
|
|
|
|
icon: service.icon,
|
|
|
|
|
url: service.openedUrl ?? service.url,
|
|
|
|
|
}));
|
|
|
|
|
// Append the matching results to the autocomplete data
|
|
|
|
|
const autoCompleteResults = results.map((result) => ({
|
2022-06-06 20:02:21 +02:00
|
|
|
label: result.phrase,
|
|
|
|
|
value: result.phrase,
|
2022-08-24 18:44:11 +02:00
|
|
|
icon: result.icon,
|
|
|
|
|
url: result.url,
|
2022-06-06 20:02:21 +02:00
|
|
|
}));
|
2022-08-24 18:44:11 +02:00
|
|
|
autocompleteData.push(...autoCompleteResults);
|
|
|
|
|
|
|
|
|
|
const AutoCompleteItem = forwardRef<HTMLDivElement, any>(
|
|
|
|
|
({ label, value, icon, url, ...others }: any, ref) => (
|
|
|
|
|
<div ref={ref} {...others}>
|
|
|
|
|
<SmallServiceItem service={{ label, value, icon, url }} />
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
2022-04-27 14:14:10 +02:00
|
|
|
return (
|
2022-05-16 13:50:08 +02:00
|
|
|
<form
|
|
|
|
|
onChange={() => {
|
|
|
|
|
// If query contains !yt or !t add "Searching on YouTube" or "Searching torrent"
|
|
|
|
|
const query = form.values.query.trim();
|
2022-07-24 23:18:01 +02:00
|
|
|
switch (query.substring(0, 3)) {
|
|
|
|
|
case '!yt':
|
|
|
|
|
setIcon(<BrandYoutube />);
|
|
|
|
|
break;
|
|
|
|
|
case '!t ':
|
|
|
|
|
setIcon(<Download />);
|
|
|
|
|
break;
|
|
|
|
|
case '!os':
|
|
|
|
|
setIcon(<IconMovie />);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
setIcon(<Search />);
|
|
|
|
|
break;
|
2022-05-16 13:50:08 +02:00
|
|
|
}
|
2022-04-28 15:05:42 +02:00
|
|
|
}}
|
2022-05-16 13:50:08 +02:00
|
|
|
onSubmit={form.onSubmit((values) => {
|
|
|
|
|
const query = values.query.trim();
|
2022-10-21 21:58:35 +02:00
|
|
|
const open_in = config.settings.searchNewTab ? '_blank' : '_self';
|
2022-05-17 01:23:19 +02:00
|
|
|
setTimeout(() => {
|
2022-07-24 23:18:01 +02:00
|
|
|
form.setValues({ query: '' });
|
|
|
|
|
switch (query.substring(0, 3)) {
|
|
|
|
|
case '!yt':
|
2022-10-21 21:58:35 +02:00
|
|
|
window.open(`https://www.youtube.com/results?search_query=${query.substring(3)}`, open_in);
|
2022-07-24 23:18:01 +02:00
|
|
|
break;
|
|
|
|
|
case '!t ':
|
2022-10-21 21:58:35 +02:00
|
|
|
window.open(`https://www.torrentdownloads.me/search/?search=${query.substring(3)}`, open_in);
|
2022-07-24 23:18:01 +02:00
|
|
|
break;
|
|
|
|
|
case '!os':
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
window.open(
|
2022-10-21 21:58:35 +02:00
|
|
|
`${queryUrl.includes('%s') ? queryUrl.replace('%s', query) : `${queryUrl}${query}`}`,
|
|
|
|
|
open_in
|
2022-07-24 23:18:01 +02:00
|
|
|
);
|
|
|
|
|
break;
|
2022-05-17 01:23:19 +02:00
|
|
|
}
|
2022-07-24 23:18:01 +02:00
|
|
|
}, 500);
|
2022-05-16 13:50:08 +02:00
|
|
|
})}
|
2022-04-28 15:05:42 +02:00
|
|
|
>
|
2022-07-24 23:18:01 +02:00
|
|
|
<Popover
|
2022-07-24 23:48:48 +02:00
|
|
|
opened={OverseerrResults.length > 0 && opened}
|
2022-07-24 23:18:01 +02:00
|
|
|
position="bottom"
|
2022-08-07 17:20:34 +02:00
|
|
|
withArrow
|
|
|
|
|
withinPortal
|
|
|
|
|
shadow="md"
|
2022-06-06 20:02:21 +02:00
|
|
|
radius="md"
|
2022-08-08 13:45:54 +02:00
|
|
|
zIndex={100}
|
2022-08-07 17:20:34 +02:00
|
|
|
trapFocus
|
2022-08-08 13:45:54 +02:00
|
|
|
transition="pop-top-right"
|
2022-08-07 17:20:34 +02:00
|
|
|
>
|
|
|
|
|
<Popover.Target>
|
2022-07-24 23:18:01 +02:00
|
|
|
<Autocomplete
|
2022-08-07 17:20:34 +02:00
|
|
|
onFocusCapture={() => setOpened(true)}
|
2022-07-24 23:18:01 +02:00
|
|
|
autoFocus
|
|
|
|
|
variant="filled"
|
2022-08-24 18:44:11 +02:00
|
|
|
itemComponent={AutoCompleteItem}
|
|
|
|
|
onItemSubmit={(item) => {
|
|
|
|
|
setOpened(false);
|
|
|
|
|
if (item.url) {
|
|
|
|
|
results.splice(0, autocompleteData.length);
|
|
|
|
|
form.reset();
|
|
|
|
|
window.open(item.url);
|
|
|
|
|
}
|
|
|
|
|
}}
|
2022-07-24 23:18:01 +02:00
|
|
|
data={autocompleteData}
|
|
|
|
|
icon={icon}
|
|
|
|
|
ref={textInput}
|
|
|
|
|
rightSectionWidth={90}
|
|
|
|
|
rightSection={
|
|
|
|
|
<div className={classes.hide}>
|
|
|
|
|
<Kbd>Ctrl</Kbd>
|
|
|
|
|
<span style={{ margin: '0 5px' }}>+</span>
|
|
|
|
|
<Kbd>K</Kbd>
|
|
|
|
|
</div>
|
|
|
|
|
}
|
|
|
|
|
radius="md"
|
|
|
|
|
size="md"
|
|
|
|
|
styles={{ rightSection: { pointerEvents: 'none' } }}
|
2022-08-22 09:50:54 +02:00
|
|
|
placeholder={t('input.placeholder')}
|
2022-07-24 23:18:01 +02:00
|
|
|
{...props}
|
|
|
|
|
{...form.getInputProps('query')}
|
|
|
|
|
/>
|
2022-08-07 17:20:34 +02:00
|
|
|
</Popover.Target>
|
|
|
|
|
|
2022-08-09 13:35:59 +02:00
|
|
|
<Popover.Dropdown>
|
|
|
|
|
<div ref={ref}>
|
|
|
|
|
<ScrollArea style={{ height: 400, width: 400 }} offsetScrollbars>
|
|
|
|
|
{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-05-16 13:50:08 +02:00
|
|
|
</form>
|
2022-04-27 14:14:10 +02:00
|
|
|
);
|
|
|
|
|
}
|