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.

Partners table performance improvements

Tim Wilsonfa2804a3bd4e
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/partners-table.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/partners-table.tsx
index 93a5a3468d..1870b838d6 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/partners-table.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/partners-table.tsx
@@ -57,7 +57,7 @@ import { Command } from "cmdk";
import { LockOpen } from "lucide-react";
import { useAction } from "next-safe-action/hooks";
import { useParams, useRouter } from "next/navigation";
-import { useEffect, useState } from "react";
+import { useEffect, useMemo, useState } from "react";
import { toast } from "sonner";
import useSWR from "swr";
import { usePartnerFilters } from "./use-partner-filters";
@@ -158,140 +158,146 @@ export function PartnersTable() {
const { pagination, setPagination } = usePagination();
- const { table, ...tableProps } = useTable({
- data: partners || [],
- columns: [
- {
- id: "partner",
- header: "Partner",
- enableHiding: false,
- minSize: 250,
- cell: ({ row }) => {
- return (
- <PartnerRowItem partner={row.original} showPermalink={false} />
- );
+ const columns = useMemo(
+ () =>
+ [
+ {
+ id: "partner",
+ header: "Partner",
+ enableHiding: false,
+ minSize: 250,
+ cell: ({ row }) => {
+ return (
+ <PartnerRowItem partner={row.original} showPermalink={false} />
+ );
+ },
},
- },
- {
- id: "group",
- header: "Group",
- cell: ({ row }) => {
- if (!groups) return "-";
- const partnerGroup =
- groups.find((g) => g.id === row.original.groupId) ??
- DEFAULT_PARTNER_GROUP;
-
- return (
- <div className="flex items-center gap-2">
- <GroupColorCircle group={partnerGroup} />
- <span className="truncate text-sm font-medium">
- {partnerGroup.name}
- </span>
- </div>
- );
+ {
+ id: "group",
+ header: "Group",
+ cell: ({ row }) => {
+ if (!groups) return "-";
+ const partnerGroup =
+ groups.find((g) => g.id === row.original.groupId) ??
+ DEFAULT_PARTNER_GROUP;
+
+ return (
+ <div className="flex items-center gap-2">
+ <GroupColorCircle group={partnerGroup} />
+ <span className="truncate text-sm font-medium">
+ {partnerGroup.name}
+ </span>
+ </div>
+ );
+ },
},
- },
- {
- id: "createdAt",
- header: "Enrolled",
- accessorFn: (d) => formatDate(d.createdAt, { month: "short" }),
- },
- {
- id: "status",
- header: "Status",
- cell: ({ row }) => {
- const badge = PartnerStatusBadges[row.original.status];
- return badge ? (
- <StatusBadge icon={null} variant={badge.variant}>
- {badge.label}
- </StatusBadge>
- ) : (
- "-"
- );
+ {
+ id: "createdAt",
+ header: "Enrolled",
+ accessorFn: (d) => formatDate(d.createdAt, { month: "short" }),
},
- },
- {
- id: "location",
- header: "Location",
- minSize: 150,
- cell: ({ row }) => {
- const country = row.original.country;
- return (
- <div className="flex items-center gap-2">
- {country && (
- <img
- alt={`${country} flag`}
- src={`https://hatscripts.github.io/circle-flags/flags/${country.toLowerCase()}.svg`}
- className="size-4 shrink-0"
- />
- )}
- <span className="min-w-0 truncate">
- {(country ? COUNTRIES[country] : null) ?? "-"}
- </span>
- </div>
- );
+ {
+ id: "status",
+ header: "Status",
+ cell: ({ row }) => {
+ const badge = PartnerStatusBadges[row.original.status];
+ return badge ? (
+ <StatusBadge icon={null} variant={badge.variant}>
+ {badge.label}
+ </StatusBadge>
+ ) : (
+ "-"
+ );
+ },
},
- },
- {
- id: "clicks",
- header: "Clicks",
- accessorFn: (d) => nFormatter(d.clicks),
- },
- {
- id: "leads",
- header: "Leads",
- accessorFn: (d) => nFormatter(d.leads),
- },
- {
- id: "conversions",
- header: "Conversions",
- accessorFn: (d) => nFormatter(d.conversions),
- },
- {
- id: "sales",
- header: "Sales",
- accessorFn: (d) => nFormatter(d.sales),
- },
- {
- id: "saleAmount",
- header: "Revenue",
- accessorFn: (d) =>
- currencyFormatter(d.saleAmount / 100, {
- minimumFractionDigits: 2,
- maximumFractionDigits: 2,
- }),
- },
- {
- id: "totalCommissions",
- header: "Commissions",
- accessorFn: (d) =>
- currencyFormatter(d.totalCommissions / 100, {
- minimumFractionDigits: 2,
- maximumFractionDigits: 2,
- }),
- },
- {
- id: "netRevenue",
- header: "Net Revenue",
- accessorFn: (d) =>
- currencyFormatter(d.netRevenue / 100, {
- minimumFractionDigits: 2,
- maximumFractionDigits: 2,
- }),
- },
- // Menu
- {
- id: "menu",
- enableHiding: false,
- minSize: 43,
- size: 43,
- maxSize: 43,
- header: () => <EditColumnsButton table={table} />,
- cell: ({ row }) => (
- <RowMenuButton row={row} workspaceId={workspaceId!} />
- ),
- },
- ].filter((c) => c.id === "menu" || partnersColumns.all.includes(c.id)),
+ {
+ id: "location",
+ header: "Location",
+ minSize: 150,
+ cell: ({ row }) => {
+ const country = row.original.country;
+ return (
+ <div className="flex items-center gap-2">
+ {country && (
+ <img
+ alt={`${country} flag`}
+ src={`https://hatscripts.github.io/circle-flags/flags/${country.toLowerCase()}.svg`}
+ className="size-4 shrink-0"
+ />
+ )}
+ <span className="min-w-0 truncate">
+ {(country ? COUNTRIES[country] : null) ?? "-"}
+ </span>
+ </div>
+ );
+ },
+ },
+ {
+ id: "clicks",
+ header: "Clicks",
+ accessorFn: (d) => nFormatter(d.clicks),
+ },
+ {
+ id: "leads",
+ header: "Leads",
+ accessorFn: (d) => nFormatter(d.leads),
+ },
+ {
+ id: "conversions",
+ header: "Conversions",
+ accessorFn: (d) => nFormatter(d.conversions),
+ },
+ {
+ id: "sales",
+ header: "Sales",
+ accessorFn: (d) => nFormatter(d.sales),
+ },
+ {
+ id: "saleAmount",
+ header: "Revenue",
+ accessorFn: (d) =>
+ currencyFormatter(d.saleAmount / 100, {
+ minimumFractionDigits: 2,
+ maximumFractionDigits: 2,
+ }),
+ },
+ {
+ id: "totalCommissions",
+ header: "Commissions",
+ accessorFn: (d) =>
+ currencyFormatter(d.totalCommissions / 100, {
+ minimumFractionDigits: 2,
+ maximumFractionDigits: 2,
+ }),
+ },
+ {
+ id: "netRevenue",
+ header: "Net Revenue",
+ accessorFn: (d) =>
+ currencyFormatter(d.netRevenue / 100, {
+ minimumFractionDigits: 2,
+ maximumFractionDigits: 2,
+ }),
+ },
+ // Menu
+ {
+ id: "menu",
+ enableHiding: false,
+ minSize: 43,
+ size: 43,
+ maxSize: 43,
+ header: ({ table }) => <EditColumnsButton table={table} />,
+ cell: ({ row }) => (
+ <RowMenuButton row={row} workspaceId={workspaceId!} />
+ ),
+ },
+ ].filter((c) => c.id === "menu" || partnersColumns.all.includes(c.id)),
+ [workspaceId, groups],
+ );
+
+ const { table, ...tableProps } = useTable({
+ data: partners || [],
+ columns,
onRowClick: (row) => {
queryParams({
set: {
diff --git a/packages/ui/src/table/table.tsx b/packages/ui/src/table/table.tsx
index a933fd9ee8..f2ae3c762e 100644
--- a/packages/ui/src/table/table.tsx
+++ b/packages/ui/src/table/table.tsx
@@ -16,6 +16,7 @@ import {
memo,
useEffect,
useMemo,
+ useRef,
useState,
} from "react";
import { Button } from "../button";
@@ -71,9 +72,7 @@ export function useTable<T extends any>(
props.selectedRows ?? {},
);
- const [lastSelectedRowId, setLastSelectedRowId] = useState<string | null>(
- null,
- );
+ const lastSelectedRowId = useRef<string | null>(null);
// Manually unset row selection if the row is no longer in the data
// There doesn't seem to be a proper solution for this: https://github.com/TanStack/table/issues/4498
@@ -148,27 +147,29 @@ export function useTable<T extends any>(
checked={row.getIsSelected()}
onClick={(e) => {
const currentId = getRowId?.(row.original);
- const lastSelectedIndex = data.findIndex(
- (l) => getRowId?.(l) === lastSelectedRowId,
- );
+ const rows = table.getRowModel().rows;
+ const lastSelectedIndex =
+ lastSelectedRowId.current !== null
+ ? rows.findIndex(
+ (row) =>
+ getRowId?.(row.original) ===
+ lastSelectedRowId.current,
+ )
+ : -1;
if (
e.shiftKey &&
- lastSelectedRowId !== null &&
+ lastSelectedRowId.current !== null &&
lastSelectedIndex !== -1
) {
// Multi-select w/ shift key
- const lastSelectedIndex = data.findIndex(
- (l) => getRowId?.(l) === lastSelectedRowId,
- );
-
const currentIndex = row.index;
const start = Math.min(lastSelectedIndex, currentIndex);
const end = Math.max(lastSelectedIndex, currentIndex);
- const rangeIds = data
+ const rangeIds = rows
.slice(start, end + 1)
- .map((l) => getRowId?.(l));
+ .map((row) => getRowId?.(row.original));
table.setRowSelection((rowSelection) => {
const alreadySelected =
@@ -183,10 +184,10 @@ export function useTable<T extends any>(
};
});
- setLastSelectedRowId(currentId ?? null);
+ lastSelectedRowId.current = currentId ?? null;
} else {
row.toggleSelected();
- setLastSelectedRowId(currentId ?? null);
+ lastSelectedRowId.current = currentId ?? null;
}
}}
title="Select"