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-02-25 23:12:27 +01:00
|
|
|
import { IconButton, Tooltip } from '@mui/material';
|
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';
|
2023-11-12 19:04:40 +01:00
|
|
|
import { useLocation, useNavigate } from 'react-router-dom';
|
2024-02-25 23:12:27 +01:00
|
|
|
import { SearchTextField } from '@/components/atoms/SearchTextField.tsx';
|
2021-10-31 12:45:34 +01:00
|
|
|
|
2022-03-12 04:46:55 +00:00
|
|
|
interface IProps {
|
2023-02-06 10:06:33 +01:00
|
|
|
autoOpen?: boolean;
|
2022-03-12 04:46:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
|
autoOpen: false,
|
|
|
|
|
};
|
|
|
|
|
|
2023-10-28 00:32:02 +02:00
|
|
|
export const AppbarSearch: React.FunctionComponent<IProps> = (props) => {
|
2023-11-12 19:04:40 +01:00
|
|
|
const { autoOpen } = props;
|
|
|
|
|
|
2023-11-05 17:22:29 +01:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
2023-11-12 19:04:40 +01:00
|
|
|
const { pathname, search: locationSearch, state: fullLocationState } = useLocation<{ wasSearchOpen?: boolean }>();
|
|
|
|
|
const { wasSearchOpen, ...locationState } = fullLocationState ?? {};
|
|
|
|
|
|
|
|
|
|
const navigate = useNavigate();
|
|
|
|
|
|
2021-12-19 18:06:27 +03:30
|
|
|
const [query, setQuery] = useQueryParam('query', StringParam);
|
2021-10-31 12:45:34 +01:00
|
|
|
const [searchOpen, setSearchOpen] = useState(!!query);
|
|
|
|
|
const inputRef = React.useRef<HTMLInputElement>();
|
|
|
|
|
|
2023-11-12 03:21:26 +01:00
|
|
|
const [searchString, setSearchString] = useState(query ?? '');
|
|
|
|
|
|
2023-11-12 19:04:40 +01:00
|
|
|
const updateSearchOpenState = (open: boolean) => {
|
|
|
|
|
setSearchOpen(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();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-11-12 03:21:26 +01:00
|
|
|
function handleChange(newQuery: string) {
|
|
|
|
|
if (newQuery === '') {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setQuery(newQuery);
|
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) => {
|
2023-11-26 14:29:03 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-03-12 04:46:55 +00:00
|
|
|
useEffect(() => {
|
2023-11-12 19:04:40 +01:00
|
|
|
if ((autoOpen && wasSearchOpen === undefined) || (wasSearchOpen && query)) {
|
|
|
|
|
updateSearchOpenState(true);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateSearchOpenState(false);
|
|
|
|
|
}, [autoOpen, pathname]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!searchOpen || !inputRef.current) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inputRef.current.focus();
|
|
|
|
|
}, [searchOpen, inputRef.current]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (wasSearchOpen === searchOpen) {
|
|
|
|
|
return;
|
2022-03-12 04:46:55 +00:00
|
|
|
}
|
2023-11-12 19:04:40 +01:00
|
|
|
|
|
|
|
|
navigate(
|
|
|
|
|
{ pathname, search: locationSearch },
|
|
|
|
|
{ replace: true, state: { ...locationState, wasSearchOpen: searchOpen } },
|
|
|
|
|
);
|
|
|
|
|
}, [searchOpen]);
|
2022-03-12 04:46:55 +00:00
|
|
|
|
2021-12-19 18:06:27 +03:30
|
|
|
useEffect(() => {
|
2023-11-12 03:21:26 +01:00
|
|
|
if (query === undefined && searchString !== undefined) {
|
|
|
|
|
setSearchString('');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-12 19:04:40 +01:00
|
|
|
if (query && searchString !== query) {
|
2023-11-12 03:21:26 +01:00
|
|
|
setSearchString(query);
|
2023-11-12 19:04:40 +01:00
|
|
|
updateSearchOpenState(true);
|
2023-11-12 03:21:26 +01:00
|
|
|
}
|
|
|
|
|
}, [query]);
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2023-03-11 18:05:58 +01:00
|
|
|
if (searchOpen) {
|
|
|
|
|
return (
|
2024-02-25 23:12:27 +01:00
|
|
|
<SearchTextField
|
|
|
|
|
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}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-31 12:45:34 +01:00
|
|
|
return (
|
2023-11-05 17:22:29 +01:00
|
|
|
<Tooltip title={t('search.title.search')}>
|
2023-11-12 19:04:40 +01:00
|
|
|
<IconButton onClick={() => updateSearchOpenState(true)}>
|
2023-11-05 17:22:29 +01:00
|
|
|
<SearchIcon />
|
|
|
|
|
</IconButton>
|
|
|
|
|
</Tooltip>
|
2021-10-31 12:45:34 +01:00
|
|
|
);
|
2022-03-12 04:46:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
AppbarSearch.defaultProps = defaultProps;
|