Skip to content

Commit 308c010

Browse files
committed
feat: refactoring related to wallet connection
1 parent b959416 commit 308c010

File tree

11 files changed

+68
-64
lines changed

11 files changed

+68
-64
lines changed

src/App.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,22 @@ import Header from "./components/Header";
1010
import { PWAModal } from "./components/PWAModal";
1111
import { SplashScreen } from "./components/Splashscreen";
1212
import { usePrivateKeyAsAuth } from "./hooks/usePrivateKeyAsAuth";
13+
import ConnectWalletDialog from "./screens/ConnectWallet";
1314
import UnderConstruction from "./screens/Errors/UnderConstruction";
1415
import Faucet from "./screens/Faucet";
1516
import SpotScreen from "./screens/SpotScreen";
1617
import { SwapScreen } from "./screens/SwapScreen";
18+
import { MODAL_TYPE } from "./stores/ModalStore";
1719
import { DEFAULT_MARKET, ROUTES } from "./constants";
20+
import { useStores } from "./stores";
1821

1922
const isUnderConstruction = false;
2023

2124
const DEFAULT_SPOT_ROUTE = `/spot/${DEFAULT_MARKET}`;
2225

2326
const App: React.FC = observer(() => {
27+
const { modalStore } = useStores();
28+
2429
usePrivateKeyAsAuth();
2530

2631
if (isUnderConstruction) {
@@ -40,6 +45,7 @@ const App: React.FC = observer(() => {
4045
<SideManageAssets />
4146
<PWAModal />
4247
<SplashScreen />
48+
<ConnectWalletDialog visible={modalStore.isOpen(MODAL_TYPE.CONNECT_MODAL)} onClose={() => modalStore.close()} />
4349
</Root>
4450
);
4551
});

src/components/Button.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ import styled from "@emotion/styled";
44
import { TEXT_TYPES, TEXT_TYPES_MAP } from "@components/Text";
55
import { media } from "@src/themes/breakpoints";
66

7-
const Button = styled.button<{
7+
export interface ButtonProps {
88
green?: boolean;
99
red?: boolean;
1010
text?: boolean;
1111
black?: boolean;
1212
grey?: boolean;
1313
fitContent?: boolean;
1414
active?: boolean;
15-
}>`
15+
}
16+
17+
const Button = styled.button<ButtonProps>`
1618
text-decoration: none;
1719
white-space: nowrap;
1820
display: flex;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import React from "react";
2+
import { observer } from "mobx-react-lite";
3+
4+
import { useStores } from "@src/stores";
5+
import { MODAL_TYPE } from "@src/stores/ModalStore";
6+
7+
import Button, { ButtonProps } from "./Button";
8+
9+
interface Props extends ButtonProps {
10+
connectText?: string;
11+
children: React.ReactNode;
12+
}
13+
14+
export const ConnectWalletButton: React.FC<Props> = observer(
15+
({ connectText = "Connect wallet", children, ...props }) => {
16+
const { accountStore, modalStore } = useStores();
17+
18+
if (!accountStore.isConnected) {
19+
return (
20+
<Button green {...props} onClick={() => modalStore.open(MODAL_TYPE.CONNECT_MODAL)}>
21+
{connectText}
22+
</Button>
23+
);
24+
}
25+
26+
return <>{children}</>;
27+
},
28+
);

src/components/Header/Header.tsx

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,29 @@ import styled from "@emotion/styled";
33
import { observer } from "mobx-react";
44

55
import Button from "@components/Button";
6-
import ConnectedWallet from "@components/Header/ConnectedWallet";
76
import DataBase from "@src/assets/icons/dataBase.svg?react";
87
import Logo from "@src/assets/icons/logo.svg?react";
98
import Menu from "@src/assets/icons/menu.svg?react";
109
import useFlag from "@src/hooks/useFlag";
1110
import { useMedia } from "@src/hooks/useMedia";
12-
import ConnectWalletDialog from "@src/screens/ConnectWallet";
11+
import { MODAL_TYPE } from "@src/stores/ModalStore";
1312
import { media } from "@src/themes/breakpoints";
1413
import { useStores } from "@stores";
1514

15+
import { ConnectWalletButton } from "../ConnectWalletButton";
1616
import { AccountInfoSheet } from "../Modal";
1717
import { SmartFlex } from "../SmartFlex";
18+
import WalletButton from "../WalletButton";
1819

19-
import ConnectedWalletButton from "./ConnectedWalletButton";
2020
import { MenuNav } from "./MenuNav";
2121
import MobileMenu from "./MobileMenu";
22+
import WalletAddressButton from "./WalletAddressButton";
2223

2324
const Header: React.FC = observer(() => {
24-
const { modalStore, accountStore, quickAssetsStore } = useStores();
25+
const { modalStore, quickAssetsStore } = useStores();
2526
const media = useMedia();
2627

2728
const [isMobileMenuOpen, openMobileMenu, closeMobileMenu] = useFlag();
28-
const [isConnectDialogVisible, openConnectDialog, closeConnectDialog] = useFlag();
2929
const [isAccountInfoSheetOpen, openAccountInfo, closeAccountInfo] = useFlag();
3030

3131
useEffect(() => {
@@ -44,30 +44,16 @@ const Header: React.FC = observer(() => {
4444
}
4545
};
4646

47+
const openConnectModal = () => modalStore.open(MODAL_TYPE.CONNECT_MODAL);
48+
4749
const renderWallet = () => {
4850
const dataOnboardingConnectKey = `connect-${media.mobile ? "mobile" : "desktop"}`;
4951

50-
if (!accountStore.address) {
51-
return (
52-
<WalletContainer data-onboarding={dataOnboardingConnectKey}>
53-
<Button fitContent green onClick={openConnectDialog}>
54-
Connect wallet
55-
</Button>
56-
</WalletContainer>
57-
);
58-
}
59-
60-
if (media.mobile) {
61-
return (
62-
<WalletContainer data-onboarding={dataOnboardingConnectKey} isVisible={!isMobileMenuOpen}>
63-
<ConnectedWalletButton onClick={openAccountInfo} />
64-
</WalletContainer>
65-
);
66-
}
52+
const walletButtonContent = media.mobile ? <WalletAddressButton onClick={openAccountInfo} /> : <WalletButton />;
6753

6854
return (
69-
<WalletContainer data-onboarding={dataOnboardingConnectKey}>
70-
<ConnectedWallet />
55+
<WalletContainer data-onboarding={dataOnboardingConnectKey} isVisible={!isMobileMenuOpen}>
56+
<ConnectWalletButton fitContent>{walletButtonContent}</ConnectWalletButton>
7157
</WalletContainer>
7258
);
7359
};
@@ -123,11 +109,8 @@ const Header: React.FC = observer(() => {
123109
isOpen={isMobileMenuOpen}
124110
onAccountClick={openAccountInfo}
125111
onClose={closeMobileMenu}
126-
onWalletConnect={openConnectDialog}
112+
onWalletConnect={openConnectModal}
127113
/>
128-
{isConnectDialogVisible ? (
129-
<ConnectWalletDialog visible={isConnectDialogVisible} onClose={closeConnectDialog} />
130-
) : null}
131114
<AccountInfoSheet isOpen={isAccountInfoSheetOpen} onClose={closeAccountInfo} />
132115
</Root>
133116
);

src/components/Header/MobileMenu.tsx

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
import React from "react";
2-
import { useLocation } from "react-router-dom";
32
import styled from "@emotion/styled";
43
import { observer } from "mobx-react-lite";
54

6-
import Text from "@components/Text";
75
import { useStores } from "@src/stores";
8-
import { media } from "@src/themes/breakpoints";
96

107
import Button from "../Button";
118
import MenuOverlay from "../MenuOverlay";
129
import SizedBox from "../SizedBox";
1310
import { SmartFlex } from "../SmartFlex";
1411

15-
import ConnectedWalletButton from "./ConnectedWalletButton";
1612
import { MenuNav } from "./MenuNav";
13+
import WalletAddressButton from "./WalletAddressButton";
1714

1815
interface IProps {
1916
isOpen: boolean;
@@ -23,8 +20,7 @@ interface IProps {
2320
}
2421

2522
const MobileMenu: React.FC<IProps> = observer(({ isOpen, onAccountClick, onWalletConnect, onClose }) => {
26-
const { accountStore, quickAssetsStore, mixPanelStore } = useStores();
27-
const location = useLocation();
23+
const { accountStore, quickAssetsStore } = useStores();
2824

2925
const handleAccountClick = () => {
3026
onAccountClick();
@@ -36,9 +32,9 @@ const MobileMenu: React.FC<IProps> = observer(({ isOpen, onAccountClick, onWalle
3632
onClose();
3733
};
3834

39-
const renderWalletButton = () => {
35+
const renderWalletAddressButton = () => {
4036
return accountStore.address ? (
41-
<ConnectedWalletButtonStyled onClick={handleAccountClick} />
37+
<WalletAddressButtonStyled onClick={handleAccountClick} />
4238
) : (
4339
<Button green onClick={handleConnectWallet}>
4440
Connect wallet
@@ -57,7 +53,7 @@ const MobileMenu: React.FC<IProps> = observer(({ isOpen, onAccountClick, onWalle
5753
<Button data-onboarding="assets-mobile" onClick={() => quickAssetsStore.setQuickAssets(true)}>
5854
ASSETS
5955
</Button>
60-
{renderWalletButton()}
56+
{renderWalletAddressButton()}
6157
</FooterContainer>
6258
</Body>
6359
</MenuOverlay>
@@ -74,19 +70,6 @@ const Body = styled.div`
7470
background: ${({ theme }) => theme.colors.bgPrimary};
7571
`;
7672

77-
const MenuItem = styled.div<{ isSelected?: boolean }>`
78-
cursor: pointer;
79-
padding: 12px 32px;
80-
81-
${Text} {
82-
width: fit-content;
83-
color: ${({ theme, isSelected }) => (isSelected ? theme.colors.textPrimary : theme.colors.textSecondary)};
84-
${media.mobile} {
85-
font-size: 16px;
86-
}
87-
}
88-
`;
89-
9073
const Container = styled(SmartFlex)`
9174
flex-direction: column;
9275
background: ${({ theme }) => `${theme.colors.bgSecondary}`};
@@ -103,7 +86,7 @@ const FooterContainer = styled(SmartFlex)`
10386
width: 100%;
10487
`;
10588

106-
const ConnectedWalletButtonStyled = styled(ConnectedWalletButton)`
89+
const WalletAddressButtonStyled = styled(WalletAddressButton)`
10790
width: 100%;
10891
height: 40px;
10992
`;

src/components/Header/ConnectedWalletButton.tsx renamed to src/components/Header/WalletAddressButton.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ interface Props {
1515
onClick?: () => void;
1616
}
1717

18-
const ConnectedWalletButton: React.FC<Props> = observer(({ isFocused, className, onClick }) => {
18+
const WalletAddressButton: React.FC<Props> = observer(({ isFocused, className, onClick }) => {
1919
const { accountStore } = useStores();
2020

2121
return (
@@ -26,7 +26,7 @@ const ConnectedWalletButton: React.FC<Props> = observer(({ isFocused, className,
2626
);
2727
});
2828

29-
export default ConnectedWalletButton;
29+
export default WalletAddressButton;
3030

3131
const ArrowIconStyled = styled.img`
3232
transition: 0.4s;

src/components/Header/ConnectedWallet.tsx renamed to src/components/WalletButton.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ import BN from "@src/utils/BN";
1616
import { getExplorerLinkByAddress } from "@src/utils/getExplorerLink";
1717
import { useStores } from "@stores";
1818

19-
import ConnectedWalletButton from "./ConnectedWalletButton";
19+
import WalletAddressButton from "./Header/WalletAddressButton";
2020

21-
const ConnectedWallet: React.FC = observer(() => {
21+
const WalletButton: React.FC = observer(() => {
2222
const { accountStore, notificationStore, balanceStore } = useStores();
2323
const { disconnect: disconnectWallet } = useWallet();
2424

@@ -89,12 +89,12 @@ const ConnectedWallet: React.FC = observer(() => {
8989
</Column>
9090
}
9191
>
92-
<ConnectedWalletButton isFocused={isFocused} />
92+
<WalletAddressButton isFocused={isFocused} />
9393
</Tooltip>
9494
);
9595
});
9696

97-
export default ConnectedWallet;
97+
export default WalletButton;
9898

9999
const Icon = styled.img`
100100
width: 24px;

src/hooks/useWallet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { useStores } from "@src/stores";
77
export const useWallet = () => {
88
const { fuel } = useFuel();
99
const { accountStore } = useStores();
10-
const { connect, isConnecting } = useConnectUI();
10+
const { connect, isConnecting, dialog } = useConnectUI();
1111

1212
const [isConnected, setIsConnected] = useState(false);
1313
const [wallet, setWallet] = useState<Account | null>(null);

src/screens/ConnectWallet/ConnectWalletDialog.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ type IProps = Omit<IDialogPropTypes, "onClose"> & {
1616
visible: boolean;
1717
};
1818

19-
// TODO: refactor account store, minting and save address in local storage
20-
2119
const ConnectWalletDialog: React.FC<IProps> = observer(({ onClose, visible }) => {
2220
const { settingsStore, mixPanelStore } = useStores();
2321
const theme = useTheme();
@@ -61,6 +59,8 @@ const ConnectWalletDialog: React.FC<IProps> = observer(({ onClose, visible }) =>
6159
}
6260
};
6361

62+
if (!visible) return;
63+
6464
return (
6565
<Dialog
6666
title={

src/screens/SpotScreen/RightBlock/CreateOrder/CreateOrder.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import Slider from "@components/Slider";
1212
import Text, { TEXT_TYPES } from "@components/Text";
1313
import TokenInput from "@components/TokenInput";
1414
import Button, { ButtonGroup } from "@src/components/Button";
15+
import { ConnectWalletButton } from "@src/components/ConnectWalletButton";
1516
import SizedBox from "@src/components/SizedBox";
1617
import { SmartFlex } from "@src/components/SmartFlex";
1718
import { DEFAULT_DECIMALS, MINIMAL_ETH_REQUIRED } from "@src/constants";
@@ -206,8 +207,8 @@ const CreateOrder: React.FC = observer(() => {
206207
<TokenInput
207208
amount={vm.inputPrice}
208209
decimals={DEFAULT_DECIMALS}
209-
displayDecimals={priceDisplayDecimals}
210210
disabled={isInputPriceDisabled}
211+
displayDecimals={priceDisplayDecimals}
211212
label="Price"
212213
setAmount={handleSetPrice}
213214
onBlur={vm.setActiveInput}
@@ -264,7 +265,7 @@ const CreateOrder: React.FC = observer(() => {
264265
</SliderContainer>
265266
{renderOrderDetails()}
266267
</ParamsContainer>
267-
{renderButton()}
268+
<ConnectWalletButton connectText="Connect wallet to trade">{renderButton()}</ConnectWalletButton>
268269

269270
<OrderTypeSheet isOpen={isOrderTooltipOpen} onClose={closeOrderTooltip} />
270271
</Root>

0 commit comments

Comments
 (0)