-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathSettings.tsx
More file actions
145 lines (128 loc) · 4.1 KB
/
Settings.tsx
File metadata and controls
145 lines (128 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { Bee } from '@ethersphere/bee-js'
import { providers } from 'ethers'
import { ReactElement, ReactNode, createContext, useEffect, useState } from 'react'
import { DEFAULT_BEE_API_HOST, DEFAULT_RPC_URL } from '../constants'
import { useGetBeeConfig } from '../hooks/apiHooks'
const LocalStorageKeys = {
providerUrl: 'json-rpc-provider',
}
interface ContextInterface {
apiUrl: string
beeApi: Bee | null
lockedApiSettings: boolean
desktopApiKey: string
isDesktop: boolean
desktopUrl: string
rpcProviderUrl: string
rpcProvider: providers.JsonRpcProvider | null
cors: string | null
dataDir: string | null
ensResolver: string | null
setApiUrl: (url: string) => void
setAndPersistJsonRpcProvider: (url: string) => void
isLoading: boolean
error: Error | null
}
const initialValues: ContextInterface = {
beeApi: null,
apiUrl: DEFAULT_BEE_API_HOST,
setApiUrl: () => {}, // eslint-disable-line
lockedApiSettings: false,
isDesktop: false,
desktopApiKey: '',
desktopUrl: window.location.origin,
setAndPersistJsonRpcProvider: async () => {}, // eslint-disable-line
rpcProviderUrl: '',
rpcProvider: null,
cors: null,
dataDir: null,
ensResolver: null,
isLoading: true,
error: null,
}
export const Context = createContext<ContextInterface>(initialValues)
export const Consumer = Context.Consumer
interface InitialSettings {
beeApiUrl?: string
lockedApiSettings?: boolean
isDesktop?: boolean
desktopUrl?: string
defaultRpcUrl?: string
}
interface Props extends InitialSettings {
children: ReactNode
}
export function Provider({ children, ...propsSettings }: Props): ReactElement {
const desktopUrl = propsSettings.desktopUrl ?? initialValues.desktopUrl
const isDesktop = Boolean(propsSettings.isDesktop)
const propsProviderUrl =
localStorage.getItem(LocalStorageKeys.providerUrl) || propsSettings.defaultRpcUrl || DEFAULT_RPC_URL
const [apiUrl, setApiUrl] = useState<string>(
sessionStorage.getItem('api_host') ?? propsSettings.beeApiUrl ?? initialValues.apiUrl,
)
const [beeApi, setBeeApi] = useState<Bee | null>(null)
const [desktopApiKey, setDesktopApiKey] = useState<string>(initialValues.desktopApiKey)
const [rpcProviderUrl, setRpcProviderUrl] = useState(propsProviderUrl)
const [rpcProvider, setRpcProvider] = useState<providers.JsonRpcProvider | null>(null)
useEffect(() => {
setRpcProvider(new providers.JsonRpcProvider(rpcProviderUrl))
}, [rpcProviderUrl])
const { config, isLoading, error } = useGetBeeConfig(desktopUrl)
useEffect(() => {
const urlSearchParams = new URLSearchParams(window.location.search)
const newApiKey = urlSearchParams.get('v')
if (newApiKey) {
localStorage.setItem('apiKey', newApiKey)
window.location.search = ''
setDesktopApiKey(newApiKey)
}
}, [])
useEffect(() => {
const url = makeHttpUrl(config?.['api-addr'] ?? apiUrl)
try {
setBeeApi(new Bee(url))
sessionStorage.setItem('api_host', url)
} catch (e) {
setBeeApi(null)
}
}, [config, apiUrl])
return (
<Context.Provider
value={{
apiUrl,
beeApi,
setApiUrl,
lockedApiSettings: Boolean(propsSettings.lockedApiSettings),
desktopApiKey,
isDesktop,
desktopUrl,
rpcProvider,
rpcProviderUrl,
cors: config?.['cors-allowed-origins'] ?? null,
dataDir: config?.['data-dir'] ?? null,
ensResolver: config?.['resolver-options'] ?? null,
setAndPersistJsonRpcProvider: setAndPersistJsonRpcProviderClosure(setRpcProviderUrl, setRpcProvider),
isLoading,
error,
}}
>
{children}
</Context.Provider>
)
}
function makeHttpUrl(string: string): string {
if (!string.startsWith('http')) {
return `http://${string}`
}
return string
}
function setAndPersistJsonRpcProviderClosure(
setProviderUrl: (url: string) => void,
setProvider: (prov: providers.JsonRpcProvider) => void,
) {
return (providerUrl: string) => {
localStorage.setItem(LocalStorageKeys.providerUrl, providerUrl)
setProviderUrl(providerUrl)
setProvider(new providers.JsonRpcProvider(providerUrl))
}
}