Skip to content

Commit 8a79f5c

Browse files
committed
clean up
1 parent a2c6be7 commit 8a79f5c

File tree

4 files changed

+49
-52
lines changed

4 files changed

+49
-52
lines changed

src/App.tsx

Lines changed: 39 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
import "./App.css";
1+
import "./styles/App.css";
22

33
import { Porto } from "porto";
44
import { useEffect, useMemo, useState } from "react";
55
import { type Address, type Chain, createWalletClient, custom } from "viem";
66
import { getAddresses, requestAddresses } from "viem/actions";
7-
import { applyChainId } from "./helpers.ts";
8-
import type { AnnounceEvent, EIP1193, EIP6963ProviderInfo } from "./types.ts";
7+
import { applyChainId } from "./utils/helpers.ts";
8+
import type {
9+
EIP1193,
10+
EIP6963AnnounceProviderEvent,
11+
EIP6963ProviderInfo,
12+
} from "./utils/types.ts";
913

1014
declare global {
1115
interface Window {
@@ -20,34 +24,30 @@ export function App() {
2024
}
2125
}, []);
2226

23-
const [providers, setProviders] = useState<{ info: EIP6963ProviderInfo; provider: EIP1193 }[]>(
24-
[],
25-
);
27+
const [providers, setProviders] = useState<
28+
{ info: EIP6963ProviderInfo; provider: EIP1193 }[]
29+
>([]);
2630

2731
useEffect(() => {
28-
const onAnnounce = (e: Event) => {
29-
const ev = e as AnnounceEvent;
32+
const onAnnounce = (ev: EIP6963AnnounceProviderEvent) => {
3033
const { info, provider } = ev.detail;
34+
3135
setProviders((prev) =>
32-
prev.some((p) => p.info.uuid === info.uuid) ? prev : [...prev, { info, provider }],
36+
prev.some((p) => p.info.uuid === info.uuid)
37+
? prev
38+
: [...prev, { info, provider }]
3339
);
3440
};
3541

36-
window.addEventListener("eip6963:announceProvider", onAnnounce as EventListener);
42+
window.addEventListener("eip6963:announceProvider", onAnnounce);
3743
window.dispatchEvent(new Event("eip6963:requestProvider"));
3844

3945
return () => {
40-
window.removeEventListener("eip6963:announceProvider", onAnnounce as EventListener);
46+
window.removeEventListener("eip6963:announceProvider", onAnnounce);
4147
};
4248
}, []);
4349

4450
const [selectedUuid, setSelectedUuid] = useState<string | null>(null);
45-
useEffect(() => {
46-
if (providers.length === 1 && !selectedUuid) {
47-
setSelectedUuid(providers[0].info.uuid);
48-
}
49-
}, [providers, selectedUuid]);
50-
5151
const selected = providers.find((p) => p.info.uuid === selectedUuid) ?? null;
5252

5353
const [account, setAccount] = useState<Address>();
@@ -58,18 +58,17 @@ export function App() {
5858
() =>
5959
selected
6060
? createWalletClient({
61-
chain,
6261
transport: custom(selected.provider),
6362
})
6463
: undefined,
65-
[selected, chain],
64+
[selected]
6665
);
6766

6867
useEffect(() => {
6968
if (!selected) return;
7069

7170
const onAccountsChanged = (accounts: readonly string[]) =>
72-
setAccount(accounts?.[0] as Address | undefined);
71+
setAccount((accounts[0] as Address) ?? undefined);
7372

7473
const onChainChanged = (raw: unknown) => {
7574
applyChainId(raw, setChainId, setChain);
@@ -89,7 +88,9 @@ export function App() {
8988
if (!selected) return;
9089

9190
try {
92-
const raw = await selected.provider.request({ method: "eth_chainId" });
91+
const raw = await selected.provider.request<string>({
92+
method: "eth_chainId",
93+
});
9394
applyChainId(raw, setChainId, setChain);
9495
} catch {
9596
setChainId(undefined);
@@ -113,27 +114,16 @@ export function App() {
113114
setAccount(addrs[0] as Address | undefined);
114115

115116
try {
116-
const raw = await selected.provider.request({ method: "eth_chainId" });
117+
const raw = await selected.provider.request<string>({
118+
method: "eth_chainId",
119+
});
117120
applyChainId(raw, setChainId, setChain);
118121
} catch {
119122
setChainId(undefined);
120123
setChain(undefined);
121124
}
122125
};
123126

124-
const disconnect = async () => {
125-
setAccount(undefined);
126-
setChainId(undefined);
127-
setChain(undefined);
128-
129-
try {
130-
await walletClient?.transport.request({
131-
method: "wallet_revokePermissions",
132-
params: [{ eth_accounts: {} }],
133-
});
134-
} catch {}
135-
};
136-
137127
return (
138128
<div className="container">
139129
<h1 className="title">Foundry</h1>
@@ -163,24 +153,26 @@ export function App() {
163153
{selected && account && (
164154
<pre className="info">
165155
{`\
166-
chain: ${chain ? `${chain.name} (${chainId})` : (chainId ?? "unknown")}
167-
rpc: ${chain?.rpcUrls?.default?.http?.[0] ?? chain?.rpcUrls?.public?.http?.[0] ?? "unknown"}`}
156+
chain: ${chain ? `${chain.name} (${chainId})` : chainId ?? "unknown"}
157+
rpc: ${
158+
chain?.rpcUrls?.default?.http?.[0] ??
159+
chain?.rpcUrls?.public?.http?.[0] ??
160+
"unknown"
161+
}`}
168162
</pre>
169163
)}
170164

171-
{selected &&
172-
(account ? (
173-
<>
174-
<div className="output">Connected: {account}</div>
175-
<button type="button" className="disconnect" onClick={disconnect}>
176-
Disconnect
177-
</button>
178-
</>
165+
{selected ? (
166+
account ? (
167+
<div className="output">Connected: {account}</div>
179168
) : (
180-
<button type="button" onClick={connect}>
169+
<button type="button" className="connect" onClick={connect}>
181170
Connect Wallet
182171
</button>
183-
))}
172+
)
173+
) : (
174+
<p>Please select a wallet</p>
175+
)}
184176
</div>
185177
);
186178
}
File renamed without changes.
File renamed without changes.

src/types.ts renamed to src/utils/types.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,26 @@ export interface EIP1193 {
1818
method: string;
1919
params?: readonly unknown[] | Record<string, unknown>;
2020
}): Promise<T>;
21-
on?<K extends keyof EIP1193Events>(event: K, listener: EIP1193Events[K]): void;
22-
removeListener?<K extends keyof EIP1193Events>(event: K, listener: EIP1193Events[K]): void;
21+
on?<K extends keyof EIP1193Events>(
22+
event: K,
23+
listener: EIP1193Events[K]
24+
): void;
25+
removeListener?<K extends keyof EIP1193Events>(
26+
event: K,
27+
listener: EIP1193Events[K]
28+
): void;
2329
}
2430

2531
export type EIP6963ProviderDetail = {
2632
info: EIP6963ProviderInfo;
2733
provider: EIP1193;
2834
};
2935

30-
export interface EIP6963AnnounceProviderEvent extends CustomEvent<EIP6963ProviderDetail> {
36+
export interface EIP6963AnnounceProviderEvent
37+
extends CustomEvent<EIP6963ProviderDetail> {
3138
type: "eip6963:announceProvider";
3239
}
3340

34-
export type AnnounceEvent = EIP6963AnnounceProviderEvent;
35-
3641
declare global {
3742
interface WindowEventMap {
3843
"eip6963:announceProvider": EIP6963AnnounceProviderEvent;

0 commit comments

Comments
 (0)