Skip to content

Commit 83a858d

Browse files
committed
clean up, simplify
1 parent 3e47049 commit 83a858d

File tree

2 files changed

+36
-61
lines changed

2 files changed

+36
-61
lines changed

src/App.tsx

Lines changed: 27 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ export function App() {
5050

5151
const lastPendingIdRef = useRef<string | null>(null);
5252
const prevSelectedUuidRef = useRef<string | null>(null);
53-
const pollFnRef = useRef<() => void>(() => {});
5453

5554
const walletClient = useMemo(() => {
5655
if (!selected) return undefined;
@@ -88,34 +87,6 @@ export function App() {
8887
} catch {}
8988
}, [account, chainId]);
9089

91-
const pollTick = useCallback(async () => {
92-
if (!confirmed) {
93-
await ensureServerConnected();
94-
}
95-
96-
try {
97-
const resp = await api<ApiOk<PendingAny> | ApiErr>("/api/transaction/request");
98-
99-
if (!isOk(resp)) {
100-
if (pending) {
101-
setPending(null);
102-
lastPendingIdRef.current = null;
103-
}
104-
} else {
105-
const tx = (resp as ApiOk<PendingAny>).data;
106-
107-
if (!lastPendingIdRef.current || lastPendingIdRef.current !== tx.id) {
108-
setPending(tx);
109-
lastPendingIdRef.current = tx.id;
110-
setLastTxHash(null);
111-
setLastTxReceipt(null);
112-
} else if (!pending) {
113-
setPending(tx);
114-
}
115-
}
116-
} catch {}
117-
}, [ensureServerConnected, pending]);
118-
11990
const connect = async () => {
12091
if (!selected || confirmed) return;
12192

@@ -134,7 +105,15 @@ export function App() {
134105
};
135106

136107
const confirm = async () => {
137-
await ensureServerConnected();
108+
if (!account || chainId == null) {
109+
return;
110+
}
111+
112+
try {
113+
await api("/api/connection", "POST", [account, chainId]);
114+
} catch {
115+
return;
116+
}
138117

139118
setConfirmed(true);
140119
};
@@ -153,7 +132,8 @@ export function App() {
153132
setLastTxReceipt(receipt);
154133

155134
await api("/api/transaction/response", "POST", { id: pending.id, hash, error: null });
156-
await pollTick();
135+
136+
setPending(null);
157137
} catch (e: unknown) {
158138
const msg =
159139
typeof e === "object" &&
@@ -173,7 +153,7 @@ export function App() {
173153
});
174154
} catch {}
175155

176-
await pollTick();
156+
setPending(null);
177157
}
178158
};
179159

@@ -255,25 +235,21 @@ export function App() {
255235
}, [selected, confirmed]);
256236

257237
useEffect(() => {
258-
pollFnRef.current = () => {
259-
void pollTick();
260-
};
261-
}, [pollTick]);
238+
if (!confirmed || pending) return;
262239

263-
// Polling loop to check for new pending transactions.
264-
useEffect(() => {
265-
if (!confirmed) return;
266-
267-
pollFnRef.current();
268-
269-
const id = window.setInterval(() => {
270-
pollFnRef.current();
240+
const id = window.setInterval(async () => {
241+
try {
242+
const resp = await api<ApiOk<PendingAny> | ApiErr>("/api/transaction/request");
243+
if (isOk(resp)) {
244+
setPending(resp.data);
245+
}
246+
} catch {}
271247
}, 1000);
272248

273249
return () => {
274250
window.clearInterval(id);
275251
};
276-
}, [confirmed]);
252+
}, [confirmed, pending]);
277253

278254
return (
279255
<div className="wrapper">
@@ -310,7 +286,12 @@ export function App() {
310286
)}
311287

312288
{selected && account && !confirmed && (
313-
<button type="button" className="wallet-confirm" onClick={confirm}>
289+
<button
290+
type="button"
291+
className="wallet-confirm"
292+
onClick={confirm}
293+
disabled={!account || chainId == null}
294+
>
314295
Confirm Connection
315296
</button>
316297
)}

src/styles/App.css

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,30 @@
1919

2020
.banner {
2121
width: 100%;
22-
border-radius: 8px;
2322
height: auto;
23+
border-radius: 8px;
2424
}
2525

26-
.wallet-selector {
26+
.wallet-selector,
27+
.wallet-connect,
28+
.wallet-send,
29+
.wallet-confirm {
2730
align-self: center;
2831
margin-top: 16px;
2932
}
3033

31-
.wallet-connect {
32-
align-self: center;
33-
}
34-
35-
.wallet-send {
36-
align-self: center;
37-
}
38-
39-
.wallet-confirm {
40-
align-self: center;
34+
.title,
35+
.section-title {
36+
color: #f8f8f8;
4137
}
4238

4339
.title {
4440
font-size: 36px;
45-
color: #f8f8f8;
4641
margin-bottom: 24px;
4742
}
4843

4944
.section-title {
5045
font-size: 24px;
51-
color: #f8f8f8;
5246
margin-bottom: 16px;
5347
}
5448

@@ -58,4 +52,4 @@
5852
border: 1px solid #e1e4e8;
5953
border-radius: 8px;
6054
padding: 8px 12px;
61-
}
55+
}

0 commit comments

Comments
 (0)