@@ -103,7 +103,7 @@ export default function useApi (address: AccountId | string | undefined, stateAp
103
103
dispatch ( { payload : availableApi . api , type : 'SET_API' } ) ;
104
104
updateEndpoint ( address , genesisHash , availableApi . endpoint ) ;
105
105
106
- console . log ( 'Successfully connected to existing API for genesis hash:: :' , genesisHash ) ;
106
+ // console.log('Successfully connected to existing API for genesis hash:', genesisHash);
107
107
108
108
return true ;
109
109
} , [ apisContext . apis , updateEndpoint ] ) ;
@@ -124,6 +124,7 @@ export default function useApi (address: AccountId | string | undefined, stateAp
124
124
toSaveApi = toSaveApi . filter ( ( sApi ) => ! isAutoMode ( sApi . endpoint ) ) ;
125
125
}
126
126
127
+ // Add the new API entry
127
128
toSaveApi . push ( {
128
129
api,
129
130
endpoint,
@@ -185,6 +186,52 @@ export default function useApi (address: AccountId | string | undefined, stateAp
185
186
updateEndpoint ( accountAddress , genesisHash , selectedEndpoint , ( ) => handleNewApi ( api , selectedEndpoint , true ) ) ;
186
187
} , [ apisContext . apis , connectToExisted , endpoints , handleNewApi , updateEndpoint ] ) ;
187
188
189
+ const addApiRequest = useCallback ( ( endpointToRequest : string , genesisHash : string ) => {
190
+ const toSaveApi = apisContext . apis [ genesisHash ] ?? [ ] ;
191
+
192
+ toSaveApi . push ( { endpoint : endpointToRequest , isRequested : true } ) ;
193
+
194
+ apisContext . apis [ genesisHash ] = toSaveApi ;
195
+ apisContext . setIt ( { ...apisContext . apis } ) ;
196
+ } , [ apisContext ] ) ;
197
+
198
+ // check api in the context
199
+ const isInContext = useCallback ( ( endpoint : string , genesisHash : string ) => {
200
+ // Check if there is a saved API that is already connected
201
+ const savedApi = apisContext ?. apis [ genesisHash ] ?. find ( ( sApi ) => sApi . endpoint === endpoint ) ;
202
+
203
+ // If the API is already being requested, skip the connection process
204
+ if ( savedApi ?. isRequested ) {
205
+ return true ;
206
+ }
207
+
208
+ if ( savedApi ?. api ?. isConnected ) {
209
+ dispatch ( { payload : savedApi . api , type : 'SET_API' } ) ;
210
+
211
+ return true ;
212
+ }
213
+
214
+ return false ;
215
+ } , [ apisContext ?. apis ] ) ;
216
+
217
+ // Handles connection request to a manual endpoint
218
+ const handleApiWithChain = useCallback ( ( manualEndpoint : string , genesisHash : string ) => {
219
+ if ( isInContext ( manualEndpoint , genesisHash ) ) {
220
+ return ;
221
+ }
222
+
223
+ addApiRequest ( manualEndpoint , genesisHash ) ;
224
+
225
+ connectToEndpoint ( manualEndpoint ) . catch ( console . error ) ;
226
+ } , [ addApiRequest , connectToEndpoint , isInContext ] ) ;
227
+
228
+ useEffect ( ( ) => {
229
+ // if _endpoint & _genesisHash are available means useApiWithChain2 is trying to create a new connection!
230
+ if ( _endpoint && _genesisHash ) {
231
+ handleApiWithChain ( _endpoint , _genesisHash ) ;
232
+ }
233
+ } , [ _endpoint , _genesisHash , handleApiWithChain ] ) ;
234
+
188
235
// Manages the API connection when the address, endpoint, or genesis hash changes
189
236
useEffect ( ( ) => {
190
237
// @ts -expect-error to bypass access to private prop
@@ -208,27 +255,14 @@ export default function useApi (address: AccountId | string | undefined, stateAp
208
255
209
256
// Check if the two endpoints are not synchronized
210
257
if ( endpoint !== endpointFromTheManager ) {
211
- // Log a message to indicate that the endpoint has not been updated yet
212
- console . log ( '📌 📌 Not updated yet! The endpoint in the manager is still different from the local one.' ) ;
258
+ // console.log('📌 📌 Not updated yet! The endpoint in the manager is still different from the local one.');
213
259
214
260
// Exit early to avoid further execution until the endpoints are in sync
215
261
return ;
216
262
}
217
- // If we reach this point, the endpoints match and we can proceed with the update
218
-
219
- // Check if there is a saved API that is already connected
220
- const savedApi = apisContext ?. apis [ chainGenesisHash ] ?. find ( ( sApi ) => sApi . endpoint === endpoint ) ;
221
-
222
- if ( savedApi ?. api && savedApi . api . isConnected ) {
223
- // console.log(`♻ Using the saved API for ${chainGenesisHash} through this endpoint ${savedApi.endpoint ?? ''}`);
224
- dispatch ( { payload : savedApi . api , type : 'SET_API' } ) ;
225
-
226
- return ;
227
- }
228
263
229
- // If the API is already being requested, skip the connection process
230
- // It can be either Auto Mode or a specific endpoint
231
- if ( savedApi ?. isRequested ) {
264
+ // To provide api from context
265
+ if ( isInContext ( endpoint , chainGenesisHash ) ) {
232
266
return ;
233
267
}
234
268
@@ -252,28 +286,9 @@ export default function useApi (address: AccountId | string | undefined, stateAp
252
286
} ) ;
253
287
}
254
288
255
- const toSaveApi = apisContext . apis [ chainGenesisHash ] ?? [ ] ;
256
-
257
- toSaveApi . push ( { endpoint, isRequested : true } ) ;
258
-
259
- apisContext . apis [ chainGenesisHash ] = toSaveApi ;
260
- apisContext . setIt ( { ...apisContext . apis } ) ;
261
-
289
+ addApiRequest ( endpoint , chainGenesisHash ) ;
262
290
// @ts -expect-error to bypass access to private prop
263
- // eslint-disable-next-line react-hooks/exhaustive-deps, @typescript-eslint/no-unsafe-member-access
264
- } , [ address , apisContext ?. apis ?. [ chainGenesisHash ] ?. length , chainGenesisHash , checkForNewOne , connectToEndpoint , endpoint , handleAutoMode , handleNewApi , state ?. api ?. _options ?. provider ?. endpoint , state . isLoading ] ) ;
265
-
266
- useEffect ( ( ) => {
267
- if ( ! chainGenesisHash || ! apisContext ?. apis [ chainGenesisHash ] ) {
268
- return ;
269
- }
270
-
271
- const savedApi = apisContext . apis [ chainGenesisHash ] . find ( ( sApi ) => sApi . endpoint === endpoint ) ;
272
-
273
- if ( savedApi ?. api ?. isConnected ) {
274
- dispatch ( { payload : savedApi . api , type : 'SET_API' } ) ;
275
- }
276
- } , [ apisContext . apis , chainGenesisHash , endpoint ] ) ;
291
+ } , [ address , addApiRequest , chainGenesisHash , checkForNewOne , connectToEndpoint , endpoint , handleAutoMode , handleNewApi , isInContext , state ?. api ?. _options ?. provider ?. endpoint , state . isLoading ] ) ;
277
292
278
293
return state . api ;
279
294
}
0 commit comments