Skip to content

Commit 435e643

Browse files
authored
Merge pull request #1460 from scroll-tech/allow-connect-wallet
Connect wallet before adding network
2 parents dfb0c32 + e906dc5 commit 435e643

File tree

2 files changed

+57
-24
lines changed

2 files changed

+57
-24
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>

src/components/Header/MobileGasPriceViewer.tsx

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const MotionBox = motion(Box)
1616

1717
const MobileGasPriceViewer = props => {
1818
const { dark } = props
19-
const { chainId } = useRainbowContext()
19+
const { chainId, walletCurrentAddress, connect } = useRainbowContext()
2020

2121
const gasPriceRef = useRef<HTMLDivElement>(null)
2222

@@ -47,6 +47,28 @@ const MobileGasPriceViewer = props => {
4747
return "-"
4848
}, [ethereumGasPrice])
4949

50+
const actionData = useMemo(() => {
51+
if (!walletCurrentAddress) {
52+
return {
53+
label: "Connect wallet to add Scroll",
54+
onClick: connect,
55+
}
56+
} else if (chainId === CHAIN_ID.L2) {
57+
return {
58+
label: "Add Scroll to wallet",
59+
onClick: () => {
60+
setwarningVisible(true)
61+
},
62+
}
63+
}
64+
return {
65+
label: "Add Scroll to wallet",
66+
onClick: async () => {
67+
await switchNetwork(CHAIN_ID.L2)
68+
},
69+
}
70+
}, [chainId, walletCurrentAddress])
71+
5072
useEffect(() => {
5173
const width = gasPriceRef.current?.offsetWidth
5274
setGasPriceWidth(width ?? 0)
@@ -56,14 +78,6 @@ const MobileGasPriceViewer = props => {
5678
setGasPricePanelVisible(!gasPricePanelVisible)
5779
}
5880

59-
const handleAddScrollToWallet = async () => {
60-
if (chainId === CHAIN_ID.L2) {
61-
setwarningVisible(true)
62-
} else {
63-
await switchNetwork(CHAIN_ID.L2)
64-
}
65-
}
66-
6781
const handleCloseWarning = () => {
6882
setwarningVisible(false)
6983
}
@@ -182,9 +196,9 @@ const MobileGasPriceViewer = props => {
182196
p: "0.8rem 2.4rem",
183197
height: "4rem",
184198
}}
185-
onClick={handleAddScrollToWallet}
199+
onClick={actionData.onClick}
186200
>
187-
Add Scroll to Wallet
201+
{actionData.label}
188202
</Button>
189203
</Box>
190204
</Collapse>

0 commit comments

Comments
 (0)