Skip to content

Commit 66db2c1

Browse files
committed
fix: connect wallet before adding network
1 parent dfb0c32 commit 66db2c1

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

src/components/Header/GasPriceViewer.tsx

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { useRainbowContext } from "@/contexts/RainbowProvider"
1212
import { switchNetwork } from "@/utils"
1313

1414
const GasPriceViewer = () => {
15-
const { chainId } = useRainbowContext()
15+
const { chainId, connect, walletCurrentAddress } = useRainbowContext()
1616
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null)
1717

1818
const [warningVisible, setwarningVisible] = useState(false)
@@ -41,18 +41,37 @@ const GasPriceViewer = () => {
4141
setAnchorEl(event.currentTarget)
4242
}
4343

44-
const handleClosePopover = () => {
44+
const handleClosePopover = e => {
45+
// allow user to add network immediately after connecting wallet
46+
if (e.relatedTarget && e.relatedTarget.getAttribute("aria-labelledby") === "rk_connect_title") {
47+
return
48+
}
4549
setAnchorEl(null)
4650
}
4751

48-
const handleAddScrollToWallet = async () => {
49-
if (chainId === CHAIN_ID.L2) {
50-
setwarningVisible(true)
51-
setAnchorEl(null)
52-
} else {
53-
await switchNetwork(CHAIN_ID.L2)
52+
const actionData = useMemo(() => {
53+
if (!walletCurrentAddress) {
54+
return {
55+
label: "Connect wallet to add Scroll",
56+
onClick: connect,
57+
}
58+
} else if (chainId === CHAIN_ID.L2) {
59+
return {
60+
label: "Add Scroll to wallet",
61+
onClick: () => {
62+
setwarningVisible(true)
63+
setAnchorEl(null)
64+
},
65+
}
5466
}
55-
}
67+
return {
68+
label: "Add Scroll to wallet",
69+
onClick: async () => {
70+
await switchNetwork(CHAIN_ID.L2)
71+
setAnchorEl(null)
72+
},
73+
}
74+
}, [chainId, walletCurrentAddress])
5675

5776
const handleCloseWarning = () => {
5877
setwarningVisible(false)
@@ -108,7 +127,7 @@ const GasPriceViewer = () => {
108127
rowGap: "2.4rem",
109128
columnGap: "0.8rem",
110129
padding: "2.4rem",
111-
width: "24.6rem",
130+
width: "26.8rem",
112131
fontSize: "1.6rem",
113132
lineHeight: "2.4rem",
114133
}}
@@ -128,12 +147,12 @@ const GasPriceViewer = () => {
128147
fontSize: "1.6rem",
129148
lineHeight: "2.4rem",
130149
gridColumn: "1 / -1",
131-
p: "0.8rem 2.4rem",
150+
p: "0.8rem",
132151
height: "4rem",
133152
}}
134-
onClick={handleAddScrollToWallet}
153+
onClick={actionData.onClick}
135154
>
136-
Add Scroll to Wallet
155+
{actionData.label}
137156
</Button>
138157
</Paper>
139158
</Fade>

0 commit comments

Comments
 (0)