Add pagination and fix few performance issues

This commit is contained in:
prayag17 committed 2023-04-25 19:52:36 +05:30
1 parent 1d31496854
commit 41c4c333d1
2 files changed
+168 -73

No files matched your search

+5 -1
View File
@@ -93,6 +93,9 @@ const anim = {
animate: { animate: {
opacity: 1, opacity: 1,
}, },
exit: {
opacity: 0,
},
}; };
const AnimationWrapper = () => { const AnimationWrapper = () => {
@@ -102,8 +105,9 @@ const AnimationWrapper = () => {
variants={anim} variants={anim}
initial="initial" initial="initial"
animate="animate" animate="animate"
exit="exit"
transition={{ transition={{
duration: 0.4, duration: 0.35,
}} }}
> >
<Outlet /> <Outlet />
+163 -72
View File
@@ -2,7 +2,7 @@
import { useState, useEffect } from "react"; import { useState, useEffect } from "react";
import { useDispatch, useSelector } from "react-redux"; import { useDispatch, useSelector } from "react-redux";
import { showAppBar, showBackButton } from "../../utils/slice/appBar"; import { showAppBar, showBackButton } from "../../utils/slice/appBar";
import { useParams, useNavigate } from "react-router-dom"; import { useParams, useNavigate, Link } from "react-router-dom";
import { useQuery } from "@tanstack/react-query"; import { useQuery } from "@tanstack/react-query";
@@ -24,12 +24,14 @@ import Accordion from "@mui/material/Accordion";
import AccordionSummary from "@mui/material/AccordionSummary"; import AccordionSummary from "@mui/material/AccordionSummary";
import AccordionDetails from "@mui/material/AccordionDetails"; import AccordionDetails from "@mui/material/AccordionDetails";
import Button from "@mui/material/Button"; import Button from "@mui/material/Button";
import Paper from "@mui/material/Paper";
import Table from "@mui/material/Table"; import Table from "@mui/material/Table";
import TableBody from "@mui/material/TableBody";
import TableContainer from "@mui/material/TableContainer"; import TableContainer from "@mui/material/TableContainer";
import TableRow from "@mui/material/TableRow"; import TableRow from "@mui/material/TableRow";
import TableCell from "@mui/material/TableCell"; import TableCell from "@mui/material/TableCell";
import IconButton from "@mui/material/IconButton"; import IconButton from "@mui/material/IconButton";
import Pagination from "@mui/material/Pagination";
import ButtonBase from "@mui/material/ButtonBase";
import { theme } from "../../theme"; import { theme } from "../../theme";
@@ -55,6 +57,7 @@ import { ErrorNotice } from "../../components/notices/errorNotice/errorNotice";
import { import {
BaseItemKind, BaseItemKind,
CollectionTypeOptions, CollectionTypeOptions,
ItemFields,
} from "@jellyfin/sdk/lib/generated-client"; } from "@jellyfin/sdk/lib/generated-client";
import { getRuntimeMusic } from "../../utils/date/time"; import { getRuntimeMusic } from "../../utils/date/time";
@@ -68,6 +71,9 @@ const LibraryView = () => {
const dispatch = useDispatch(); const dispatch = useDispatch();
const appBarVisiblity = useSelector((state) => state.appBar.visible); const appBarVisiblity = useSelector((state) => state.appBar.visible);
const [page, setPage] = useState(1);
const maxDisplayItems = 50;
if (!appBarVisiblity) { if (!appBarVisiblity) {
dispatch(showAppBar()); dispatch(showAppBar());
} }
@@ -104,6 +110,7 @@ const LibraryView = () => {
const result = await getItemsApi(window.api).getItems({ const result = await getItemsApi(window.api).getItems({
userId: user.data.Id, userId: user.data.Id,
ids: [libraryId], ids: [libraryId],
fields: [ItemFields.RecursiveItemCount],
}); });
return result.data; return result.data;
}; };
@@ -296,6 +303,8 @@ const LibraryView = () => {
isHd: isHD ? true : undefined, isHd: isHD ? true : undefined,
is4K: is4K ? true : undefined, is4K: is4K ? true : undefined,
is3D: is3D ? true : undefined, is3D: is3D ? true : undefined,
startIndex: maxDisplayItems * (page - 1),
limit: maxDisplayItems,
}); });
} }
return result.data; return result.data;
@@ -306,6 +315,7 @@ const LibraryView = () => {
"libraryView", "libraryView",
"currentLibItems", "currentLibItems",
id, id,
`page: ${page}`,
[ [
currentViewType, currentViewType,
sortAscending, sortAscending,
@@ -331,7 +341,7 @@ const LibraryView = () => {
queryFn: () => fetchLibItems(id), queryFn: () => fetchLibItems(id),
enabled: currentLib.isSuccess, enabled: currentLib.isSuccess,
networkMode: "always", networkMode: "always",
refetchOnWindowFocus: false, keepPreviousData: true,
}); });
const handleCurrentViewType = (e) => { const handleCurrentViewType = (e) => {
@@ -821,7 +831,16 @@ const LibraryView = () => {
alignItems: "center", alignItems: "center",
}} }}
> >
<CircularProgress /> <CircularProgress
variant="indeterminate"
sx={{
position: "absolute",
top: "50%",
left: "50%",
transform: "translate(-50%,50%)",
width: "50%",
}}
/>
</Box> </Box>
) : items.data.TotalRecordCount == 0 ? ( ) : items.data.TotalRecordCount == 0 ? (
<EmptyNotice /> <EmptyNotice />
@@ -829,7 +848,12 @@ const LibraryView = () => {
<Grid2 container columns={{ xs: 2, sm: 4, md: 8 }}> <Grid2 container columns={{ xs: 2, sm: 4, md: 8 }}>
{items.data.Items.map((item, index) => { {items.data.Items.map((item, index) => {
return ( return (
<Grid2 key={index} xs={1} sm={1} md={1}> <Grid2
key={item.Id}
xs={1}
sm={1}
md={1}
>
<Card <Card
itemName={item.Name} itemName={item.Name}
itemId={item.Id} itemId={item.Id}
@@ -894,77 +918,144 @@ const LibraryView = () => {
) : ( ) : (
<TableContainer> <TableContainer>
<Table> <Table>
{items.data.Items.map((item, index) => { <TableBody>
return ( {items.data.Items.map((item, index) => {
<TableRow return (
key={index} <TableRow
sx={{ key={index}
"& td,& th": { sx={{
borderBottom: "& td,& th": {
"1px solid rgb(255 255 255 / 0.1)", borderBottom:
}, "1px solid rgb(255 255 255 / 0.1)",
"&:hover": { },
background: "&:last-child td, &:last-child th":
"rgb(255 255 255 / 0.05)", { border: 0 },
}, "&:hover": {
}} background:
> "rgb(255 255 255 / 0.05)",
<TableCell width="4.5em"> },
<Box className="library-list-image-container"> }}
{!!item.ImageTags >
.Primary && ( <TableCell width="4.5em">
<img <Box className="library-list-image-container">
className="library-list-image" {!!item
src={`${window.api.basePath}/Items/${item.Id}/Images/Primary?quality=80&tag=${item.ImageTags.Primary}`} .ImageTags
/> .Primary && (
)} <img
<Box className="library-list-icon-container"> className="library-list-image"
<MdiMusic className="library-list-icon" /> src={`${window.api.basePath}/Items/${item.Id}/Images/Primary?quality=80&tag=${item.ImageTags.Primary}`}
/>
)}
<Box className="library-list-icon-container">
<MdiMusic className="library-list-icon" />
</Box>
</Box> </Box>
</Box> </TableCell>
</TableCell> <TableCell>
<TableCell> <Typography variant="h6">
<Typography variant="h6"> {item.Name}
{item.Name} </Typography>
</Typography> <Stack
<Typography direction="row"
variant="subtitle1" divider={
sx={{ <Typography
opacity: 0.7, variant="subtitle1"
}} sx={{
> opacity: 0.7,
{item.Album} }}
</Typography> >
</TableCell> ,
<TableCell> </Typography>
<Typography variant="subtitle1"> }
{getRuntimeMusic( >
item.RunTimeTicks, {item.Artists.map(
)} (
</Typography> artist,
</TableCell> aindex,
<TableCell width="1em"> ) => {
<IconButton return (
onClick={() => { <Typography
handleLiking( variant="subtitle1"
item, sx={{
); opacity: 0.7,
}} }}
> >
{item.UserData {
.IsFavorite ? ( artist
<MdiHeart /> }
) : ( </Typography>
<MdiHeartOutline /> );
)} },
</IconButton> )}
</TableCell> </Stack>
</TableRow> </TableCell>
); <TableCell>
})} <Typography variant="subtitle1">
{getRuntimeMusic(
item.RunTimeTicks,
)}
</Typography>
</TableCell>
<TableCell width="1em">
<IconButton
onClick={() => {
handleLiking(
item,
);
}}
>
{item.UserData
.IsFavorite ? (
<MdiHeart />
) : (
<MdiHeartOutline />
)}
</IconButton>
</TableCell>
</TableRow>
);
})}
</TableBody>
</Table> </Table>
</TableContainer> </TableContainer>
)} )}
{!items.isFetching &&
items.isSuccess &&
items.data.TotalRecordCount > 75 && (
<Stack
alignItems="center"
justifyContent="center"
paddingTop={2}
direction="row"
>
<Typography
variant="subtitle2"
sx={{ opacity: 0.5 }}
>
{maxDisplayItems * (page - 1)} -{" "}
{items.data.TotalRecordCount <=
maxDisplayItems * page
? items.data.TotalRecordCount
: maxDisplayItems * page}{" "}
of {items.data.TotalRecordCount}
</Typography>
<Pagination
page={page}
onChange={(e, val) => {
setPage(val);
}}
count={Math.ceil(
items.data.TotalRecordCount /
maxDisplayItems,
)}
sx={{
width: "fit-content",
}}
/>
</Stack>
)}
{items.isError && <ErrorNotice />} {items.isError && <ErrorNotice />}
</Box> </Box>
</Box> </Box>