Skip to content

Commit

Permalink
Feature/search bar improvements (#455)
Browse files Browse the repository at this point in the history
* Require search to be committed with enter

* Extract back navigation logic into hook

* Navigate to previous page when canceling search

This makes the search the next page in the history stack instead of making it the previous one

* Close and open search depending on the location and available query

* Navigate to previous page in case new query is empty
  • Loading branch information
schroda authored Nov 12, 2023
1 parent 0c7498d commit 35c34b6
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 35 deletions.
23 changes: 5 additions & 18 deletions src/components/navbar/DefaultNavBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ import GetAppIcon from '@mui/icons-material/GetApp';
import GetAppOutlinedIcon from '@mui/icons-material/GetAppOutlined';
import SettingsIcon from '@mui/icons-material/Settings';
import ArrowBack from '@mui/icons-material/ArrowBack';
import { useLocation, useNavigate } from 'react-router-dom';
import { useLocation } from 'react-router-dom';
import { createPortal } from 'react-dom';
import { NavbarItem } from '@/typings';
import { NavBarContext } from '@/components/context/NavbarContext';
import { ExtensionOutlinedIcon } from '@/components/util/CustomExtensionOutlinedIcon';
import { DesktopSideBar } from '@/components/navbar/navigation/DesktopSideBar';
import { MobileBottomBar } from '@/components/navbar/navigation/MobileBottomBar';
import { useHistory } from '@/util/useHistory';
import { useBackButton } from '@/util/useBackButton.ts';

const navbarItems: Array<NavbarItem> = [
{
Expand Down Expand Up @@ -86,12 +86,11 @@ const navbarItems: Array<NavbarItem> = [
];

export function DefaultNavBar() {
const { title, action, override, defaultBackTo: backToUrl } = useContext(NavBarContext);
const { title, action, override } = useContext(NavBarContext);

const theme = useTheme();
const navigate = useNavigate();
const { pathname, ...location } = useLocation();
const history = useHistory();
const { pathname } = useLocation();
const handleBack = useBackButton();

const isMobileWidth = useMediaQuery(theme.breakpoints.down('sm'));
const isMainRoute = navbarItems.some(({ path }) => path === pathname);
Expand All @@ -108,18 +107,6 @@ export function DefaultNavBar() {
navbar = <DesktopSideBar navBarItems={navbarItems.filter((it) => it.show !== 'mobile')} />;
}

const handleBack = () => {
const isLastPageInHistory = location.key === 'default';
const wasPreviousPageReader = history[history.length - 2]?.match(/\/manga\/[0-9]+\/chapter\/[0-9]+.*/g);

if (isLastPageInHistory || wasPreviousPageReader) {
navigate(backToUrl ?? '');
return;
}

navigate(-1);
};

return (
<Box sx={{ flexGrow: 1 }}>
<AppBar position="fixed" color="default">
Expand Down
50 changes: 40 additions & 10 deletions src/components/util/AppbarSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { IconButton, Input, Tooltip } from '@mui/material';
import CancelIcon from '@mui/icons-material/Cancel';
import { useQueryParam, StringParam } from 'use-query-params';
import { useTranslation } from 'react-i18next';
import { useBackButton } from '@/util/useBackButton.ts';

interface IProps {
autoOpen?: boolean;
Expand All @@ -23,22 +24,32 @@ const defaultProps = {

export const AppbarSearch: React.FunctionComponent<IProps> = (props) => {
const { t } = useTranslation();
const handleBack = useBackButton();

const { autoOpen } = props;
const [query, setQuery] = useQueryParam('query', StringParam);
const [searchOpen, setSearchOpen] = useState(!!query);
const inputRef = React.useRef<HTMLInputElement>();

function handleChange(e: React.ChangeEvent<HTMLInputElement>) {
setQuery(e.target.value === '' ? undefined : e.target.value);
const [searchString, setSearchString] = useState(query ?? '');

function handleChange(newQuery: string) {
if (newQuery === '') {
handleBack();
return;
}

setQuery(newQuery);
}

const cancelSearch = () => {
setQuery(null);
setSearchString('');
setSearchOpen(false);

handleBack();
};
const handleBlur = () => {
if (!query) setSearchOpen(false);
if (!searchString) setSearchOpen(false);
};
const openSearch = () => {
setSearchOpen(true);
Expand All @@ -48,10 +59,16 @@ export const AppbarSearch: React.FunctionComponent<IProps> = (props) => {
});
};

const handleSearchShortcut = (e: KeyboardEvent) => {
const handleKeyboardEvent = (e: KeyboardEvent) => {
if (e.code === 'F3' || (e.ctrlKey && e.code === 'KeyF')) {
e.preventDefault();
openSearch();
return;
}

if (e.code === 'Enter') {
e.preventDefault();
handleChange(searchString);
}
};

Expand All @@ -62,18 +79,31 @@ export const AppbarSearch: React.FunctionComponent<IProps> = (props) => {
}, []);

useEffect(() => {
window.addEventListener('keydown', handleSearchShortcut);
if (query === undefined && searchString !== undefined) {
setSearchString('');
setSearchOpen(false);
return;
}

if (searchString === '' && !!query) {
setSearchString(query);
setSearchOpen(true);
}
}, [query]);

useEffect(() => {
window.addEventListener('keydown', handleKeyboardEvent);

return () => {
window.removeEventListener('keydown', handleSearchShortcut);
window.removeEventListener('keydown', handleKeyboardEvent);
};
}, [handleSearchShortcut]);
}, [handleKeyboardEvent]);

if (searchOpen) {
return (
<Input
value={query || ''}
onChange={handleChange}
value={searchString}
onChange={(e) => setSearchString(e.target.value)}
onBlur={handleBlur}
inputRef={inputRef}
endAdornment={
Expand Down
4 changes: 3 additions & 1 deletion src/screens/SearchAll.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { AppbarSearch } from '@/components/util/AppbarSearch';
import { LangSelect } from '@/components/navbar/action/LangSelect';
import { MangaGrid } from '@/components/MangaGrid';
import { useDebounce } from '@/components/manga/hooks';
import { NavBarContext } from '@/components/context/NavbarContext.tsx';
import { NavBarContext, useSetDefaultBackTo } from '@/components/context/NavbarContext.tsx';

type SourceLoadingState = { isLoading: boolean; hasResults: boolean; emptySearch: boolean };
type SourceToLoadingStateMap = Map<string, SourceLoadingState>;
Expand Down Expand Up @@ -161,6 +161,8 @@ export const SearchAll: React.FC = () => {

const { setTitle, setAction } = useContext(NavBarContext);

useSetDefaultBackTo('sources/all/search');

const [query] = useQueryParam('query', StringParam);
const searchString = useDebounce(query, TRIGGER_SEARCH_THRESHOLD);

Expand Down
17 changes: 11 additions & 6 deletions src/screens/SourceMangas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
GetSourceMangasFetchMutation,
GetSourceMangasFetchMutationVariables,
} from '@/lib/graphql/generated/graphql.ts';
import { NavBarContext } from '@/components/context/NavbarContext.tsx';
import { NavBarContext, useSetDefaultBackTo } from '@/components/context/NavbarContext.tsx';

const ContentTypeMenu = styled('div')(({ theme }) => ({
display: 'flex',
Expand Down Expand Up @@ -198,21 +198,26 @@ const useSourceManga = (
export function SourceMangas() {
const { t } = useTranslation();
const { setTitle, setAction } = useContext(NavBarContext);

const theme = useTheme();
const isLargeScreen = useMediaQuery(theme.breakpoints.up('sm'));

const { sourceId } = useParams<{ sourceId: string }>();

const navigate = useNavigate();
const { pathname, state: locationState } =
useLocation<{
contentType: SourceContentType;
filtersToApply: IPos[];
clearCache: boolean;
}>() ?? {};
const {
contentType: currentLocationContentType = SourceContentType.POPULAR,
filtersToApply: currentLocationFiltersToApply = [],
clearCache = false,
} = useLocation<{
contentType: SourceContentType;
filtersToApply: IPos[];
clearCache: boolean;
}>().state ?? {};
} = locationState ?? {};

useSetDefaultBackTo(pathname);

const { options } = useLibraryOptionsContext();
const [query] = useQueryParam('query', StringParam);
Expand Down
31 changes: 31 additions & 0 deletions src/util/useBackButton.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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/.
*/

import { useLocation, useNavigate } from 'react-router-dom';
import { useContext } from 'react';
import { useHistory } from '@/util/useHistory.ts';
import { NavBarContext } from '@/components/context/NavbarContext.tsx';

export const useBackButton = () => {
const navigate = useNavigate();
const history = useHistory();
const location = useLocation();
const { defaultBackTo: backToUrl } = useContext(NavBarContext);

return () => {
const isLastPageInHistory = location.key === 'default';
const wasPreviousPageReader = history[history.length - 2]?.match(/\/manga\/[0-9]+\/chapter\/[0-9]+.*/g);

if (isLastPageInHistory || wasPreviousPageReader) {
navigate(backToUrl ?? '');
return;
}

navigate(-1);
};
};

0 comments on commit 35c34b6

Please sign in to comment.