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 groupBy=type

Steven Tey6dfaca4c5ca7
diff --git a/apps/web/app/api/partner-profile/programs/[programId]/earnings/timeseries/route.ts b/apps/web/app/api/partner-profile/programs/[programId]/earnings/timeseries/route.ts
index 96581f0d65..6d172ccfb4 100644
--- a/apps/web/app/api/partner-profile/programs/[programId]/earnings/timeseries/route.ts
+++ b/apps/web/app/api/partner-profile/programs/[programId]/earnings/timeseries/route.ts
@@ -18,7 +18,7 @@ export const GET = withPartnerProfile(
const {
groupBy,
- type = "sale",
+ type,
status,
linkId,
customerId,
@@ -54,6 +54,7 @@ export const GET = withPartnerProfile(
AND createdAt < ${endDate}
AND programId = ${program.id}
AND partnerId = ${partner.id}
+ ${type ? Prisma.sql`AND type = ${type}` : Prisma.sql``}
${payoutId ? Prisma.sql`AND payoutId = ${payoutId}` : Prisma.sql``}
${linkId ? Prisma.sql`AND linkId = ${linkId}` : Prisma.sql``}
${customerId ? Prisma.sql`AND customerId = ${customerId}` : Prisma.sql``}
@@ -92,11 +93,12 @@ export const GET = withPartnerProfile(
data: groupBy
? {
...(groupBy === "type"
- ? {
- sale: 0,
- lead: 0,
- click: 0,
- }
+ ? Object.fromEntries(
+ ["sale", "lead", "click"]
+ // only show filtered type if type filter is provided
+ .filter((t) => (type ? type === t : true))
+ .map((t) => [t, 0]),
+ )
: Object.fromEntries(
links
// only show filtered link if linkId filter is provided
diff --git a/apps/web/app/partners.dub.co/(dashboard)/programs/[programSlug]/earnings/earnings-composite-chart.tsx b/apps/web/app/partners.dub.co/(dashboard)/programs/[programSlug]/earnings/earnings-composite-chart.tsx
index 015e407d00..8c009500f3 100644
--- a/apps/web/app/partners.dub.co/(dashboard)/programs/[programSlug]/earnings/earnings-composite-chart.tsx
+++ b/apps/web/app/partners.dub.co/(dashboard)/programs/[programSlug]/earnings/earnings-composite-chart.tsx
@@ -6,12 +6,14 @@ import usePartnerEarningsCount from "@/lib/swr/use-partner-earnings-count";
import { usePartnerEarningsTimeseries } from "@/lib/swr/use-partner-earnings-timeseries";
import usePartnerLinks from "@/lib/swr/use-partner-links";
import { LinkIcon } from "@/ui/links/link-icon";
+import { CommissionTypeIcon } from "@/ui/partners/comission-type-icon";
import { SaleStatusBadges } from "@/ui/partners/sale-status-badges";
import SimpleDateRangePicker from "@/ui/shared/simple-date-range-picker";
import { Filter, LoadingSpinner, useRouterStuff } from "@dub/ui";
import { Areas, TimeSeriesChart, XAxis, YAxis } from "@dub/ui/charts";
-import { CircleDotted, Hyperlink, User } from "@dub/ui/icons";
+import { CircleDotted, Hyperlink, Sliders, User } from "@dub/ui/icons";
import {
+ capitalize,
cn,
currencyFormatter,
getPrettyUrl,
@@ -30,6 +32,12 @@ const LINE_COLORS = [
"text-yellow-500",
];
+const EVENT_TYPE_LINE_COLORS = {
+ sale: "text-teal-500",
+ lead: "text-purple-500",
+ click: "text-blue-500",
+};
+
const MAX_LINES = LINE_COLORS.length;
export function EarningsCompositeChart() {
@@ -39,17 +47,19 @@ export function EarningsCompositeChart() {
start,
end,
interval = "30d",
+ groupBy = "linkId",
} = searchParamsObj as {
start?: string;
end?: string;
interval?: IntervalOptions;
+ groupBy?: "linkId" | "type";
};
const { links } = usePartnerLinks();
const { data } = usePartnerEarningsTimeseries({
interval,
- groupBy: "linkId",
+ groupBy,
start: start ? new Date(start) : undefined,
end: end ? new Date(end) : undefined,
});
@@ -76,11 +86,14 @@ export function EarningsCompositeChart() {
return earningsB - earningsA;
})
.slice(0, MAX_LINES)
- .map((linkId, idx) => ({
- id: linkId,
+ .map((item, idx) => ({
+ id: item,
isActive: true,
- valueAccessor: (d) => (d.values[linkId] || 0) / 100,
- colorClassName: LINE_COLORS[idx % LINE_COLORS.length],
+ valueAccessor: (d) => (d.values[item] || 0) / 100,
+ colorClassName:
+ groupBy === "type"
+ ? EVENT_TYPE_LINE_COLORS[item]
+ : LINE_COLORS[idx % LINE_COLORS.length],
}))
: [],
],
@@ -149,7 +162,7 @@ export function EarningsCompositeChart() {
<span className="min-w-0 truncate font-medium text-neutral-700">
{link?.shortLink
? getPrettyUrl(link.shortLink)
- : id}
+ : capitalize(id)}
</span>
</div>
<p className="text-right text-neutral-500">
@@ -210,17 +223,16 @@ function EarningsTableControls() {
const filters = useMemo(
() => [
- // TODO: add this back once we support CPC and CPL rewards
- // {
- // key: "type",
- // icon: Sliders,
- // label: "Type",
- // options: ["click", "lead", "sale"].map((slug) => ({
- // value: slug,
- // label: capitalize(slug) as string,
- // icon: <CommissionTypeIcon type={slug} />,
- // })),
- // },
+ {
+ key: "type",
+ icon: Sliders,
+ label: "Type",
+ options: ["click", "lead", "sale"].map((slug) => ({
+ value: slug,
+ label: capitalize(slug) as string,
+ icon: <CommissionTypeIcon type={slug} />,
+ })),
+ },
{
key: "linkId",
icon: Hyperlink,
@@ -275,8 +287,9 @@ function EarningsTableControls() {
);
const activeFilters = useMemo(() => {
- const { linkId, customerId, status } = searchParamsObj;
+ const { type, linkId, customerId, status } = searchParamsObj;
return [
+ ...(type ? [{ key: "type", value: type }] : []),
...(linkId ? [{ key: "linkId", value: linkId }] : []),
...(customerId ? [{ key: "customerId", value: customerId }] : []),
...(status ? [{ key: "status", value: status }] : []),
diff --git a/apps/web/app/partners.dub.co/(dashboard)/programs/[programSlug]/earnings/earnings-table.tsx b/apps/web/app/partners.dub.co/(dashboard)/programs/[programSlug]/earnings/earnings-table.tsx
index 2eb29355e4..5ba60c959a 100644
--- a/apps/web/app/partners.dub.co/(dashboard)/programs/[programSlug]/earnings/earnings-table.tsx
+++ b/apps/web/app/partners.dub.co/(dashboard)/programs/[programSlug]/earnings/earnings-table.tsx
@@ -80,12 +80,11 @@ export function EarningsTablePartner({ limit }: { limit?: number }) {
id: "type",
header: "Type",
accessorKey: "type",
- // TODO: add this back once we have a way to filter by type
- // meta: {
- // filterParams: ({ getValue }) => ({
- // type: getValue(),
- // }),
- // },
+ meta: {
+ filterParams: ({ getValue }) => ({
+ type: getValue(),
+ }),
+ },
cell: ({ row }) => <CommissionTypeBadge type={row.original.type} />,
},
{
diff --git a/apps/web/lib/swr/use-partner-earnings-timeseries.ts b/apps/web/lib/swr/use-partner-earnings-timeseries.ts
index dd9610f8c5..83ca48323a 100644
--- a/apps/web/lib/swr/use-partner-earnings-timeseries.ts
+++ b/apps/web/lib/swr/use-partner-earnings-timeseries.ts
@@ -29,7 +29,7 @@ export function usePartnerEarningsTimeseries(
: { interval: params?.interval ?? "30d" }),
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
},
- { include: ["linkId", "customerId", "status"] },
+ { include: ["type", "linkId", "customerId", "status"] },
)}`,
fetcher,
{