Skip to content

Commit d482dd3

Browse files
committed
fix render loop b/c of auto lint..?
1 parent dd610da commit d482dd3

File tree

2 files changed

+38
-52
lines changed

2 files changed

+38
-52
lines changed

src/App.tsx

Lines changed: 38 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,13 @@ import "./styles/App.css";
22

33
import { Porto } from "porto";
44
import { useEffect, useMemo, useRef, useState } from "react";
5-
import {
6-
type Address,
7-
type Chain,
8-
createWalletClient,
9-
custom,
10-
type TransactionReceipt,
11-
} from "viem";
5+
import { type Address, type Chain, createWalletClient, custom } from "viem";
126
import { getAddresses, requestAddresses, waitForTransactionReceipt } from "viem/actions";
13-
import { api, isOk, pick, readAddr, renderJSON } from "./utils/api.ts";
147
import {
158
applyChainId,
169
ensureChainSelected,
17-
getChainById,
1810
readPendingChainId,
11+
getChainById,
1912
} from "./utils/helpers.ts";
2013
import type {
2114
ApiErr,
@@ -25,6 +18,7 @@ import type {
2518
EIP6963ProviderInfo,
2619
PendingAny,
2720
} from "./utils/types.ts";
21+
import { api, pick, readAddr, renderJSON } from "./utils/api.ts";
2822

2923
declare global {
3024
interface Window {
@@ -49,7 +43,7 @@ export function App() {
4943
const [account, setAccount] = useState<Address>();
5044
const [chainId, setChainId] = useState<number>();
5145
const [chain, setChain] = useState<Chain>();
52-
const [lastTxReceipt, setLastTxReceipt] = useState<TransactionReceipt | null>(null);
46+
const [lastTxReceipt, setLastTxReceipt] = useState<any | null>(null);
5347
const [lastTxHash, setLastTxHash] = useState<string | null>(null);
5448

5549
const pollRef = useRef<number | null>(null);
@@ -68,11 +62,12 @@ export function App() {
6862
const resp = await api<
6963
ApiOk<{ connected: boolean; account?: string; chainId?: number }> | ApiErr
7064
>("/api/connection");
71-
if (!isOk(resp)) return;
65+
const ok = resp && (resp as ApiOk<any>).status === "ok";
66+
const data = ok ? (resp as ApiOk<any>).data : null;
7267

73-
const serverConnected = !!resp.data?.connected;
74-
const serverAccount = (resp.data?.account as string | undefined)?.toLowerCase();
75-
const serverChainId = resp.data?.chainId as number | undefined;
68+
const serverConnected = !!data?.connected;
69+
const serverAccount = (data?.account as string | undefined)?.toLowerCase();
70+
const serverChainId = data?.chainId as number | undefined;
7671

7772
if (!account || chainId == null) {
7873
if (serverConnected) {
@@ -133,9 +128,7 @@ export function App() {
133128
const signAndSendCurrent = async () => {
134129
if (!walletClient || !selected || !pending) return;
135130

136-
const tx = pending;
137-
138-
console.log(tx);
131+
let tx = pending;
139132

140133
try {
141134
const targetChainId = readPendingChainId(tx) ?? chainId;
@@ -170,10 +163,8 @@ export function App() {
170163
const nonce = tx.nonce as number | undefined;
171164

172165
const params: any = { account: from, to, value, data, gas, nonce };
173-
174-
if (gasPrice) {
175-
params.gasPrice = gasPrice;
176-
} else if (maxFeePerGas || maxPriorityFeePerGas) {
166+
if (gasPrice) params.gasPrice = gasPrice;
167+
else if (maxFeePerGas || maxPriorityFeePerGas) {
177168
params.maxFeePerGas = maxFeePerGas;
178169
params.maxPriorityFeePerGas = maxPriorityFeePerGas;
179170
}
@@ -182,7 +173,6 @@ export function App() {
182173
...params,
183174
chain: desiredChain,
184175
});
185-
186176
console.log("tx sent:", { id: tx.id, hash: lastHash });
187177
setLastTxHash(lastHash);
188178

@@ -197,7 +187,7 @@ export function App() {
197187

198188
try {
199189
await api("/api/transaction/response", "POST", {
200-
id: tx?.id,
190+
id: tx!.id,
201191
hash: null,
202192
error: String(e?.message ?? e),
203193
});
@@ -223,7 +213,7 @@ export function App() {
223213

224214
useEffect(() => {
225215
if (selectedUuid) resetClientState();
226-
}, [selectedUuid, resetClientState]);
216+
}, [selectedUuid]);
227217

228218
useEffect(() => {
229219
if (providers.length === 1 && !selected) {
@@ -288,7 +278,7 @@ export function App() {
288278
if (pollRef.current) window.clearInterval(pollRef.current);
289279
pollRef.current = null;
290280
};
291-
}, [pollTick]);
281+
}, [account, chainId, selected]);
292282

293283
return (
294284
<div className="wrapper">
@@ -317,22 +307,24 @@ export function App() {
317307

318308
{providers.length === 0 && <p>No wallets found.</p>}
319309

320-
{selected && account && (
321-
<>
322-
<div className="section-title">Connected</div>
323-
<pre className="box">
324-
{`\
310+
{selected && account && (<>
311+
<div className="section-title">Connected</div>
312+
<pre className="box">
313+
{`\
325314
account: ${account}
326315
chain: ${chain ? `${chain.name} (${chainId})` : (chainId ?? "unknown")}
327316
rpc: ${chain?.rpcUrls?.default?.http?.[0] ?? chain?.rpcUrls?.public?.http?.[0] ?? "unknown"}`}
328-
</pre>
329-
</>
330-
)}
331-
332-
{selected && !account && (
333-
<button type="button" className="connect" onClick={connect}>
334-
Connect Wallet
335-
</button>
317+
</pre>
318+
</>)}
319+
320+
{selected ? (
321+
!account && (
322+
<button type="button" className="connect" onClick={connect}>
323+
Connect Wallet
324+
</button>
325+
)
326+
) : (
327+
<p>Please select a wallet</p>
336328
)}
337329

338330
{selected && account && (
@@ -346,17 +338,17 @@ rpc: ${chain?.rpcUrls?.default?.http?.[0] ?? chain?.rpcUrls?.public?.http?.[
346338

347339
{selected && account && lastTxHash && (
348340
<div style={{ display: "grid", gap: 12, marginTop: 16 }}>
349-
<div className="section-title">Transaction Hash</div>
350-
<pre className="box">{lastTxHash}</pre>
351-
352-
{lastTxReceipt && (
353-
<>
341+
<div>
342+
<div className="section-title">Transaction Hash</div>
343+
<pre className="box">{lastTxHash}</pre>
344+
345+
<div>
354346
<div className="section-title">Receipt</div>
355347
<pre className="box">
356-
{lastTxReceipt ? renderJSON(lastTxReceipt) : "No receipt available"}
348+
{lastTxReceipt ? renderJSON(lastTxReceipt) : "Waiting for receipt..."}
357349
</pre>
358-
</>
359-
)}
350+
</div>
351+
</div>
360352
</div>
361353
)}
362354

src/utils/api.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import type { ApiErr, ApiOk } from "./types";
2-
31
export const ENDPOINT = "http://127.0.0.1:9545";
42

53
export const api = async <T = unknown>(
@@ -46,7 +44,3 @@ export const readHex = (obj: any, ...keys: string[]): `0x${string}` | undefined
4644

4745
export const renderJSON = (obj: unknown) =>
4846
JSON.stringify(obj, (_k, v) => (typeof v === "bigint" ? v.toString() : v), 2);
49-
50-
export const isOk = <T>(r: ApiOk<T> | ApiErr | null | undefined): r is ApiOk<T> => {
51-
return !!r && (r as ApiOk<T>).status === "ok";
52-
};

0 commit comments

Comments
 (0)