Skip to content

Commit

Permalink
fix: provider is by default null and account page redirect to provide…
Browse files Browse the repository at this point in the history
…r setup (#437)
  • Loading branch information
vojtechsimetka committed Jun 24, 2022
1 parent a756eed commit 2e0eeb7
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 42 deletions.
54 changes: 28 additions & 26 deletions src/components/SideBar.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Divider, Drawer, Grid, Link as MUILink, List } from '@material-ui/core'
import { createStyles, makeStyles, Theme } from '@material-ui/core/styles'
import { ReactElement } from 'react'
import { ReactElement, useContext } from 'react'
import { Link } from 'react-router-dom'
import FilesIcon from 'remixicon-react/ArrowUpDownLineIcon'
import DocsIcon from 'remixicon-react/BookOpenLineIcon'
import ExternalLinkIcon from 'remixicon-react/ExternalLinkLineIcon'
import HomeIcon from 'remixicon-react/Home3LineIcon'
import SettingsIcon from 'remixicon-react/Settings2LineIcon'
import AccountIcon from 'remixicon-react/Wallet3LineIcon'
import { Context as TopUpContext } from '../providers/TopUp'
import DashboardLogo from '../assets/dashboard-logo.svg'
import DesktopLogo from '../assets/desktop-logo.svg'
import { config } from '../config'
Expand All @@ -17,31 +18,6 @@ import Feedback from './Feedback'
import SideBarItem from './SideBarItem'
import SideBarStatus from './SideBarStatus'

const navBarItems = [
{
label: 'Info',
path: ROUTES.INFO,
icon: HomeIcon,
},
{
label: 'Files',
path: ROUTES.UPLOAD,
icon: FilesIcon,
pathMatcherSubstring: '/files/',
},
{
label: 'Account',
path: ROUTES.ACCOUNT_WALLET,
icon: AccountIcon,
pathMatcherSubstring: '/account/',
},
{
label: 'Settings',
path: ROUTES.SETTINGS,
icon: SettingsIcon,
},
]

const drawerWidth = 300

const useStyles = makeStyles((theme: Theme) =>
Expand Down Expand Up @@ -91,6 +67,32 @@ const useStyles = makeStyles((theme: Theme) =>
export default function SideBar(): ReactElement {
const classes = useStyles()
const { isBeeDesktop } = useIsBeeDesktop()
const { providerUrl } = useContext(TopUpContext)

const navBarItems = [
{
label: 'Info',
path: ROUTES.INFO,
icon: HomeIcon,
},
{
label: 'Files',
path: ROUTES.UPLOAD,
icon: FilesIcon,
pathMatcherSubstring: '/files/',
},
{
label: 'Account',
path: providerUrl === null ? ROUTES.WALLET : ROUTES.ACCOUNT_WALLET,
icon: AccountIcon,
pathMatcherSubstring: '/account/',
},
{
label: 'Settings',
path: ROUTES.SETTINGS,
icon: SettingsIcon,
},
]

return (
<Drawer className={classes.drawer} variant="permanent" anchor="left" classes={{ paper: classes.drawerPaper }}>
Expand Down
1 change: 1 addition & 0 deletions src/pages/gift-code/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default function Index(): ReactElement {

useEffect(() => {
async function mapGiftWallets() {
if (!provider) return
const results = []
for (const giftWallet of giftWallets) {
results.push(await ResolvedWallet.make(giftWallet, provider))
Expand Down
4 changes: 3 additions & 1 deletion src/pages/rpc/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export default function Index(): ReactElement {
const navigate = useNavigate()

async function onSubmit() {
if (!localProviderUrl) return

try {
await Rpc.eth_getBlockByNumber(new providers.JsonRpcProvider(localProviderUrl))
enqueueSnackbar('Connected to RPC provider successfully.', { variant: 'success' })
Expand Down Expand Up @@ -54,7 +56,7 @@ export default function Index(): ReactElement {
name="rpc-endpoint"
label="RPC Endpoint"
onChange={event => setLocalProviderUrl(event.target.value)}
defaultValue={providerUrl}
defaultValue={providerUrl || ''}
/>
</Box>
<SwarmButton iconType={Check} onClick={onSubmit}>
Expand Down
4 changes: 2 additions & 2 deletions src/pages/top-up/GiftCardFund.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function GiftCardFund(): ReactElement {
const navigate = useNavigate()

useEffect(() => {
if (!privateKeyString) {
if (!privateKeyString || !provider) {
return
}

Expand All @@ -43,7 +43,7 @@ export function GiftCardFund(): ReactElement {
}

async function onFund() {
if (!wallet || !nodeAddresses) {
if (!wallet || !nodeAddresses || !providerUrl) {
return
}

Expand Down
2 changes: 2 additions & 0 deletions src/pages/top-up/GiftCardTopUpIndex.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export function GiftCardTopUpIndex(): ReactElement {
const navigate = useNavigate()

async function onProceed() {
if (!provider) return

setLoading(true)
try {
const wallet = new Wallet(giftCode, provider)
Expand Down
2 changes: 1 addition & 1 deletion src/pages/top-up/Swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function Swap({ header }: Props): ReactElement {
const bzzAfterSwap = new BzzToken(daiToSwap.toBigNumber.dividedToIntegerBy(200))

async function onSwap() {
if (hasSwapped) {
if (hasSwapped || !providerUrl) {
return
}
setLoading(true)
Expand Down
2 changes: 1 addition & 1 deletion src/providers/Bee.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ export function Provider({ children }: Props): ReactElement {
}, [beeDebugApi]) // eslint-disable-line react-hooks/exhaustive-deps

useEffect(() => {
if (nodeAddresses?.ethereum) {
if (nodeAddresses?.ethereum && provider) {
WalletAddress.make(nodeAddresses.ethereum, provider).then(setWalletAddress)
}
}, [nodeAddresses, provider])
Expand Down
16 changes: 10 additions & 6 deletions src/providers/TopUp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ const LocalStorageKeys = {
}

interface ContextInterface {
providerUrl: string
provider: providers.JsonRpcProvider
providerUrl: string | null
provider: providers.JsonRpcProvider | null
giftWallets: Wallet[]
setProviderUrl: (providerUrl: string) => void
addGiftWallet: (wallet: Wallet) => void
}

const providerUrl = localStorage.getItem('json-rpc-provider') || null

const initialValues: ContextInterface = {
providerUrl: '',
provider: new providers.JsonRpcProvider(),
providerUrl,
provider: providerUrl ? new providers.JsonRpcProvider(providerUrl) : null,
giftWallets: [],
setProviderUrl: () => {}, // eslint-disable-line
addGiftWallet: () => {}, // eslint-disable-line
Expand All @@ -33,11 +35,13 @@ interface Props {
}

export function Provider({ children }: Props): ReactElement {
const [providerUrl, setProviderUrl] = useState(localStorage.getItem('json-rpc-provider') || initialValues.providerUrl)
const [provider, setProvider] = useState(new providers.JsonRpcProvider(providerUrl))
const [providerUrl, setProviderUrl] = useState(initialValues.providerUrl)
const [provider, setProvider] = useState(initialValues.provider)
const [giftWallets, setGiftWallets] = useState(initialValues.giftWallets)

useEffect(() => {
if (provider === null) return

const existingGiftWallets = localStorage.getItem(LocalStorageKeys.giftWallets)

if (existingGiftWallets) {
Expand Down
5 changes: 0 additions & 5 deletions src/utils/desktop.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import axios from 'axios'
import { getJson, postJson, sendRequest } from './net'

interface DesktopStatus {
Expand All @@ -16,10 +15,6 @@ export async function getDesktopStatus(): Promise<DesktopStatus> {
return response as DesktopStatus
}

export async function getGasFromFaucet(address: string): Promise<void> {
await axios.post(`http://getxdai.co/${address}/0.1`)
}

export async function upgradeToLightNode(rpcProvider: string): Promise<void> {
await updateDesktopConfiguration({
'chain-enable': true,
Expand Down

0 comments on commit 2e0eeb7

Please sign in to comment.