From cad9db3ff22c54528e802ce75fc7613e2cfbdcff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Tue, 1 Oct 2019 17:53:25 +0200 Subject: [PATCH] pond: Show basic node info in node index --- lotuspond/front/src/App.js | 76 +-------------------- lotuspond/front/src/NodeIndex.js | 110 +++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 75 deletions(-) create mode 100644 lotuspond/front/src/NodeIndex.js diff --git a/lotuspond/front/src/App.js b/lotuspond/front/src/App.js index e5761e083..475369738 100644 --- a/lotuspond/front/src/App.js +++ b/lotuspond/front/src/App.js @@ -3,81 +3,7 @@ import './App.css'; import { BrowserRouter as Router, Route, Link } from "react-router-dom"; import Pond from "./Pond"; import SingleNode from "./SingleNode"; - -class Index extends React.Component { - constructor(props) { - super(props) - - this.state = {rpcUrl: "ws://127.0.0.1:1234/rpc/v0", rpcToken: ''} - - const initialState = JSON.parse(window.localStorage.getItem('saved-nodes')) - if (initialState) { - this.state.nodes = initialState - } else { - this.state.nodes = [] - } - } - - componentDidUpdate(prevProps, prevState, snapshot) { - window.localStorage.setItem('saved-nodes', JSON.stringify(this.state.nodes)) - } - - onAdd = () => { - this.setState({addingNode: true}) - } - - update = (name) => (e) => this.setState({ [name]: e.target.value }) - - tokenOk = () => { - let m = this.state.rpcToken.match(/\.(.+)\./) - // TODO: eww - if(m && atob(m[1]) === '{"Allow":["read","write","sign","admin"]}') { - return ( - -Token OK- -
- -
-
- ) - } - return -Expecting valid admin token- - } - - addNode = async () => { - this.setState(p => ({nodes: [...p.nodes, {addr: this.state.rpcUrl, token: this.state.rpcToken}], addingNode: true})) - } - - render() { - return ( -
-
-
- { - this.state.nodes.map((node, i) =>
- {i}. {node.addr} [OPEN UI] -
) - } -
- - -
-
-
- Open Pond -
-
-
- ) - } -} +import Index from "./NodeIndex"; class App extends React.Component { constructor(props) { diff --git a/lotuspond/front/src/NodeIndex.js b/lotuspond/front/src/NodeIndex.js new file mode 100644 index 000000000..676326c64 --- /dev/null +++ b/lotuspond/front/src/NodeIndex.js @@ -0,0 +1,110 @@ +import React from 'react'; +import {Link} from "react-router-dom"; +import {Client} from "rpc-websockets"; + +class Index extends React.Component { + constructor(props) { + super(props) + + this.state = {rpcUrl: "ws://127.0.0.1:1234/rpc/v0", rpcToken: '', conns: {}, info: {}} + + const initialState = JSON.parse(window.localStorage.getItem('saved-nodes')) + if (initialState) { + this.state.nodes = initialState + } else { + this.state.nodes = [] + } + this.state.nodes.forEach((n, i) => this.connTo(i, n)) + } + + componentDidUpdate(prevProps, prevState, snapshot) { + window.localStorage.setItem('saved-nodes', JSON.stringify(this.state.nodes)) + //this.state.nodes.filter(i => [i, this.state.conns[i]]).forEach(([i, n]) => this.connTo(i, n)) + } + + componentWillUnmount() { + Object.keys(this.state.conns).forEach(c => this.state.conns[c].close()) + } + + async updateInfo(n) { + const conn = this.state.conns[n] + const head = await conn.call('Filecoin.ChainHead', []) + const peers = await conn.call('Filecoin.NetPeers', []) + + this.setState(p => ({info: {...p.info, [n]: {head, peers}}})) + } + + connTo = async (n, node) => { + const client = new Client(`${node.addr}?token=${node.token}`) + client.on('open', async () => { + this.setState(p => ({conns: {...p.conns, [n]: client}})) + setInterval(() => this.updateInfo(n), 1333) + }) + } + + onAdd = () => { + this.setState({addingNode: true}) + } + + update = (name) => (e) => this.setState({ [name]: e.target.value }) + + tokenOk = () => { + let m = this.state.rpcToken.match(/\.(.+)\./) + // TODO: eww + if(m && atob(m[1]) === '{"Allow":["read","write","sign","admin"]}') { + return ( + -Token OK- +
+ +
+
+ ) + } + return -Expecting valid admin token- + } + + addNode = async () => { + this.setState(p => ({nodes: [...p.nodes, {addr: this.state.rpcUrl, token: this.state.rpcToken}], addingNode: true})) + } + + render() { + return ( +
+
+
+ { + this.state.nodes.map((node, i) => { + let info = [no conn] + if (this.state.info[i]) { + const ni = this.state.info[i] + info = H:{ni.head.Height}; Peers:{ni.peers.length} + } + + return
+ {i}. {node.addr} [OPEN UI] {info} +
} + ) + } +
+ + +
+
+
+ Open Pond +
+
+
+ ) + } +} + +export default Index \ No newline at end of file