Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduced override filters while searching setting #242

Merged
merged 6 commits into from
Mar 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/components/library/LibraryMangaGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import MangaGrid from 'components/MangaGrid';
import { useLibraryOptionsContext } from 'components/context/LibraryOptionsContext';
import { StringParam, useQueryParam } from 'use-query-params';
import { useMediaQuery, useTheme } from '@mui/material';
import { useSearchSettings } from 'util/searchSettings';

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

Expand Down Expand Up @@ -46,13 +47,16 @@ const filterManga = (
query: NullAndUndefined<string>,
unread: NullAndUndefined<boolean>,
downloaded: NullAndUndefined<boolean>,
overrideFilters: boolean,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think "ignoreFilters" would be better fitting over all.
"overrideFilters" sounds to me like the filters will get replaced with different ones, while they are just ignored if the flag is set to true

): IMangaCard[] =>
manga.filter((m) => {
const isFiltered = downloadedFilter(downloaded, m) && unreadFilter(unread, m);
if (query) {
return queryFilter(query, m);
const isQueried = queryFilter(query, m);
if (overrideFilters) return isQueried;
return isFiltered && isQueried;
}

return downloadedFilter(downloaded, m) && unreadFilter(unread, m);
return isFiltered;
});

const sortByUnread = (a: IMangaCard, b: IMangaCard): number =>
Expand Down Expand Up @@ -114,11 +118,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.overrideFilters));
setLastPageNum(defaultPageNumber);
window.scrollTo(0, 0);
}, [mangas, query, unread, downloaded]);
}, [mangas, query, unread, downloaded, settings.overrideFilters]);

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
33 changes: 33 additions & 0 deletions src/screens/settings/SearchSettings.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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';

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

const setSettingValue = (key: keyof ISearchSettings, value: boolean) => {
requestUpdateServerMetadata(metadata ?? {}, [[key, value]]).catch(() =>
makeToast('Failed to save the default reader settings to the server', 'warning'),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you forgot to update the error message (still says reader settings)

);
};
return (
<ListItem>
<ListItemIcon>
<SearchIcon />
</ListItemIcon>
<ListItemText primary="Override Filters when Searching" />
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you think about something like "Ignore filters while searching"? I feel like that is easier to understand then "Override"

<ListItemSecondaryAction>
<Switch
edge="end"
checked={settings.overrideFilters}
onChange={(e) => setSettingValue('overrideFilters', e.target.checked)}
/>
</ListItemSecondaryAction>
</ListItem>
);
}
6 changes: 5 additions & 1 deletion src/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ interface IMetadataHolder<VALUES extends AllowedMetadataValueTypes = string> {

type AllowedMetadataValueTypes = string | boolean | number | undefined;

type MangaMetadataKeys = keyof IReaderSettings;
type MangaMetadataKeys = keyof (IReaderSettings & ISearchSettings);

type AppMetadataKeys = MangaMetadataKeys;

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

interface ISearchSettings {
overrideFilters: boolean;
}

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

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

const getReaderSettingsWithDefaultValueFallback = (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as for the error message, name inlcudes "ReaderSettings"

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 = getReaderSettingsWithDefaultValueFallback(meta);

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