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

Feature/library add bookmark filter #687

Merged
merged 1 commit into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions src/components/context/LibraryOptionsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const DefaultLibraryOptions: LibraryOptions = {
SourcegridLayout: GridLayout.Compact,

downloaded: undefined,
bookmarked: undefined,
sortDesc: undefined,
sorts: undefined,
unread: undefined,
Expand Down
5 changes: 5 additions & 0 deletions src/components/library/LibraryOptionsPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ export const LibraryOptionsPanel: React.FC<IProps> = ({ open, onClose }) => {
checked={options.downloaded}
onChange={(c) => handleFilterChange('downloaded', c)}
/>
<ThreeStateCheckboxInput
label={t('global.filter.label.bookmarked')}
checked={options.bookmarked}
onChange={(c) => handleFilterChange('bookmarked', c)}
/>
<FormLabel sx={{ mt: 2 }}>{t('global.filter.label.tracked')}</FormLabel>
{loggedInTrackers.map((tracker) => (
<ThreeStateCheckboxInput
Expand Down
25 changes: 20 additions & 5 deletions src/components/library/useGetVisibleLibraryMangas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ const downloadedFilter = (downloaded: NullAndUndefined<boolean>, { downloadCount
}
};

const bookmarkedFilter = (bookmarked: NullAndUndefined<boolean>, { bookmarkCount }: TManga): boolean => {
switch (bookmarked) {
case true:
return !!bookmarkCount && bookmarkCount >= 1;
case false:
return bookmarkCount === 0;
default:
return true;
}
};

const queryFilter = (query: NullAndUndefined<string>, { title }: TManga): boolean => {
if (!query) return true;
return title.toLowerCase().includes(query.toLowerCase());
Expand Down Expand Up @@ -68,6 +79,7 @@ const filterManga = (
query: NullAndUndefined<string>,
unread: NullAndUndefined<boolean>,
downloaded: NullAndUndefined<boolean>,
bookmarked: NullAndUndefined<boolean>,
tracker: LibraryOptions['tracker'],
ignoreFilters: boolean,
): TManga[] =>
Expand All @@ -76,7 +88,10 @@ const filterManga = (
const matchesSearch = queryFilter(query, manga) || queryGenreFilter(query, manga);
const matchesFilters =
ignoreFiltersWhileSearching ||
(downloadedFilter(downloaded, manga) && unreadFilter(unread, manga) && trackerFilter(tracker, manga));
(downloadedFilter(downloaded, manga) &&
unreadFilter(unread, manga) &&
bookmarkedFilter(bookmarked, manga) &&
trackerFilter(tracker, manga));

return matchesSearch && matchesFilters;
});
Expand Down Expand Up @@ -136,12 +151,12 @@ const sortManga = (
export const useGetVisibleLibraryMangas = (mangas: TManga[]) => {
const [query] = useQueryParam('query', StringParam);
const { options } = useLibraryOptionsContext();
const { unread, downloaded, tracker } = options;
const { unread, downloaded, bookmarked, tracker } = options;
const { settings } = useMetadataServerSettings();

const filteredMangas = useMemo(
() => filterManga(mangas, query, unread, downloaded, tracker, settings.ignoreFilters),
[mangas, query, unread, downloaded, tracker, settings.ignoreFilters],
() => filterManga(mangas, query, unread, downloaded, bookmarked, tracker, settings.ignoreFilters),
[mangas, query, unread, downloaded, bookmarked, tracker, settings.ignoreFilters],
);
const sortedMangas = useMemo(
() => sortManga(filteredMangas, options.sorts, options.sortDesc),
Expand All @@ -150,7 +165,7 @@ export const useGetVisibleLibraryMangas = (mangas: TManga[]) => {

const isATrackFilterActive = Object.values(options.tracker).some((trackFilterState) => trackFilterState != null);
const showFilteredOutMessage =
(unread != null || downloaded != null || !!query || isATrackFilterActive) &&
(unread != null || downloaded != null || bookmarked != null || !!query || isATrackFilterActive) &&
filteredMangas.length === 0 &&
mangas.length > 0;

Expand Down
1 change: 1 addition & 0 deletions src/lib/graphql/Fragments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ export const PARTIAL_MANGA_FIELDS = gql`
...BASE_MANGA_FIELDS
unreadCount
downloadCount
bookmarkCount
categories {
nodes {
...FULL_CATEGORY_FIELDS
Expand Down
Loading