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.

Add performance bounty progress UI

Tim Wilson256f066731d0
diff --git a/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts b/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts
index 9ab108fb58..8d734f7383 100644
--- a/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts
+++ b/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts
@@ -66,7 +66,7 @@ export const PATCH = withWorkspace(async ({ workspace, params, req }) => {
data: {
name,
description,
- startsAt,
+ startsAt: startsAt!, // Can remove the ! when we're on a newer TS version (currently 5.4.4)
endsAt,
rewardAmount,
...(bounty.type === "submission" && {
diff --git a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/bounties/route.ts b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/bounties/route.ts
index d8b87f0ca0..f1b6ded10a 100644
--- a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/bounties/route.ts
+++ b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/bounties/route.ts
@@ -1,44 +1,79 @@
import { getProgramEnrollmentOrThrow } from "@/lib/api/programs/get-program-enrollment-or-throw";
import { withPartnerProfile } from "@/lib/auth/partner";
-import { BountyWithSubmissionsSchema } from "@/lib/zod/schemas/bounties";
+import { BountyWithPartnerDataSchema } from "@/lib/zod/schemas/bounties";
import { prisma } from "@dub/prisma";
import { NextResponse } from "next/server";
import { z } from "zod";
// GET /api/partner-profile/programs/[programId]/bounties – get available bounties for an enrolled program
export const GET = withPartnerProfile(async ({ partner, params }) => {
- const { program } = await getProgramEnrollmentOrThrow({
+ const { program, totalCommissions } = await getProgramEnrollmentOrThrow({
partnerId: partner.id,
programId: params.programId,
});
- const bounties = await prisma.bounty.findMany({
- where: {
- programId: program.id,
- startsAt: {
- lte: new Date(),
- },
- OR: [
- {
- endsAt: null,
+ const [bounties, linkMetrics] = await Promise.all([
+ prisma.bounty.findMany({
+ where: {
+ programId: program.id,
+ startsAt: {
+ lte: new Date(),
},
- {
- endsAt: {
- gt: new Date(),
+ OR: [
+ {
+ endsAt: null,
+ },
+ {
+ endsAt: {
+ gt: new Date(),
+ },
+ },
+ ],
+ },
+ include: {
+ submissions: {
+ where: {
+ partnerId: partner.id,
},
},
- ],
- },
- include: {
- submissions: {
- where: {
- partnerId: partner.id,
- },
+ workflow: true,
},
- },
- });
+ }),
+ prisma.link.aggregate({
+ _sum: {
+ leads: true,
+ conversions: true,
+ saleAmount: true,
+ },
+ where: {
+ programId: program.id,
+ partnerId: partner.id,
+ },
+ }),
+ ]);
return NextResponse.json(
- z.array(BountyWithSubmissionsSchema).parse(bounties),
+ z.array(BountyWithPartnerDataSchema).parse(
+ bounties.map((bounty) => {
+ const triggerConditions = Array.isArray(
+ bounty.workflow?.triggerConditions,
+ )
+ ? bounty.workflow?.triggerConditions
+ : [];
+
+ return {
+ ...bounty,
+ performanceCondition:
+ triggerConditions.length > 0 ? triggerConditions[0] : null,
+
+ partner: {
+ leads: linkMetrics._sum.leads ?? 0,
+ conversions: linkMetrics._sum.conversions ?? 0,
+ saleAmount: linkMetrics._sum.saleAmount ?? 0,
+ totalCommissions,
+ },
+ };
+ }),
+ ),
);
});
diff --git a/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/bounties/bounty-card.tsx b/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/bounties/bounty-card.tsx
index 9ae84b6bd0..eea8c50d03 100644
--- a/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/bounties/bounty-card.tsx
+++ b/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/bounties/bounty-card.tsx
@@ -1,11 +1,12 @@
-import { BountyWithSubmissionsProps } from "@/lib/types";
+import { BountyWithPartnerDataProps } from "@/lib/types";
+import { BountyPerformance } from "@/ui/partners/bounties/bounty-performance";
import { BountyThumbnailImage } from "@/ui/partners/bounties/bounty-thumbnail-image";
import { useClaimBountyModal } from "@/ui/partners/bounties/claim-bounty-modal";
-import { Button, StatusBadge } from "@dub/ui";
+import { StatusBadge } from "@dub/ui";
import { Calendar6 } from "@dub/ui/icons";
import { formatDate } from "@dub/utils";
-export function BountyCard({ bounty }: { bounty: BountyWithSubmissionsProps }) {
+export function BountyCard({ bounty }: { bounty: BountyWithPartnerDataProps }) {
const submission = bounty.submissions?.[0];
const { claimBountyModal, setShowClaimBountyModal } = useClaimBountyModal({
@@ -14,42 +15,39 @@ export function BountyCard({ bounty }: { bounty: BountyWithSubmissionsProps }) {
});
return (
- <div className="border-border-subtle flex flex-col gap-5 rounded-xl border bg-white p-5">
+ <>
{claimBountyModal}
- <div className="relative flex h-[132px] items-center justify-center rounded-lg bg-neutral-100 py-1.5">
- <div className="relative size-full">
- <BountyThumbnailImage bounty={bounty} />
+ <button
+ type="button"
+ onClick={() => setShowClaimBountyModal(true)}
+ className="border-border-subtle hover:border-border-default group relative flex cursor-pointer flex-col gap-5 overflow-hidden rounded-xl border bg-white p-5 text-left transition-all hover:shadow-lg"
+ >
+ <div className="relative flex h-[132px] items-center justify-center rounded-lg bg-neutral-100 py-1.5">
+ <div className="relative size-full">
+ <BountyThumbnailImage bounty={bounty} />
+ </div>
</div>
- </div>
- <div className="flex flex-col gap-1.5">
- <h3 className="text-content-emphasis truncate text-sm font-semibold">
- {bounty.name}
- </h3>
+ <div className="flex min-w-0 flex-col gap-1.5">
+ <h3 className="text-content-emphasis truncate text-sm font-semibold">
+ {bounty.name}
+ </h3>
- <div className="text-content-subtle flex items-center gap-2 text-sm font-medium">
- <Calendar6 className="size-3.5" />
- <span>
- {bounty.endsAt ? (
- <>
- Ends
- {formatDate(bounty.endsAt, { month: "short" })}
- </>
- ) : (
- "No end date"
- )}
- </span>
+ <div className="text-content-subtle flex items-center gap-2 text-sm font-medium">
+ <Calendar6 className="size-3.5" />
+ <span>
+ {bounty.endsAt ? (
+ <>Ends {formatDate(bounty.endsAt, { month: "short" })}</>
+ ) : (
+ "No end date"
+ )}
+ </span>
+ </div>
</div>
- </div>
- <div className="flex grow flex-col justify-end">
- {submission ? (
- <button
- type="button"
- onClick={() => setShowClaimBountyModal(true)}
- className="w-fit"
- >
- {submission.status === "pending" ? (
+ <div className="flex grow flex-col justify-end">
+ {submission ? (
+ submission.status === "pending" ? (
<StatusBadge variant="pending" icon={null}>
Submitted{" "}
{submission.createdAt &&
@@ -65,18 +63,20 @@ export function BountyCard({ bounty }: { bounty: BountyWithSubmissionsProps }) {
{submission.reviewedAt &&
formatDate(submission.reviewedAt, { month: "short" })}
</StatusBadge>
- )}
- </button>
- ) : (
- <Button
- variant="primary"
- text="Claim bounty"
- className="h-7 w-fit rounded-lg px-2.5"
- onClick={() => setShowClaimBountyModal(true)}
- />
- )}
- </div>
- </div>
+ )
+ ) : bounty.type === "performance" ? (
+ <BountyPerformance bounty={bounty} />
+ ) : (
+ <div
+ className="group-hover:ring-border-subtle flex h-7 w-fit items-center rounded-lg bg-black px-2.5 text-sm text-white transition-all group-hover:ring-2"
+ onClick={() => setShowClaimBountyModal(true)}
+ >
+ Claim bounty
+ </div>
+ )}
+ </div>
+ </button>
+ </>
);
}
diff --git a/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/bounties/page-client.tsx b/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/bounties/page-client.tsx
index f31f58876e..938cd202b6 100644
--- a/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/bounties/page-client.tsx
+++ b/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/bounties/page-client.tsx
@@ -1,6 +1,6 @@
"use client";
-import { BountyWithSubmissionsProps } from "@/lib/types";
+import { BountyWithPartnerDataProps } from "@/lib/types";
import { PageWidthWrapper } from "@/ui/layout/page-width-wrapper";
import { AnimatedEmptyState } from "@/ui/shared/animated-empty-state";
import { Heart, Trophy } from "@dub/ui/icons";
@@ -16,7 +16,7 @@ export function BountiesPageClient() {
data: bounties,
isLoading,
error,
- } = useSWR<BountyWithSubmissionsProps[]>(
+ } = useSWR<BountyWithPartnerDataProps[]>(
`/api/partner-profile/programs/${programSlug}/bounties`,
fetcher,
{
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/[bountyId]/bounty-submissions-table.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/[bountyId]/bounty-submissions-table.tsx
index e8286a6dbc..597c867e3d 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/[bountyId]/bounty-submissions-table.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/[bountyId]/bounty-submissions-table.tsx
@@ -1,6 +1,9 @@
"use client";
-import { isCurrencyAttribute } from "@/lib/api/workflows/utils";
+import {
+ WORKFLOW_TRIGGER_ATTRIBUTE_LABELS,
+ isCurrencyAttribute,
+} from "@/lib/api/workflows/utils";
import useBounty from "@/lib/swr/use-bounty";
import useGroups from "@/lib/swr/use-groups";
import useWorkspace from "@/lib/swr/use-workspace";
@@ -53,13 +56,6 @@ const PERFORMANCE_ATTRIBUTE_TO_SORTABLE_COLUMNS = {
totalCommissions: "commissions",
} as const;
-const PERFORMANCE_ATTRIBUTE_LABELS = {
- totalLeads: "Leads",
- totalConversions: "Conversions",
- totalSaleAmount: "Revenue",
- totalCommission: "Commissions",
-} as const;
-
export function BountySubmissionsTable() {
const { bounty, isLoading: isBountyLoading } = useBounty();
const { groups } = useGroups();
@@ -134,7 +130,7 @@ export function BountySubmissionsTable() {
: "leads";
const metricColumnLabel = performanceCondition?.attribute
- ? PERFORMANCE_ATTRIBUTE_LABELS[performanceCondition.attribute]
+ ? WORKFLOW_TRIGGER_ATTRIBUTE_LABELS[performanceCondition.attribute]
: "Progress";
const { table, ...tableProps } = useTable({
diff --git a/apps/web/lib/api/workflows/utils.ts b/apps/web/lib/api/workflows/utils.ts
index 61741b354e..a08093c984 100644
--- a/apps/web/lib/api/workflows/utils.ts
+++ b/apps/web/lib/api/workflows/utils.ts
@@ -1,4 +1,11 @@
import { WorkflowConditionAttribute } from "@/lib/types";
+export const WORKFLOW_TRIGGER_ATTRIBUTE_LABELS = {
+ totalLeads: "Leads",
+ totalConversions: "Conversions",
+ totalSaleAmount: "Revenue",
+ totalCommission: "Commissions",
+} as const;
+
export const isCurrencyAttribute = (activity: WorkflowConditionAttribute) =>
activity === "totalCommission" || activity === "totalSaleAmount";
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index 9954c53923..86a9441db8 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -28,7 +28,7 @@ import {
BountySchemaExtended,
bountyStatsSchema,
BountySubmissionExtendedSchema,
- BountyWithSubmissionsSchema,
+ BountyWithPartnerDataSchema,
SUBMISSION_REQUIREMENTS,
} from "./zod/schemas/bounties";
import {
@@ -529,8 +529,8 @@ export type BountyProps = z.infer<typeof BountySchema>;
export type BountyExtendedProps = z.infer<typeof BountySchemaExtended>;
-export type BountyWithSubmissionsProps = z.infer<
- typeof BountyWithSubmissionsSchema
+export type BountyWithPartnerDataProps = z.infer<
+ typeof BountyWithPartnerDataSchema
>;
export type BountySubmissionProps = z.infer<
diff --git a/apps/web/lib/zod/schemas/bounties.ts b/apps/web/lib/zod/schemas/bounties.ts
index 94f8c44752..cd9c68368b 100644
--- a/apps/web/lib/zod/schemas/bounties.ts
+++ b/apps/web/lib/zod/schemas/bounties.ts
@@ -116,8 +116,14 @@ export const BountySubmissionExtendedSchema = z.object({
}).nullable(),
});
-export const BountyWithSubmissionsSchema = BountySchema.extend({
+export const BountyWithPartnerDataSchema = BountySchema.extend({
submissions: z.array(BountySubmissionSchema).nullable(),
+ partner: EnrolledPartnerSchema.pick({
+ leads: true,
+ conversions: true,
+ saleAmount: true,
+ totalCommissions: true,
+ }),
});
export const BOUNTIES_MAX_PAGE_SIZE = 100;
diff --git a/apps/web/ui/partners/bounties/bounty-performance.tsx b/apps/web/ui/partners/bounties/bounty-performance.tsx
new file mode 100644
index 0000000000..60ed743ec0
--- /dev/null
+++ b/apps/web/ui/partners/bounties/bounty-performance.tsx
@@ -0,0 +1,67 @@
+import {
+ WORKFLOW_TRIGGER_ATTRIBUTE_LABELS,
+ isCurrencyAttribute,
+} from "@/lib/api/workflows/utils";
+import { BountyWithPartnerDataProps } from "@/lib/types";
+import { currencyFormatter, nFormatter } from "@dub/utils";
+
+export function BountyPerformance({
+ bounty,
+}: {
+ bounty: BountyWithPartnerDataProps;
+}) {
+ const performanceCondition = bounty.performanceCondition;
+
+ if (!performanceCondition) return null;
+
+ const attribute = performanceCondition.attribute;
+ const target = performanceCondition.value;
+ let value: number | undefined = undefined;
+
+ if (attribute === "totalLeads") value = bounty.partner.leads;
+ else if (attribute === "totalConversions") value = bounty.partner.conversions;
+ else if (attribute === "totalSaleAmount") value = bounty.partner.saleAmount;
+ else if (attribute === "totalCommission")
+ value = bounty.partner.totalCommissions;
+
+ const formattedValue =
+ value === undefined
+ ? "-"
+ : isCurrencyAttribute(attribute)
+ ? currencyFormatter(value / 100)
+ : nFormatter(value);
+
+ const formattedTarget = isCurrencyAttribute(attribute)
+ ? currencyFormatter(target / 100)
+ : nFormatter(target);
+
+ const metricLabel =
+ WORKFLOW_TRIGGER_ATTRIBUTE_LABELS[
+ performanceCondition.attribute
+ ].toLowerCase();
+
+ return (
+ <div className="flex flex-col gap-2">
+ <div className="h-1.5 w-full overflow-hidden rounded-full bg-neutral-200">
+ {value && (
+ <div
+ style={{
+ width: Math.min(Math.max(value / target, 0), 1) * 100 + "%",
+ }}
+ className="h-full rounded-full bg-orange-600"
+ />
+ )}
+ </div>
+ <span className="text-content-default text-xs font-medium">
+ <strong className="text-content-emphasis font-semibold">
+ {formattedValue}
+ </strong>{" "}
+ of{" "}
+ <strong className="text-content-emphasis font-semibold">
+ {formattedTarget}
+ </strong>{" "}
+ {metricLabel} generated
+ </span>
+ </div>
+ );
+}
diff --git a/apps/web/ui/partners/bounties/claim-bounty-modal.tsx b/apps/web/ui/partners/bounties/claim-bounty-modal.tsx
index 304452cc57..36f3c377ec 100644
--- a/apps/web/ui/partners/bounties/claim-bounty-modal.tsx
+++ b/apps/web/ui/partners/bounties/claim-bounty-modal.tsx
@@ -2,7 +2,7 @@ import { createBountySubmissionAction } from "@/lib/actions/partners/create-boun
import { uploadBountySubmissionFileAction } from "@/lib/actions/partners/upload-bounty-submission-file";
import { mutatePrefix } from "@/lib/swr/mutate";
import useProgramEnrollment from "@/lib/swr/use-program-enrollment";
-import { BountyProps, BountySubmissionProps } from "@/lib/types";
+import { BountySubmissionProps, BountyWithPartnerDataProps } from "@/lib/types";
import { X } from "@/ui/shared/icons";
import {
AnimatedSizeContainer,
@@ -23,6 +23,7 @@ import { Dispatch, SetStateAction, useState } from "react";
import ReactTextareaAutosize from "react-textarea-autosize";
import { toast } from "sonner";
import { v4 as uuid } from "uuid";
+import { BountyPerformance } from "./bounty-performance";
import { BountyThumbnailImage } from "./bounty-thumbnail-image";
const MAX_FILES = 4;
@@ -30,11 +31,8 @@ const MAX_URLS = 4;
type ClaimBountyModalProps = {
setShowModal: Dispatch<SetStateAction<boolean>>;
- bounty: BountyProps;
- submission?: Pick<
- BountySubmissionProps,
- "status" | "createdAt" | "reviewedAt"
- >;
+ bounty: BountyWithPartnerDataProps;
+ submission?: BountySubmissionProps["submission"];
};
function ClaimBountyModalContent({
@@ -176,17 +174,14 @@ function ClaimBountyModalContent({
<Calendar6 className="size-3.5" />
<span>
{bounty.endsAt ? (
- <>
- Ends
- {formatDate(bounty.endsAt, { month: "short" })}
- </>
+ <>Ends {formatDate(bounty.endsAt, { month: "short" })}</>
) : (
"No end date"
)}
</span>
</div>
- {submission && (
+ {submission ? (
<div className="mt-3">
<StatusBadge
variant={
@@ -210,6 +205,12 @@ function ClaimBountyModalContent({
: "Rejected"}
</StatusBadge>
</div>
+ ) : (
+ bounty.type === "performance" && (
+ <div className="mt-5">
+ <BountyPerformance bounty={bounty} />
+ </div>
+ )
)}
</div>
@@ -372,7 +373,7 @@ function ClaimBountyModalContent({
</motion.div>
{/* Action buttons */}
- {!submission && (
+ {bounty.type !== "performance" && !submission && (
<div className="border-border-subtle border-t p-5">
<div
className={cn(