From 87b0b71cc63098a5d886ff47d52715c250d1b659 Mon Sep 17 00:00:00 2001 From: Vojtech Simetka Date: Fri, 29 Apr 2022 12:30:46 +0500 Subject: [PATCH] feat: add bee-desktop settings capabilities (#323) * refactor: make the config readonly and extract endpoint calls to hook (+2 squashed commits) Squashed commits: [91ffe45] feat: add swap-endpoint [e1d0c3a] feat: add bee-desktop settings capabilities * feat: use the request mechanism that uses the bee-desktop API key * fix: properly reset the error or on error set the config to null --- src/components/ExpandableListItemInput.tsx | 6 ++- src/hooks/apiHooks.tsx | 49 ++++++++++++++++++++++ src/pages/settings/index.tsx | 30 ++++++++++++- src/providers/Settings.tsx | 15 ++++++- src/utils/net.ts | 4 +- 5 files changed, 96 insertions(+), 8 deletions(-) diff --git a/src/components/ExpandableListItemInput.tsx b/src/components/ExpandableListItemInput.tsx index b681f62..08b8bed 100644 --- a/src/components/ExpandableListItemInput.tsx +++ b/src/components/ExpandableListItemInput.tsx @@ -55,7 +55,7 @@ interface Props { confirmLabelDisabled?: boolean loading?: boolean onChange?: (value: string) => void - onConfirm: (value: string) => void + onConfirm?: (value: string) => void mapperFn?: (value: string) => string locked?: boolean } @@ -138,7 +138,9 @@ export default function ExpandableListItemKey({ } loading={loading} iconType={Search} - onClick={() => onConfirm(inputValue)} + onClick={() => { + if (onConfirm) onConfirm(inputValue) + }} > {confirmLabel || 'Save'} diff --git a/src/hooks/apiHooks.tsx b/src/hooks/apiHooks.tsx index f6cf714..c7e5253 100644 --- a/src/hooks/apiHooks.tsx +++ b/src/hooks/apiHooks.tsx @@ -1,6 +1,7 @@ import axios from 'axios' import { useEffect, useState } from 'react' import { config } from '../config' +import { getJson } from '../utils/net' export interface LatestBeeReleaseHook { latestBeeRelease: LatestBeeRelease | null @@ -44,6 +45,54 @@ export const useIsBeeDesktop = (conf: Config = config): IsBeeDesktopHook => { return { isBeeDesktop, isLoading } } +export interface BeeConfig { + 'api-addr': string + 'debug-api-addr': string + 'debug-api-enable': boolean + password: string + 'swap-enable': boolean + 'swap-initial-deposit': bigint + mainnet: boolean + 'full-node': boolean + 'chain-enable': boolean + 'cors-allowed-origins': string + 'resolver-options': string + 'use-postage-snapshot': boolean + 'data-dir': string + transaction: string + 'block-hash': string + 'swap-endpoint'?: string +} + +export interface GetBeeConfig { + config: BeeConfig | null + isLoading: boolean + error: Error | null +} + +export const useGetBeeConfig = (conf: Config = config): GetBeeConfig => { + const [beeConfig, setBeeConfig] = useState(null) + const [isLoading, setLoading] = useState(true) + const [error, setError] = useState(null) + + useEffect(() => { + getJson(`${conf.BEE_DESKTOP_URL}/config`) + .then(beeConf => { + setBeeConfig(beeConf) + setError(null) + }) + .catch((err: Error) => { + setError(err) + setBeeConfig(null) + }) + .finally(() => { + setLoading(false) + }) + }, [conf]) + + return { config: beeConfig, isLoading, error } +} + export const useLatestBeeRelease = (): LatestBeeReleaseHook => { const [latestBeeRelease, setLatestBeeRelease] = useState(null) const [isLoadingLatestBeeRelease, setLoading] = useState(false) diff --git a/src/pages/settings/index.tsx b/src/pages/settings/index.tsx index af3bb63..21355a6 100644 --- a/src/pages/settings/index.tsx +++ b/src/pages/settings/index.tsx @@ -1,10 +1,36 @@ +import CircularProgress from '@material-ui/core/CircularProgress' import { ReactElement, useContext } from 'react' import ExpandableList from '../../components/ExpandableList' import ExpandableListItemInput from '../../components/ExpandableListItemInput' import { Context as SettingsContext } from '../../providers/Settings' -export default function Settings(): ReactElement { - const { apiUrl, apiDebugUrl, setApiUrl, setDebugApiUrl, lockedApiSettings } = useContext(SettingsContext) +export default function SettingsPage(): ReactElement { + const { apiUrl, apiDebugUrl, setApiUrl, setDebugApiUrl, lockedApiSettings, config, isLoading } = + useContext(SettingsContext) + + if (isLoading) { + return ( +
+ +
+ ) + } + + // Run within Bee Desktop, display read only config + if (config) { + return ( + + + + + + + {config['swap-endpoint'] && ( + + )} + + ) + } return ( diff --git a/src/providers/Settings.tsx b/src/providers/Settings.tsx index 51090c7..036a642 100644 --- a/src/providers/Settings.tsx +++ b/src/providers/Settings.tsx @@ -1,6 +1,7 @@ import { Bee, BeeDebug } from '@ethersphere/bee-js' import { createContext, ReactChild, ReactElement, useEffect, useState } from 'react' import { config } from '../config' +import { BeeConfig, useGetBeeConfig } from '../hooks/apiHooks' interface ContextInterface { apiUrl: string @@ -11,6 +12,9 @@ interface ContextInterface { setDebugApiUrl: (url: string) => void lockedApiSettings: boolean desktopApiKey: string + config: BeeConfig | null + isLoading: boolean + error: Error | null } const initialValues: ContextInterface = { @@ -22,6 +26,9 @@ const initialValues: ContextInterface = { setDebugApiUrl: () => {}, // eslint-disable-line lockedApiSettings: false, desktopApiKey: '', + config: null, + isLoading: true, + error: null, } export const Context = createContext(initialValues) @@ -46,9 +53,10 @@ export function Provider({ const [beeDebugApi, setBeeDebugApi] = useState(null) const [lockedApiSettings] = useState(Boolean(extLockedApiSettings)) const [desktopApiKey, setDesktopApiKey] = useState(initialValues.desktopApiKey) + const { config, isLoading, error } = useGetBeeConfig() - const url = beeApiUrl || apiUrl - const debugUrl = beeDebugApiUrl || apiDebugUrl + const url = config?.['api-addr'] || beeApiUrl || apiUrl + const debugUrl = config?.['debug-api-addr'] || beeDebugApiUrl || apiDebugUrl useEffect(() => { const urlSearchParams = new URLSearchParams(window.location.search) @@ -90,6 +98,9 @@ export function Provider({ setDebugApiUrl, lockedApiSettings, desktopApiKey, + config, + isLoading, + error, }} > {children} diff --git a/src/utils/net.ts b/src/utils/net.ts index 384b63f..4e8377a 100644 --- a/src/utils/net.ts +++ b/src/utils/net.ts @@ -2,8 +2,8 @@ import axios from 'axios' -export function getJson(url: string): Promise> { - return sendRequest(url, 'GET') +export function getJson>(url: string): Promise { + return sendRequest(url, 'GET') as Promise } export function postJson(url: string, data?: Record): Promise> {