From b798fa0e68b367fe324ef64507b1405b642da6e0 Mon Sep 17 00:00:00 2001 From: Cafe137 <77121044+Cafe137@users.noreply.github.com> Date: Thu, 24 Nov 2022 14:06:00 +0100 Subject: [PATCH] fix: always set rpc to newly provided value in desktop (#591) * fix: always set rpc to newly provided value in desktop * fix: always set new rpc and restart node * fix: disable version check in desktop mode * fix: only set rpc in desktop when in light mode * refactor: simplify code --- src/App.tsx | 4 ++-- src/pages/settings/index.tsx | 17 ++++++++--------- src/providers/Bee.tsx | 14 ++++++++++++-- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index fd93903..6c85f56 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,7 +1,7 @@ import CssBaseline from '@material-ui/core/CssBaseline' import { ThemeProvider } from '@material-ui/core/styles' import { SnackbarProvider } from 'notistack' -import React, { ReactElement } from 'react' +import { ReactElement } from 'react' import { HashRouter as Router } from 'react-router-dom' import './App.css' import Dashboard from './layout/Dashboard' @@ -47,7 +47,7 @@ const App = ({ desktopUrl={desktopUrl} > - + diff --git a/src/pages/settings/index.tsx b/src/pages/settings/index.tsx index 784cac4..63206f5 100644 --- a/src/pages/settings/index.tsx +++ b/src/pages/settings/index.tsx @@ -1,12 +1,11 @@ import CircularProgress from '@material-ui/core/CircularProgress' +import { useSnackbar } from 'notistack' import { ReactElement, useContext } from 'react' import ExpandableList from '../../components/ExpandableList' import ExpandableListItemInput from '../../components/ExpandableListItemInput' import { Context as BeeContext } from '../../providers/Bee' import { Context as SettingsContext } from '../../providers/Settings' -import { useSnackbar } from 'notistack' -import { restartBeeNode, setJsonRpcInDesktop } from '../../utils/desktop' -import { BeeModes } from '@ethersphere/bee-js' +import { getDesktopConfiguration, restartBeeNode, setJsonRpcInDesktop } from '../../utils/desktop' export default function SettingsPage(): ReactElement { const { @@ -24,17 +23,17 @@ export default function SettingsPage(): ReactElement { desktopUrl, setAndPersistJsonRpcProvider, } = useContext(SettingsContext) - const { refresh, nodeInfo } = useContext(BeeContext) + const { refresh } = useContext(BeeContext) const { enqueueSnackbar, closeSnackbar } = useSnackbar() async function handleSetRpcUrl(value: string) { try { setAndPersistJsonRpcProvider(value) - // We can't set the RPC URL to the `swap-endpoint` Bee config value unless the Bee node is already in - // light mode as setting this config value, basically upgrades the node to light mode. - if (isDesktop && nodeInfo?.beeMode === BeeModes.LIGHT) { - await setJsonRpcInDesktop(desktopUrl, rpcProviderUrl) + const shouldUpdateDesktop = isDesktop && (await getDesktopConfiguration(desktopUrl))['swap-endpoint'] + + if (shouldUpdateDesktop) { + await setJsonRpcInDesktop(desktopUrl, value) const snackKey = enqueueSnackbar('RPC endpoint successfully changed, restarting Bee node...', { variant: 'success', }) @@ -48,7 +47,7 @@ export default function SettingsPage(): ReactElement { await refresh() } catch (e) { console.error(e) //eslint-disable-line - enqueueSnackbar(`Failed to change RPC endpoint. Error: ${e}`, { variant: 'error' }) + enqueueSnackbar(`Failed to change RPC endpoint. ${e}`, { variant: 'error' }) } } diff --git a/src/providers/Bee.tsx b/src/providers/Bee.tsx index 6177452..47238c9 100644 --- a/src/providers/Bee.tsx +++ b/src/providers/Bee.tsx @@ -127,11 +127,12 @@ function getStatus( chequebookAddress: ChequebookAddressResponse | null, chequebookBalance: ChequebookBalance | null, error: Error | null, + isDesktop: boolean, ): Status { const status: Status = { ...initialValues.status } // Version check - status.version.isEnabled = true + status.version.isEnabled = !isDesktop status.version.checkState = debugApiHealth && semver.satisfies(debugApiHealth.version, PackageJson.engines.bee, { @@ -194,7 +195,15 @@ function determineOverallStatus(debugApiHealth: Health | null, debugApiReadiness // This does not need to be exposed and works much better as variable than state variable which may trigger some unnecessary re-renders let isRefreshing = false -export function Provider({ children }: Props): ReactElement { +interface InitialSettings { + isDesktop?: boolean +} + +interface Props extends InitialSettings { + children: ReactChild +} + +export function Provider({ children, isDesktop }: Props): ReactElement { const { beeApi, beeDebugApi } = useContext(SettingsContext) const [apiHealth, setApiHealth] = useState(false) const [debugApiHealth, setDebugApiHealth] = useState(null) @@ -403,6 +412,7 @@ export function Provider({ children }: Props): ReactElement { chequebookAddress, chequebookBalance, error, + Boolean(isDesktop), ) useEffect(() => {