Files
Homarr/components/SearchBar/SearchBar.tsx

120 lines
3.1 KiB
TypeScript
Raw Normal View History

2022-04-28 17:27:39 +02:00
import {
Input,
TextInput,
Text,
ActionIcon,
useMantineTheme,
Center,
Popover,
} from '@mantine/core';
2022-04-27 20:10:51 +02:00
import { useForm } from '@mantine/hooks';
2022-04-27 14:14:10 +02:00
import { showNotification } from '@mantine/notifications';
import { useState, useEffect } from 'react';
2022-04-28 17:27:39 +02:00
import {
Search,
ArrowRight,
ArrowLeft,
BrandYoutube,
Download,
InfoCircle,
FileX,
} from 'tabler-icons-react';
2022-05-01 14:46:45 +02:00
import { loadSettings } from '../../tools/settings';
import { Settings } from '../../tools/types';
2022-04-27 14:14:10 +02:00
export default function SearchBar(props: any) {
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-04-27 14:14:10 +02:00
const theme = useMantineTheme();
const [config, setConfig] = useState<Settings>({
2022-04-27 14:14:10 +02:00
searchBar: true,
2022-04-27 23:18:57 +02:00
searchUrl: 'https://www.google.com/search?q=',
2022-04-27 20:10:51 +02:00
});
2022-04-27 23:18:57 +02:00
2022-04-27 20:10:51 +02:00
const querryUrl = config.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
});
useEffect(() => {
const config = loadSettings('settings');
2022-04-27 14:14:10 +02:00
if (config) {
showNotification({
autoClose: 1000,
title: <Text>Config loaded</Text>,
message: undefined,
});
setConfig(config);
}
}, []);
2022-04-27 20:10:51 +02:00
if (!config.searchBar) {
return null;
}
2022-04-27 14:14:10 +02:00
return (
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 />);
} else if (isTorrent) {
setIcon(<Download />);
} else {
setIcon(<Search />);
}
}}
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://thepiratebay.org/search.php?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={<Search size={18} />}
radius="md"
rightSection={icon}
size="md"
placeholder="Search the web"
{...props}
{...form.getInputProps('querry')}
/>
}
>
<Text>
tip: You can prefix your querry with <b>!yt</b> or <b>!t</b> to research on youtube or for
a torrent
</Text>
</Popover>
2022-04-27 20:10:51 +02:00
</form>
2022-04-27 14:14:10 +02:00
);
}