Files
suwayomi-material-you-webui/src/modules/core/components/AppbarSearch.tsx
T

134 lines
4.3 KiB
TypeScript
Raw Normal View History

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/.
*/
2021-12-19 18:06:27 +03:30
import React, { useState, useEffect } 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';
import { useLocation } from 'react-router-dom';
2024-09-06 16:59:27 +02:00
import { useTheme } from '@mui/material/styles';
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 {
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) => {
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-12 19:04:40 +01:00
const { t } = useTranslation();
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);
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 ?? '');
if (prevLocationKey !== location.key) {
setPrevLocationKey(location.key);
setSearchString(query ?? '');
setIsSearchOpen(!isClosable || !!query);
}
const isOpen = isSearchOpen || !!query;
2023-11-12 19:04:40 +01:00
const updateSearchOpenState = (open: boolean) => {
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);
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
2023-11-12 03:21:26 +01:00
const handleKeyboardEvent = (e: KeyboardEvent) => {
if (e.key === 'F3' || (e.ctrlKey && e.key === 'f')) {
2021-12-19 18:06:27 +03:30
e.preventDefault();
2023-11-12 19:04:40 +01:00
updateSearchOpenState(true);
2021-12-19 18:06:27 +03:30
}
};
2023-11-12 03:21:26 +01:00
useEffect(() => {
window.addEventListener('keydown', handleKeyboardEvent);
2021-12-19 18:06:27 +03:30
return () => {
2023-11-12 03:21:26 +01:00
window.removeEventListener('keydown', handleKeyboardEvent);
2021-12-19 18:06:27 +03:30
};
2023-11-12 03:21:26 +01:00
}, [handleKeyboardEvent]);
2021-12-19 18:06:27 +03:30
if (isOpen) {
2023-03-11 18:05:58 +01:00
return (
2024-02-25 23:12:27 +01:00
<SearchTextField
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)}
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
};