Make fleet environment part of the state

This commit is contained in:
Franck Royer 2021-07-15 14:14:45 +10:00
parent bc3b00b13e
commit 9ffb33cdde
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
1 changed files with 8 additions and 7 deletions

View File

@ -81,6 +81,7 @@ export default function App() {
const persistedNick = window.localStorage.getItem('nick');
return persistedNick !== null ? persistedNick : generate();
});
const [fleetEnv] = useState<Environment>(defaultFleetEnv);
useEffect(() => {
localStorage.setItem('nick', nick);
@ -89,10 +90,10 @@ export default function App() {
useEffect(() => {
if (waku) return;
initWaku(setWaku)
initWaku(fleetEnv, setWaku)
.then(() => console.log('Waku init done'))
.catch((e) => console.log('Waku init failed ', e));
}, [waku]);
}, [fleetEnv, waku]);
useEffect(() => {
if (!waku) return;
@ -175,7 +176,7 @@ export default function App() {
);
}
async function initWaku(setter: (waku: Waku) => void) {
async function initWaku(fleetEnv: Environment, setter: (waku: Waku) => void) {
try {
const waku = await Waku.create({
libp2p: {
@ -190,7 +191,7 @@ async function initWaku(setter: (waku: Waku) => void) {
setter(waku);
const nodes = await getNodes();
const nodes = await getStatusFleetNodes(fleetEnv);
await Promise.all(
nodes.map((addr) => {
return waku.dial(addr);
@ -201,11 +202,11 @@ async function initWaku(setter: (waku: Waku) => void) {
}
}
function getNodes() {
function defaultFleetEnv() {
// Works with react-scripts
if (process?.env?.NODE_ENV === 'development') {
return getStatusFleetNodes(Environment.Test);
return Environment.Test;
} else {
return getStatusFleetNodes(Environment.Prod);
return Environment.Prod;
}
}