import React from 'react'; import Contacts from './Contacts/index.jsx' import Messages from './Messages/index.jsx' import Channels from './Channels/index.jsx' import { Tab } from 'semantic-ui-react' import Login from './Login.jsx' import {ctx} from './context.jsx' import Subscriptions from './Subscriptions/index.jsx'; import Media from './Media/index.jsx'; import Tribes from './Tribes/index.jsx'; class Root extends React.Component { constructor(props) { super(props) this.state = { identity_pubkey: null, balance: null, pending_open_balance: null, messages: [], contacts: [], subscriptions: [], chats: [], } this.getSubscriptions = this.getSubscriptions.bind(this) } componentDidMount() { document.title = location.hostname if (this.props.isLoggedIn) { this.getBalance() this.getInfo() this.connectSocket() this.getContacts() this.getMessages() this.getSubscriptions() this.getChats() this.listPayments() this.getLogs() } } connectSocket() { let protocol = (location.protocol == 'https:') ? 'wss' : 'ws' this.socket = new WebSocket(`${protocol}://${location.hostname}:${location.port}/sphinx_chat`) this.socket.onopen = () => { console.log('socket opened') } this.socket.onmessage = event => { const message = JSON.parse(event.data) console.log('received message', { message }) const messages = [...this.state.messages, message] this.setState({messages}) } } listPayments() { fetch('/payments').then(r => r.json()).then(body => { console.log("payments",body) }) } getLogs() { fetch('/logs').then(r => r.json()).then(body => { console.log("logs",body) }) } getBalance() { fetch('/balance').then(r => r.json()).then(body => { const { balance, pending_open_balance } = body.response console.log(balance) this.setState({ balance, pending_open_balance }) }) } getSubscriptions(){ fetch('/subscriptions').then(r => r.json()).then(body => { this.setState({ subscriptions: body.response }) }) } getInfo() { fetch('/getinfo').then(r => r.json()).then(body => { console.log(body.response) const { identity_pubkey } = body.response this.setState({ identity_pubkey }) }) } getMessages() { fetch('/messages').then(r => r.json()).then(body => { console.log("MESSAGES",body.response) this.setState({ messages: body.response.new_messages }) }) } getContacts() { fetch('/contacts').then(r => r.json()).then(body => { this.setState({ contacts: body.response.contacts, }) }) } getChats() { fetch('/chats').then(r => r.json()).then(body => { console.log("CHATS",body.response) this.setState({ chats: body.response }) }) } render() { const { balance, pending_open_balance, messages, channels, identity_pubkey, contacts, chats } = this.state if (!this.props.isLoggedIn) { return } return ( Sphinx Chat Host: {document.location.host} Identity Pubkey: {identity_pubkey} Channel balance: {balance} Pending open balance: {pending_open_balance} }, { menuItem: 'Messages', render: () => }, { menuItem: 'Channels', render: () => }, { menuItem: 'Subscriptions', render: () => }, { menuItem: 'Media', render: () => }, { menuItem: 'Tribes', render: () => }, ]} renderActiveOnly={true} /> ) } } Root.contextType = ctx export default Root;