@@ -4,7 +4,7 @@ import usePartners from "@/lib/swr/use-partners";
4
4
import { EnrolledPartnerProps } from "@/lib/types" ;
5
5
import { Combobox , Table , useTable } from "@dub/ui" ;
6
6
import { cn , DICEBEAR_AVATAR_URL , pluralize } from "@dub/utils" ;
7
- import { useMemo , useState } from "react" ;
7
+ import { useEffect , useMemo , useState } from "react" ;
8
8
import { useDebounce } from "use-debounce" ;
9
9
10
10
interface RewardPartnersTableProps {
@@ -20,6 +20,9 @@ export function RewardPartnersTable({
20
20
} : RewardPartnersTableProps ) {
21
21
const [ search , setSearch ] = useState ( "" ) ;
22
22
const [ debouncedSearch ] = useDebounce ( search , 500 ) ;
23
+ const [ selectedPartners , setSelectedPartners ] = useState <
24
+ Pick < EnrolledPartnerProps , "id" | "name" | "email" | "image" > [ ]
25
+ > ( [ ] ) ;
23
26
24
27
// Get all partners for the table
25
28
const { data : allPartners } = usePartners ( { } ) ;
@@ -55,34 +58,32 @@ export function RewardPartnersTable({
55
58
[ partnerIds , options ] ,
56
59
) ;
57
60
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 ] ) ;
71
74
72
75
const handlePartnerSelection = (
73
76
selectedOptions : typeof selectedPartnersOptions ,
74
77
) => {
75
78
// Get all currently selected IDs
76
79
const currentIds = new Set ( partnerIds ) ;
77
-
80
+
78
81
// Add new selections
79
82
selectedOptions . forEach ( ( { value } ) => {
80
83
currentIds . add ( value ) ;
81
84
} ) ;
82
-
83
- // Convert back to array
84
- const newSelectedIds = Array . from ( currentIds ) ;
85
- setPartners ( newSelectedIds ) ;
85
+
86
+ setPartners ( Array . from ( currentIds ) ) ;
86
87
} ;
87
88
88
89
const table = useTable ( {
0 commit comments