2022-05-16 13:50:08 +02:00
|
|
|
import { TextInput, Text, Popover, Kbd, Group } from '@mantine/core';
|
|
|
|
|
import { useForm, useHotkeys } from '@mantine/hooks';
|
|
|
|
|
import { useRef, useState } from 'react';
|
2022-05-04 07:30:06 +02:00
|
|
|
import { Search, BrandYoutube, Download } from 'tabler-icons-react';
|
2022-05-02 15:09:39 +02:00
|
|
|
import { useConfig } from '../../tools/state';
|
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-04-28 15:05:42 +02:00
|
|
|
const [icon, setIcon] = useState(<Search />);
|
2022-05-16 12:38:46 +02:00
|
|
|
const queryUrl = config.settings.searchUrl || 'https://www.google.com/search?q=';
|
2022-05-16 13:50:08 +02:00
|
|
|
const textInput: any = useRef(null);
|
|
|
|
|
useHotkeys([['ctrl+K', () => textInput.current.focus()]]);
|
|
|
|
|
|
|
|
|
|
const rightSection = (
|
|
|
|
|
<div style={{ display: 'flex', alignItems: 'center' }}>
|
|
|
|
|
<Kbd>Ctrl</Kbd>
|
|
|
|
|
<span style={{ margin: '0 5px' }}>+</span>
|
|
|
|
|
<Kbd>K</Kbd>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2022-04-27 23:18:57 +02:00
|
|
|
|
2022-04-27 20:10:51 +02:00
|
|
|
const form = useForm({
|
|
|
|
|
initialValues: {
|
2022-05-16 12:38:46 +02:00
|
|
|
query: '',
|
2022-04-27 20:10:51 +02:00
|
|
|
},
|
2022-04-27 14:14:10 +02:00
|
|
|
});
|
2022-04-27 20:10:51 +02:00
|
|
|
|
2022-05-04 07:30:06 +02:00
|
|
|
if (config.settings.searchBar === false) {
|
2022-04-27 20:10:51 +02:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
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) => {
|
|
|
|
|
// Find if query is prefixed by !yt or !t
|
|
|
|
|
const query = values.query.trim();
|
|
|
|
|
const isYoutube = query.startsWith('!yt');
|
|
|
|
|
const isTorrent = query.startsWith('!t');
|
|
|
|
|
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 {
|
|
|
|
|
window.open(`${queryUrl}${values.query}`);
|
|
|
|
|
}
|
|
|
|
|
})}
|
2022-04-28 15:05:42 +02:00
|
|
|
>
|
2022-05-16 13:50:08 +02:00
|
|
|
<Popover
|
|
|
|
|
opened={opened}
|
|
|
|
|
position="bottom"
|
|
|
|
|
placement="start"
|
|
|
|
|
width={260}
|
|
|
|
|
withArrow
|
|
|
|
|
radius="md"
|
|
|
|
|
trapFocus={false}
|
|
|
|
|
transition="pop-bottom-right"
|
|
|
|
|
onFocusCapture={() => setOpened(true)}
|
|
|
|
|
onBlurCapture={() => setOpened(false)}
|
|
|
|
|
target={
|
|
|
|
|
<Group direction="row">
|
2022-05-02 18:10:56 +02:00
|
|
|
<TextInput
|
2022-05-16 15:55:22 +02:00
|
|
|
width={'100%'}
|
2022-05-02 18:10:56 +02:00
|
|
|
variant="filled"
|
|
|
|
|
icon={icon}
|
2022-05-16 13:50:08 +02:00
|
|
|
ref={textInput}
|
|
|
|
|
rightSectionWidth={90}
|
|
|
|
|
rightSection={rightSection}
|
2022-05-02 18:10:56 +02:00
|
|
|
radius="md"
|
|
|
|
|
size="md"
|
2022-05-16 13:50:08 +02:00
|
|
|
styles={{ rightSection: { pointerEvents: 'none' } }}
|
|
|
|
|
placeholder="Search the web..."
|
2022-05-02 18:10:56 +02:00
|
|
|
{...props}
|
2022-05-16 12:38:46 +02:00
|
|
|
{...form.getInputProps('query')}
|
2022-05-02 18:10:56 +02:00
|
|
|
/>
|
2022-05-16 13:50:08 +02:00
|
|
|
</Group>
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
<Text>
|
|
|
|
|
tip: Use the prefixes <b>!yt</b> and <b>!t</b> in front of your query to search on YouTube
|
|
|
|
|
or for a Torrent respectively.
|
|
|
|
|
</Text>
|
|
|
|
|
</Popover>
|
|
|
|
|
</form>
|
2022-04-27 14:14:10 +02:00
|
|
|
);
|
|
|
|
|
}
|