Files
Homarr/src/components/modules/search/SearchModule.tsx

119 lines
3.5 KiB
TypeScript
Raw Normal View History

import { TextInput, Kbd, createStyles, Text, Popover } from '@mantine/core';
2022-05-16 13:50:08 +02:00
import { useForm, useHotkeys } from '@mantine/hooks';
2022-05-17 01:52:43 +02:00
import { useRef, useState } from 'react';
import { Search, BrandYoutube, Download } from 'tabler-icons-react';
import { useConfig } from '../../../tools/state';
import { IModule } from '../modules';
2022-04-27 14:14:10 +02:00
2022-05-16 15:56:01 +02:00
const useStyles = createStyles((theme) => ({
hide: {
[theme.fn.smallerThan('sm')]: {
display: 'none',
},
display: 'flex',
alignItems: 'center',
},
}));
export const SearchModule: IModule = {
title: 'Search Bar',
description: 'Show the current time and date in a card',
icon: Search,
component: SearchBar,
};
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=';
const textInput = useRef<HTMLInputElement>();
2022-05-17 01:52:43 +02:00
useHotkeys([['ctrl+K', () => textInput.current && textInput.current.focus()]]);
2022-05-16 13:50:08 +02:00
2022-05-16 15:56:01 +02:00
const { classes, cx } = useStyles();
2022-05-16 13:50:08 +02:00
const rightSection = (
2022-05-16 15:56:01 +02:00
<div className={classes.hide}>
2022-05-16 13:50:08 +02:00
<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
// If enabled modules doesn't contain the module, return null
if (!config.settings.enabledModules.includes(SearchModule.title)) {
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) => {
const query = values.query.trim();
const isYoutube = query.startsWith('!yt');
const isTorrent = query.startsWith('!t');
form.setValues({ query: '' });
setTimeout(() => {
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}`);
}
}, 20);
2022-05-16 13:50:08 +02:00
})}
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={
2022-05-16 15:56:01 +02:00
<TextInput
variant="filled"
icon={icon}
ref={textInput}
rightSectionWidth={90}
rightSection={rightSection}
radius="md"
size="md"
styles={{ rightSection: { pointerEvents: 'none' } }}
placeholder="Search the web..."
{...props}
{...form.getInputProps('query')}
/>
2022-05-16 13:50:08 +02:00
}
>
<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
);
}