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.

Share the program activity chart data between the grid and the table

Raficccd951af794
diff --git a/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/programs-table.tsx b/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/programs-table.tsx
index 24b1f70e76..f19aa9b0a7 100644
--- a/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/programs-table.tsx
+++ b/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/programs-table.tsx
@@ -1,7 +1,7 @@
"use client";
import { constructPartnerLink } from "@/lib/partners/construct-partner-link";
-import { usePartnerProfileEarningsTimeseries } from "@/lib/swr/use-partner-profile-earnings";
+import { usePartnerProgramActivity } from "@/lib/swr/use-partner-profile-earnings";
import useProgramEnrollments from "@/lib/swr/use-program-enrollments";
import useProgramEnrollmentsCount from "@/lib/swr/use-program-enrollments-count";
import { ProgramEnrollmentProps } from "@/lib/types";
@@ -309,20 +309,7 @@ function ProgramActivity({
}: {
programEnrollment: ProgramEnrollmentProps;
}) {
- // one request for every program on the page (SWR dedupes it across rows)
- const { data: timeseries } = usePartnerProfileEarningsTimeseries({
- groupBy: "programId",
- interval: "1y",
- });
-
- const data = useMemo(
- () =>
- timeseries?.map(({ start, data }) => ({
- date: new Date(start),
- value: data?.[programEnrollment.programId] ?? 0,
- })),
- [timeseries, programEnrollment.programId],
- );
+ const data = usePartnerProgramActivity(programEnrollment.programId);
return (
<div className="h-6 w-36">
diff --git a/apps/web/lib/swr/use-partner-profile-earnings.ts b/apps/web/lib/swr/use-partner-profile-earnings.ts
index 3d3b0d7ec9..e6ed81a7de 100644
--- a/apps/web/lib/swr/use-partner-profile-earnings.ts
+++ b/apps/web/lib/swr/use-partner-profile-earnings.ts
@@ -1,5 +1,6 @@
import { fetcher } from "@dub/utils";
import { useSession } from "next-auth/react";
+import { useMemo } from "react";
import useSWR from "swr";
import { DUB_PARTNERS_ANALYTICS_INTERVAL } from "../analytics/constants";
import { IntervalOptions } from "../analytics/types";
@@ -46,6 +47,24 @@ export function usePartnerProfileEarningsTimeseries({
);
}
+// the activity chart of one program: its earnings in the last year, from one
+// request for all programs (SWR dedupes it across the cards and table rows)
+export function usePartnerProgramActivity(programId: string) {
+ const { data: timeseries } = usePartnerProfileEarningsTimeseries({
+ groupBy: "programId",
+ interval: "1y",
+ });
+
+ return useMemo(
+ () =>
+ timeseries?.map(({ start, data }) => ({
+ date: new Date(start),
+ value: data?.[programId] ?? 0,
+ })),
+ [timeseries, programId],
+ );
+}
+
// GET /api/partner-profile/earnings/analytics
export function usePartnerProfileEarningsAnalytics<
G extends PartnerProfileEarningsAnalyticsGroupBy,
@@ -54,7 +73,9 @@ export function usePartnerProfileEarningsAnalytics<
limit,
...dateRange
}: DateRange & { groupBy: G; limit?: number }) {
- return usePartnerProfileEarningsSWR<PartnerProfileEarningsAnalyticsByGroup[G]>(
+ return usePartnerProfileEarningsSWR<
+ PartnerProfileEarningsAnalyticsByGroup[G]
+ >(
"/analytics",
{
groupBy,
diff --git a/apps/web/ui/partners/program-card.tsx b/apps/web/ui/partners/program-card.tsx
index 88f7b922a3..dd5ae9c67b 100644
--- a/apps/web/ui/partners/program-card.tsx
+++ b/apps/web/ui/partners/program-card.tsx
@@ -2,7 +2,7 @@
import { constructPartnerLink } from "@/lib/partners/construct-partner-link";
import { getProgramApplicationRejectionReasonLabel } from "@/lib/program-applications/program-application-rejection";
-import { usePartnerProfileEarningsTimeseries } from "@/lib/swr/use-partner-profile-earnings";
+import { usePartnerProgramActivity } from "@/lib/swr/use-partner-profile-earnings";
import {
PartnerProfileProgramEnrollmentProps,
ProgramEnrollmentProps,
@@ -26,7 +26,7 @@ import {
import NumberFlow from "@number-flow/react";
import Link from "next/link";
import { useRouter } from "next/navigation";
-import { type ReactNode, useMemo } from "react";
+import { type ReactNode } from "react";
function RejectionTooltipRow({
icon,
@@ -273,22 +273,8 @@ function ProgramCardStatus({
);
}
-// the activity chart for one program, from the earnings of all programs in one
-// request (SWR dedupes it across the cards)
function ProgramCardActivity({ programId }: { programId: string }) {
- const { data: timeseries } = usePartnerProfileEarningsTimeseries({
- groupBy: "programId",
- interval: "1y",
- });
-
- const chartData = useMemo(
- () =>
- timeseries?.map(({ start, data }) => ({
- date: new Date(start),
- value: data?.[programId] ?? 0,
- })),
- [timeseries, programId],
- );
+ const chartData = usePartnerProgramActivity(programId);
return (
<div className="-mx-5 h-16">