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.

update EarningsSummary

Steven Tey7de45af1462a
diff --git a/apps/web/app/app.dub.co/embed/referrals/activity.tsx b/apps/web/app/app.dub.co/embed/referrals/activity.tsx
index 34c7fad1f2..4b48126e67 100644
--- a/apps/web/app/app.dub.co/embed/referrals/activity.tsx
+++ b/apps/web/app/app.dub.co/embed/referrals/activity.tsx
@@ -1,6 +1,6 @@
import { CursorRays } from "@/ui/layout/sidebar/icons/cursor-rays";
import { InfoTooltip, MiniAreaChart } from "@dub/ui";
-import { cn, nFormatter } from "@dub/utils";
+import { cn, currencyFormatter, nFormatter } from "@dub/utils";
import { fetcher } from "@dub/utils/src/functions";
import { AnalyticsTimeseries } from "dub/models/components";
import { SVGProps, useId } from "react";
@@ -10,10 +10,12 @@ export function ReferralsEmbedActivity({
clicks,
leads,
sales,
+ saleAmount,
}: {
clicks: number;
leads: number;
sales: number;
+ saleAmount: number;
}) {
const isEmpty = clicks === 0 && leads === 0 && sales === 0;
const { data: analytics } = useSWR<AnalyticsTimeseries[]>(
@@ -46,10 +48,11 @@ export function ReferralsEmbedActivity({
{
label: "Sales",
value: sales,
+ subValue: saleAmount,
description:
"Total number of leads that converted to a paid account",
},
- ].map(({ label, value, description }) => (
+ ].map(({ label, value, subValue, description }) => (
<div
key={label}
className="relative flex flex-col justify-between p-4"
@@ -60,7 +63,12 @@ export function ReferralsEmbedActivity({
<InfoTooltip content={description} />
</span>
<span className="text-content-default text-base font-medium leading-none">
- {nFormatter(value, { full: true })}
+ {nFormatter(value, { full: true })}{" "}
+ {subValue && (
+ <span className="text-content-subtle text-xs">
+ ({currencyFormatter(subValue / 100)})
+ </span>
+ )}
</span>
</div>
<div className="xs:block hidden h-12">
diff --git a/apps/web/app/app.dub.co/embed/referrals/payouts.tsx b/apps/web/app/app.dub.co/embed/referrals/earnings-summary.tsx
similarity index 67%
rename from apps/web/app/app.dub.co/embed/referrals/payouts.tsx
rename to apps/web/app/app.dub.co/embed/referrals/earnings-summary.tsx
index 884d6ca3ed..77abc0833b 100644
--- a/apps/web/app/app.dub.co/embed/referrals/payouts.tsx
+++ b/apps/web/app/app.dub.co/embed/referrals/earnings-summary.tsx
@@ -1,20 +1,19 @@
-import { PayoutStatus } from "@dub/prisma/client";
import { Button, InfoTooltip } from "@dub/ui";
import { currencyFormatter } from "@dub/utils";
-export function ReferralsEmbedPayouts({
- payouts,
+export function ReferralsEmbedEarningsSummary({
+ earnings,
programSlug,
}: {
- payouts: { status: PayoutStatus; amount: number }[];
+ earnings: { upcoming: number; paid: number };
programSlug: string;
}) {
return (
<div className="border-border-subtle bg-bg-default flex flex-col justify-between gap-4 rounded-lg border p-4">
<div className="flex items-center justify-between">
<div className="flex items-center gap-1">
- <p className="text-content-subtle text-sm">Payouts</p>
- <InfoTooltip content="Payouts are processed at the start of each month. Your earnings are automatically transferred to your bank account." />
+ <p className="text-content-subtle text-sm">Earnings</p>
+ <InfoTooltip content="Summary of your commission earnings from your referrals." />
</div>
<a
href={`https://partners.dub.co/${programSlug}/register`}
@@ -31,15 +30,11 @@ export function ReferralsEmbedPayouts({
{[
{
label: "Upcoming",
- value:
- payouts.find((payout) => payout.status === PayoutStatus.pending)
- ?.amount ?? 0,
+ value: earnings.upcoming,
},
{
label: "Paid",
- value:
- payouts.find((payout) => payout.status === PayoutStatus.completed)
- ?.amount ?? 0,
+ value: earnings.paid,
},
].map(({ label, value }) => (
<div key={label} className="flex justify-between text-sm">
diff --git a/apps/web/app/app.dub.co/embed/referrals/page-client.tsx b/apps/web/app/app.dub.co/embed/referrals/page-client.tsx
index 8f86d8dee4..ebd8d7285c 100644
--- a/apps/web/app/app.dub.co/embed/referrals/page-client.tsx
+++ b/apps/web/app/app.dub.co/embed/referrals/page-client.tsx
@@ -6,7 +6,7 @@ import { programResourcesSchema } from "@/lib/zod/schemas/program-resources";
import { HeroBackground } from "@/ui/partners/hero-background";
import { ProgramRewardList } from "@/ui/partners/program-reward-list";
import { ThreeDots } from "@/ui/shared/icons";
-import { Link, PayoutStatus, Program } from "@dub/prisma/client";
+import { Link, Program } from "@dub/prisma/client";
import {
Button,
Check,
@@ -23,9 +23,9 @@ import { AnimatePresence } from "framer-motion";
import { useEffect, useMemo, useState } from "react";
import { ReferralsEmbedActivity } from "./activity";
import { ReferralsEmbedEarnings } from "./earnings";
+import { ReferralsEmbedEarningsSummary } from "./earnings-summary";
import { ReferralsEmbedFAQ } from "./faq";
import { ReferralsEmbedLeaderboard } from "./leaderboard";
-import { ReferralsEmbedPayouts } from "./payouts";
import { ReferralsEmbedQuickstart } from "./quickstart";
import { ReferralsEmbedResources } from "./resources";
import { ThemeOptions } from "./theme-options";
@@ -36,7 +36,7 @@ export function ReferralsEmbedPageClient({
links,
rewards,
discount,
- payouts,
+ earnings,
stats,
themeOptions,
}: {
@@ -44,14 +44,15 @@ export function ReferralsEmbedPageClient({
links: Link[];
rewards: RewardProps[];
discount?: DiscountProps | null;
- payouts: {
- status: PayoutStatus;
- amount: number;
- }[];
+ earnings: {
+ upcoming: number;
+ paid: number;
+ };
stats: {
clicks: number;
leads: number;
sales: number;
+ saleAmount: number;
};
themeOptions: ThemeOptions;
}) {
@@ -176,12 +177,11 @@ export function ReferralsEmbedPageClient({
</div>
</div>
<div className="mt-4 grid gap-2 sm:h-32 sm:grid-cols-3">
- <ReferralsEmbedActivity
- clicks={stats.clicks}
- leads={stats.leads}
- sales={stats.sales}
+ <ReferralsEmbedActivity {...stats} />
+ <ReferralsEmbedEarningsSummary
+ earnings={earnings}
+ programSlug={program.slug}
/>
- <ReferralsEmbedPayouts payouts={payouts} programSlug={program.slug} />
</div>
<div className="mt-4">
<div className="border-border-subtle flex items-center border-b">
diff --git a/apps/web/app/app.dub.co/embed/referrals/utils.ts b/apps/web/app/app.dub.co/embed/referrals/utils.ts
index eb56a45f18..dcef8f75a3 100644
--- a/apps/web/app/app.dub.co/embed/referrals/utils.ts
+++ b/apps/web/app/app.dub.co/embed/referrals/utils.ts
@@ -29,7 +29,7 @@ export const getReferralsEmbedData = async (token: string) => {
notFound();
}
- const [rewards, discount, payouts] = await Promise.all([
+ const [rewards, discount, commissions] = await Promise.all([
determinePartnerRewards({
partnerId,
programId,
@@ -40,12 +40,15 @@ export const getReferralsEmbedData = async (token: string) => {
partnerId,
}),
- prisma.payout.groupBy({
+ prisma.commission.groupBy({
by: ["status"],
_sum: {
- amount: true,
+ earnings: true,
},
where: {
+ earnings: {
+ gt: 0,
+ },
programId,
partnerId,
},
@@ -59,14 +62,20 @@ export const getReferralsEmbedData = async (token: string) => {
links,
rewards: rewards as RewardProps[],
discount,
- payouts: payouts.map((payout) => ({
- status: payout.status,
- amount: payout._sum.amount ?? 0,
- })),
+ earnings: {
+ upcoming: commissions.reduce((acc, c) => {
+ if (c.status === "pending" || c.status === "processed") {
+ return acc + (c._sum.earnings ?? 0);
+ }
+ return acc;
+ }, 0),
+ paid: commissions.find((c) => c.status === "paid")?._sum.earnings ?? 0,
+ },
stats: {
clicks: links.reduce((acc, link) => acc + link.clicks, 0),
leads: links.reduce((acc, link) => acc + link.leads, 0),
sales: links.reduce((acc, link) => acc + link.sales, 0),
+ saleAmount: links.reduce((acc, link) => acc + link.saleAmount, 0),
},
};
};