@@ -34,29 +34,50 @@ export default function useWalletConnectPairs(): Pairs {
3434 pairsRef . current = localStorageData ;
3535
3636 const updatePair = useCallback ( ( topic : string , data : Partial < Omit < Pair , 'topic' > > | ( ( pair : Pair ) => Pair ) ) => {
37- const [ , setPairs ] = pairsRef . current ;
37+ const [ latestPairs , setPairs ] = pairsRef . current ;
38+
39+ const index = latestPairs . findIndex ( ( item ) => item . topic === topic ) ;
40+ if ( index !== - 1 ) {
41+ const oldPair = latestPairs [ index ] ;
42+ const newPairing = typeof data === 'function' ? data ( oldPair ) : { ...oldPair , ...data } ;
43+ const newPairings = [ ...latestPairs ] ;
44+ newPairings [ index ] = newPairing ;
45+ pairsRef . current = [ newPairings , setPairs ] ;
46+ }
47+
3848 setPairs ( ( pairs : Pair [ ] ) => {
39- const index = pairs . findIndex ( ( item ) => item . topic === topic ) ;
40- if ( index === - 1 ) {
49+ const idx = pairs . findIndex ( ( item ) => item . topic === topic ) ;
50+ if ( idx === - 1 ) {
4151 return pairs ;
4252 }
4353
44- const oldPair = pairs [ index ] ;
54+ const oldPair = pairs [ idx ] ;
4555 const newPairing = typeof data === 'function' ? data ( oldPair ) : { ...oldPair , ...data } ;
4656 const newPairings = [ ...pairs ] ;
47- newPairings [ index ] = newPairing ;
57+ newPairings [ idx ] = newPairing ;
4858
4959 return newPairings ;
5060 } ) ;
5161 } , [ ] ) ;
5262
5363 const removePair = useCallback ( ( topic : string ) => {
54- const [ , setPairs ] = pairsRef . current ;
64+ const [ latestPairs , setPairs ] = pairsRef . current ;
65+ if ( ! latestPairs . some ( ( item ) => item . topic === topic ) ) {
66+ return ;
67+ }
68+
69+ pairsRef . current = [ latestPairs . filter ( ( item ) => item . topic !== topic ) , setPairs ] ;
70+
5571 setPairs ( ( pairs : Pair [ ] ) => pairs . filter ( ( item ) => item . topic !== topic ) ) ;
5672 } , [ ] ) ;
5773
5874 const removePairBySession = useCallback ( ( sessionTopic : string ) => {
59- const [ , setPairs ] = pairsRef . current ;
75+ const [ latestPairs , setPairs ] = pairsRef . current ;
76+ pairsRef . current = [
77+ latestPairs . filter ( ( item ) => ! item . sessions . find ( ( session ) => session . topic === sessionTopic ) ) ,
78+ setPairs ,
79+ ] ;
80+
6081 setPairs ( ( pairs : Pair [ ] ) =>
6182 pairs . filter ( ( item ) => ! item . sessions . find ( ( session ) => session . topic === sessionTopic ) ) ,
6283 ) ;
@@ -78,19 +99,31 @@ export default function useWalletConnectPairs(): Pairs {
7899 } , [ ] ) ;
79100
80101 const addPair = useCallback ( ( pair : Pair ) => {
81- const [ , setPairs ] = pairsRef . current ;
102+ const [ latestPairs , setPairs ] = pairsRef . current ;
103+ if ( latestPairs . findIndex ( ( item ) => item . topic === pair . topic ) !== - 1 ) {
104+ throw new Error ( 'Pair already exists' ) ;
105+ }
106+
107+ pairsRef . current = [ [ ...latestPairs , pair ] , setPairs ] ;
108+
82109 setPairs ( ( pairs : Pair [ ] ) => {
83- const index = pairs . findIndex ( ( item ) => item . topic === pair . topic ) ;
84- if ( index !== - 1 ) {
85- throw new Error ( 'Pair already exists' ) ;
110+ if ( pairs . some ( ( item ) => item . topic === pair . topic ) ) {
111+ return pairs ;
86112 }
87-
88113 return [ ...pairs , pair ] ;
89114 } ) ;
90115 } , [ ] ) ;
91116
92117 const removeSessionFromPair = useCallback ( ( sessionTopic : string ) => {
93- const [ , setPairs ] = pairsRef . current ;
118+ const [ latestPairs , setPairs ] = pairsRef . current ;
119+ pairsRef . current = [
120+ latestPairs . map ( ( pair ) => ( {
121+ ...pair ,
122+ sessions : pair . sessions . filter ( ( item ) => item . topic !== sessionTopic ) ,
123+ } ) ) ,
124+ setPairs ,
125+ ] ;
126+
94127 setPairs ( ( pairs : Pair [ ] ) =>
95128 pairs . map ( ( pair ) => ( {
96129 ...pair ,
0 commit comments