|
1 | | -import { createContext, useEffect, useState } from "react"; |
2 | | -import { GoGear } from "react-icons/go"; |
| 1 | +import { useEffect, useState } from "react"; |
3 | 2 | import { ThemeProvider } from "styled-components"; |
4 | 3 |
|
5 | | -import { Alert, Modal } from "@namada/components"; |
6 | 4 | import { ColorMode, getTheme } from "@namada/utils"; |
7 | 5 |
|
8 | | -import { |
9 | | - AppContainer, |
10 | | - BackgroundImage, |
11 | | - BottomSection, |
12 | | - ContentContainer, |
13 | | - FaucetContainer, |
14 | | - GlobalStyles, |
15 | | - InfoContainer, |
16 | | - SettingsButton, |
17 | | - SettingsButtonContainer, |
18 | | - TopSection, |
19 | | -} from "App/App.components"; |
20 | | -import { FaucetForm } from "App/Faucet"; |
| 6 | +import { GlobalStyles } from "App/App.components"; |
21 | 7 |
|
22 | | -import { API, toNam } from "utils"; |
23 | | -import dotsBackground from "../../public/bg-dots.svg"; |
24 | | -import { |
25 | | - AppBanner, |
26 | | - AppHeader, |
27 | | - CallToActionCard, |
28 | | - CardsContainer, |
29 | | - Faq, |
30 | | -} from "./Common"; |
31 | | -import { SettingsForm } from "./SettingsForm"; |
32 | | - |
33 | | -const DEFAULT_URL = "http://localhost:5000"; |
34 | | -const DEFAULT_LIMIT = 1_000_000_000; |
35 | | - |
36 | | -const { |
37 | | - NAMADA_INTERFACE_FAUCET_API_URL: faucetApiUrl = DEFAULT_URL, |
38 | | - NAMADA_INTERFACE_PROXY: isProxied, |
39 | | - NAMADA_INTERFACE_PROXY_PORT: proxyPort = 9000, |
40 | | -} = process.env; |
41 | | - |
42 | | -const baseUrl = |
43 | | - isProxied ? `http://localhost:${proxyPort}/proxy` : faucetApiUrl; |
44 | | -const runFullNodeUrl = "https://docs.namada.net/operators/ledger"; |
45 | | -const becomeBuilderUrl = "https://docs.namada.net/integrating-with-namada"; |
46 | | - |
47 | | -type Settings = { |
48 | | - difficulty?: number; |
49 | | - tokens?: Record<string, string>; |
50 | | - startsAt: number; |
51 | | - startsAtText?: string; |
52 | | - withdrawLimit: number; |
53 | | -}; |
54 | | - |
55 | | -type AppContext = { |
56 | | - baseUrl: string; |
57 | | - settingsError?: string; |
58 | | - api: API; |
59 | | - isTestnetLive: boolean; |
60 | | - settings: Settings; |
61 | | - setApi: (api: API) => void; |
62 | | - setUrl: (url: string) => void; |
63 | | - setIsModalOpen: (value: boolean) => void; |
64 | | -}; |
65 | | - |
66 | | -const START_TIME_UTC = 1702918800; |
67 | | -const START_TIME_TEXT = new Date(START_TIME_UTC * 1000).toLocaleString( |
68 | | - "en-gb", |
69 | | - { |
70 | | - timeZone: "UTC", |
71 | | - month: "long", |
72 | | - day: "numeric", |
73 | | - year: "numeric", |
74 | | - hour: "numeric", |
75 | | - minute: "numeric", |
76 | | - } |
77 | | -); |
78 | | - |
79 | | -export const AppContext = createContext<AppContext | null>(null); |
| 8 | +import { Config, getConfig } from "config"; |
| 9 | +import { FaucetApp } from "./FaucetApp"; |
80 | 10 |
|
81 | 11 | export const App = (): JSX.Element => { |
82 | 12 | const initialColorMode = "dark"; |
83 | | - |
| 13 | + const [config, setConfig] = useState<Config>(); |
84 | 14 | const [colorMode, _] = useState<ColorMode>(initialColorMode); |
85 | | - const [isTestnetLive, setIsTestnetLive] = useState(true); |
86 | | - const [settings, setSettings] = useState<Settings>({ |
87 | | - startsAt: START_TIME_UTC, |
88 | | - startsAtText: `${START_TIME_TEXT} UTC`, |
89 | | - withdrawLimit: toNam(DEFAULT_LIMIT), |
90 | | - }); |
91 | | - const [url, setUrl] = useState(localStorage.getItem("baseUrl") || baseUrl); |
92 | | - const [api, setApi] = useState<API>(new API(url)); |
93 | | - const [isModalOpen, setIsModalOpen] = useState(false); |
94 | | - const [settingsError, setSettingsError] = useState<string>(); |
95 | 15 | const theme = getTheme(colorMode); |
96 | 16 |
|
97 | | - const fetchSettings = async (api: API): Promise<void> => { |
98 | | - const { |
99 | | - difficulty, |
100 | | - tokens_alias_to_address: tokens, |
101 | | - withdraw_limit: withdrawLimit = DEFAULT_LIMIT, |
102 | | - } = await api.settings().catch((e) => { |
103 | | - const message = e.errors?.message; |
104 | | - setSettingsError(`Error requesting settings: ${message?.join(" ")}`); |
105 | | - throw new Error(e); |
106 | | - }); |
107 | | - // Append difficulty level and tokens to settings |
108 | | - setSettings({ |
109 | | - ...settings, |
110 | | - difficulty, |
111 | | - tokens, |
112 | | - withdrawLimit: toNam(withdrawLimit), |
113 | | - }); |
114 | | - }; |
115 | | - |
116 | 17 | useEffect(() => { |
117 | | - // Sync url to localStorage |
118 | | - localStorage.setItem("baseUrl", url); |
119 | | - const api = new API(url); |
120 | | - setApi(api); |
121 | | - const { startsAt } = settings; |
122 | | - const now = new Date(); |
123 | | - const nowUTC = Date.UTC( |
124 | | - now.getUTCFullYear(), |
125 | | - now.getUTCMonth(), |
126 | | - now.getUTCDate(), |
127 | | - now.getUTCHours(), |
128 | | - now.getUTCMinutes() |
129 | | - ); |
130 | | - const startsAtToMilliseconds = startsAt * 1000; |
131 | | - if (nowUTC < startsAtToMilliseconds) { |
132 | | - setIsTestnetLive(false); |
133 | | - } |
134 | | - |
135 | | - // Fetch settings from faucet API |
136 | | - fetchSettings(api) |
137 | | - .then(() => setSettingsError(undefined)) |
138 | | - .catch((e) => setSettingsError(`Failed to load settings! ${e}`)); |
139 | | - }, [url]); |
| 18 | + getConfig().then((config) => setConfig(config)); |
| 19 | + }, []); |
140 | 20 |
|
141 | 21 | return ( |
142 | | - <AppContext.Provider |
143 | | - value={{ |
144 | | - api, |
145 | | - isTestnetLive, |
146 | | - baseUrl: url, |
147 | | - settingsError, |
148 | | - settings, |
149 | | - setApi, |
150 | | - setUrl, |
151 | | - setIsModalOpen, |
152 | | - }} |
153 | | - > |
154 | | - <ThemeProvider theme={theme}> |
155 | | - <GlobalStyles colorMode={colorMode} /> |
156 | | - <AppBanner /> |
157 | | - <BackgroundImage imageUrl={dotsBackground} /> |
158 | | - <AppContainer> |
159 | | - <ContentContainer> |
160 | | - <SettingsButtonContainer> |
161 | | - <SettingsButton |
162 | | - onClick={() => setIsModalOpen(true)} |
163 | | - title="Settings" |
164 | | - > |
165 | | - <GoGear /> |
166 | | - </SettingsButton> |
167 | | - </SettingsButtonContainer> |
168 | | - |
169 | | - <TopSection> |
170 | | - <AppHeader /> |
171 | | - </TopSection> |
172 | | - <FaucetContainer> |
173 | | - {settingsError && ( |
174 | | - <InfoContainer> |
175 | | - <Alert type="error">{settingsError}</Alert> |
176 | | - </InfoContainer> |
177 | | - )} |
178 | | - |
179 | | - <FaucetForm isTestnetLive={isTestnetLive} /> |
180 | | - </FaucetContainer> |
181 | | - {isModalOpen && ( |
182 | | - <Modal onClose={() => setIsModalOpen(false)}> |
183 | | - <SettingsForm /> |
184 | | - </Modal> |
185 | | - )} |
186 | | - <BottomSection> |
187 | | - <CardsContainer> |
188 | | - <CallToActionCard |
189 | | - description="Contribute to the Namada network's resiliency" |
190 | | - title="RUN A FULL NODE" |
191 | | - href={runFullNodeUrl} |
192 | | - /> |
193 | | - <CallToActionCard |
194 | | - description="Integrate Namada into applications or extend its capabilities" |
195 | | - title="BECOME A BUILDER" |
196 | | - href={becomeBuilderUrl} |
197 | | - /> |
198 | | - </CardsContainer> |
199 | | - <Faq /> |
200 | | - </BottomSection> |
201 | | - </ContentContainer> |
202 | | - </AppContainer> |
203 | | - </ThemeProvider> |
204 | | - </AppContext.Provider> |
| 22 | + <ThemeProvider theme={theme}> |
| 23 | + <GlobalStyles colorMode={colorMode} /> |
| 24 | + {config && <FaucetApp config={config} />} |
| 25 | + </ThemeProvider> |
205 | 26 | ); |
206 | 27 | }; |
0 commit comments