Skip to content

Commit

Permalink
Introduce override filters while searching setting (#242)
Browse files Browse the repository at this point in the history
* Introduced override filters while searching setting

* update branch; change strings and method names

* fix eslint issue

* code review changes

* moved search setting types into its own type

---------

Co-authored-by: akxer <>
  • Loading branch information
akabhirav authored Mar 4, 2023
1 parent dcc18bb commit a67370b
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/components/library/LibraryMangaGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { useLibraryOptionsContext } from 'components/context/LibraryOptionsConte
import { StringParam, useQueryParam } from 'use-query-params';
import { useMediaQuery, useTheme } from '@mui/material';
import { IMangaCard, LibrarySortMode, NullAndUndefined } from 'typings';
import { useSearchSettings } from 'util/searchSettings';

const FILTERED_OUT_MESSAGE = 'There are no Manga matching this filter';

Expand Down Expand Up @@ -53,12 +54,12 @@ const filterManga = (
query: NullAndUndefined<string>,
unread: NullAndUndefined<boolean>,
downloaded: NullAndUndefined<boolean>,
ignoreFilters: boolean,
): IMangaCard[] =>
mangas.filter(
(manga) =>
(queryFilter(query, manga) || queryGenreFilter(query, manga)) &&
downloadedFilter(downloaded, manga) &&
unreadFilter(unread, manga),
(ignoreFilters || (downloadedFilter(downloaded, manga) && unreadFilter(unread, manga))),
);

const sortByUnread = (a: IMangaCard, b: IMangaCard): number =>
Expand Down Expand Up @@ -120,11 +121,13 @@ const LibraryMangaGrid: React.FC<LibraryMangaGridProps & { lastLibraryUpdate: nu
const isLargeScreen = useMediaQuery(theme.breakpoints.up('sm'));
const defaultPageNumber = isLargeScreen ? 4 : 1;
const [lastPageNum, setLastPageNum] = useState<number>(defaultPageNumber);
const { settings } = useSearchSettings();

useEffect(() => {
setFilteredManga(filterManga(mangas, query, unread, downloaded));
setFilteredManga(filterManga(mangas, query, unread, downloaded, settings.ignoreFilters));
setLastPageNum(defaultPageNumber);
window.scrollTo(0, 0);
}, [mangas, query, unread, downloaded]);
}, [mangas, query, unread, downloaded, settings.ignoreFilters]);

useEffect(() => {
setSortedManga(sortManga(filteredManga, options.sorts, options.sortDesc));
Expand Down
2 changes: 2 additions & 0 deletions src/screens/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import NavbarContext from 'components/context/NavbarContext';
import DarkTheme from 'components/context/DarkTheme';
import useLocalStorage from 'util/useLocalStorage';
import ListItemLink from 'components/util/ListItemLink';
import SearchSettings from 'screens/settings/SearchSettings';

export default function Settings() {
const { setTitle, setAction } = useContext(NavbarContext);
Expand Down Expand Up @@ -121,6 +122,7 @@ export default function Settings() {
<Switch edge="end" checked={darkTheme} onChange={() => setDarkTheme(!darkTheme)} />
</ListItemSecondaryAction>
</ListItem>
<SearchSettings />
<ListItemButton>
<ListItemIcon>
<ViewModuleIcon />
Expand Down
34 changes: 34 additions & 0 deletions src/screens/settings/SearchSettings.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { ListItem, ListItemText, Switch } from '@mui/material';
import ListItemSecondaryAction from '@mui/material/ListItemSecondaryAction';
import React from 'react';
import { useSearchSettings } from 'util/searchSettings';
import { requestUpdateServerMetadata } from 'util/metadata';
import makeToast from 'components/util/Toast';
import ListItemIcon from '@mui/material/ListItemIcon';
import SearchIcon from '@mui/icons-material/Search';
import { SearchMetadataKeys } from 'typings';

export default function SearchSettings() {
const { metadata, settings } = useSearchSettings();

const setSettingValue = (key: SearchMetadataKeys, value: boolean) => {
requestUpdateServerMetadata(metadata ?? {}, [[key, value]]).catch(() =>
makeToast('Failed to save the default search settings to the server', 'warning'),
);
};
return (
<ListItem>
<ListItemIcon>
<SearchIcon />
</ListItemIcon>
<ListItemText primary="Ignore Filters when Searching" />
<ListItemSecondaryAction>
<Switch
edge="end"
checked={settings.ignoreFilters}
onChange={(e) => setSettingValue('ignoreFilters', e.target.checked)}
/>
</ListItemSecondaryAction>
</ListItem>
);
}
8 changes: 7 additions & 1 deletion src/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ export type AllowedMetadataValueTypes = string | boolean | number | undefined;

export type MangaMetadataKeys = keyof IReaderSettings;

export type AppMetadataKeys = MangaMetadataKeys;
export type SearchMetadataKeys = keyof ISearchSettings;

export type AppMetadataKeys = MangaMetadataKeys | SearchMetadataKeys;

export type MetadataKeyValuePair = [AppMetadataKeys, AllowedMetadataValueTypes];

Expand Down Expand Up @@ -190,6 +192,10 @@ export interface IReaderSettings {
readerType: ReaderType;
}

export interface ISearchSettings {
ignoreFilters: boolean;
}

export interface IReaderPage {
index: number;
src: string;
Expand Down
31 changes: 31 additions & 0 deletions src/util/searchSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { useQuery } from 'util/client';
import { getMetadataFrom } from 'util/metadata';
import { IMetadata, ISearchSettings, MetadataKeyValuePair } from 'typings';

export const getDefaultSettings = () =>
({
ignoreFilters: false,
} as ISearchSettings);

const getSearchSettingsWithDefaultValueFallback = (
meta?: IMetadata,
defaultSettings?: ISearchSettings,
applyMetadataMigration: boolean = true,
): ISearchSettings => ({
...(getMetadataFrom(
{ meta },
Object.entries(defaultSettings ?? getDefaultSettings()) as MetadataKeyValuePair[],
applyMetadataMigration,
) as unknown as ISearchSettings),
});

export const useSearchSettings = (): {
metadata?: IMetadata;
settings: ISearchSettings;
loading: boolean;
} => {
const { data: meta, loading } = useQuery<IMetadata>('/api/v1/meta');
const settings = getSearchSettingsWithDefaultValueFallback(meta);

return { metadata: meta, settings, loading };
};

0 comments on commit a67370b

Please sign in to comment.