2022-05-29 18:42:58 +02:00
|
|
|
import {
|
2022-11-29 20:32:24 +09:00
|
|
|
ActionIcon,
|
2022-11-30 09:15:03 +09:00
|
|
|
Autocomplete,
|
2022-11-29 20:32:24 +09:00
|
|
|
Box,
|
|
|
|
|
createStyles,
|
|
|
|
|
Divider,
|
|
|
|
|
Kbd,
|
|
|
|
|
Menu,
|
|
|
|
|
Popover,
|
|
|
|
|
ScrollArea,
|
|
|
|
|
Tooltip,
|
|
|
|
|
} from '@mantine/core';
|
|
|
|
|
import { useDebouncedValue, useHotkeys } from '@mantine/hooks';
|
|
|
|
|
import { showNotification } from '@mantine/notifications';
|
2022-12-04 17:36:30 +01:00
|
|
|
import { IconBrandYoutube, IconDownload, IconMovie, IconSearch } from '@tabler/icons';
|
2023-01-26 23:23:11 +09:00
|
|
|
import { useQuery } from '@tanstack/react-query';
|
2022-05-29 21:39:57 +02:00
|
|
|
import axios from 'axios';
|
2022-12-04 17:36:30 +01:00
|
|
|
import { useTranslation } from 'next-i18next';
|
|
|
|
|
import React, { forwardRef, useEffect, useRef, useState } from 'react';
|
|
|
|
|
import { useConfigContext } from '../../../config/provider';
|
|
|
|
|
import { OverseerrMediaDisplay } from '../../../modules/common';
|
|
|
|
|
import { IModule } from '../../../modules/ModuleTypes';
|
2023-01-30 21:12:45 +01:00
|
|
|
import { ConfigType } from '../../../types/config';
|
2023-01-20 20:24:25 +01:00
|
|
|
import { searchUrls } from '../../Settings/Common/SearchEngine/SearchEngineSelector';
|
|
|
|
|
import Tip from '../Tip';
|
2023-01-19 10:39:39 +09:00
|
|
|
import { useCardStyles } from '../useCardStyles';
|
2023-01-20 20:24:25 +01:00
|
|
|
import SmallAppItem from './SmallAppItem';
|
2022-05-16 15:56:01 +02:00
|
|
|
|
2022-05-17 02:04:44 +02:00
|
|
|
export const SearchModule: IModule = {
|
2022-08-24 20:13:53 +02:00
|
|
|
title: 'Search',
|
2022-11-29 20:32:24 +09:00
|
|
|
icon: IconSearch,
|
2022-12-04 17:36:30 +01:00
|
|
|
component: Search,
|
2022-08-25 11:07:25 +02:00
|
|
|
id: 'search',
|
2022-05-17 02:04:44 +02:00
|
|
|
};
|
|
|
|
|
|
2022-11-29 20:32:24 +09:00
|
|
|
interface ItemProps extends React.ComponentPropsWithoutRef<'div'> {
|
|
|
|
|
label: string;
|
|
|
|
|
disabled: boolean;
|
|
|
|
|
value: string;
|
|
|
|
|
description: string;
|
|
|
|
|
icon: React.ReactNode;
|
|
|
|
|
url: string;
|
|
|
|
|
shortcut: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const useStyles = createStyles((theme) => ({
|
|
|
|
|
item: {
|
|
|
|
|
'&[data-hovered]': {
|
|
|
|
|
backgroundColor: theme.colors[theme.primaryColor][theme.fn.primaryShade()],
|
|
|
|
|
color: theme.white,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
2022-12-04 17:36:30 +01:00
|
|
|
export function Search() {
|
2022-11-29 20:32:24 +09:00
|
|
|
const { t } = useTranslation('modules/search');
|
2022-12-04 17:36:30 +01:00
|
|
|
const { config } = useConfigContext();
|
2022-11-29 20:32:24 +09:00
|
|
|
const [searchQuery, setSearchQuery] = useState('');
|
2023-01-22 18:01:46 +01:00
|
|
|
const [debounced] = useDebouncedValue(searchQuery, 250);
|
|
|
|
|
const { classes: cardClasses } = useCardStyles(true);
|
2022-12-04 17:36:30 +01:00
|
|
|
|
2022-12-23 17:17:57 +01:00
|
|
|
const isOverseerrEnabled = config?.apps.some(
|
|
|
|
|
(x) => x.integration.type === 'overseerr' || x.integration.type === 'jellyseerr'
|
|
|
|
|
);
|
2022-12-18 22:27:01 +01:00
|
|
|
const overseerrApp = config?.apps.find(
|
2022-12-20 11:45:33 +09:00
|
|
|
(app) => app.integration?.type === 'overseerr' || app.integration?.type === 'jellyseerr'
|
2022-08-09 13:35:59 +02:00
|
|
|
);
|
2022-12-04 17:36:30 +01:00
|
|
|
const searchEngineSettings = config?.settings.common.searchEngine;
|
|
|
|
|
const searchEngineUrl = !searchEngineSettings
|
|
|
|
|
? searchUrls.google
|
|
|
|
|
: searchEngineSettings.type === 'custom'
|
|
|
|
|
? searchEngineSettings.properties.template
|
|
|
|
|
: searchUrls[searchEngineSettings.type];
|
2022-07-24 23:18:01 +02:00
|
|
|
|
2022-11-29 20:32:24 +09:00
|
|
|
const searchEnginesList: ItemProps[] = [
|
|
|
|
|
{
|
|
|
|
|
icon: <IconSearch />,
|
|
|
|
|
disabled: false,
|
|
|
|
|
label: t('searchEngines.search.name'),
|
|
|
|
|
value: 'search',
|
|
|
|
|
description: t('searchEngines.search.description'),
|
2022-12-04 17:36:30 +01:00
|
|
|
url: searchEngineUrl,
|
2022-11-29 20:32:24 +09:00
|
|
|
shortcut: 's',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
icon: <IconDownload />,
|
|
|
|
|
disabled: false,
|
|
|
|
|
label: t('searchEngines.torrents.name'),
|
|
|
|
|
value: 'torrents',
|
|
|
|
|
description: t('searchEngines.torrents.description'),
|
|
|
|
|
url: 'https://www.torrentdownloads.me/search/?search=',
|
|
|
|
|
shortcut: 't',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
icon: <IconBrandYoutube />,
|
|
|
|
|
disabled: false,
|
|
|
|
|
label: t('searchEngines.youtube.name'),
|
|
|
|
|
value: 'youtube',
|
|
|
|
|
description: t('searchEngines.youtube.description'),
|
|
|
|
|
url: 'https://www.youtube.com/results?search_query=',
|
|
|
|
|
shortcut: 'y',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
icon: <IconMovie />,
|
2022-12-18 22:27:01 +01:00
|
|
|
disabled: !(isOverseerrEnabled === true && overseerrApp !== undefined),
|
2022-11-29 20:32:24 +09:00
|
|
|
label: t('searchEngines.overseerr.name'),
|
|
|
|
|
value: 'overseerr',
|
|
|
|
|
description: t('searchEngines.overseerr.description'),
|
2022-12-18 22:27:01 +01:00
|
|
|
url: `${overseerrApp?.url}search?query=`,
|
2022-11-29 20:32:24 +09:00
|
|
|
shortcut: 'm',
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
const [selectedSearchEngine, setSearchEngine] = useState<ItemProps>(searchEnginesList[0]);
|
2022-12-18 22:27:01 +01:00
|
|
|
const matchingApps =
|
|
|
|
|
config?.apps.filter((app) => {
|
2022-12-04 17:36:30 +01:00
|
|
|
if (searchQuery === '' || searchQuery === undefined) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-12-18 22:27:01 +01:00
|
|
|
return app.name.toLowerCase().includes(searchQuery.toLowerCase());
|
2022-12-04 17:36:30 +01:00
|
|
|
}) ?? [];
|
2022-12-18 22:27:01 +01:00
|
|
|
const autocompleteData = matchingApps.map((app) => ({
|
|
|
|
|
label: app.name,
|
|
|
|
|
value: app.name,
|
|
|
|
|
icon: app.appearance.iconUrl,
|
2022-12-22 11:29:51 +09:00
|
|
|
url: app.behaviour.externalUrl ?? app.url,
|
2022-11-30 09:15:03 +09:00
|
|
|
}));
|
|
|
|
|
const AutoCompleteItem = forwardRef<HTMLDivElement, any>(
|
|
|
|
|
({ label, value, icon, url, ...others }: any, ref) => (
|
|
|
|
|
<div ref={ref} {...others}>
|
2022-12-18 22:27:01 +01:00
|
|
|
<SmallAppItem app={{ label, value, icon, url }} />
|
2022-11-30 09:15:03 +09:00
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
);
|
2022-11-30 00:42:10 +09:00
|
|
|
useEffect(() => {
|
|
|
|
|
// Refresh the default search engine every time the config for it changes #521
|
|
|
|
|
setSearchEngine(searchEnginesList[0]);
|
2022-12-04 17:36:30 +01:00
|
|
|
}, [searchEngineUrl]);
|
2022-11-29 20:32:24 +09:00
|
|
|
const textInput = useRef<HTMLInputElement>(null);
|
2022-12-04 17:36:30 +01:00
|
|
|
useHotkeys([['mod+K', () => textInput.current?.focus()]]);
|
2022-11-29 20:32:24 +09:00
|
|
|
const { classes } = useStyles();
|
2023-01-30 21:12:45 +01:00
|
|
|
const openTarget = getOpenTarget(config);
|
2022-07-24 23:18:01 +02:00
|
|
|
const [opened, setOpened] = useState(false);
|
|
|
|
|
|
2023-01-28 23:01:15 +01:00
|
|
|
const {
|
|
|
|
|
data: OverseerrResults,
|
|
|
|
|
isLoading,
|
|
|
|
|
error,
|
|
|
|
|
} = useQuery(
|
2023-01-26 23:23:11 +09:00
|
|
|
['overseerr', debounced],
|
|
|
|
|
async () => {
|
2023-01-31 11:45:52 +09:00
|
|
|
const res = await axios.get(`/api/modules/overseerr?query=${debounced}`);
|
|
|
|
|
return res.data.results ?? [];
|
2023-01-26 23:23:11 +09:00
|
|
|
},
|
|
|
|
|
{
|
2023-01-31 11:45:52 +09:00
|
|
|
enabled:
|
|
|
|
|
isOverseerrEnabled === true &&
|
|
|
|
|
selectedSearchEngine.value === 'overseerr' &&
|
|
|
|
|
debounced.length > 3,
|
2023-01-26 23:23:11 +09:00
|
|
|
refetchOnWindowFocus: false,
|
|
|
|
|
refetchOnMount: false,
|
|
|
|
|
refetchInterval: false,
|
2022-07-24 23:18:01 +02:00
|
|
|
}
|
2023-01-26 23:23:11 +09:00
|
|
|
);
|
|
|
|
|
|
2022-12-04 17:36:30 +01:00
|
|
|
const isModuleEnabled = config?.settings.customization.layout.enabledSearchbar;
|
2022-07-24 23:18:01 +02:00
|
|
|
if (!isModuleEnabled) {
|
2022-04-27 20:10:51 +02:00
|
|
|
return null;
|
|
|
|
|
}
|
2023-01-30 21:12:45 +01:00
|
|
|
|
2022-11-29 20:32:24 +09:00
|
|
|
//TODO: Fix the bug where clicking anything inside the Modal to ask for a movie
|
|
|
|
|
// will close it (Because it closes the underlying Popover)
|
2022-04-27 14:14:10 +02:00
|
|
|
return (
|
2022-11-30 09:15:33 +09:00
|
|
|
<Box style={{ width: '100%', maxWidth: 400 }}>
|
2022-07-24 23:18:01 +02:00
|
|
|
<Popover
|
2023-01-28 23:01:15 +01:00
|
|
|
opened={OverseerrResults && OverseerrResults.length > 0 && opened && searchQuery.length > 3}
|
2022-07-24 23:18:01 +02:00
|
|
|
position="bottom"
|
2022-08-07 17:20:34 +02:00
|
|
|
withinPortal
|
|
|
|
|
shadow="md"
|
2022-06-06 20:02:21 +02:00
|
|
|
radius="md"
|
2022-08-08 13:45:54 +02:00
|
|
|
zIndex={100}
|
|
|
|
|
transition="pop-top-right"
|
2022-08-07 17:20:34 +02:00
|
|
|
>
|
|
|
|
|
<Popover.Target>
|
2022-11-30 09:15:03 +09:00
|
|
|
<Autocomplete
|
2022-11-29 20:32:24 +09:00
|
|
|
ref={textInput}
|
2022-08-07 17:20:34 +02:00
|
|
|
onFocusCapture={() => setOpened(true)}
|
2022-07-24 23:18:01 +02:00
|
|
|
autoFocus
|
2022-11-29 20:32:24 +09:00
|
|
|
rightSection={<SearchModuleMenu />}
|
|
|
|
|
placeholder={t(`searchEngines.${selectedSearchEngine.value}.description`)}
|
|
|
|
|
value={searchQuery}
|
2022-11-30 09:15:03 +09:00
|
|
|
onChange={(currentString) => tryMatchSearchEngine(currentString, setSearchQuery)}
|
|
|
|
|
itemComponent={AutoCompleteItem}
|
|
|
|
|
data={autocompleteData}
|
|
|
|
|
onItemSubmit={(item) => {
|
|
|
|
|
setOpened(false);
|
|
|
|
|
if (item.url) {
|
|
|
|
|
setSearchQuery('');
|
2023-01-30 21:12:45 +01:00
|
|
|
window.open(item.openedUrl ? item.openedUrl : item.url, openTarget);
|
2022-11-30 09:15:03 +09:00
|
|
|
}
|
|
|
|
|
}}
|
2022-11-29 20:32:24 +09:00
|
|
|
// Replace %s if it is in selectedSearchEngine.url with searchQuery, otherwise append searchQuery at the end of it
|
|
|
|
|
onKeyDown={(event) => {
|
2022-11-30 09:15:03 +09:00
|
|
|
if (
|
|
|
|
|
event.key === 'Enter' &&
|
|
|
|
|
searchQuery.length > 0 &&
|
|
|
|
|
autocompleteData.length === 0
|
|
|
|
|
) {
|
2022-11-29 20:32:24 +09:00
|
|
|
if (selectedSearchEngine.url.includes('%s')) {
|
2023-01-30 21:12:45 +01:00
|
|
|
window.open(selectedSearchEngine.url.replace('%s', searchQuery), openTarget);
|
2022-11-29 20:32:24 +09:00
|
|
|
} else {
|
2023-01-30 21:12:45 +01:00
|
|
|
window.open(selectedSearchEngine.url + searchQuery, openTarget);
|
2022-11-29 20:32:24 +09:00
|
|
|
}
|
2022-08-24 18:44:11 +02:00
|
|
|
}
|
|
|
|
|
}}
|
2023-01-19 10:39:39 +09:00
|
|
|
classNames={{
|
|
|
|
|
input: cardClasses.card,
|
|
|
|
|
}}
|
|
|
|
|
radius="lg"
|
2022-11-30 09:15:03 +09:00
|
|
|
size="md"
|
2022-07-24 23:18:01 +02:00
|
|
|
/>
|
2022-08-07 17:20:34 +02:00
|
|
|
</Popover.Target>
|
2022-08-09 13:35:59 +02:00
|
|
|
<Popover.Dropdown>
|
2023-01-26 23:23:11 +09:00
|
|
|
<ScrollArea style={{ height: '80vh', maxWidth: '90vw' }} offsetScrollbars>
|
2023-01-30 21:12:45 +01:00
|
|
|
{OverseerrResults &&
|
|
|
|
|
OverseerrResults.slice(0, 4).map((result: any, index: number) => (
|
|
|
|
|
<React.Fragment key={index}>
|
|
|
|
|
<OverseerrMediaDisplay key={result.id} media={result} />
|
|
|
|
|
{index < OverseerrResults.length - 1 && index < 3 && (
|
|
|
|
|
<Divider variant="dashed" my="xs" />
|
|
|
|
|
)}
|
|
|
|
|
</React.Fragment>
|
|
|
|
|
))}
|
2023-01-26 23:23:11 +09:00
|
|
|
</ScrollArea>
|
2022-08-07 17:20:34 +02:00
|
|
|
</Popover.Dropdown>
|
2022-07-24 23:18:01 +02:00
|
|
|
</Popover>
|
2022-11-29 20:32:24 +09:00
|
|
|
</Box>
|
2022-04-27 14:14:10 +02:00
|
|
|
);
|
2022-11-29 20:32:24 +09:00
|
|
|
|
|
|
|
|
function tryMatchSearchEngine(query: string, setSearchQuery: (value: string) => void) {
|
|
|
|
|
const foundSearchEngine = searchEnginesList.find(
|
|
|
|
|
(engine) => query.includes(`!${engine.shortcut}`) && !engine.disabled
|
|
|
|
|
);
|
|
|
|
|
if (foundSearchEngine) {
|
|
|
|
|
setSearchQuery(query.replace(`!${foundSearchEngine.shortcut}`, ''));
|
|
|
|
|
changeSearchEngine(foundSearchEngine);
|
|
|
|
|
} else {
|
|
|
|
|
setSearchQuery(query);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function SearchModuleMenu() {
|
|
|
|
|
return (
|
|
|
|
|
<Menu shadow="md" width={200} withinPortal classNames={classes}>
|
|
|
|
|
<Menu.Target>
|
|
|
|
|
<ActionIcon>{selectedSearchEngine.icon}</ActionIcon>
|
|
|
|
|
</Menu.Target>
|
|
|
|
|
|
|
|
|
|
<Menu.Dropdown>
|
|
|
|
|
{searchEnginesList.map((item) => (
|
|
|
|
|
<Tooltip
|
|
|
|
|
multiline
|
|
|
|
|
label={item.description}
|
|
|
|
|
withinPortal
|
|
|
|
|
width={200}
|
|
|
|
|
position="left"
|
|
|
|
|
key={item.value}
|
|
|
|
|
>
|
|
|
|
|
<Menu.Item
|
|
|
|
|
key={item.value}
|
|
|
|
|
icon={item.icon}
|
|
|
|
|
rightSection={<Kbd>!{item.shortcut}</Kbd>}
|
|
|
|
|
disabled={item.disabled}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
changeSearchEngine(item);
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{item.label}
|
|
|
|
|
</Menu.Item>
|
|
|
|
|
</Tooltip>
|
|
|
|
|
))}
|
|
|
|
|
<Menu.Divider />
|
|
|
|
|
<Menu.Label>
|
|
|
|
|
<Tip>
|
|
|
|
|
{t('tip')} <Kbd>mod+k</Kbd>{' '}
|
|
|
|
|
</Tip>
|
|
|
|
|
</Menu.Label>
|
|
|
|
|
</Menu.Dropdown>
|
|
|
|
|
</Menu>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function changeSearchEngine(item: ItemProps) {
|
|
|
|
|
setSearchEngine(item);
|
|
|
|
|
showNotification({
|
|
|
|
|
radius: 'lg',
|
|
|
|
|
disallowClose: true,
|
|
|
|
|
id: 'spotlight',
|
|
|
|
|
autoClose: 1000,
|
|
|
|
|
icon: <ActionIcon size="sm">{item.icon}</ActionIcon>,
|
|
|
|
|
message: t('switchedSearchEngine', { searchEngine: t(`searchEngines.${item.value}.name`) }),
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-04-27 14:14:10 +02:00
|
|
|
}
|
2023-01-30 21:12:45 +01:00
|
|
|
|
|
|
|
|
const getOpenTarget = (config: ConfigType | undefined): '_blank' | '_self' => {
|
|
|
|
|
if (!config || config.settings.common.searchEngine.properties.openInNewTab === undefined) {
|
|
|
|
|
return '_blank';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return config.settings.common.searchEngine.properties.openInNewTab ? '_blank' : '_self';
|
|
|
|
|
};
|