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.

wip default selection

Kiran Ke0835dc672c9
diff --git a/apps/web/ui/partners/payout-invoice-sheet.tsx b/apps/web/ui/partners/payout-invoice-sheet.tsx
index 3f2801ed78..0f1dd8a8a2 100644
--- a/apps/web/ui/partners/payout-invoice-sheet.tsx
+++ b/apps/web/ui/partners/payout-invoice-sheet.tsx
@@ -27,7 +27,7 @@ import {
currencyFormatter,
DICEBEAR_AVATAR_URL,
} from "@dub/utils";
-import { Row } from "@tanstack/react-table";
+import { Row, RowSelectionState } from "@tanstack/react-table";
import { useAction } from "next-safe-action/hooks";
import { useParams } from "next/navigation";
import {
@@ -110,6 +110,17 @@ function PayoutInvoiceSheetContent({ setIsOpen }: PayoutInvoiceSheetProps) {
[payouts],
);
+ // Automatically select all pending payouts on first load
+ useEffect(() => {
+ if (
+ pendingPayouts &&
+ pendingPayouts.length > 0 &&
+ selectedPayouts.length === 0
+ ) {
+ setSelectedPayouts(pendingPayouts);
+ }
+ }, [pendingPayouts, selectedPayouts]);
+
// Set the first payment method as the selected payment method
useEffect(() => {
if (!paymentMethods || !paymentMethods.length) {
@@ -302,6 +313,10 @@ function PayoutInvoiceSheetContent({ setIsOpen }: PayoutInvoiceSheetProps) {
getRowId: (originalRow: PayoutResponse) => originalRow.id,
onRowSelectionChange: (rows: Row<PayoutResponse>[]) =>
setSelectedPayouts(rows.map((row) => row.original)),
+ selectedRows: (selectedPayouts || []).reduce((acc, payout) => {
+ acc[payout.id] = true;
+ return acc;
+ }, {} as RowSelectionState),
} as any);
return (
diff --git a/packages/ui/src/table/table.tsx b/packages/ui/src/table/table.tsx
index c0841d1835..6c94a13e2e 100644
--- a/packages/ui/src/table/table.tsx
+++ b/packages/ui/src/table/table.tsx
@@ -60,6 +60,7 @@ type UseTableProps<T> = {
onRowSelectionChange?: (rows: Row<T>[]) => void;
selectedRows?: RowSelectionState;
getRowId?: (row: T) => string;
+ defaultSelectedRows?: RowSelectionState;
// Table styles
className?: string;
@@ -99,9 +100,17 @@ export function useTable<T extends any>(
props.columnVisibility ?? {},
);
- const [rowSelection, setRowSelection] = useState<RowSelectionState>(
- props.selectedRows ?? {},
- );
+ const [rowSelection, setRowSelection] = useState<RowSelectionState>({});
+
+ useEffect(() => {
+ if (props.selectedRows && !deepEqual(props.selectedRows, rowSelection)) {
+ setRowSelection(props.selectedRows);
+ }
+ }, [props.selectedRows, rowSelection]);
+
+ useEffect(() => {
+ props.onRowSelectionChange?.(table.getSelectedRowModel().rows);
+ }, [rowSelection]);
// Update internal columnVisibility when prop value changes
useEffect(() => {
@@ -117,13 +126,6 @@ export function useTable<T extends any>(
props.onColumnVisibilityChange?.(columnVisibility);
}, [columnVisibility]);
- // Call onRowSelectionChange when internal rowSelection changes
- useEffect(() => {
- props.onRowSelectionChange?.(
- Object.keys(rowSelection).map((key) => table.getRow(key)),
- );
- }, [rowSelection]);
-
const table = useReactTable({
data,
rowCount,