2021-10-31 12:45:34 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) Contributors to the Suwayomi project
|
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
|
|
|
|
|
2025-05-02 17:56:56 +02:00
|
|
|
import React, { useState } from 'react';
|
2021-10-31 12:45:34 +01:00
|
|
|
import SearchIcon from '@mui/icons-material/Search';
|
2024-04-14 15:50:12 +02:00
|
|
|
import IconButton from '@mui/material/IconButton';
|
2021-12-19 18:06:27 +03:30
|
|
|
import { useQueryParam, StringParam } from 'use-query-params';
|
2023-11-05 17:22:29 +01:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2024-04-07 15:22:03 +02:00
|
|
|
import { useLocation } from 'react-router-dom';
|
2024-09-06 16:59:27 +02:00
|
|
|
import { useTheme } from '@mui/material/styles';
|
2025-05-02 17:56:56 +02:00
|
|
|
import { useHotkeys } from 'react-hotkeys-hook';
|
2025-02-01 14:24:38 +01:00
|
|
|
import { CustomTooltip } from '@/modules/core/components/CustomTooltip.tsx';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { SearchTextField } from '@/modules/core/components/inputs/SearchTextField.tsx';
|
2021-10-31 12:45:34 +01:00
|
|
|
|
2022-03-12 04:46:55 +00:00
|
|
|
interface IProps {
|
2024-04-07 15:22:03 +02:00
|
|
|
isClosable?: boolean;
|
2022-03-12 04:46:55 +00:00
|
|
|
}
|
|
|
|
|
|
2023-10-28 00:32:02 +02:00
|
|
|
export const AppbarSearch: React.FunctionComponent<IProps> = (props) => {
|
2024-04-07 15:22:03 +02:00
|
|
|
const { isClosable = true } = props;
|
2023-11-12 19:04:40 +01:00
|
|
|
|
2024-09-06 16:59:27 +02:00
|
|
|
const theme = useTheme();
|
2023-11-05 17:22:29 +01:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
2024-04-07 15:22:03 +02:00
|
|
|
const [prevLocationKey, setPrevLocationKey] = useState<string>();
|
|
|
|
|
const location = useLocation();
|
2023-11-12 19:04:40 +01:00
|
|
|
|
2021-12-19 18:06:27 +03:30
|
|
|
const [query, setQuery] = useQueryParam('query', StringParam);
|
2024-04-07 15:22:03 +02:00
|
|
|
const [isSearchOpen, setIsSearchOpen] = useState(!isClosable || !!query);
|
2025-04-18 20:42:41 +02:00
|
|
|
const inputRef = React.useRef<HTMLInputElement>(undefined);
|
2021-10-31 12:45:34 +01:00
|
|
|
|
2023-11-12 03:21:26 +01:00
|
|
|
const [searchString, setSearchString] = useState(query ?? '');
|
|
|
|
|
|
2024-04-07 15:22:03 +02:00
|
|
|
if (prevLocationKey !== location.key) {
|
|
|
|
|
setPrevLocationKey(location.key);
|
2024-04-10 23:46:08 +02:00
|
|
|
setSearchString(query ?? '');
|
|
|
|
|
setIsSearchOpen(!isClosable || !!query);
|
2024-04-07 15:22:03 +02:00
|
|
|
}
|
|
|
|
|
|
2024-04-10 23:46:08 +02:00
|
|
|
const isOpen = isSearchOpen || !!query;
|
2024-04-07 15:22:03 +02:00
|
|
|
|
2023-11-12 19:04:40 +01:00
|
|
|
const updateSearchOpenState = (open: boolean) => {
|
2024-04-07 15:22:03 +02:00
|
|
|
if (!isClosable) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setIsSearchOpen(open);
|
2023-11-12 19:04:40 +01:00
|
|
|
|
|
|
|
|
// try to focus input component since in case of navigating to the previous/next page in the browser history
|
|
|
|
|
// the "openSearch" state might not change and thus, won't trigger a focus
|
|
|
|
|
if (open) {
|
|
|
|
|
inputRef.current?.focus();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-11-12 03:21:26 +01:00
|
|
|
function handleChange(newQuery: string) {
|
|
|
|
|
if (newQuery === '') {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setQuery(newQuery);
|
2024-04-07 15:22:03 +02:00
|
|
|
updateSearchOpenState(false);
|
2021-10-31 12:45:34 +01:00
|
|
|
}
|
2023-02-06 10:06:33 +01:00
|
|
|
|
2021-10-31 12:45:34 +01:00
|
|
|
const cancelSearch = () => {
|
2023-11-12 03:21:26 +01:00
|
|
|
setSearchString('');
|
2023-11-12 19:04:40 +01:00
|
|
|
setQuery(undefined);
|
|
|
|
|
updateSearchOpenState(false);
|
2021-10-31 12:45:34 +01:00
|
|
|
};
|
2023-02-06 10:06:33 +01:00
|
|
|
const handleBlur = () => {
|
2023-11-12 19:04:40 +01:00
|
|
|
if (!searchString) updateSearchOpenState(false);
|
2021-10-31 12:45:34 +01:00
|
|
|
};
|
2021-12-19 18:06:27 +03:30
|
|
|
|
2025-05-02 17:56:56 +02:00
|
|
|
useHotkeys(
|
|
|
|
|
'ctrl+f, F3',
|
|
|
|
|
() => {
|
2023-11-12 19:04:40 +01:00
|
|
|
updateSearchOpenState(true);
|
2025-05-02 17:56:56 +02:00
|
|
|
},
|
|
|
|
|
{ preventDefault: true },
|
|
|
|
|
);
|
2021-12-19 18:06:27 +03:30
|
|
|
|
2024-04-07 15:22:03 +02:00
|
|
|
if (isOpen) {
|
2023-03-11 18:05:58 +01:00
|
|
|
return (
|
2024-02-25 23:12:27 +01:00
|
|
|
<SearchTextField
|
2024-04-07 15:22:03 +02:00
|
|
|
autoFocus
|
2024-02-25 23:12:27 +01:00
|
|
|
variant="standard"
|
2023-11-12 03:21:26 +01:00
|
|
|
value={searchString}
|
2024-02-25 23:12:27 +01:00
|
|
|
onCancel={cancelSearch}
|
2023-11-12 03:21:26 +01:00
|
|
|
onChange={(e) => setSearchString(e.target.value)}
|
2023-11-26 14:29:03 +01:00
|
|
|
onKeyDown={(e) => {
|
|
|
|
|
if (e.key === 'Enter') {
|
|
|
|
|
handleChange(searchString);
|
|
|
|
|
}
|
|
|
|
|
}}
|
2023-03-11 18:05:58 +01:00
|
|
|
onBlur={handleBlur}
|
|
|
|
|
inputRef={inputRef}
|
2024-09-06 16:59:27 +02:00
|
|
|
sx={{
|
|
|
|
|
...theme.applyStyles('light', {
|
|
|
|
|
'& .MuiInput-underline:before': {
|
|
|
|
|
borderBottomColor: 'primary.contrastText', // Default color
|
|
|
|
|
},
|
|
|
|
|
'& .MuiInput-underline:hover:before': {
|
|
|
|
|
borderBottomColor: 'primary.contrastText', // Hover color
|
|
|
|
|
},
|
|
|
|
|
'& .MuiInput-underline:after': {
|
|
|
|
|
borderBottomColor: 'primary.dark', // Focused color
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
}}
|
|
|
|
|
cancelButtonProps={{ sx: { ...theme.applyStyles('light', { color: 'primary.contrastText' }) } }}
|
2023-03-11 18:05:58 +01:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-31 12:45:34 +01:00
|
|
|
return (
|
2025-02-01 14:24:38 +01:00
|
|
|
<CustomTooltip title={t('search.title.search')}>
|
2024-09-06 16:59:27 +02:00
|
|
|
<IconButton onClick={() => updateSearchOpenState(true)} color="inherit">
|
2023-11-05 17:22:29 +01:00
|
|
|
<SearchIcon />
|
|
|
|
|
</IconButton>
|
2025-02-01 14:24:38 +01:00
|
|
|
</CustomTooltip>
|
2021-10-31 12:45:34 +01:00
|
|
|
);
|
2022-03-12 04:46:55 +00:00
|
|
|
};
|