Updates pagination update (#68)

* make it work

* refactor & update styling to mui v5

* ditch styled, this looks bad
This commit is contained in:
Aria Moradi
2021-11-10 23:04:04 +03:30
committed by GitHub
parent c9e4901c15
commit 63d9a64542
2 changed files with 87 additions and 60 deletions

View File

@@ -1,6 +1,3 @@
/* eslint-disable @typescript-eslint/no-shadow */
/* eslint-disable react/destructuring-assignment */
/* eslint-disable react/jsx-props-no-spreading */
/* /*
* Copyright (C) Contributors to the Suwayomi project * Copyright (C) Contributors to the Suwayomi project
* *
@@ -8,9 +5,10 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. */ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import React, { useContext, useEffect, useState } from 'react'; import React, {
useContext, useEffect, useState, useRef,
} from 'react';
import { useHistory } from 'react-router-dom'; import { useHistory } from 'react-router-dom';
import makeStyles from '@mui/styles/makeStyles';
import Card from '@mui/material/Card'; import Card from '@mui/material/Card';
import CardContent from '@mui/material/CardContent'; import CardContent from '@mui/material/CardContent';
import IconButton from '@mui/material/IconButton'; import IconButton from '@mui/material/IconButton';
@@ -20,44 +18,8 @@ import Typography from '@mui/material/Typography';
import NavbarContext from 'components/context/NavbarContext'; import NavbarContext from 'components/context/NavbarContext';
import client from 'util/client'; import client from 'util/client';
import useLocalStorage from 'util/useLocalStorage'; import useLocalStorage from 'util/useLocalStorage';
import EmptyView from 'components/util/EmptyView';
const useStyles = makeStyles((theme) => ({ import LoadingPlaceholder from 'components/util/LoadingPlaceholder';
root: {
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
padding: 16,
},
bullet: {
display: 'inline-block',
margin: '0 2px',
transform: 'scale(0.8)',
},
title: {
fontSize: 14,
},
pos: {
marginBottom: 12,
},
icon: {
width: theme.spacing(7),
height: theme.spacing(7),
flex: '0 0 auto',
marginRight: 16,
imageRendering: 'pixelated',
},
card: {
margin: '10px',
'&:hover': {
backgroundColor: theme.palette.action.hover,
transition: 'background-color 100ms cubic-bezier(0.4, 0, 0.2, 1) 0ms',
},
'&:active': {
backgroundColor: theme.palette.action.selected,
transition: 'background-color 100ms cubic-bezier(0.4, 0, 0.2, 1) 0ms',
},
},
}));
function epochToDate(epoch: number) { function epochToDate(epoch: number) {
const date = new Date(0); // The 0 there is the key, which sets the date to the epoch const date = new Date(0); // The 0 there is the key, which sets the date to the epoch
@@ -81,16 +43,17 @@ function getDateString(date: Date) {
return date.toLocaleDateString(); return date.toLocaleDateString();
} }
function groupByDate(updates: IMangaChapter[]): [string, IMangaChapter[]][] { function groupByDate(updates: IMangaChapter[]):
[string, { item: IMangaChapter, globalIdx: number }[] ][] {
if (updates.length === 0) return []; if (updates.length === 0) return [];
const groups = {}; const groups = {};
updates.forEach((item) => { updates.forEach((item, globalIdx) => {
const key = getDateString(epochToDate(item.chapter.fetchedAt)); const key = getDateString(epochToDate(item.chapter.fetchedAt));
// @ts-ignore // @ts-ignore
if (groups[key] === undefined) groups[key] = []; if (groups[key] === undefined) groups[key] = [];
// @ts-ignore // @ts-ignore
groups[key].push(item); groups[key].push({ item, globalIdx });
}); });
// @ts-ignore // @ts-ignore
@@ -104,11 +67,13 @@ const initialQueue = {
} as IQueue; } as IQueue;
export default function Updates() { export default function Updates() {
const classes = useStyles();
const history = useHistory(); const history = useHistory();
const { setTitle, setAction } = useContext(NavbarContext); const { setTitle, setAction } = useContext(NavbarContext);
const [updateEntries, setUpdateEntries] = useState<IMangaChapter[]>([]); const [updateEntries, setUpdateEntries] = useState<IMangaChapter[]>([]);
const [hasNextPage, setHasNextPage] = useState(true);
const [fetched, setFetched] = useState(false);
const [lastPageNum, setLastPageNum] = useState(0);
const [serverAddress] = useLocalStorage<String>('serverBaseURL', ''); const [serverAddress] = useLocalStorage<String>('serverBaseURL', '');
const [useCache] = useLocalStorage<boolean>('useCache', true); const [useCache] = useLocalStorage<boolean>('useCache', true);
@@ -135,12 +100,39 @@ export default function Updates() {
}, []); }, []);
useEffect(() => { useEffect(() => {
client.get('/api/v1/update/recentChapters') if (hasNextPage) {
client.get(`/api/v1/update/recentChapters/${lastPageNum}`)
.then((response) => response.data) .then((response) => response.data)
.then((updates: IMangaChapter[]) => { .then(({ hasNextPage: fetchedHasNextPage, page }: PaginatedList<IMangaChapter>) => {
setUpdateEntries(updates); setUpdateEntries([
...updateEntries,
...page,
]);
setHasNextPage(fetchedHasNextPage);
setFetched(true);
}); });
}, []); }
}, [lastPageNum]);
const lastEntry = useRef<HTMLDivElement>(null);
const scrollHandler = () => {
if (lastEntry.current) {
const rect = lastEntry.current.getBoundingClientRect();
if (((rect.y + rect.height) / window.innerHeight < 2) && hasNextPage) {
setLastPageNum(lastPageNum + 1);
}
}
};
useEffect(() => {
window.addEventListener('scroll', scrollHandler, true);
return () => {
window.removeEventListener('scroll', scrollHandler, true);
};
}, [hasNextPage, updateEntries]);
if (!fetched) { return <LoadingPlaceholder />; }
if (fetched && updateEntries.length === 0) { return <EmptyView message="You don't have any updates yet." />; }
const downloadStatusStringFor = (chapter: IChapter) => { const downloadStatusStringFor = (chapter: IChapter) => {
let rtn = ''; let rtn = '';
@@ -163,20 +155,50 @@ export default function Updates() {
<> <>
{groupByDate(updateEntries).map((dateGroup) => ( {groupByDate(updateEntries).map((dateGroup) => (
<div key={dateGroup[0]}> <div key={dateGroup[0]}>
<h1 style={{ marginLeft: 25 }}> <Typography
variant="h5"
sx={{
ml: 3,
my: 2,
fontWeight: 700,
}}
>
{dateGroup[0]} {dateGroup[0]}
</h1> </Typography>
{dateGroup[1].map(({ chapter, manga }) => ( {dateGroup[1].map(({ item: { chapter, manga }, globalIdx }) => (
<Card <Card
key={`${manga.title}-${chapter.name}`} ref={globalIdx === updateEntries.length - 1 ? lastEntry : undefined}
className={classes.card} key={globalIdx}
sx={{
margin: '10px',
'&:hover': {
backgroundColor: 'action.hover',
transition: 'background-color 100ms cubic-bezier(0.4, 0, 0.2, 1) 0ms',
},
'&:active': {
backgroundColor: 'action.selected',
transition: 'background-color 100ms cubic-bezier(0.4, 0, 0.2, 1) 0ms',
},
}}
onClick={() => history.push(`/manga/${chapter.mangaId}/chapter/${chapter.index}`)} onClick={() => history.push(`/manga/${chapter.mangaId}/chapter/${chapter.index}`)}
> >
<CardContent className={classes.root}> <CardContent sx={{
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
padding: 2,
}}
>
<div style={{ display: 'flex' }}> <div style={{ display: 'flex' }}>
<Avatar <Avatar
variant="rounded" variant="rounded"
className={classes.icon} sx={{
width: 56,
height: 56,
flex: '0 0 auto',
marginRight: 2,
imageRendering: 'pixelated',
}}
src={`${serverAddress}${manga.thumbnailUrl}?useCache=${useCache}`} src={`${serverAddress}${manga.thumbnailUrl}?useCache=${useCache}`}
/> />
<div style={{ display: 'flex', flexDirection: 'column' }}> <div style={{ display: 'flex', flexDirection: 'column' }}>

5
src/typings.d.ts vendored
View File

@@ -203,3 +203,8 @@ interface NavbarItem {
IconComponent: OverridableComponent<SvgIconTypeMap<{}, 'svg'>>, IconComponent: OverridableComponent<SvgIconTypeMap<{}, 'svg'>>,
show: 'mobile' | 'desktop' | 'both' show: 'mobile' | 'desktop' | 'both'
} }
interface PaginatedList<T> {
page: T[],
hasNextPage: boolean
}