2021-05-21 00:50:55 -07:00
|
|
|
import React from "react";
|
2021-06-21 10:54:31 -07:00
|
|
|
import { Box, Button, Flex, Select } from "@chakra-ui/react";
|
2022-09-14 20:19:14 -07:00
|
|
|
import Link from "next/link";
|
|
|
|
import { useRouter } from "next/router";
|
2021-05-21 00:50:55 -07:00
|
|
|
|
Paginate the user outfits page
My main inspiration for doing this is actually our potentially-huge upcoming Vercel bill lol
From inspecting my Honeycomb dashboard, it looks like the main offender for backend CPU time usage is outfit images. And it looks like they come in big spikes, of lots of low usage and then suddenly 1,000 requests in one minute.
My suspicion is that this is from users with many saved outfits loading their outfit page, which previously would show all of them at once.
We do have `loading="lazy"` set, but not all browsers support that yet, and I've had trouble pinning down the exact behavior anyway!
Anyway, paginating makes for a better experience for those huge-list users anyway. We've been meaning to do it, so here we go!
My hope is that this drastically decreases backend CPU hours immediately 🤞 If not, we'll need to investigate in more detail where these outfit image requests are actually coming from!
Note that I added the pagination to the existing `outfits` GraphQL endpoint, rather than creating a new one. I felt comfortable doing this because it requires login anyway, so I'm confident that other clients aren't using it; and because, while this kind of thing often creates a risk of problems with frontend and backend code getting out of sync, I think someone running old frontend code will just see only their first 30 outfits (but no pagination toolbar), and get confused and refresh the page, at which point they'll see all of them. (And I actually _prefer_ that slightly confusing UX, to avoid getting more giant spikes of outfit image requests, lol :p)
2021-11-01 19:33:40 -07:00
|
|
|
function PaginationToolbar({
|
|
|
|
isLoading,
|
|
|
|
totalCount,
|
|
|
|
numPerPage = 30,
|
|
|
|
...props
|
|
|
|
}) {
|
2022-10-14 18:01:31 -07:00
|
|
|
const {
|
|
|
|
numTotalPages,
|
|
|
|
currentPageNumber,
|
|
|
|
goToPageNumber,
|
|
|
|
buildPageUrl,
|
|
|
|
} = useRouterPagination(totalCount, numPerPage);
|
2021-06-21 13:50:49 -07:00
|
|
|
|
2022-10-14 18:01:31 -07:00
|
|
|
const pagesAreLoaded = currentPageNumber != null && numTotalPages != null;
|
|
|
|
const hasPrevPage = pagesAreLoaded && currentPageNumber > 1;
|
|
|
|
const hasNextPage = pagesAreLoaded && currentPageNumber < numTotalPages;
|
2021-05-21 00:50:55 -07:00
|
|
|
|
2022-10-14 18:01:31 -07:00
|
|
|
const prevPageUrl = hasPrevPage ? buildPageUrl(currentPageNumber - 1) : null;
|
|
|
|
const nextPageUrl = hasNextPage ? buildPageUrl(currentPageNumber + 1) : null;
|
2021-06-21 10:54:31 -07:00
|
|
|
|
2021-05-21 00:50:55 -07:00
|
|
|
return (
|
2021-06-21 10:21:25 -07:00
|
|
|
<Flex align="center" justify="space-between" {...props}>
|
2022-09-14 20:19:14 -07:00
|
|
|
<LinkOrButton
|
2022-10-14 18:01:31 -07:00
|
|
|
href={prevPageUrl}
|
2022-09-14 20:19:14 -07:00
|
|
|
_disabled={{
|
|
|
|
cursor: isLoading ? "wait" : "not-allowed",
|
|
|
|
opacity: 0.4,
|
|
|
|
}}
|
2022-10-14 18:01:31 -07:00
|
|
|
isDisabled={!hasPrevPage}
|
2021-05-21 00:50:55 -07:00
|
|
|
>
|
|
|
|
← Prev
|
2022-09-14 20:19:14 -07:00
|
|
|
</LinkOrButton>
|
2021-06-21 10:21:25 -07:00
|
|
|
{numTotalPages && (
|
2021-06-21 10:54:31 -07:00
|
|
|
<Flex align="center">
|
|
|
|
<Box flex="0 0 auto">Page</Box>
|
|
|
|
<Box width="1" />
|
|
|
|
<PageNumberSelect
|
|
|
|
currentPageNumber={currentPageNumber}
|
|
|
|
numTotalPages={numTotalPages}
|
|
|
|
onChange={goToPageNumber}
|
|
|
|
marginBottom="-2px"
|
|
|
|
/>
|
|
|
|
<Box width="1" />
|
|
|
|
<Box flex="0 0 auto">of {numTotalPages}</Box>
|
|
|
|
</Flex>
|
2021-06-21 10:21:25 -07:00
|
|
|
)}
|
2022-09-14 20:19:14 -07:00
|
|
|
<LinkOrButton
|
2022-10-14 18:01:31 -07:00
|
|
|
href={nextPageUrl}
|
2022-09-14 20:19:14 -07:00
|
|
|
_disabled={{
|
|
|
|
cursor: isLoading ? "wait" : "not-allowed",
|
|
|
|
opacity: 0.4,
|
|
|
|
}}
|
2022-10-14 18:01:31 -07:00
|
|
|
isDisabled={!hasNextPage}
|
2021-05-21 00:50:55 -07:00
|
|
|
>
|
|
|
|
Next →
|
2022-09-14 20:19:14 -07:00
|
|
|
</LinkOrButton>
|
2021-05-21 00:50:55 -07:00
|
|
|
</Flex>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-10-14 18:01:31 -07:00
|
|
|
function useRouterPagination(totalCount, numPerPage) {
|
|
|
|
const { query, push: pushHistory } = useRouter();
|
|
|
|
|
|
|
|
const currentOffset = parseInt(query.offset) || 0;
|
|
|
|
|
|
|
|
const currentPageIndex = Math.floor(currentOffset / numPerPage);
|
|
|
|
const currentPageNumber = currentPageIndex + 1;
|
|
|
|
const numTotalPages = totalCount ? Math.ceil(totalCount / numPerPage) : null;
|
|
|
|
|
|
|
|
const buildPageUrl = React.useCallback(
|
|
|
|
(newPageNumber) => {
|
|
|
|
const newParams = new URLSearchParams(query);
|
|
|
|
const newPageIndex = newPageNumber - 1;
|
|
|
|
const newOffset = newPageIndex * numPerPage;
|
|
|
|
newParams.set("offset", newOffset);
|
|
|
|
return "?" + newParams.toString();
|
|
|
|
},
|
|
|
|
[query, numPerPage]
|
|
|
|
);
|
|
|
|
|
|
|
|
const goToPageNumber = React.useCallback(
|
|
|
|
(newPageNumber) => {
|
|
|
|
pushHistory(buildPageUrl(newPageNumber));
|
|
|
|
},
|
|
|
|
[buildPageUrl, pushHistory]
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
numTotalPages,
|
|
|
|
currentPageNumber,
|
|
|
|
goToPageNumber,
|
|
|
|
buildPageUrl,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-09-14 20:19:14 -07:00
|
|
|
function LinkOrButton({ href, ...props }) {
|
|
|
|
if (href != null) {
|
|
|
|
return (
|
|
|
|
<Link href={href} passHref>
|
|
|
|
<Button as="a" {...props} />
|
|
|
|
</Link>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return <Button {...props} />;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 10:54:31 -07:00
|
|
|
function PageNumberSelect({
|
|
|
|
currentPageNumber,
|
|
|
|
numTotalPages,
|
|
|
|
onChange,
|
|
|
|
...props
|
|
|
|
}) {
|
|
|
|
const allPageNumbers = Array.from({ length: numTotalPages }, (_, i) => i + 1);
|
|
|
|
|
|
|
|
const handleChange = React.useCallback(
|
|
|
|
(e) => onChange(Number(e.target.value)),
|
|
|
|
[onChange]
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Select
|
|
|
|
value={currentPageNumber}
|
|
|
|
onChange={handleChange}
|
|
|
|
width="7ch"
|
|
|
|
variant="flushed"
|
|
|
|
textAlign="center"
|
|
|
|
{...props}
|
|
|
|
>
|
|
|
|
{allPageNumbers.map((pageNumber) => (
|
|
|
|
<option key={pageNumber} value={pageNumber}>
|
|
|
|
{pageNumber}
|
|
|
|
</option>
|
|
|
|
))}
|
|
|
|
</Select>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-05-21 00:50:55 -07:00
|
|
|
export default PaginationToolbar;
|