Files
Homarr/src/components/SearchBar/SearchBar.tsx

91 lines
2.7 KiB
TypeScript
Raw Normal View History

import { TextInput, Text, Popover, Box } from '@mantine/core';
2022-04-27 20:10:51 +02:00
import { useForm } from '@mantine/hooks';
import { useState } from 'react';
import { Search, BrandYoutube, Download } from 'tabler-icons-react';
import { useConfig } from '../../tools/state';
2022-04-27 14:14:10 +02:00
export default function SearchBar(props: any) {
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 />);
const querryUrl = config.settings.searchUrl || 'https://www.google.com/search?q=';
2022-04-27 23:18:57 +02:00
2022-04-27 20:10:51 +02:00
const form = useForm({
initialValues: {
querry: '',
},
2022-04-27 14:14:10 +02:00
});
2022-04-27 20:10:51 +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 (
<Box
2022-05-14 10:09:22 +02:00
mb={"xl"}
style={{
width: '100%',
2022-04-28 15:05:42 +02:00
}}
>
<form
onChange={() => {
// If querry contains !yt or !t add "Searching on YouTube" or "Searching torrent"
const querry = form.values.querry.trim();
const isYoutube = querry.startsWith('!yt');
const isTorrent = querry.startsWith('!t');
if (isYoutube) {
setIcon(<BrandYoutube size={22} />);
} else if (isTorrent) {
setIcon(<Download size={22} />);
} else {
setIcon(<Search size={22} />);
}
2022-04-28 17:27:39 +02:00
}}
onSubmit={form.onSubmit((values) => {
// Find if querry is prefixed by !yt or !t
const querry = values.querry.trim();
const isYoutube = querry.startsWith('!yt');
const isTorrent = querry.startsWith('!t');
if (isYoutube) {
window.open(`https://www.youtube.com/results?search_query=${querry.substring(3)}`);
} else if (isTorrent) {
window.open(`https://bitsearch.to/search?q=${querry.substring(3)}`);
} else {
window.open(`${querryUrl}${values.querry}`);
}
})}
2022-04-28 17:27:39 +02:00
>
<Popover
opened={opened}
style={{
width: '100%',
}}
position="bottom"
placement="start"
withArrow
trapFocus={false}
transition="pop-top-left"
onFocusCapture={() => setOpened(true)}
onBlurCapture={() => setOpened(false)}
target={
<TextInput
variant="filled"
color="blue"
icon={icon}
radius="md"
size="md"
placeholder="Search the web"
{...props}
{...form.getInputProps('querry')}
/>
}
>
<Text>
2022-05-15 15:57:58 +02:00
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>
</Box>
2022-04-27 14:14:10 +02:00
);
}