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

92 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 />);
2022-05-16 12:38:46 +02:00
const queryUrl = 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: {
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
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
mb="xl"
style={{
width: '100%',
2022-04-28 15:05:42 +02:00
}}
>
<form
onChange={() => {
2022-05-16 12:38:46 +02:00
// 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 17:27:39 +02:00
}}
onSubmit={form.onSubmit((values) => {
2022-05-16 12:38:46 +02:00
// 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) {
2022-05-16 12:38:46 +02:00
window.open(`https://www.youtube.com/results?search_query=${query.substring(3)}`);
} else if (isTorrent) {
2022-05-16 12:38:46 +02:00
window.open(`https://bitsearch.to/search?q=${query.substring(3)}`);
} else {
2022-05-16 12:38:46 +02:00
window.open(`${queryUrl}${values.query}`);
}
})}
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}
2022-05-16 12:38:46 +02:00
{...form.getInputProps('query')}
/>
}
>
<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>
</Box>
2022-04-27 14:14:10 +02:00
);
}