Skip to content

Commit 23f4b36

Browse files
fix: fix useApi interaction with useApiWithChain2 (#1556)
--------- Co-authored-by: Nick <[email protected]>
1 parent 6cd91ff commit 23f4b36

File tree

1 file changed

+53
-38
lines changed
  • packages/extension-polkagate/src/hooks

1 file changed

+53
-38
lines changed

packages/extension-polkagate/src/hooks/useApi.ts

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export default function useApi (address: AccountId | string | undefined, stateAp
103103
dispatch({ payload: availableApi.api, type: 'SET_API' });
104104
updateEndpoint(address, genesisHash, availableApi.endpoint);
105105

106-
console.log('Successfully connected to existing API for genesis hash:::', genesisHash);
106+
// console.log('Successfully connected to existing API for genesis hash:', genesisHash);
107107

108108
return true;
109109
}, [apisContext.apis, updateEndpoint]);
@@ -124,6 +124,7 @@ export default function useApi (address: AccountId | string | undefined, stateAp
124124
toSaveApi = toSaveApi.filter((sApi) => !isAutoMode(sApi.endpoint));
125125
}
126126

127+
// Add the new API entry
127128
toSaveApi.push({
128129
api,
129130
endpoint,
@@ -185,6 +186,52 @@ export default function useApi (address: AccountId | string | undefined, stateAp
185186
updateEndpoint(accountAddress, genesisHash, selectedEndpoint, () => handleNewApi(api, selectedEndpoint, true));
186187
}, [apisContext.apis, connectToExisted, endpoints, handleNewApi, updateEndpoint]);
187188

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+
188235
// Manages the API connection when the address, endpoint, or genesis hash changes
189236
useEffect(() => {
190237
// @ts-expect-error to bypass access to private prop
@@ -208,27 +255,14 @@ export default function useApi (address: AccountId | string | undefined, stateAp
208255

209256
// Check if the two endpoints are not synchronized
210257
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.');
213259

214260
// Exit early to avoid further execution until the endpoints are in sync
215261
return;
216262
}
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-
}
228263

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)) {
232266
return;
233267
}
234268

@@ -252,28 +286,9 @@ export default function useApi (address: AccountId | string | undefined, stateAp
252286
});
253287
}
254288

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);
262290
// @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]);
277292

278293
return state.api;
279294
}

0 commit comments

Comments
 (0)