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-07 17:20:34 +02:00
|
|
|
import React, { 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-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-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 = {
|
|
|
|
|
title: 'Search Bar',
|
2022-07-24 23:18:01 +02:00
|
|
|
description: 'Search bar to search the web, youtube, torrents or overseerr',
|
2022-05-17 02:04:44 +02:00
|
|
|
icon: Search,
|
|
|
|
|
component: SearchBar,
|
|
|
|
|
};
|
|
|
|
|
|
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();
|
|
|
|
|
const isModuleEnabled = config.modules?.[SearchModule.title]?.enabled ?? false;
|
|
|
|
|
const isOverseerrEnabled = config.modules?.[OverseerrModule.title]?.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-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-07 12:14:57 +02:00
|
|
|
.get(`/api/modules/overseerr?query=${form.values.query.replace('!os ', '')}`)
|
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-06-06 20:02:21 +02:00
|
|
|
const autocompleteData = results.map((result) => ({
|
|
|
|
|
label: result.phrase,
|
|
|
|
|
value: result.phrase,
|
|
|
|
|
}));
|
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-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':
|
|
|
|
|
window.open(`https://www.youtube.com/results?search_query=${query.substring(3)}`);
|
|
|
|
|
break;
|
|
|
|
|
case '!t ':
|
|
|
|
|
window.open(`https://www.torrentdownloads.me/search/?search=${query.substring(3)}`);
|
|
|
|
|
break;
|
|
|
|
|
case '!os':
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
window.open(
|
|
|
|
|
`${queryUrl.includes('%s') ? queryUrl.replace('%s', query) : `${queryUrl}${query}`}`
|
|
|
|
|
);
|
|
|
|
|
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"
|
|
|
|
|
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' } }}
|
|
|
|
|
placeholder="Search the web..."
|
|
|
|
|
{...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
|
|
|
);
|
|
|
|
|
}
|