Skip to content

Commit 078ef2c

Browse files
committed
nits
1 parent 97e2b6a commit 078ef2c

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

src/App.tsx

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,18 @@ export function App() {
8787

8888
const pollTick = async () => {
8989
await ensureServerConnected();
90+
9091
try {
9192
const resp = await api<ApiOk<PendingAny> | ApiErr>("/api/transaction/request");
93+
9294
if (!resp || (resp as ApiErr).status !== "ok") {
9395
if (pending) {
9496
setPending(null);
9597
lastPendingIdRef.current = null;
9698
}
9799
} else {
98100
const tx = (resp as ApiOk<PendingAny>).data;
101+
99102
if (!lastPendingIdRef.current || lastPendingIdRef.current !== tx.id) {
100103
setPending(tx);
101104
lastPendingIdRef.current = tx.id ?? null;
@@ -128,7 +131,7 @@ export function App() {
128131
const signAndSendCurrent = async () => {
129132
if (!walletClient || !selected || !pending) return;
130133

131-
let tx = pending;
134+
const tx = pending;
132135

133136
try {
134137
const targetChainId = readPendingChainId(tx) ?? chainId;
@@ -211,28 +214,37 @@ export function App() {
211214
} catch {}
212215
}, []);
213216

217+
// Upon switching wallets, reset state.
214218
useEffect(() => {
215-
if (selectedUuid) resetClientState();
219+
if (selectedUuid) {
220+
resetClientState();
221+
}
216222
}, [selectedUuid, resetClientState]);
217223

224+
// Auto-select if only one wallet is available.
218225
useEffect(() => {
219226
if (providers.length === 1 && !selected) {
220227
setSelectedUuid(providers[0].info.uuid);
221228
}
222229
}, [providers, selected]);
223230

231+
// Listen for new provider announcements.
224232
useEffect(() => {
225233
const onAnnounce = (ev: EIP6963AnnounceProviderEvent) => {
226234
const { info, provider } = ev.detail;
235+
227236
setProviders((prev) =>
228237
prev.some((p) => p.info.uuid === info.uuid) ? prev : [...prev, { info, provider }],
229238
);
230239
};
240+
231241
window.addEventListener("eip6963:announceProvider", onAnnounce);
232242
window.dispatchEvent(new Event("eip6963:requestProvider"));
243+
233244
return () => window.removeEventListener("eip6963:announceProvider", onAnnounce);
234245
}, []);
235246

247+
// Listen for account and chain changes.
236248
useEffect(() => {
237249
if (!selected) return;
238250

@@ -248,6 +260,7 @@ export function App() {
248260
};
249261
}, [selected]);
250262

263+
// Upon account or chainId change, update state.
251264
useEffect(() => {
252265
(async () => {
253266
if (!selected) return;
@@ -269,13 +282,19 @@ export function App() {
269282
})();
270283
}, [selected, walletClient]);
271284

285+
// Polling loop to check for new pending transactions.
272286
useEffect(() => {
273287
pollTick();
274288

275-
if (!pollRef.current) pollRef.current = window.setInterval(pollTick, 1000);
289+
if (!pollRef.current) {
290+
pollRef.current = window.setInterval(pollTick, 1000);
291+
}
276292

277293
return () => {
278-
if (pollRef.current) window.clearInterval(pollRef.current);
294+
if (pollRef.current) {
295+
window.clearInterval(pollRef.current);
296+
}
297+
279298
pollRef.current = null;
280299
};
281300
}, [account, chainId, selected]);

0 commit comments

Comments
 (0)