2022-09-11 13:42:41 -04:00
|
|
|
import {LitElement, html} from './lit-all.min.js';
|
|
|
|
import * as tfrpc from '/static/tfrpc.js';
|
|
|
|
|
|
|
|
class TfTabConnectionsElement extends LitElement {
|
|
|
|
static get properties() {
|
|
|
|
return {
|
|
|
|
broadcasts: {type: Array},
|
|
|
|
identities: {type: Array},
|
|
|
|
connections: {type: Array},
|
|
|
|
users: {type: Object},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
let self = this;
|
|
|
|
this.broadcasts = [];
|
|
|
|
this.identities = [];
|
|
|
|
this.connections = [];
|
|
|
|
this.users = {};
|
|
|
|
tfrpc.rpc.getAllIdentities().then(function(identities) {
|
|
|
|
self.identities = identities || [];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-11-08 20:47:47 -05:00
|
|
|
render_connection_summary(connection) {
|
|
|
|
if (connection.address && connection.port) {
|
|
|
|
return html`(<small>${connection.address}:${connection.port}</small>)`;
|
2022-11-08 22:51:31 -05:00
|
|
|
} else if (connection.tunnel) {
|
|
|
|
return html`(room peer)`;
|
2022-11-08 20:47:47 -05:00
|
|
|
} else {
|
|
|
|
return JSON.stringify(connection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-08 22:51:31 -05:00
|
|
|
render_room_peers(connection) {
|
|
|
|
let self = this;
|
|
|
|
let peers = this.broadcasts.filter(x => x.tunnel?.id == connection);
|
|
|
|
if (peers.length) {
|
|
|
|
return html`
|
|
|
|
<ul>
|
2022-11-12 22:36:48 -05:00
|
|
|
${peers.map(x => html`${self.render_room_peer(x)}`)}
|
2022-11-08 22:51:31 -05:00
|
|
|
</ul>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-12 22:36:48 -05:00
|
|
|
async _tunnel(portal, target) {
|
|
|
|
let request_number = await tfrpc.rpc.connectionSendJson(portal, {name: ['tunnel', 'connect'], args: [{portal: portal, target: target}], type: 'duplex'});
|
|
|
|
return tfrpc.rpc.createTunnel(portal, request_number, target);
|
|
|
|
}
|
|
|
|
|
|
|
|
render_room_peer(connection) {
|
|
|
|
let self = this;
|
|
|
|
return html`
|
|
|
|
<li>
|
|
|
|
<input type="button" @click=${() => self._tunnel(connection.tunnel.id, connection.pubkey)} value="Connect"></input>
|
|
|
|
<tf-user id=${connection.pubkey} .users=${this.users}></tf-user>
|
|
|
|
</li>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
2022-11-08 22:51:31 -05:00
|
|
|
render_broadcast(connection) {
|
|
|
|
return html`
|
|
|
|
<li>
|
|
|
|
<input type="button" @click=${() => tfrpc.rpc.connect(connection)} value="Connect"></input>
|
|
|
|
<tf-user id=${connection.pubkey} .users=${this.users}></tf-user>
|
|
|
|
${this.render_connection_summary(connection)}
|
|
|
|
</li>
|
|
|
|
`
|
|
|
|
}
|
|
|
|
|
2022-09-11 13:42:41 -04:00
|
|
|
render() {
|
2022-11-08 20:47:47 -05:00
|
|
|
let self = this;
|
2022-09-11 13:42:41 -04:00
|
|
|
return html`
|
2022-11-08 20:47:47 -05:00
|
|
|
<h2>New Connection</h2>
|
2022-11-19 21:09:52 -05:00
|
|
|
<div style="display: flex; flex-direction: column">
|
|
|
|
<textarea id="code"></textarea>
|
|
|
|
</div>
|
2022-11-08 20:47:47 -05:00
|
|
|
<input type="button" @click=${() => tfrpc.rpc.connect(self.renderRoot.getElementById('code').value)} value="Connect"></input>
|
2022-09-11 13:42:41 -04:00
|
|
|
<h2>Broadcasts</h2>
|
|
|
|
<ul>
|
2022-11-08 22:51:31 -05:00
|
|
|
${this.broadcasts.filter(x => x.address).map(x => self.render_broadcast(x))}
|
2022-09-11 13:42:41 -04:00
|
|
|
</ul>
|
|
|
|
<h2>Connections</h2>
|
|
|
|
<ul>
|
2022-11-08 20:47:47 -05:00
|
|
|
${this.connections.map(x => html`
|
|
|
|
<li>
|
|
|
|
<input type="button" @click=${() => tfrpc.rpc.closeConnection(x)} value="Close"></input>
|
|
|
|
<tf-user id=${x} .users=${this.users}></tf-user>
|
2022-11-08 22:51:31 -05:00
|
|
|
${self.render_room_peers(x)}
|
2022-11-08 20:47:47 -05:00
|
|
|
</li>
|
|
|
|
`)}
|
2022-09-11 13:42:41 -04:00
|
|
|
</ul>
|
|
|
|
<h2>Local Accounts</h2>
|
|
|
|
<ul>
|
|
|
|
${this.identities.map(x => html`<li><tf-user id=${x} .users=${this.users}></tf-user></li>`)}
|
|
|
|
</ul>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
customElements.define('tf-tab-connections', TfTabConnectionsElement);
|