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.

Improve manual commission flow to reuse existing events if present

Steven Tey8c803cc346c4
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/commissions/create-commission-sheet.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/commissions/create-commission-sheet.tsx
index 791da6911d..81b5bfb2af 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/commissions/create-commission-sheet.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/commissions/create-commission-sheet.tsx
@@ -17,6 +17,7 @@ import { X } from "@/ui/shared/icons";
import {
AnimatedSizeContainer,
Button,
+ LoadingSpinner,
Sheet,
SmartDateTimePicker,
Switch,
@@ -46,6 +47,7 @@ function CreateCommissionSheetContent(props: CreateCommissionSheetProps) {
const { rewards, loading: rewardsLoading } = useRewards();
const [hasCustomLeadEventDate, setHasCustomLeadEventDate] = useState(false);
const [hasCustomLeadEventName, setHasCustomLeadEventName] = useState(false);
+ const [useExistingEvents, setUseExistingEvents] = useState(true);
const [commissionType, setCommissionType] =
useState<CommissionType>("custom");
@@ -176,6 +178,43 @@ function CreateCommissionSheetContent(props: CreateCommissionSheetProps) {
return false;
}, [commissionType, partnerId, linkId, customerId, saleAmount, amount]);
+ // fetch existing events for given customer (filtered by commissionType)
+ const [existingEvents, setExistingEventss] = useState<
+ { type: string; date?: string; amount?: number }[]
+ >([]);
+ const [existingEventsLoading, setExistingEventssLoading] = useState(false);
+
+ useEffect(() => {
+ if (!customerId || !commissionType) {
+ setExistingEventss([]);
+ setExistingEventssLoading(false);
+ return;
+ }
+
+ // Simulate API fetch with delay
+ const fetchData = async () => {
+ setExistingEventssLoading(true);
+ try {
+ await new Promise((resolve) => setTimeout(resolve, 1000)); // Simulate network delay
+
+ // Mock data - replace with actual API call
+ const mockEvents = [
+ { type: "Lead", date: "2024-01-15", amount: 50 },
+ { type: "Sale", date: "2024-01-20", amount: 250 },
+ ];
+
+ setExistingEventss(mockEvents);
+ } catch (error) {
+ console.error("Failed to fetch existing events:", error);
+ setExistingEventss([]);
+ } finally {
+ setExistingEventssLoading(false);
+ }
+ };
+
+ fetchData();
+ }, [customerId, commissionType]);
+
return (
<form onSubmit={handleSubmit(onSubmit)} className="flex h-full flex-col">
<div className="sticky top-0 z-10 border-b border-neutral-200 bg-white">
@@ -412,7 +451,7 @@ function CreateCommissionSheetContent(props: CreateCommissionSheetProps) {
{(commissionType === "sale" || commissionType === "lead") && (
<ProgramSheetAccordionItem value="customer">
<ProgramSheetAccordionTrigger>
- Customer
+ Customer and commission details
</ProgramSheetAccordionTrigger>
<ProgramSheetAccordionContent>
<div className="grid grid-cols-1 gap-6">
@@ -435,278 +474,343 @@ function CreateCommissionSheetContent(props: CreateCommissionSheetProps) {
</div>
</div>
- {customerId && (
- <AnimatedSizeContainer
- height
- transition={{ ease: "easeInOut", duration: 0.2 }}
- style={{
- height: hasCustomLeadEventDate ? "auto" : "0px",
- overflow: "hidden",
- }}
- >
- <div className="flex flex-col gap-6">
- <div className="flex items-center gap-4">
- <Switch
- fn={setHasCustomLeadEventDate}
- checked={hasCustomLeadEventDate}
- trackDimensions="w-8 h-4"
- thumbDimensions="w-3 h-3"
- thumbTranslate="translate-x-4"
+ {customerId &&
+ (existingEventsLoading ? (
+ <div className="flex items-center justify-center py-12">
+ <LoadingSpinner className="h-6 w-6" />
+ </div>
+ ) : existingEvents && existingEvents.length > 0 ? (
+ <div className="space-y-6">
+ <div>
+ <label htmlFor="eventSource">
+ <h2 className="text-sm font-medium text-neutral-900">
+ Event source
+ </h2>
+ </label>
+ <ToggleGroup
+ className="mt-2 flex w-full items-center gap-1 rounded-md border border-neutral-200 bg-neutral-50 p-1"
+ optionClassName="h-8 flex items-center justify-center rounded-md flex-1 text-sm normal-case"
+ indicatorClassName="bg-white"
+ options={[
+ {
+ value: "existing",
+ label: "Use existing events",
+ },
+ { value: "new", label: "Create from scratch" },
+ ]}
+ selected={useExistingEvents ? "existing" : "new"}
+ selectAction={(value: string) =>
+ setUseExistingEvents(value === "existing")
+ }
/>
- <div className="flex flex-col gap-1">
- <h3 className="text-sm font-medium text-neutral-700">
- Set a custom lead event date
- </h3>
- </div>
</div>
- {hasCustomLeadEventDate && (
- <div className="p-px">
- <SmartDateTimePicker
- value={leadEventDate}
- onChange={(date) => {
- setValue("leadEventDate", date, {
- shouldDirty: true,
- });
- }}
- label="Lead event date"
- placeholder='E.g. "2024-03-01", "Last Thursday", "2 hours ago"'
- />
+ {useExistingEvents ? (
+ <div className="rounded-md border border-neutral-200 bg-neutral-50 p-4">
+ <h3 className="mb-3 text-sm font-medium text-neutral-900">
+ Select an existing events
+ </h3>
+ <div className="space-y-2">
+ {existingEvents.map((event, index) => (
+ <button
+ key={index}
+ type="button"
+ className="flex w-full items-center justify-between rounded-md bg-white p-3 text-sm hover:bg-neutral-50 focus:bg-neutral-100 focus:outline-none"
+ >
+ <div>
+ <span className="font-medium">
+ {event.type}
+ </span>
+ {event.date && (
+ <span className="ml-2 text-neutral-500">
+ {new Date(
+ event.date,
+ ).toLocaleDateString()}
+ </span>
+ )}
+ </div>
+ {event.amount && (
+ <span className="text-neutral-700">
+ ${event.amount}
+ </span>
+ )}
+ </button>
+ ))}
+ </div>
</div>
- )}
+ ) : null}
</div>
- </AnimatedSizeContainer>
- )}
-
- {customerId && (
- <AnimatedSizeContainer
- height
- transition={{ ease: "easeInOut", duration: 0.2 }}
- style={{
- height: hasCustomLeadEventName ? "auto" : "0px",
- overflow: "hidden",
- }}
- >
- <div className="flex flex-col gap-6">
- <div className="flex items-center gap-4">
- <Switch
- fn={setHasCustomLeadEventName}
- checked={hasCustomLeadEventName}
- trackDimensions="w-8 h-4"
- thumbDimensions="w-3 h-3"
- thumbTranslate="translate-x-4"
- />
- <div className="flex flex-col gap-1">
- <h3 className="text-sm font-medium text-neutral-700">
- Set a custom lead event name
- </h3>
+ ) : null)}
+
+ {customerId &&
+ !existingEventsLoading &&
+ (!existingEvents?.length || !useExistingEvents) &&
+ (commissionType === "lead" ? (
+ <>
+ <AnimatedSizeContainer
+ height
+ transition={{ ease: "easeInOut", duration: 0.2 }}
+ style={{
+ height: hasCustomLeadEventDate ? "auto" : "0px",
+ overflow: "hidden",
+ }}
+ >
+ <div className="flex flex-col gap-6">
+ <div className="flex items-center gap-4">
+ <Switch
+ fn={setHasCustomLeadEventDate}
+ checked={hasCustomLeadEventDate}
+ trackDimensions="w-8 h-4"
+ thumbDimensions="w-3 h-3"
+ thumbTranslate="translate-x-4"
+ />
+ <div className="flex flex-col gap-1">
+ <h3 className="text-sm font-medium text-neutral-700">
+ Set a custom lead event date
+ </h3>
+ </div>
+ </div>
+
+ {hasCustomLeadEventDate && (
+ <div className="p-px">
+ <SmartDateTimePicker
+ value={leadEventDate}
+ onChange={(date) => {
+ setValue("leadEventDate", date, {
+ shouldDirty: true,
+ });
+ }}
+ label="Lead event date"
+ placeholder='E.g. "2024-03-01", "Last Thursday", "2 hours ago"'
+ />
+ </div>
+ )}
</div>
+ </AnimatedSizeContainer>
+ <AnimatedSizeContainer
+ height
+ transition={{ ease: "easeInOut", duration: 0.2 }}
+ style={{
+ height: hasCustomLeadEventName ? "auto" : "0px",
+ overflow: "hidden",
+ }}
+ >
+ <div className="flex flex-col gap-6">
+ <div className="flex items-center gap-4">
+ <Switch
+ fn={setHasCustomLeadEventName}
+ checked={hasCustomLeadEventName}
+ trackDimensions="w-8 h-4"
+ thumbDimensions="w-3 h-3"
+ thumbTranslate="translate-x-4"
+ />
+ <div className="flex flex-col gap-1">
+ <h3 className="text-sm font-medium text-neutral-700">
+ Set a custom lead event name
+ </h3>
+ </div>
+ </div>
+
+ {hasCustomLeadEventName && (
+ <div className="p-px">
+ <input
+ type="text"
+ className={cn(
+ "block w-full rounded-md border-neutral-300 px-3 py-2 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
+ errors.leadEventName &&
+ "border-red-600 focus:border-red-500 focus:ring-red-600",
+ )}
+ {...register("leadEventName", {
+ setValueAs: (value) =>
+ value === "" ? null : value,
+ })}
+ placeholder="Enter lead event name"
+ />
+ </div>
+ )}
+ </div>
+ </AnimatedSizeContainer>
+ </>
+ ) : commissionType === "sale" ? (
+ <div className="grid grid-cols-1 gap-6">
+ <div>
+ <SmartDateTimePicker
+ value={saleEventDate}
+ onChange={(date) => {
+ setValue("saleEventDate", date, {
+ shouldDirty: true,
+ });
+ }}
+ label="Sale date"
+ placeholder='E.g. "2024-03-01", "Last Thursday", "2 hours ago"'
+ />
</div>
- {hasCustomLeadEventName && (
- <div className="p-px">
- <input
- type="text"
- className={cn(
- "block w-full rounded-md border-neutral-300 px-3 py-2 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
- errors.leadEventName &&
- "border-red-600 focus:border-red-500 focus:ring-red-600",
+ <div>
+ <label
+ htmlFor="saleAmount"
+ className="flex items-center space-x-2"
+ >
+ <h2 className="text-sm font-medium text-neutral-900">
+ Sale amount
+ </h2>
+ </label>
+ <div className="relative mt-2 rounded-md shadow-sm">
+ <span className="absolute inset-y-0 left-0 flex items-center pl-3 text-sm text-neutral-400">
+ $
+ </span>
+ <Controller
+ name="saleAmount"
+ control={control}
+ rules={{
+ min: 0,
+ }}
+ render={({ field }) => (
+ <input
+ {...field}
+ type="number"
+ onWheel={(e) => e.currentTarget.blur()}
+ className={cn(
+ "block w-full rounded-md border-neutral-300 pl-6 pr-12 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
+ errors.saleAmount &&
+ "border-red-600 focus:border-red-500 focus:ring-red-600",
+ )}
+ value={
+ field.value == null || isNaN(field.value)
+ ? ""
+ : field.value
+ }
+ onChange={(e) => {
+ const val = e.target.value;
+ field.onChange(
+ val === "" ? null : parseFloat(val),
+ );
+ }}
+ placeholder="0.00"
+ />
)}
- {...register("leadEventName", {
- setValueAs: (value) =>
- value === "" ? null : value,
- })}
- placeholder="Enter lead event name"
/>
+ <span className="absolute inset-y-0 right-0 flex items-center pr-3 text-sm text-neutral-400">
+ USD
+ </span>
</div>
- )}
- </div>
- </AnimatedSizeContainer>
- )}
- </div>
- </ProgramSheetAccordionContent>
- </ProgramSheetAccordionItem>
- )}
-
- {commissionType === "sale" && (
- <ProgramSheetAccordionItem value="sale">
- <ProgramSheetAccordionTrigger>
- Sale
- </ProgramSheetAccordionTrigger>
- <ProgramSheetAccordionContent>
- <div className="grid grid-cols-1 gap-6">
- <div>
- <SmartDateTimePicker
- value={saleEventDate}
- onChange={(date) => {
- setValue("saleEventDate", date, {
- shouldDirty: true,
- });
- }}
- label="Sale date"
- placeholder='E.g. "2024-03-01", "Last Thursday", "2 hours ago"'
- />
- </div>
-
- <div>
- <label
- htmlFor="saleAmount"
- className="flex items-center space-x-2"
- >
- <h2 className="text-sm font-medium text-neutral-900">
- Sale amount
- </h2>
- </label>
- <div className="relative mt-2 rounded-md shadow-sm">
- <span className="absolute inset-y-0 left-0 flex items-center pl-3 text-sm text-neutral-400">
- $
- </span>
- <Controller
- name="saleAmount"
- control={control}
- rules={{
- min: 0,
- }}
- render={({ field }) => (
- <input
- {...field}
- type="number"
- onWheel={(e) => e.currentTarget.blur()}
- className={cn(
- "block w-full rounded-md border-neutral-300 pl-6 pr-12 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
- errors.saleAmount &&
- "border-red-600 focus:border-red-500 focus:ring-red-600",
- )}
- value={
- field.value == null || isNaN(field.value)
- ? ""
- : field.value
- }
- onChange={(e) => {
- const val = e.target.value;
- field.onChange(
- val === "" ? null : parseFloat(val),
- );
- }}
- placeholder="0.00"
- />
- )}
- />
- <span className="absolute inset-y-0 right-0 flex items-center pr-3 text-sm text-neutral-400">
- USD
- </span>
- </div>
- </div>
-
- <AnimatedSizeContainer
- height
- transition={{ ease: "easeInOut", duration: 0.2 }}
- className={!hasInvoiceId ? "hidden" : ""}
- style={{ display: !hasInvoiceId ? "none" : "block" }}
- >
- <div className="flex items-center gap-4">
- <Switch
- fn={setHasInvoiceId}
- checked={hasInvoiceId}
- trackDimensions="w-8 h-4"
- thumbDimensions="w-3 h-3"
- thumbTranslate="translate-x-4"
- />
- <div className="flex gap-1">
- <h3 className="text-sm font-medium text-neutral-700">
- Add{" "}
- </h3>
- <span className="rounded-md border border-neutral-200 bg-neutral-100 px-1 py-0.5 text-xs">
- invoiceID
- </span>
- </div>
- </div>
-
- {hasInvoiceId && (
- <div className="mt-4">
- <label
- htmlFor="invoiceId"
- className="flex items-center space-x-2"
- >
- <h2 className="text-sm font-medium text-neutral-900">
- Invoice ID
- </h2>
- </label>
- <div className="mt-2 p-px">
- <input
- type="text"
- id="invoiceId"
- className={cn(
- "block w-full rounded-md border-neutral-300 px-3 py-2 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
- errors.invoiceId &&
- "border-red-600 focus:border-red-500 focus:ring-red-600",
- )}
- {...register("invoiceId", {
- required: hasInvoiceId,
- setValueAs: (value) =>
- value === "" ? null : value,
- })}
- placeholder="Enter invoice ID"
- />
</div>
- </div>
- )}
- </AnimatedSizeContainer>
- <AnimatedSizeContainer
- height
- transition={{ ease: "easeInOut", duration: 0.2 }}
- className={!hasProductId ? "hidden" : ""}
- style={{ display: !hasProductId ? "none" : "block" }}
- >
- <div className="flex items-center gap-4">
- <Switch
- fn={setHasProductId}
- checked={hasProductId}
- trackDimensions="w-8 h-4"
- thumbDimensions="w-3 h-3"
- thumbTranslate="translate-x-4"
- />
- <div className="flex gap-1">
- <h3 className="text-sm font-medium text-neutral-700">
- Add{" "}
- </h3>
- <span className="rounded-md border border-neutral-200 bg-neutral-100 px-1 py-0.5 text-xs">
- productID
- </span>
- </div>
- </div>
+ <AnimatedSizeContainer
+ height
+ transition={{ ease: "easeInOut", duration: 0.2 }}
+ className={!hasInvoiceId ? "hidden" : ""}
+ style={{
+ display: !hasInvoiceId ? "none" : "block",
+ }}
+ >
+ <div className="flex items-center gap-4">
+ <Switch
+ fn={setHasInvoiceId}
+ checked={hasInvoiceId}
+ trackDimensions="w-8 h-4"
+ thumbDimensions="w-3 h-3"
+ thumbTranslate="translate-x-4"
+ />
+ <div className="flex gap-1">
+ <h3 className="text-sm font-medium text-neutral-700">
+ Add{" "}
+ </h3>
+ <span className="rounded-md border border-neutral-200 bg-neutral-100 px-1 py-0.5 text-xs">
+ invoiceID
+ </span>
+ </div>
+ </div>
- {hasProductId && (
- <div className="mt-4">
- <label
- htmlFor="productId"
- className="flex items-center space-x-2"
+ {hasInvoiceId && (
+ <div className="mt-4">
+ <label
+ htmlFor="invoiceId"
+ className="flex items-center space-x-2"
+ >
+ <h2 className="text-sm font-medium text-neutral-900">
+ Invoice ID
+ </h2>
+ </label>
+ <div className="mt-2 p-px">
+ <input
+ type="text"
+ id="invoiceId"
+ className={cn(
+ "block w-full rounded-md border-neutral-300 px-3 py-2 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
+ errors.invoiceId &&
+ "border-red-600 focus:border-red-500 focus:ring-red-600",
+ )}
+ {...register("invoiceId", {
+ required: hasInvoiceId,
+ setValueAs: (value) =>
+ value === "" ? null : value,
+ })}
+ placeholder="Enter invoice ID"
+ />
+ </div>
+ </div>
+ )}
+ </AnimatedSizeContainer>
+
+ <AnimatedSizeContainer
+ height
+ transition={{ ease: "easeInOut", duration: 0.2 }}
+ className={!hasProductId ? "hidden" : ""}
+ style={{
+ display: !hasProductId ? "none" : "block",
+ }}
>
- <h2 className="text-sm font-medium text-neutral-900">
- Product ID
- </h2>
- </label>
- <div className="mt-2 p-px">
- <input
- type="text"
- id="productId"
- className={cn(
- "block w-full rounded-md border-neutral-300 px-3 py-2 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
- errors.productId &&
- "border-red-600 focus:border-red-500 focus:ring-red-600",
- )}
- {...register("productId", {
- required: hasProductId,
- setValueAs: (value) =>
- value === "" ? null : value,
- })}
- placeholder="Enter product ID"
- />
- </div>
+ <div className="flex items-center gap-4">
+ <Switch
+ fn={setHasProductId}
+ checked={hasProductId}
+ trackDimensions="w-8 h-4"
+ thumbDimensions="w-3 h-3"
+ thumbTranslate="translate-x-4"
+ />
+ <div className="flex gap-1">
+ <h3 className="text-sm font-medium text-neutral-700">
+ Add{" "}
+ </h3>
+ <span className="rounded-md border border-neutral-200 bg-neutral-100 px-1 py-0.5 text-xs">
+ productID
+ </span>
+ </div>
+ </div>
+
+ {hasProductId && (
+ <div className="mt-4">
+ <label
+ htmlFor="productId"
+ className="flex items-center space-x-2"
+ >
+ <h2 className="text-sm font-medium text-neutral-900">
+ Product ID
+ </h2>
+ </label>
+ <div className="mt-2 p-px">
+ <input
+ type="text"
+ id="productId"
+ className={cn(
+ "block w-full rounded-md border-neutral-300 px-3 py-2 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
+ errors.productId &&
+ "border-red-600 focus:border-red-500 focus:ring-red-600",
+ )}
+ {...register("productId", {
+ required: hasProductId,
+ setValueAs: (value) =>
+ value === "" ? null : value,
+ })}
+ placeholder="Enter product ID"
+ />
+ </div>
+ </div>
+ )}
+ </AnimatedSizeContainer>
</div>
- )}
- </AnimatedSizeContainer>
+ ) : null)}
</div>
</ProgramSheetAccordionContent>
</ProgramSheetAccordionItem>