Files
Homarr/components/SearchBar/SearchBar.tsx

83 lines
2.4 KiB
TypeScript
Raw Normal View History

2022-04-27 23:18:57 +02:00
import { Input, TextInput, Text, ActionIcon, useMantineTheme, Center } 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 15:05:42 +02:00
import { Search, ArrowRight, ArrowLeft, BrandYoutube, Download } from 'tabler-icons-react';
2022-04-27 14:14:10 +02:00
import { Config, loadConfig } from '../../tools/config';
export default function SearchBar(props: any) {
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<Config>({
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 = loadConfig('settings');
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-27 20:10:51 +02:00
<TextInput
2022-04-28 15:05:42 +02:00
variant="filled"
color="blue"
2022-04-27 20:10:51 +02:00
icon={<Search size={18} />}
2022-04-28 15:05:42 +02:00
radius="md"
rightSection={icon}
2022-04-27 20:10:51 +02:00
size="md"
placeholder="Search the web"
{...props}
{...form.getInputProps('querry')}
/>
</form>
2022-04-27 14:14:10 +02:00
);
}