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 partners table columns

Steven Tey8d55c6e2599d
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/partners/partner-table.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/partners/partner-table.tsx
index cb89bae70c..1cc01a13f6 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/partners/partner-table.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/partners/partner-table.tsx
@@ -2,6 +2,7 @@
import usePartnersCount from "@/lib/swr/use-partners-count";
import { EnrolledPartnerProps } from "@/lib/types";
+import EditColumnsButton from "@/ui/analytics/events/edit-columns-button";
import { PartnerDetailsSheet } from "@/ui/partners/partner-details-sheet";
import { PartnerStatusBadges } from "@/ui/partners/partner-status-badges";
import { AnimatedEmptyState } from "@/ui/shared/animated-empty-state";
@@ -34,6 +35,7 @@ import { Command } from "cmdk";
import { useParams, useRouter } from "next/navigation";
import { useEffect, useState } from "react";
import useSWR from "swr";
+import { partnersColumns, useColumnVisibility } from "./use-column-visibility";
import { usePartnerFilters } from "./use-partner-filters";
export function PartnerTable() {
@@ -75,13 +77,16 @@ export function PartnerTable() {
}
}, [searchParams, partners]);
+ const { columnVisibility, setColumnVisibility } = useColumnVisibility();
const { pagination, setPagination } = usePagination();
- const table = useTable({
+ const { table, ...tableProps } = useTable({
data: partners || [],
columns: [
{
+ id: "partner",
header: "Partner",
+ enableHiding: false,
cell: ({ row }) => {
return (
<div className="flex items-center gap-2">
@@ -104,6 +109,7 @@ export function PartnerTable() {
accessorFn: (d) => formatDate(d.createdAt, { month: "short" }),
},
{
+ id: "status",
header: "Status",
cell: ({ row }) => {
const badge = PartnerStatusBadges[row.original.status];
@@ -117,6 +123,7 @@ export function PartnerTable() {
},
},
{
+ id: "location",
header: "Location",
cell: ({ row }) => {
const country = row.original.country;
@@ -134,22 +141,24 @@ export function PartnerTable() {
);
},
},
- // TODO: Add these once we support hiding/showing columns
- // {
- // header: "Clicks",
- // accessorFn: (d) =>
- // d.status !== "pending"
- // ? nFormatter(d.link?.clicks, { full: true })
- // : "-",
- // },
- // {
- // header: "Leads",
- // accessorFn: (d) =>
- // d.status !== "pending"
- // ? nFormatter(d.link?.leads, { full: true })
- // : "-",
- // },
{
+ id: "clicks",
+ header: "Clicks",
+ accessorFn: (d) =>
+ d.status !== "pending"
+ ? nFormatter(d.link?.clicks, { full: true })
+ : "-",
+ },
+ {
+ id: "leads",
+ header: "Leads",
+ accessorFn: (d) =>
+ d.status !== "pending"
+ ? nFormatter(d.link?.leads, { full: true })
+ : "-",
+ },
+ {
+ id: "sales",
header: "Sales",
accessorFn: (d) =>
d.status !== "pending"
@@ -174,9 +183,10 @@ export function PartnerTable() {
minSize: 43,
size: 43,
maxSize: 43,
+ header: () => <EditColumnsButton table={table} />,
cell: ({ row }) => <RowMenuButton row={row} />,
},
- ],
+ ].filter((c) => c.id === "menu" || partnersColumns.all.includes(c.id)),
onRowClick: (row) => {
queryParams({
set: {
@@ -186,6 +196,8 @@ export function PartnerTable() {
},
pagination,
onPaginationChange: setPagination,
+ columnVisibility,
+ onColumnVisibilityChange: setColumnVisibility,
sortableColumns: ["createdAt", "earnings"],
sortBy,
sortOrder: order,
@@ -242,7 +254,7 @@ export function PartnerTable() {
</AnimatedSizeContainer>
</div>
{partners?.length !== 0 ? (
- <Table {...table} />
+ <Table {...tableProps} table={table} />
) : (
<AnimatedEmptyState
title="No partners found"
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/partners/use-column-visibility.ts b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/partners/use-column-visibility.ts
new file mode 100644
index 0000000000..dfc922427c
--- /dev/null
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/partners/use-column-visibility.ts
@@ -0,0 +1,41 @@
+import { useLocalStorage } from "@dub/ui";
+import { VisibilityState } from "@tanstack/react-table";
+
+export const partnersColumns = {
+ all: [
+ "partner",
+ "createdAt",
+ "status",
+ "location",
+ "clicks",
+ "leads",
+ "sales",
+ "earnings",
+ ],
+ defaultVisible: [
+ "partner",
+ "createdAt",
+ "status",
+ "location",
+ "sales",
+ "earnings",
+ ],
+};
+
+export function useColumnVisibility() {
+ const [columnVisibility, setColumnVisibility] =
+ useLocalStorage<VisibilityState>(
+ "partners-table-columns",
+ Object.fromEntries(
+ partnersColumns.all.map((id) => [
+ id,
+ partnersColumns.defaultVisible.includes(id),
+ ]),
+ ),
+ );
+
+ return {
+ columnVisibility,
+ setColumnVisibility,
+ };
+}
diff --git a/apps/web/ui/analytics/events/events-table.tsx b/apps/web/ui/analytics/events/events-table.tsx
index bd0084e28d..2f1edc4e06 100644
--- a/apps/web/ui/analytics/events/events-table.tsx
+++ b/apps/web/ui/analytics/events/events-table.tsx
@@ -23,7 +23,6 @@ import {
COUNTRIES,
REGIONS,
capitalize,
- currencyFormatter,
fetcher,
getApexDomain,
getPrettyUrl,
@@ -42,7 +41,7 @@ import { EventsContext } from "./events-provider";
import { exampleData } from "./example-data";
import FilterButton from "./filter-button";
import { RowMenuButton } from "./row-menu-button";
-import { getEventColumns, useColumnVisibility } from "./use-column-visibility";
+import { eventColumns, useColumnVisibility } from "./use-column-visibility";
export type EventDatum =
| z.infer<typeof clickEventResponseSchema>
@@ -58,11 +57,9 @@ type ColumnMeta = {
export default function EventsTable({
requiresUpgrade,
upgradeOverlay,
- partners = false,
}: {
requiresUpgrade?: boolean;
upgradeOverlay?: ReactNode;
- partners?: boolean;
}) {
const { searchParams, queryParams } = useRouterStuff();
const { setExportQueryString } = useContext(EventsContext);
@@ -72,9 +69,7 @@ export default function EventsTable({
eventsApiPath,
totalEvents,
} = useContext(AnalyticsContext);
- const { columnVisibility, setColumnVisibility } = useColumnVisibility({
- partners,
- });
+ const { columnVisibility, setColumnVisibility } = useColumnVisibility();
const sortBy = searchParams.get("sort") || "timestamp";
const order = searchParams.get("order") === "asc" ? "asc" : "desc";
@@ -126,39 +121,6 @@ export default function EventsTable({
</span>
) || <span className="text-gray-400">-</span>,
},
- ...(!partners
- ? [
- {
- id: "link",
- header: "Link",
- accessorKey: "link",
- minSize: 250,
- maxSize: 200,
- meta: {
- filterParams: ({ getValue }) => ({
- domain: getValue().domain,
- key: getValue().key,
- }),
- },
- cell: ({ getValue }) => (
- <div className="flex items-center gap-3">
- <LinkLogo
- apexDomain={getApexDomain(getValue().url)}
- className="size-4 shrink-0 sm:size-4"
- />
- <CopyText
- value={getValue().shortLink}
- successMessage="Copied link to clipboard!"
- >
- <span className="truncate" title={getValue().shortLink}>
- {getPrettyUrl(getValue().shortLink)}
- </span>
- </CopyText>
- </div>
- ),
- },
- ]
- : []),
{
id: "customer",
header: "Customer",
@@ -419,7 +381,6 @@ export default function EventsTable({
id: "saleAmount",
header: "Sale Amount",
accessorKey: "sale.amount",
- enableHiding: partners,
minSize: 120,
cell: ({ getValue }) => (
<div className="flex items-center gap-2">
@@ -428,43 +389,6 @@ export default function EventsTable({
</div>
),
},
- ...(!partners
- ? [
- // Sale invoice ID
- {
- id: "invoiceId",
- header: "Invoice ID",
- accessorKey: "sale.invoiceId",
- maxSize: 200,
- cell: ({ getValue }) =>
- (
- <span className="truncate" title={getValue()}>
- {getValue()}
- </span>
- ) || <span className="text-gray-400">-</span>,
- },
- ]
- : [
- // Earnings amount
- {
- id: "earnings",
- header: "Earnings",
- accessorKey: "earnings",
- enableHiding: false,
- minSize: 120,
- cell: ({ getValue }) => (
- <div className="flex items-center gap-2">
- <span>
- {currencyFormatter(getValue() / 100, {
- minimumFractionDigits: 2,
- maximumFractionDigits: 2,
- })}
- </span>
- <span className="text-gray-400">USD</span>
- </div>
- ),
- },
- ]),
// Date
{
id: "timestamp",
@@ -506,10 +430,7 @@ export default function EventsTable({
header: ({ table }) => <EditColumnsButton table={table} />,
cell: ({ row }) => <RowMenuButton row={row} />,
},
- ].filter(
- (c) =>
- c.id === "menu" || getEventColumns(partners)[tab].all.includes(c.id),
- ),
+ ].filter((c) => c.id === "menu" || eventColumns[tab].all.includes(c.id)),
[tab],
);
diff --git a/apps/web/ui/analytics/events/use-column-visibility.ts b/apps/web/ui/analytics/events/use-column-visibility.ts
index 2c0856ef30..4b5e3a1515 100644
--- a/apps/web/ui/analytics/events/use-column-visibility.ts
+++ b/apps/web/ui/analytics/events/use-column-visibility.ts
@@ -2,15 +2,10 @@ import { EventType } from "@/lib/analytics/types";
import { useLocalStorage } from "@dub/ui";
import { VisibilityState } from "@tanstack/react-table";
-export const getEventColumns: (
- partners?: boolean,
-) => Record<EventType, { all: string[]; defaultVisible: string[] }> = (
- partners,
-) => ({
+export const eventColumns = {
clicks: {
all: [
"trigger",
- ...(!partners ? ["link"] : []),
"country",
"city",
"region",
@@ -23,19 +18,11 @@ export const getEventColumns: (
"ip",
"timestamp",
],
- defaultVisible: [
- "trigger",
- ...(!partners ? ["link"] : []),
- "country",
- "device",
- ...(partners ? ["refererUrl"] : []),
- "timestamp",
- ],
+ defaultVisible: ["trigger", "country", "device", "timestamp"],
},
leads: {
all: [
"event",
- ...(!partners ? ["link"] : []),
"customer",
"country",
"city",
@@ -49,20 +36,12 @@ export const getEventColumns: (
"ip",
"timestamp",
],
- defaultVisible: [
- "event",
- ...(!partners ? ["link"] : []),
- "customer",
- "country",
- "device",
- "timestamp",
- ],
+ defaultVisible: ["event", "customer", "country", "device", "timestamp"],
},
sales: {
all: [
"event",
"customer",
- ...(!partners ? ["link", "invoiceId"] : []),
"country",
"city",
"region",
@@ -75,38 +54,25 @@ export const getEventColumns: (
"ip",
"timestamp",
"saleAmount",
- ...(partners ? ["earnings"] : []),
- ],
- defaultVisible: [
- "event",
- ...(!partners ? ["link"] : []),
- "customer",
- "country",
- "saleAmount",
- ...(partners ? ["earnings"] : []),
- "timestamp",
],
+ defaultVisible: ["event", "customer", "country", "saleAmount", "timestamp"],
},
-});
+};
-const getDefaultColumnVisibility = (tab: EventType, partners?: boolean) => {
- const columns = getEventColumns(partners)[tab];
+const getDefaultColumnVisibility = (tab: EventType) => {
+ const columns = eventColumns[tab];
return Object.fromEntries(
columns.all.map((id) => [id, columns.defaultVisible.includes(id)]),
);
};
-export function useColumnVisibility({
- partners,
-}: {
- partners?: boolean;
-} = {}) {
+export function useColumnVisibility() {
const [columnVisibility, setColumnVisibility] = useLocalStorage<
Record<EventType, VisibilityState>
>("events-columns", {
- clicks: getDefaultColumnVisibility("clicks", partners),
- leads: getDefaultColumnVisibility("leads", partners),
- sales: getDefaultColumnVisibility("sales", partners),
+ clicks: getDefaultColumnVisibility("clicks"),
+ leads: getDefaultColumnVisibility("leads"),
+ sales: getDefaultColumnVisibility("sales"),
});
return {