import { TextInput, Kbd, createStyles, Text, Popover } from '@mantine/core'; import { useForm, useHotkeys } from '@mantine/hooks'; import { useRef, useState } from 'react'; import { IconSearch as Search, IconBrandYoutube as BrandYoutube, IconDownload as Download, } from '@tabler/icons'; import { useConfig } from '../../../tools/state'; import { IModule } from '../modules'; 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, }; export default function SearchBar(props: any) { const { config, setConfig } = useConfig(); const [opened, setOpened] = useState(false); const [icon, setIcon] = useState(); const queryUrl = config.settings.searchUrl ?? 'https://www.google.com/search?q='; const textInput = useRef(); useHotkeys([['ctrl+K', () => textInput.current && textInput.current.focus()]]); const { classes, cx } = useStyles(); const rightSection = (
Ctrl + K
); const form = useForm({ initialValues: { query: '', }, }); // If enabled modules doesn't contain the module, return null // If module in enabled const exists = config.modules?.[SearchModule.title]?.enabled ?? false; if (!exists) { return null; } return (
{ // 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(); } else if (isTorrent) { setIcon(); } else { setIcon(); } }} 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); })} > setOpened(true)} onBlurCapture={() => setOpened(false)} target={ } > tip: Use the prefixes !yt and !t in front of your query to search on YouTube or for a Torrent respectively. ); }