Files
suwayomi-material-you-webui/src/base/components/AppbarSearch.tsx

129 lines
4.1 KiB
TypeScript
Raw Normal View History

/*
* 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';
import SearchIcon from '@mui/icons-material/Search';
import IconButton from '@mui/material/IconButton';
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-05-02 17:56:56 +02:00
import { useHotkeys } from 'react-hotkeys-hook';
import { CustomTooltip } from '@/base/components/CustomTooltip.tsx';
import { SearchTextField } from '@/base/components/inputs/SearchTextField.tsx';
import { SearchParam } from '@/base/Base.types.ts';
interface IProps {
isClosable?: boolean;
}
2023-10-28 00:32:02 +02:00
export const AppbarSearch: React.FunctionComponent<IProps> = (props) => {
const { isClosable = true } = props;
2024-09-06 16:59:27 +02:00
const theme = useTheme();
2023-11-05 17:22:29 +01:00
const { t } = useTranslation();
const [prevLocationKey, setPrevLocationKey] = useState<string>();
const location = useLocation();
2025-07-23 01:52:41 +02:00
const [query, setQuery] = useQueryParam(SearchParam.QUERY, StringParam);
const [isSearchOpen, setIsSearchOpen] = useState(!isClosable || !!query);
2025-04-18 20:42:41 +02:00
const inputRef = React.useRef<HTMLInputElement>(undefined);
const [searchString, setSearchString] = useState(query ?? '');
if (prevLocationKey !== location.key) {
setPrevLocationKey(location.key);
setSearchString(query ?? '');
setIsSearchOpen(!isClosable || !!query);
}
const isOpen = isSearchOpen || !!query;
const updateSearchOpenState = (open: boolean) => {
if (!isClosable) {
return;
}
setIsSearchOpen(open);
// 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();
}
};
function handleChange(newQuery: string) {
if (newQuery === '') {
return;
}
setQuery(newQuery);
updateSearchOpenState(false);
}
const cancelSearch = () => {
setSearchString('');
setQuery(undefined);
updateSearchOpenState(false);
};
const handleBlur = () => {
if (!searchString) updateSearchOpenState(false);
};
2025-05-02 17:56:56 +02:00
useHotkeys(
'ctrl+f, F3',
() => {
updateSearchOpenState(true);
2025-05-02 17:56:56 +02:00
},
{ preventDefault: true },
);
if (isOpen) {
return (
2024-02-25 23:12:27 +01:00
<SearchTextField
autoFocus
2024-02-25 23:12:27 +01:00
variant="standard"
value={searchString}
2024-02-25 23:12:27 +01:00
onCancel={cancelSearch}
onChange={(e) => setSearchString(e.target.value)}
onKeyDown={(e) => {
if (e.key === 'Enter') {
handleChange(searchString);
}
}}
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' }) } }}
/>
);
}
return (
<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>
</CustomTooltip>
);
};