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

131 lines
4.1 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/.
*/
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';
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';
2026-01-10 19:45:02 +01:00
import { useLingui } from '@lingui/react/macro';
import { CustomTooltip } from '@/base/components/CustomTooltip.tsx';
import { SearchTextField } from '@/base/components/inputs/SearchTextField.tsx';
import { SearchParam } from '@/base/Base.types.ts';
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();
2026-01-10 19:45:02 +01:00
const { t } = useLingui();
2023-11-12 19:04:40 +01:00
const [prevLocationKey, setPrevLocationKey] = useState<string>();
const location = useLocation();
2023-11-12 19:04:40 +01:00
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);
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 = () => {
2026-03-11 00:11:29 +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
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 (
2026-01-10 19:45:02 +01:00
<CustomTooltip title={t`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
};