Skip to content
This repository has been archived by the owner on Jan 24, 2023. It is now read-only.

Commit

Permalink
Disable auto network switch/add user network switch (#610)
Browse files Browse the repository at this point in the history
* -auto network switch prompt
+ui for switching network/app

* Remove `PoolAccordion`
Rename `Vault-ConnectWallet` string to `Network-ConnectWallet`
Remove `Network-Error` string

* fix fantom app url

* add manual wallet switch note

* Remove Network-Error and rename Vault-ConnectWallet for newly added pl translations
  • Loading branch information
ReflectiveChimp authored Jul 6, 2021
1 parent 859936c commit 629bf04
Show file tree
Hide file tree
Showing 30 changed files with 212 additions and 159 deletions.
4 changes: 2 additions & 2 deletions src/components/Header/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Menu from '@material-ui/icons/Menu';
import Close from '@material-ui/icons/Close';
import WbSunny from '@material-ui/icons/WbSunny';
import NightsStay from '@material-ui/icons/NightsStay';
import { getNetworkBuyLink } from '../../features/helpers/getNetworkData';
import { getNetworkBuyUrl } from '../../features/helpers/getNetworkData';
import styles from './styles';

const useStyles = makeStyles(styles);
Expand Down Expand Up @@ -134,7 +134,7 @@ const LinkSidebar = ({ name, label, icon, classes }) => (
);

const getLinkUrl = name => {
return name === 'buy' ? getNetworkBuyLink() : `https://${name}.beefy.finance`;
return name === 'buy' ? getNetworkBuyUrl() : `https://${name}.beefy.finance`;
};

export default Header;
108 changes: 108 additions & 0 deletions src/components/NetworkConnectNotice/NetworkConnectNotice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import React, { useCallback, useMemo, useState } from 'react';
import Button from '@material-ui/core/Button';
import { networkSettings, networkSetup } from 'common/networkSetup';
import { getNetworkAppUrl, getNetworkFriendlyName } from 'features/helpers/getNetworkData';
import { makeStyles } from '@material-ui/core/styles';
import styles from './styles';
import { useTranslation } from 'react-i18next';

const useStyles = makeStyles(styles);
const targetNetworkId = Number(process.env.REACT_APP_NETWORK_ID);

export function NetworkConnectNotice({
web3,
address,
networkId,
connectWallet,
disconnectWallet,
}) {
const [networkSetupError, setNetworkSetupError] = useState(null);
const { t } = useTranslation();
const haveConnection = !!web3;
const haveAddress = !!address;
const isCorrectNetwork = networkId === targetNetworkId;
const isSupportedNetwork = networkId && networkId in networkSettings;
const targetNetworkFriendlyName = getNetworkFriendlyName();
const classes = useStyles();
let notice = null;

const targetNetworkSetup = useCallback(() => {
setNetworkSetupError(null);

networkSetup(targetNetworkId)
.then(() => {
setNetworkSetupError(null);
})
.catch(e => {
if (typeof e === 'object' && typeof e.message === 'string') {
setNetworkSetupError(e.message);
} else if (typeof e === 'string') {
setNetworkSetupError(e);
} else {
setNetworkSetupError(t('Network-UnknownError'));
}
});
}, [setNetworkSetupError, t]);

const supportedNetwork = useMemo(() => {
return isSupportedNetwork
? {
id: networkId,
app: getNetworkAppUrl(networkId),
name: getNetworkFriendlyName(networkId),
}
: null;
}, [isSupportedNetwork, networkId]);

if (!haveConnection) {
notice = (
<>
<div className={classes.message}>
{t('Network-ConnectionRequired', { network: targetNetworkFriendlyName })}
</div>
<div className={classes.actions}>
<Button onClick={connectWallet} className={classes.button}>
{t('Network-ConnectWallet')}
</Button>
</div>
</>
);
} else if (!isCorrectNetwork) {
notice = (
<>
<div className={classes.message}>
{t('Network-Supports', { network: targetNetworkFriendlyName })}{' '}
{isSupportedNetwork
? t('Network-ConnectedTo', { network: supportedNetwork.name })
: t('Network-ConnectedUnsupported')}
</div>
<div className={classes.actions}>
<Button onClick={targetNetworkSetup} className={classes.button}>
{t('Network-SwitchToNetwork', { network: targetNetworkFriendlyName })}
</Button>
{isSupportedNetwork ? (
<Button href={supportedNetwork.app} className={classes.button}>
{t('Network-GoToApp', { network: supportedNetwork.name })}
</Button>
) : null}
<Button onClick={disconnectWallet} className={classes.button}>
{t('Network-DisconnectWallet')}
</Button>
</div>
<div className={classes.note}>{t('Network-SwitchNote')}</div>
{networkSetupError ? <div className={classes.error}>{networkSetupError}</div> : ''}
</>
);
} else if (!haveAddress) {
notice = (
<>
<div className={classes.message}>
{t('Network-ConnectedTo', { network: targetNetworkFriendlyName })}
</div>
<div className={classes.error}>{t('Network-NoWalletAddress')}</div>
</>
);
}

return notice ? <div className={classes.notice}>{notice}</div> : null;
}
34 changes: 34 additions & 0 deletions src/components/NetworkConnectNotice/styles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const styles = theme => ({
notice: {
backgroundColor: theme.palette.background.secondary,
padding: 25,
marginBottom: 25,
textAlign: 'center',
color: theme.palette.primary.main,
'& > :last-child': {
marginBottom: 0,
},
},
message: {
marginBottom: 15,
},
actions: {
margin: '-10px -10px 15px 0',
},
button: {
border: '1px solid ' + theme.palette.background.border,
padding: '4px 8px',
backgroundColor: theme.palette.background.default,
textTransform: 'none',
margin: '10px 10px 0 0',
},
note: {
marginBottom: 15,
fontStyle: 'italic',
},
error: {
color: 'red',
},
});

export default styles;
26 changes: 0 additions & 26 deletions src/components/NetworkError/NetworkError.js

This file was deleted.

19 changes: 0 additions & 19 deletions src/components/NetworkError/styles.js

This file was deleted.

20 changes: 16 additions & 4 deletions src/features/helpers/getNetworkData.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ const networkTxUrls = {
250: hash => `https://ftmscan.com/tx/${hash}`,
};

const networkAppUrls = {
56: 'https://app.beefy.finance/',
128: 'https://heco.beefy.finance/',
43114: 'https://avax.beefy.finance/',
137: 'https://polygon.beefy.finance/',
250: 'https://fantom.beefy.finance/',
};

const networkFriendlyName = {
56: 'BSC',
128: 'HECO',
Expand All @@ -41,7 +49,7 @@ const networkFriendlyName = {
250: 'Fantom',
};

const networkBuyLinks = {
const networkBuyUrls = {
56: 'https://app.1inch.io/#/r/0xF4cb25a1FF50E319c267b3E51CBeC2699FB2A43B',
128: 'https://ht.mdex.com/#/swap?inputCurrency=0xa71edc38d189767582c38a3145b5873052c3e47a&outputCurrency=0x765277eebeca2e31912c9946eae1021199b39c61',
137: 'https://app.1inch.io/#/r/0xF4cb25a1FF50E319c267b3E51CBeC2699FB2A43B',
Expand All @@ -50,7 +58,7 @@ const networkBuyLinks = {
};

export const getNetworkCoin = () => {
return nativeCoins.find(coin => coin.chainId == process.env.REACT_APP_NETWORK_ID);
return nativeCoins.find(coin => coin.chainId === Number(process.env.REACT_APP_NETWORK_ID));
};

export const getNetworkPools = () => {
Expand Down Expand Up @@ -361,5 +369,9 @@ export const getNetworkConnectors = t => {
};

export const getNetworkTxUrl = networkTxUrls[process.env.REACT_APP_NETWORK_ID];
export const getNetworkFriendlyName = () => networkFriendlyName[process.env.REACT_APP_NETWORK_ID];
export const getNetworkBuyLink = () => networkBuyLinks[process.env.REACT_APP_NETWORK_ID];
export const getNetworkFriendlyName = (networkId = process.env.REACT_APP_NETWORK_ID) =>
networkFriendlyName[networkId];
export const getNetworkBuyUrl = (networkId = process.env.REACT_APP_NETWORK_ID) =>
networkBuyUrls[networkId];
export const getNetworkAppUrl = (networkId = process.env.REACT_APP_NETWORK_ID) =>
networkAppUrls[networkId];
44 changes: 21 additions & 23 deletions src/features/home/App.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import React, { useEffect, useMemo, useState } from 'react';
import { ThemeProvider, StylesProvider } from '@material-ui/core/styles';
import { makeStyles } from '@material-ui/core/styles';
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { makeStyles, ThemeProvider, StylesProvider } from '@material-ui/core/styles';
import Header from 'components/Header/Header';
import HeaderLinks from 'components/HeaderLinks/HeaderLinks';
import NetworksProvider from 'components/NetworksProvider/NetworksProvider';
import NetworksModal from 'components/NetworksModal/NetworksModal';
import NetworkError from 'components/NetworkError/NetworkError';
import { useTranslation } from 'react-i18next';
import { SnackbarProvider } from 'notistack';
import { Notifier } from 'features/common';
import Footer from 'components/Footer/Footer';
import Pastures from 'components/Pastures/Pastures';
import { NetworkConnectNotice } from 'components/NetworkConnectNotice/NetworkConnectNotice';
import appStyle from './jss/appStyle.js';
import { createWeb3Modal } from '../web3';
import { useConnectWallet, useDisconnectWallet } from './redux/hooks';
import useNightMode from './hooks/useNightMode';
import createTheme from './jss/appTheme';
import { networkSetup } from 'common/networkSetup';

const themes = { light: null, dark: null };
const getTheme = mode => {
Expand All @@ -25,11 +23,10 @@ const getTheme = mode => {

export default function App({ children }) {
const { t } = useTranslation();
const { connectWallet, web3, address, networkId, connected, connectWalletPending } =
useConnectWallet();
const { connectWallet, web3, address, networkId, connected } = useConnectWallet();
const { disconnectWallet } = useDisconnectWallet();
const [web3Modal, setModal] = useState(null);
const [networkError, setNetworkError] = useState(null);

const { isNightMode, setNightMode } = useNightMode();
const theme = useMemo(() => getTheme(isNightMode ? 'dark' : 'light'), [isNightMode]);
const useStyles = useMemo(() => {
Expand All @@ -47,17 +44,13 @@ export default function App({ children }) {
}
}, [web3Modal, connectWallet]);

useEffect(() => {
if (
web3 &&
address &&
!connectWalletPending &&
networkId &&
Boolean(networkId !== Number(process.env.REACT_APP_NETWORK_ID))
) {
networkSetup(process.env.REACT_APP_NETWORK_ID).catch(setNetworkError);
}
}, [web3, address, networkId, connectWalletPending, t]);
const connectWalletCallback = useCallback(() => {
connectWallet(web3Modal);
}, [web3Modal, connectWallet]);

const disconnectWalletCallback = useCallback(() => {
disconnectWallet(web3, web3Modal);
}, [web3, web3Modal, disconnectWallet]);

return (
<StylesProvider injectFirst>
Expand All @@ -71,8 +64,8 @@ export default function App({ children }) {
<HeaderLinks
address={address}
connected={connected}
connectWallet={() => connectWallet(web3Modal)}
disconnectWallet={() => disconnectWallet(web3, web3Modal)}
connectWallet={connectWalletCallback}
disconnectWallet={disconnectWalletCallback}
isNightMode={isNightMode}
setNightMode={() => setNightMode(!isNightMode)}
/>
Expand All @@ -82,12 +75,17 @@ export default function App({ children }) {
/>
<div className={classes.container}>
<div className={classes.children}>
{networkError && <NetworkError network={process.env.REACT_APP_NETWORK_ID} />}
<NetworkConnectNotice
web3={web3}
address={address}
connectWallet={connectWalletCallback}
disconnectWallet={disconnectWalletCallback}
networkId={networkId}
/>
{Boolean(networkId === Number(process.env.REACT_APP_NETWORK_ID)) && children}
<Notifier />
</div>
</div>

<Footer />
<Pastures />
</div>
Expand Down
12 changes: 5 additions & 7 deletions src/features/vault/components/Pool/Pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import BigNumber from 'bignumber.js';

import { byDecimals } from 'features/helpers/bignumber';
import PoolSummary from '../PoolSummary/PoolSummary';
import PoolAccordion from '../PoolDetails/PoolAccordion';
import styles from './styles';
import { useSelector } from 'react-redux';
import PoolActions from '../PoolActions/PoolActions';
import AccordionDetails from '@material-ui/core/AccordionActions';

const useStyles = makeStyles(styles);

Expand Down Expand Up @@ -55,12 +56,9 @@ const Pool = ({
fetchVaultsDataDone={fetchVaultsDataDone}
/>
<Divider variant="middle" className={classes.divider} />
<PoolAccordion
pool={pool}
balanceSingle={balanceSingle}
sharesBalance={sharesBalance}
index={index}
/>
<AccordionDetails style={{ justifyContent: 'space-between' }}>
<PoolActions pool={pool} balanceSingle={balanceSingle} sharesBalance={sharesBalance} />
</AccordionDetails>
</Accordion>
</Grid>
);
Expand Down
Loading

0 comments on commit 629bf04

Please sign in to comment.