2022-07-21 11:43:43 +02:00
|
|
|
import { Kbd, createStyles, Autocomplete } from '@mantine/core';
|
2022-05-29 21:39:57 +02:00
|
|
|
import { useDebouncedValue, useForm, useHotkeys } from '@mantine/hooks';
|
|
|
|
|
import { useEffect, useRef, useState } from 'react';
|
2022-05-29 18:42:58 +02:00
|
|
|
import {
|
|
|
|
|
IconSearch as Search,
|
|
|
|
|
IconBrandYoutube as BrandYoutube,
|
|
|
|
|
IconDownload as Download,
|
|
|
|
|
} from '@tabler/icons';
|
2022-05-29 21:39:57 +02:00
|
|
|
import axios from 'axios';
|
2022-07-23 22:22:55 +02:00
|
|
|
import { useConfig } from '../../tools/state';
|
|
|
|
|
import { IModule } from '../ModuleTypes';
|
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',
|
|
|
|
|
description: 'Show the current time and date in a card',
|
|
|
|
|
icon: Search,
|
|
|
|
|
component: SearchBar,
|
|
|
|
|
};
|
|
|
|
|
|
2022-04-27 14:14:10 +02:00
|
|
|
export default function SearchBar(props: any) {
|
2022-05-02 15:09:39 +02:00
|
|
|
const { config, setConfig } = useConfig();
|
2022-04-28 17:27:39 +02:00
|
|
|
const [opened, setOpened] = useState(false);
|
2022-05-29 21:39:57 +02:00
|
|
|
const [results, setOpenedResults] = useState(false);
|
2022-04-28 15:05:42 +02:00
|
|
|
const [icon, setIcon] = useState(<Search />);
|
2022-05-23 12:38:10 +02:00
|
|
|
const queryUrl = config.settings.searchUrl ?? 'https://www.google.com/search?q=';
|
2022-05-17 01:23:19 +02:00
|
|
|
const textInput = useRef<HTMLInputElement>();
|
2022-05-29 21:39:57 +02:00
|
|
|
// Find a service with the type of 'Overseerr'
|
|
|
|
|
const form = useForm({
|
|
|
|
|
initialValues: {
|
|
|
|
|
query: '',
|
|
|
|
|
},
|
|
|
|
|
});
|
2022-05-16 13:50:08 +02:00
|
|
|
|
2022-05-29 21:39:57 +02:00
|
|
|
const [debounced, cancel] = useDebouncedValue(form.values.query, 250);
|
2022-06-06 20:02:21 +02:00
|
|
|
const [results, setResults] = useState<any[]>([]);
|
2022-05-29 21:39:57 +02:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (form.values.query !== debounced || form.values.query === '') return;
|
2022-06-06 20:02:21 +02:00
|
|
|
axios
|
|
|
|
|
.get(`/api/modules/search?q=${form.values.query}`)
|
|
|
|
|
.then((res) => setResults(res.data ?? []));
|
2022-05-29 21:39:57 +02:00
|
|
|
}, [debounced]);
|
|
|
|
|
useHotkeys([['ctrl+K', () => textInput.current && textInput.current.focus()]]);
|
2022-05-16 15:56:01 +02:00
|
|
|
const { classes, cx } = useStyles();
|
2022-05-16 13:50:08 +02:00
|
|
|
const rightSection = (
|
2022-05-16 15:56:01 +02:00
|
|
|
<div className={classes.hide}>
|
2022-05-16 13:50:08 +02:00
|
|
|
<Kbd>Ctrl</Kbd>
|
|
|
|
|
<span style={{ margin: '0 5px' }}>+</span>
|
|
|
|
|
<Kbd>K</Kbd>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2022-04-27 23:18:57 +02:00
|
|
|
|
2022-05-17 02:04:44 +02:00
|
|
|
// If enabled modules doesn't contain the module, return null
|
2022-05-22 20:42:10 +02:00
|
|
|
// If module in enabled
|
|
|
|
|
|
|
|
|
|
const exists = config.modules?.[SearchModule.title]?.enabled ?? false;
|
|
|
|
|
if (!exists) {
|
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();
|
|
|
|
|
const isYoutube = query.startsWith('!yt');
|
|
|
|
|
const isTorrent = query.startsWith('!t');
|
|
|
|
|
if (isYoutube) {
|
|
|
|
|
setIcon(<BrandYoutube size={22} />);
|
|
|
|
|
} else if (isTorrent) {
|
|
|
|
|
setIcon(<Download size={22} />);
|
|
|
|
|
} else {
|
|
|
|
|
setIcon(<Search size={22} />);
|
|
|
|
|
}
|
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();
|
|
|
|
|
const isYoutube = query.startsWith('!yt');
|
|
|
|
|
const isTorrent = query.startsWith('!t');
|
2022-05-17 20:58:55 +02:00
|
|
|
form.setValues({ query: '' });
|
2022-05-17 01:23:19 +02:00
|
|
|
setTimeout(() => {
|
|
|
|
|
if (isYoutube) {
|
|
|
|
|
window.open(`https://www.youtube.com/results?search_query=${query.substring(3)}`);
|
|
|
|
|
} else if (isTorrent) {
|
|
|
|
|
window.open(`https://bitsearch.to/search?q=${query.substring(3)}`);
|
|
|
|
|
} else {
|
2022-06-25 14:02:53 +02:00
|
|
|
window.open(
|
|
|
|
|
`${
|
|
|
|
|
queryUrl.includes('%s')
|
|
|
|
|
? queryUrl.replace('%s', values.query)
|
|
|
|
|
: queryUrl + values.query
|
|
|
|
|
}`
|
|
|
|
|
);
|
2022-05-17 01:23:19 +02:00
|
|
|
}
|
|
|
|
|
}, 20);
|
2022-05-16 13:50:08 +02:00
|
|
|
})}
|
2022-04-28 15:05:42 +02:00
|
|
|
>
|
2022-06-28 19:09:02 +02:00
|
|
|
<Autocomplete
|
|
|
|
|
autoFocus
|
|
|
|
|
variant="filled"
|
|
|
|
|
data={autocompleteData}
|
|
|
|
|
icon={icon}
|
|
|
|
|
ref={textInput}
|
|
|
|
|
rightSectionWidth={90}
|
|
|
|
|
rightSection={rightSection}
|
2022-06-06 20:02:21 +02:00
|
|
|
radius="md"
|
2022-06-28 19:09:02 +02:00
|
|
|
size="md"
|
|
|
|
|
styles={{ rightSection: { pointerEvents: 'none' } }}
|
|
|
|
|
placeholder="Search the web..."
|
|
|
|
|
{...props}
|
|
|
|
|
{...form.getInputProps('query')}
|
|
|
|
|
/>
|
2022-05-16 13:50:08 +02:00
|
|
|
</form>
|
2022-04-27 14:14:10 +02:00
|
|
|
);
|
|
|
|
|
}
|