Skip to content

Commit 6fc0472

Browse files
committed
table fix
1 parent 503a1b5 commit 6fc0472

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

apps/web/ui/partners/reward-partners-table.tsx

+20-19
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import usePartners from "@/lib/swr/use-partners";
44
import { EnrolledPartnerProps } from "@/lib/types";
55
import { Combobox, Table, useTable } from "@dub/ui";
66
import { cn, DICEBEAR_AVATAR_URL, pluralize } from "@dub/utils";
7-
import { useMemo, useState } from "react";
7+
import { useEffect, useMemo, useState } from "react";
88
import { useDebounce } from "use-debounce";
99

1010
interface RewardPartnersTableProps {
@@ -20,6 +20,9 @@ export function RewardPartnersTable({
2020
}: RewardPartnersTableProps) {
2121
const [search, setSearch] = useState("");
2222
const [debouncedSearch] = useDebounce(search, 500);
23+
const [selectedPartners, setSelectedPartners] = useState<
24+
Pick<EnrolledPartnerProps, "id" | "name" | "email" | "image">[]
25+
>([]);
2326

2427
// Get all partners for the table
2528
const { data: allPartners } = usePartners({});
@@ -55,34 +58,32 @@ export function RewardPartnersTable({
5558
[partnerIds, options],
5659
);
5760

58-
const selectedPartners = useMemo(
59-
() =>
60-
partnerIds
61-
.map((id) => allPartners?.find((partner) => partner.id === id))
62-
.filter((p): p is NonNullable<typeof p> => p != null)
63-
.map((partner) => ({
64-
id: partner.id,
65-
name: partner.name,
66-
email: partner.email,
67-
image: partner.image,
68-
})),
69-
[partnerIds, allPartners],
70-
);
61+
// Update selected partners when partnerIds changes
62+
useEffect(() => {
63+
const updatedPartners = partnerIds
64+
.map((id) => allPartners?.find((partner) => partner.id === id))
65+
.filter((p): p is NonNullable<typeof p> => p != null)
66+
.map((partner) => ({
67+
id: partner.id,
68+
name: partner.name,
69+
email: partner.email,
70+
image: partner.image,
71+
}));
72+
setSelectedPartners(updatedPartners);
73+
}, [partnerIds, allPartners]);
7174

7275
const handlePartnerSelection = (
7376
selectedOptions: typeof selectedPartnersOptions,
7477
) => {
7578
// Get all currently selected IDs
7679
const currentIds = new Set(partnerIds);
77-
80+
7881
// Add new selections
7982
selectedOptions.forEach(({ value }) => {
8083
currentIds.add(value);
8184
});
82-
83-
// Convert back to array
84-
const newSelectedIds = Array.from(currentIds);
85-
setPartners(newSelectedIds);
85+
86+
setPartners(Array.from(currentIds));
8687
};
8788

8889
const table = useTable({

0 commit comments

Comments
 (0)