Skip to content

Commit

Permalink
feat: add bee-desktop settings capabilities (#323)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
vojtechsimetka authored Apr 29, 2022
1 parent 8114fa7 commit 87b0b71
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 8 deletions.
6 changes: 4 additions & 2 deletions src/components/ExpandableListItemInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -138,7 +138,9 @@ export default function ExpandableListItemKey({
}
loading={loading}
iconType={Search}
onClick={() => onConfirm(inputValue)}
onClick={() => {
if (onConfirm) onConfirm(inputValue)
}}
>
{confirmLabel || 'Save'}
</SwarmButton>
Expand Down
49 changes: 49 additions & 0 deletions src/hooks/apiHooks.tsx
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<BeeConfig | null>(null)
const [isLoading, setLoading] = useState<boolean>(true)
const [error, setError] = useState<Error | null>(null)

useEffect(() => {
getJson<BeeConfig>(`${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<LatestBeeRelease | null>(null)
const [isLoadingLatestBeeRelease, setLoading] = useState<boolean>(false)
Expand Down
30 changes: 28 additions & 2 deletions src/pages/settings/index.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<div style={{ textAlign: 'center', width: '100%' }}>
<CircularProgress />
</div>
)
}

// Run within Bee Desktop, display read only config
if (config) {
return (
<ExpandableList label="Bee Desktop Settings" defaultOpen>
<ExpandableListItemInput label="Bee API" value={config['api-addr']} locked />
<ExpandableListItemInput label="Bee Debug API" value={config['debug-api-addr']} locked />
<ExpandableListItemInput label="CORS" value={config['cors-allowed-origins']} locked />
<ExpandableListItemInput label="Data DIR" value={config['data-dir']} locked />
<ExpandableListItemInput label="ENS resolver URL" value={config['resolver-options']} locked />
{config['swap-endpoint'] && (
<ExpandableListItemInput label="SWAP endpoint" value={config['swap-endpoint']} locked />
)}
</ExpandableList>
)
}

return (
<ExpandableList label="API Settings" defaultOpen>
Expand Down
15 changes: 13 additions & 2 deletions src/providers/Settings.tsx
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 = {
Expand All @@ -22,6 +26,9 @@ const initialValues: ContextInterface = {
setDebugApiUrl: () => {}, // eslint-disable-line
lockedApiSettings: false,
desktopApiKey: '',
config: null,
isLoading: true,
error: null,
}

export const Context = createContext<ContextInterface>(initialValues)
Expand All @@ -46,9 +53,10 @@ export function Provider({
const [beeDebugApi, setBeeDebugApi] = useState<BeeDebug | null>(null)
const [lockedApiSettings] = useState<boolean>(Boolean(extLockedApiSettings))
const [desktopApiKey, setDesktopApiKey] = useState<string>(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)
Expand Down Expand Up @@ -90,6 +98,9 @@ export function Provider({
setDebugApiUrl,
lockedApiSettings,
desktopApiKey,
config,
isLoading,
error,
}}
>
{children}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/net.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import axios from 'axios'

export function getJson(url: string): Promise<Record<string, any>> {
return sendRequest(url, 'GET')
export function getJson<T extends Record<string, any>>(url: string): Promise<T> {
return sendRequest(url, 'GET') as Promise<T>
}

export function postJson(url: string, data?: Record<string, any>): Promise<Record<string, unknown>> {
Expand Down

0 comments on commit 87b0b71

Please sign in to comment.