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';
|
2023-11-05 17:22:29 +01:00
|
|
|
import { IconButton, Input, Tooltip } from '@mui/material';
|
2021-10-31 12:45:34 +01:00
|
|
|
import CancelIcon from '@mui/icons-material/Cancel';
|
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 03:21:26 +01:00
|
|
|
import { useBackButton } from '@/util/useBackButton.ts';
|
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-05 17:22:29 +01:00
|
|
|
const { t } = useTranslation();
|
2023-11-12 03:21:26 +01:00
|
|
|
const handleBack = useBackButton();
|
2023-11-05 17:22:29 +01:00
|
|
|
|
2022-03-12 04:46:55 +00:00
|
|
|
const { autoOpen } = props;
|
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 ?? '');
|
|
|
|
|
|
|
|
|
|
function handleChange(newQuery: string) {
|
|
|
|
|
if (newQuery === '') {
|
|
|
|
|
handleBack();
|
|
|
|
|
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('');
|
2021-10-31 12:45:34 +01:00
|
|
|
setSearchOpen(false);
|
2023-11-12 03:21:26 +01:00
|
|
|
|
|
|
|
|
handleBack();
|
2021-10-31 12:45:34 +01:00
|
|
|
};
|
2023-02-06 10:06:33 +01:00
|
|
|
const handleBlur = () => {
|
2023-11-12 03:21:26 +01:00
|
|
|
if (!searchString) setSearchOpen(false);
|
2023-02-06 10:06:33 +01:00
|
|
|
};
|
2021-10-31 12:45:34 +01:00
|
|
|
const openSearch = () => {
|
|
|
|
|
setSearchOpen(true);
|
|
|
|
|
// Put Focus Action at the end of the Callstack so Input actually exists on the dom
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
if (inputRef && inputRef.current) inputRef.current.focus();
|
|
|
|
|
});
|
|
|
|
|
};
|
2021-12-19 18:06:27 +03:30
|
|
|
|
2023-11-12 03:21:26 +01:00
|
|
|
const handleKeyboardEvent = (e: KeyboardEvent) => {
|
2023-02-06 10:06:33 +01:00
|
|
|
if (e.code === 'F3' || (e.ctrlKey && e.code === 'KeyF')) {
|
2021-12-19 18:06:27 +03:30
|
|
|
e.preventDefault();
|
|
|
|
|
openSearch();
|
2023-11-12 03:21:26 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (e.code === 'Enter') {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
handleChange(searchString);
|
2021-12-19 18:06:27 +03:30
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-03-12 04:46:55 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (autoOpen) {
|
|
|
|
|
openSearch();
|
|
|
|
|
}
|
|
|
|
|
}, []);
|
|
|
|
|
|
2021-12-19 18:06:27 +03:30
|
|
|
useEffect(() => {
|
2023-11-12 03:21:26 +01:00
|
|
|
if (query === undefined && searchString !== undefined) {
|
|
|
|
|
setSearchString('');
|
|
|
|
|
setSearchOpen(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (searchString === '' && !!query) {
|
|
|
|
|
setSearchString(query);
|
|
|
|
|
setSearchOpen(true);
|
|
|
|
|
}
|
|
|
|
|
}, [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 (
|
|
|
|
|
<Input
|
2023-11-12 03:21:26 +01:00
|
|
|
value={searchString}
|
|
|
|
|
onChange={(e) => setSearchString(e.target.value)}
|
2023-03-11 18:05:58 +01:00
|
|
|
onBlur={handleBlur}
|
|
|
|
|
inputRef={inputRef}
|
|
|
|
|
endAdornment={
|
|
|
|
|
<IconButton onClick={cancelSearch}>
|
|
|
|
|
<CancelIcon />
|
|
|
|
|
</IconButton>
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-31 12:45:34 +01:00
|
|
|
return (
|
2023-11-05 17:22:29 +01:00
|
|
|
<Tooltip title={t('search.title.search')}>
|
|
|
|
|
<IconButton onClick={openSearch}>
|
|
|
|
|
<SearchIcon />
|
|
|
|
|
</IconButton>
|
|
|
|
|
</Tooltip>
|
2021-10-31 12:45:34 +01:00
|
|
|
);
|
2022-03-12 04:46:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
AppbarSearch.defaultProps = defaultProps;
|