Skip to content

Added the Download filter and download badge #62

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

Merged
merged 3 commits into from
Nov 7, 2021
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
47 changes: 31 additions & 16 deletions src/components/MangaCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,21 @@ const MangaTitle = styled(Typography)({
textShadow: '0px 0px 3px #000000',
});

const UnreadBadge = styled(Typography)(({ theme }) => ({
const BadgeConatiner = styled('div')({
display: 'flex',
position: 'absolute',
top: 2,
left: 2,
backgroundColor: theme.palette.primary.dark,
top: 5,
left: 5,
height: 'fit-content',
borderRadius: '5px',
color: 'white',
padding: '0.1em',
paddingInline: '0.3em',
fontSize: '1.05rem',
}));
overflow: 'hidden',
'& p': {
color: 'white',
padding: '0.1em',
paddingInline: '0.2em',
fontSize: '1.05rem',
},
});

const truncateText = (str: string, maxLength: number) => {
const ending = '...';
Expand All @@ -69,7 +73,7 @@ interface IProps {
const MangaCard = React.forwardRef<HTMLDivElement, IProps>((props: IProps, ref) => {
const {
manga: {
id, title, thumbnailUrl, unreadCount: unread,
id, title, thumbnailUrl, downloadCount, unreadCount: unread,
},
} = props;

Expand All @@ -93,12 +97,24 @@ const MangaCard = React.forwardRef<HTMLDivElement, IProps>((props: IProps, ref)
height: '100%',
}}
>
{unread! > 0
&& (
<UnreadBadge>

<BadgeConatiner>
{unread! > 0 && (
<Typography
sx={{ backgroundColor: 'primary.dark' }}
>
{unread}
</UnreadBadge>
) }
</Typography>
)}
{downloadCount! > 0 && (
<Typography sx={{
backgroundColor: 'success.dark',
}}
>
{downloadCount}
</Typography>
)}
</BadgeConatiner>
<SpinnerImage
alt={title}
src={`${serverAddress}${thumbnailUrl}?useCache=${useCache}`}
Expand All @@ -107,7 +123,6 @@ const MangaCard = React.forwardRef<HTMLDivElement, IProps>((props: IProps, ref)
width: '100%',
}}
spinnerStyle={{
minHeight: '400px',
display: 'grid',
placeItems: 'center',
}}
Expand Down
30 changes: 28 additions & 2 deletions src/components/library/LibraryMangaGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,41 @@ function unreadFilter(unread: NullAndUndefined<boolean>, { unreadCount }: IManga
}
}

function downloadedFilter(downloaded: NullAndUndefined<boolean>,
{ downloadCount }: IMangaCard): boolean {
switch (downloaded) {
case true:
return !!downloadCount && downloadCount >= 1;
case false:
return downloadCount === 0;
default:
return true;
}
}

// function downloadedFilter(downloaded: NullAndUndefined<boolean>,
// { downloadCount }: IMangaCard): boolean {
// switch (downloaded) {
// case true:
// return !!downloaded && downloadCount >= 1;
// case false:
// return downloadCount === 0;
// default:
// return true;
// }
// }

function queryFilter(query: NullAndUndefined<string>, { title }: IMangaCard): boolean {
if (!query) return true;
return title.toLowerCase().includes(query.toLowerCase());
}

function filterManga(mangas: IMangaCard[]): IMangaCard[] {
const { unread, query } = useLibraryOptions();
const { downloaded, unread, query } = useLibraryOptions();
return mangas
.filter((manga) => unreadFilter(unread, manga) && queryFilter(query, manga));
.filter((manga) => downloadedFilter(downloaded, manga)
&& unreadFilter(unread, manga)
&& queryFilter(query, manga));
}

export default function LibraryMangaGrid(props: IMangaGridProps) {
Expand Down
7 changes: 5 additions & 2 deletions src/components/library/LibraryOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ import useLibraryOptions from 'util/useLibraryOptions';
import ThreeStateCheckbox from 'components/util/ThreeStateCheckbox';

function Options() {
const { unread, setUnread } = useLibraryOptions();
const {
downloaded, setDownloaded, unread, setUnread,
} = useLibraryOptions();
return (
<div>
<div style={{ display: 'Flex', flexDirection: 'column' }}>
<FormControlLabel control={<ThreeStateCheckbox name="Unread" checked={unread} onChange={setUnread} />} label="Unread" />
<FormControlLabel control={<ThreeStateCheckbox name="Downloaded" checked={downloaded} onChange={setDownloaded} />} label="Downloaded" />
</div>
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ interface IMangaCard {
title: string
thumbnailUrl: string
unreadCount?: number
downloadCount?: number
}

interface IManga {
Expand All @@ -58,6 +59,7 @@ interface IManga {

freshData: boolean
unreadCount?: number
downloadCount?: number
}

interface IChapter {
Expand Down
12 changes: 9 additions & 3 deletions src/util/useLibraryOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { BooleanParam, useQueryParams, StringParam } from 'use-query-params';
export type NullAndUndefined<T> = T | null | undefined;

interface IUseLibraryOptions {
downloaded: NullAndUndefined<boolean>
setDownloaded: (downloaded: NullAndUndefined<boolean>)=>void
unread: NullAndUndefined<boolean>
setUnread: (unread: NullAndUndefined<boolean>) => void
query: NullAndUndefined<string>
Expand All @@ -20,19 +22,23 @@ interface IUseLibraryOptions {

export default function useLibraryOptions(): IUseLibraryOptions {
const [searchQuery, setSearchQuery] = useQueryParams({
downloaded: BooleanParam,
unread: BooleanParam,
query: StringParam,
});
const { unread, query } = searchQuery;
const { downloaded, unread, query } = searchQuery;
const setDownloaded = (newDownloaded: NullAndUndefined<boolean>) => {
setSearchQuery(Object.assign(searchQuery, { downloaded: newDownloaded }), 'replace');
};
const setUnread = (newUnread: NullAndUndefined<boolean>) => {
setSearchQuery(Object.assign(searchQuery, { unread: newUnread }), 'replace');
};
const setQuery = (newQuery: NullAndUndefined<string>) => {
setSearchQuery(Object.assign(searchQuery, { query: newQuery }), 'replace');
};
// eslint-disable-next-line eqeqeq
const active = !(unread == undefined);
const active = !(unread == undefined) && !(downloaded == undefined);
return {
unread, setUnread, active, query, setQuery,
downloaded, setDownloaded, unread, setUnread, active, query, setQuery,
};
}