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-
lotus auth create-token --perm admin
): {this.tokenOk()}