🐛 Fix Popover open state

This commit is contained in:
ajnart
2022-08-09 13:35:59 +02:00
parent 67a274804f
commit c76ef9643b

View File

@@ -1,5 +1,5 @@
import { Kbd, createStyles, Autocomplete, Popover, ScrollArea, Divider } from '@mantine/core'; import { Kbd, createStyles, Autocomplete, Popover, ScrollArea, Divider } from '@mantine/core';
import { useDebouncedValue, useHotkeys } from '@mantine/hooks'; import { useClickOutside, useDebouncedValue, useHotkeys } from '@mantine/hooks';
import { useForm } from '@mantine/form'; import { useForm } from '@mantine/form';
import React, { useEffect, useRef, useState } from 'react'; import React, { useEffect, useRef, useState } from 'react';
import { import {
@@ -38,7 +38,9 @@ export default function SearchBar(props: any) {
const { config } = useConfig(); const { config } = useConfig();
const isModuleEnabled = config.modules?.[SearchModule.title]?.enabled ?? false; const isModuleEnabled = config.modules?.[SearchModule.title]?.enabled ?? false;
const isOverseerrEnabled = config.modules?.[OverseerrModule.title]?.enabled ?? false; const isOverseerrEnabled = config.modules?.[OverseerrModule.title]?.enabled ?? false;
const OverseerrService = config.services.find((service) => service.type === 'Overseerr' || service.type === 'Jellyseerr'); const OverseerrService = config.services.find(
(service) => service.type === 'Overseerr' || service.type === 'Jellyseerr'
);
const queryUrl = config.settings.searchUrl ?? 'https://www.google.com/search?q='; const queryUrl = config.settings.searchUrl ?? 'https://www.google.com/search?q=';
const [OverseerrResults, setOverseerrResults] = useState<any[]>([]); const [OverseerrResults, setOverseerrResults] = useState<any[]>([]);
@@ -46,6 +48,7 @@ export default function SearchBar(props: any) {
const [icon, setIcon] = useState(<Search />); const [icon, setIcon] = useState(<Search />);
const [results, setResults] = useState<any[]>([]); const [results, setResults] = useState<any[]>([]);
const [opened, setOpened] = useState(false); const [opened, setOpened] = useState(false);
const ref = useClickOutside(() => setOpened(false));
const textInput = useRef<HTMLInputElement>(); const textInput = useRef<HTMLInputElement>();
useHotkeys([['ctrl+K', () => textInput.current && textInput.current.focus()]]); useHotkeys([['ctrl+K', () => textInput.current && textInput.current.focus()]]);
@@ -61,7 +64,8 @@ export default function SearchBar(props: any) {
if (OverseerrService === undefined && isOverseerrEnabled) { if (OverseerrService === undefined && isOverseerrEnabled) {
showNotification({ showNotification({
title: 'Overseerr integration', title: 'Overseerr integration',
message: 'Module enabled but no service is configured with the type "Overseerr" / "Jellyseerr"', message:
'Module enabled but no service is configured with the type "Overseerr" / "Jellyseerr"',
color: 'red', color: 'red',
}); });
} }
@@ -177,7 +181,8 @@ export default function SearchBar(props: any) {
/> />
</Popover.Target> </Popover.Target>
<Popover.Dropdown onMouseLeave={() => setOpened(false)}> <Popover.Dropdown>
<div ref={ref}>
<ScrollArea style={{ height: 400, width: 400 }} offsetScrollbars> <ScrollArea style={{ height: 400, width: 400 }} offsetScrollbars>
{OverseerrResults.slice(0, 5).map((result, index) => ( {OverseerrResults.slice(0, 5).map((result, index) => (
<React.Fragment key={index}> <React.Fragment key={index}>
@@ -186,6 +191,7 @@ export default function SearchBar(props: any) {
</React.Fragment> </React.Fragment>
))} ))}
</ScrollArea> </ScrollArea>
</div>
</Popover.Dropdown> </Popover.Dropdown>
</Popover> </Popover>
</form> </form>