Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Show the last Programs page when the page in the URL is past the end

Rafidadad1f6bae7
diff --git a/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/use-programs-page.ts b/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/use-programs-page.ts
index 1a0c58d825..47ca82f7e1 100644
--- a/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/use-programs-page.ts
+++ b/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/use-programs-page.ts
@@ -1,6 +1,6 @@
import useProgramEnrollments from "@/lib/swr/use-program-enrollments";
-import { usePagination, useRouterStuff } from "@dub/ui";
-import { useEffect } from "react";
+import { PaginationState, usePagination, useRouterStuff } from "@dub/ui";
+import { SetStateAction, useCallback, useMemo } from "react";
import { PROGRAMS_PAGE_SIZE, ProgramTab } from "./program-tabs";
// one page of the programs of a tab, with the sort from the URL. The grid and
@@ -18,19 +18,29 @@ export function useProgramsPage({
count?: number;
includeInactiveDates?: boolean;
}) {
- const { searchParams, queryParams } = useRouterStuff();
- const { pagination, setPagination } = usePagination(PROGRAMS_PAGE_SIZE);
+ const { searchParams } = useRouterStuff();
+ const { pagination: urlPagination, setPagination: setUrlPagination } =
+ usePagination(PROGRAMS_PAGE_SIZE);
// a page past the end (an old link, or the last invite of a page accepted)
- // has no programs and no footer, so go back to the first page
- useEffect(() => {
- if (
- count &&
- pagination.pageIndex > Math.ceil(count / pagination.pageSize)
- ) {
- queryParams({ del: "page" });
- }
- }, [count, pagination.pageIndex, pagination.pageSize]);
+ // has no programs and no footer, so show the last page instead. This does
+ // not change the URL, because usePagination also writes the page to the URL.
+ const pagination = useMemo(() => {
+ const lastPage = count ? Math.ceil(count / urlPagination.pageSize) : 0;
+
+ return lastPage && urlPagination.pageIndex > lastPage
+ ? { ...urlPagination, pageIndex: lastPage }
+ : urlPagination;
+ }, [count, urlPagination]);
+
+ // Previous and Next start from the page on screen, not the page in the URL
+ const setPagination = useCallback(
+ (update: SetStateAction<PaginationState>) =>
+ setUrlPagination(
+ typeof update === "function" ? update(pagination) : update,
+ ),
+ [pagination, setUrlPagination],
+ );
const sortBy: "name" | undefined =
searchParams.get("sortBy") === "name" ? "name" : undefined;