From 366809dd16cb1a3f8bc0aaceeb968a4476daae39 Mon Sep 17 00:00:00 2001 From: Nicho Date: Tue, 28 Nov 2023 14:46:20 -0600 Subject: [PATCH] Limit geoblocked users to legacy withdraw page --- .../components/Layouts/CoreLayout/Account.tsx | 5 ----- .../Layouts/V2Layout/Nav/WalletNav.tsx | 12 +----------- .../components/Layouts/V2Layout/V2Account.tsx | 6 ------ .../src/components/Layouts/V2Layout/index.tsx | 19 +++++++++++++++++-- .../Core/DappPages/Legacy/ClaimFromVaults.tsx | 12 +++++++++++- apps/dapp/src/hooks/use-geo-blocked.tsx | 5 ++++- 6 files changed, 33 insertions(+), 26 deletions(-) diff --git a/apps/dapp/src/components/Layouts/CoreLayout/Account.tsx b/apps/dapp/src/components/Layouts/CoreLayout/Account.tsx index f88c5415c..e446963e6 100644 --- a/apps/dapp/src/components/Layouts/CoreLayout/Account.tsx +++ b/apps/dapp/src/components/Layouts/CoreLayout/Account.tsx @@ -21,16 +21,11 @@ export const Account = () => { const isSmallDesktop = useMediaQuery({ query: queryVerySmallDesktop, }); - const { isBlocked } = useGeoBlocked(); if (connecting) { return ; } - if (isBlocked) { - return ; - } - if (wallet) { const disconnectButton = ( { const { isNavCollapsed, onClickHandler } = props; const [{ wallet, connecting }, connect, disconnect] = useConnectWallet(); const { walletAddress } = useWallet(); - const { isBlocked } = useGeoBlocked(); const [isWalletDropdownOpen, setIsWalletDropdownOpen] = useState(false); const [{ connectedChain }] = useSetChain(); @@ -46,8 +44,6 @@ export const WalletNav = (props: WalletNavProps) => { const explorerUrl = getChainExplorerURL(walletAddress || '', currentChainId); const connectWallet = async () => { - // TODO: What to do in this case? - if (isBlocked) return alert('Restricted Jurisdiction'); if (onClickHandler) onClickHandler(); await connect(); }; @@ -60,7 +56,7 @@ export const WalletNav = (props: WalletNavProps) => { return ( setIsWalletDropdownOpen(false)}> {connecting && } - {!connecting && !isBlocked && wallet ? ( + {!connecting && wallet ? ( setIsWalletDropdownOpen(!isWalletDropdownOpen)}> @@ -92,12 +88,6 @@ export const WalletNav = (props: WalletNavProps) => { {!isNavCollapsed && Connect} )} - {!connecting && isBlocked && ( - <> - - {!isNavCollapsed && Blocked} - - )} ); }; diff --git a/apps/dapp/src/components/Layouts/V2Layout/V2Account.tsx b/apps/dapp/src/components/Layouts/V2Layout/V2Account.tsx index 83dd3e3fd..051863127 100644 --- a/apps/dapp/src/components/Layouts/V2Layout/V2Account.tsx +++ b/apps/dapp/src/components/Layouts/V2Layout/V2Account.tsx @@ -18,21 +18,15 @@ export const V2Account = () => { const { walletAddress } = useWallet(); const [{ connectedChain }] = useSetChain(); const currentChainId = useMemo(() => parseInt(connectedChain?.id || '', 16), [connectedChain]); - const { isBlocked } = useGeoBlocked(); const isSmallDesktop = useMediaQuery({ query: queryVerySmallDesktop, }); - if (connecting) { return ; } - if (isBlocked) { - return ; - } - if (wallet) { const disconnectButton = ( { query: queryMinTablet, }); const loc = useLocation(); + const navigate = useNavigate(); + const { isBlocked, loading } = useGeoBlocked(); const [menuNavItems, setMenuNavItems] = useState>([ { label: 'Trade', @@ -71,6 +74,18 @@ const V2Layout = () => { }, ]); + // Handle geoblocked users + useEffect(() => { + // Define all permitted paths + const permittedPaths = ['/dapp/legacy']; + if (loading || (!loading && !isBlocked)) return; + // Force redirect to permitted path + if (!permittedPaths.find((path) => path === loc.pathname)) navigate(permittedPaths[0]); + // Remove nav items that are not permitted + const newMenuNavItems = menuNavItems.filter((menuItem) => permittedPaths.find((path) => path === menuItem.linkTo)); + setMenuNavItems(newMenuNavItems); + }, [loading, isBlocked, loc]); + const onSelectMenuNavItems = async (selectedMenuItem: MenuNavItem) => { await setMenuNavItems((prevSelectedMenuNavItems) => { const newSelectedMenuNavItems = prevSelectedMenuNavItems.map((prevMenuItem) => { diff --git a/apps/dapp/src/components/Pages/Core/DappPages/Legacy/ClaimFromVaults.tsx b/apps/dapp/src/components/Pages/Core/DappPages/Legacy/ClaimFromVaults.tsx index 1454c416d..1d0c0f14c 100644 --- a/apps/dapp/src/components/Pages/Core/DappPages/Legacy/ClaimFromVaults.tsx +++ b/apps/dapp/src/components/Pages/Core/DappPages/Legacy/ClaimFromVaults.tsx @@ -10,6 +10,8 @@ import { VaultButton } from '../../VaultPages/VaultContent'; import { useTokenContractAllowance } from 'hooks/core/use-token-contract-allowance'; import env from 'constants/env'; import _ from 'lodash'; +import { useConnectWallet } from '@web3-onboard/react'; +import { TradeButton } from '../../NewUI/Home'; const EMPTY_CLAIM_STATE = { claimSubvaultAddress: '', @@ -17,6 +19,7 @@ const EMPTY_CLAIM_STATE = { }; export const ClaimFromVaults = () => { + const [{ wallet }, connect] = useConnectWallet(); const { balances: { balances, isLoading: balancesIsLoading }, vaultGroups: { vaultGroups, isLoading: vaultGroupsIsLoading }, @@ -103,7 +106,14 @@ export const ClaimFromVaults = () => { )} - {Number(claimState.claimAmount) === 0 ? ( + {!wallet ? ( + { + connect(); + }} + /> + ) : Number(claimState.claimAmount) === 0 ? ( { const [isBlocked, setIsBlocked] = useState(false); - + const [loading, setLoading] = useState(true); + useEffect(() => { const checkBlocked = async () => { const blocked = await fetch(`${window.location.href}api/geoblock`) @@ -10,11 +11,13 @@ export const useGeoBlocked = () => { .then((res) => res.blocked) .catch(() => false); setIsBlocked(blocked); + setLoading(false); }; checkBlocked(); }, []); return { isBlocked, + loading, }; };