New `/fleet` command to change target Status fleet

This commit is contained in:
Franck Royer 2021-07-15 14:25:59 +10:00
parent 9ffb33cdde
commit 53412e2fb4
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
2 changed files with 47 additions and 10 deletions

View File

@ -81,19 +81,17 @@ export default function App() {
const persistedNick = window.localStorage.getItem('nick');
return persistedNick !== null ? persistedNick : generate();
});
const [fleetEnv] = useState<Environment>(defaultFleetEnv);
const [fleetEnv, setFleetEnv] = useState<Environment>(defaultFleetEnv);
useEffect(() => {
localStorage.setItem('nick', nick);
}, [nick]);
useEffect(() => {
if (waku) return;
initWaku(fleetEnv, setWaku)
.then(() => console.log('Waku init done'))
.catch((e) => console.log('Waku init failed ', e));
}, [fleetEnv, waku]);
}, [fleetEnv]);
useEffect(() => {
if (!waku) return;
@ -163,7 +161,13 @@ export default function App() {
newMessages={newMessages}
archivedMessages={archivedMessages}
commandHandler={(input: string) => {
const { command, response } = handleCommand(input, waku, setNick);
const { command, response } = handleCommand(
input,
waku,
setNick,
fleetEnv,
setFleetEnv
);
const commandMessages = response.map((msg) => {
return Message.fromUtf8String(command, msg);
});

View File

@ -1,12 +1,13 @@
import { multiaddr } from 'multiaddr';
import PeerId from 'peer-id';
import { Waku } from 'js-waku';
import { Environment, Waku } from 'js-waku';
function help(): string[] {
return [
'/nick <nickname>: set a new nickname',
'/info: some information about the node',
'/connect <Multiaddr>: connect to the given peer',
'/fleet <prod|test>: connect to this fleet; beware it restarts waku node.',
'/help: Display this help',
];
}
@ -22,11 +23,14 @@ function nick(
return [`New nick: ${nick}`];
}
function info(waku: Waku | undefined): string[] {
function info(waku: Waku | undefined, fleetEnv: Environment): string[] {
if (!waku) {
return ['Waku node is starting'];
}
return [`PeerId: ${waku.libp2p.peerId.toB58String()}`];
return [
`PeerId: ${waku.libp2p.peerId.toB58String()}`,
`Fleet environment: ${fleetEnv}`,
];
}
function connect(peer: string | undefined, waku: Waku | undefined): string[] {
@ -78,6 +82,28 @@ function peers(waku: Waku | undefined): string[] {
return response;
}
function fleet(
newFleetEnv: string | undefined,
currFleetEnv: Environment,
setFleetEnv: (fleetEnv: Environment) => void
): string[] {
switch (newFleetEnv) {
case Environment.Test:
setFleetEnv(newFleetEnv);
break;
case Environment.Prod:
setFleetEnv(newFleetEnv);
break;
default:
return [
`Incorrect values, acceptable values are ${Environment.Test}, ${Environment.Prod}`,
`Current fleet environment is ${currFleetEnv}`,
];
}
return [`New fleet Environment: ${newFleetEnv}`];
}
function connections(waku: Waku | undefined): string[] {
if (!waku) {
return ['Waku node is starting'];
@ -107,7 +133,9 @@ function connections(waku: Waku | undefined): string[] {
export default function handleCommand(
input: string,
waku: Waku | undefined,
setNick: (nick: string) => void
setNick: (nick: string) => void,
currFleetEnv: Environment,
setFleetEnv: (fleetEnv: Environment) => void
): { command: string; response: string[] } {
let response: string[] = [];
const args = parseInput(input);
@ -120,7 +148,7 @@ export default function handleCommand(
nick(args.shift(), setNick).map((str) => response.push(str));
break;
case '/info':
info(waku).map((str) => response.push(str));
info(waku, currFleetEnv).map((str) => response.push(str));
break;
case '/connect':
connect(args.shift(), waku).map((str) => response.push(str));
@ -131,6 +159,11 @@ export default function handleCommand(
case '/connections':
connections(waku).map((str) => response.push(str));
break;
case '/fleet':
fleet(args.shift(), currFleetEnv, setFleetEnv).map((str) =>
response.push(str)
);
break;
default:
response.push(`Unknown Command '${command}'`);
}