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.

Open the first Programs tab that has programs

Rafi9f0f7c3ac13d
diff --git a/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/page-client.tsx b/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/page-client.tsx
index ce061ad10a..15e19b8050 100644
--- a/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/page-client.tsx
+++ b/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/page-client.tsx
@@ -2,6 +2,7 @@
import useProgramEnrollments from "@/lib/swr/use-program-enrollments";
import useProgramEnrollmentsCount from "@/lib/swr/use-program-enrollments-count";
+import useProgramEnrollmentsStatusCounts from "@/lib/swr/use-program-enrollments-status-counts";
import { PageWidthWrapper } from "@/ui/layout/page-width-wrapper";
import { ProgramCard, ProgramCardSkeleton } from "@/ui/partners/program-card";
import { ProgramInviteCard } from "@/ui/partners/program-invite-card";
@@ -17,8 +18,6 @@ import { ProgramsTable } from "./programs-table";
type ProgramsView = "grid" | "table";
-const INACTIVE_TAB = PROGRAM_TABS.find(({ id }) => id === "inactive")!;
-
export function PartnersDashboardPageClient() {
const { searchParams, queryParams } = useRouterStuff();
@@ -27,27 +26,33 @@ export function PartnersDashboardPageClient() {
"grid",
);
- const tab =
- PROGRAM_TABS.find(({ id }) => id === searchParams.get("tab")) ??
- PROGRAM_TABS[0];
const search = searchParams.get("search") || undefined;
- const { count } = useProgramEnrollmentsCount({
- status: tab.statuses.join(","),
- search,
- });
+ const { counts: statusCounts } = useProgramEnrollmentsStatusCounts();
- const { count: invitationsCount } = useProgramEnrollmentsCount({
- status: "invited",
- });
+ const getTabCount = (programTab: ProgramTab) =>
+ statusCounts
+ ? programTab.statuses.reduce(
+ (sum, status) => sum + (statusCounts[status] ?? 0),
+ 0,
+ )
+ : undefined;
- const { count: inactiveCount } = useProgramEnrollmentsCount({
- status: INACTIVE_TAB.statuses.join(","),
- });
+ // without a tab in the URL, open the first tab that has programs, so that
+ // partners with only applications or invitations do not see an empty tab
+ const tab =
+ PROGRAM_TABS.find(({ id }) => id === searchParams.get("tab")) ??
+ (statusCounts
+ ? PROGRAM_TABS.find((programTab) => getTabCount(programTab)) ??
+ PROGRAM_TABS[0]
+ : undefined);
// the Inactive tab shows only when the partner has an inactive program
const tabs = PROGRAM_TABS.filter(
- ({ id }) => id !== "inactive" || inactiveCount || tab.id === "inactive",
+ (programTab) =>
+ programTab.id !== "inactive" ||
+ getTabCount(programTab) ||
+ tab?.id === "inactive",
);
return (
@@ -86,10 +91,10 @@ export function PartnersDashboardPageClient() {
icon,
badge:
id === "invitations"
- ? formatTabBadgeCount(invitationsCount)
+ ? formatTabBadgeCount(statusCounts?.invited)
: undefined,
}))}
- currentTabId={tab.id}
+ currentTabId={tab?.id ?? ""}
setCurrentTabId={(id) =>
queryParams({
set: { tab: id },
@@ -98,12 +103,10 @@ export function PartnersDashboardPageClient() {
}
/>
<div className="border-border-subtle -mx-px -mb-px overflow-clip rounded-xl border bg-white">
- {count === 0 ? (
- <ProgramsEmptyState tab={tab} search={search} />
- ) : view === "table" ? (
- <ProgramsTable tab={tab} search={search} />
+ {tab ? (
+ <ProgramsTabContent tab={tab} search={search} view={view} />
) : (
- <ProgramsGrid tab={tab} search={search} />
+ <ProgramsGridSkeleton />
)}
</div>
</div>
@@ -111,6 +114,41 @@ export function PartnersDashboardPageClient() {
);
}
+function ProgramsTabContent({
+ tab,
+ search,
+ view,
+}: {
+ tab: ProgramTab;
+ search?: string;
+ view: ProgramsView;
+}) {
+ const { count } = useProgramEnrollmentsCount({
+ status: tab.statuses.join(","),
+ search,
+ });
+
+ if (count === 0) {
+ return <ProgramsEmptyState tab={tab} search={search} />;
+ }
+
+ return view === "table" ? (
+ <ProgramsTable tab={tab} search={search} />
+ ) : (
+ <ProgramsGrid tab={tab} search={search} />
+ );
+}
+
+function ProgramsGridSkeleton() {
+ return (
+ <div className="@md/page:grid-cols-2 @3xl/page:grid-cols-3 grid gap-4 p-4">
+ {Array.from({ length: 3 }).map((_, idx) => (
+ <ProgramCardSkeleton key={idx} />
+ ))}
+ </div>
+ );
+}
+
function ProgramsGrid({ tab, search }: { tab: ProgramTab; search?: string }) {
const { programEnrollments, isLoading } = useProgramEnrollments({
includeRewardsDiscounts: true,
diff --git a/apps/web/lib/swr/use-program-enrollments-status-counts.ts b/apps/web/lib/swr/use-program-enrollments-status-counts.ts
new file mode 100644
index 0000000000..bc6816be11
--- /dev/null
+++ b/apps/web/lib/swr/use-program-enrollments-status-counts.ts
@@ -0,0 +1,37 @@
+import { fetcher } from "@dub/utils";
+import { ProgramEnrollmentStatus } from "@prisma/client";
+import { useSession } from "next-auth/react";
+import { useMemo } from "react";
+import useSWR from "swr";
+
+// GET /api/partner-profile/programs/count?groupBy=status
+export default function useProgramEnrollmentsStatusCounts() {
+ const { data: session } = useSession();
+ const partnerId = session?.user?.["defaultPartnerId"];
+
+ const { data, isLoading, error } = useSWR<
+ { status: ProgramEnrollmentStatus; _count: number }[]
+ >(
+ partnerId && "/api/partner-profile/programs/count?groupBy=status",
+ fetcher,
+ {
+ dedupingInterval: 60000,
+ },
+ );
+
+ const counts = useMemo(
+ () =>
+ data
+ ? (Object.fromEntries(
+ data.map(({ status, _count }) => [status, _count]),
+ ) as Record<ProgramEnrollmentStatus, number>)
+ : undefined,
+ [data],
+ );
+
+ return {
+ counts,
+ isLoading,
+ error,
+ };
+}