2022-09-11 13:42:41 -04:00
|
|
|
import {LitElement, html} from './lit-all.min.js';
|
|
|
|
import * as tfrpc from '/static/tfrpc.js';
|
2024-01-12 21:55:52 -05:00
|
|
|
import {styles} from './tf-styles.js';
|
2022-09-11 13:42:41 -04:00
|
|
|
|
|
|
|
class TfTabConnectionsElement extends LitElement {
|
|
|
|
static get properties() {
|
|
|
|
return {
|
|
|
|
broadcasts: {type: Array},
|
|
|
|
identities: {type: Array},
|
|
|
|
connections: {type: Array},
|
2023-01-17 19:57:54 -05:00
|
|
|
stored_connections: {type: Array},
|
2022-09-11 13:42:41 -04:00
|
|
|
users: {type: Object},
|
2023-03-29 18:02:12 -04:00
|
|
|
};
|
2022-09-11 13:42:41 -04:00
|
|
|
}
|
|
|
|
|
2024-01-12 21:55:52 -05:00
|
|
|
static styles = styles;
|
|
|
|
|
2022-09-11 13:42:41 -04:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
let self = this;
|
|
|
|
this.broadcasts = [];
|
|
|
|
this.identities = [];
|
|
|
|
this.connections = [];
|
2023-01-17 19:57:54 -05:00
|
|
|
this.stored_connections = [];
|
2022-09-11 13:42:41 -04:00
|
|
|
this.users = {};
|
2024-02-24 11:09:34 -05:00
|
|
|
tfrpc.rpc.getAllIdentities().then(function (identities) {
|
2022-09-11 13:42:41 -04:00
|
|
|
self.identities = identities || [];
|
|
|
|
});
|
2024-02-24 11:09:34 -05:00
|
|
|
tfrpc.rpc.getStoredConnections().then(function (connections) {
|
2023-01-17 19:57:54 -05:00
|
|
|
self.stored_connections = connections || [];
|
|
|
|
});
|
2022-09-11 13:42:41 -04:00
|
|
|
}
|
|
|
|
|
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;
|
2024-02-24 11:09:34 -05:00
|
|
|
let peers = this.broadcasts.filter((x) => x.tunnel?.id == connection);
|
2022-11-08 22:51:31 -05:00
|
|
|
if (peers.length) {
|
2024-02-24 11:09:34 -05:00
|
|
|
let connections = this.connections.map((x) => x.id);
|
|
|
|
return html`${peers
|
|
|
|
.filter((x) => connections.indexOf(x.pubkey) == -1)
|
|
|
|
.map((x) => html`${self.render_room_peer(x)}`)}`;
|
2022-11-08 22:51:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-12 22:36:48 -05:00
|
|
|
async _tunnel(portal, target) {
|
2023-01-17 18:10:17 -05:00
|
|
|
return tfrpc.rpc.createTunnel(portal, target);
|
2022-11-12 22:36:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
render_room_peer(connection) {
|
|
|
|
let self = this;
|
|
|
|
return html`
|
|
|
|
<li>
|
2024-02-24 11:09:34 -05:00
|
|
|
<button
|
|
|
|
class="w3-button w3-dark-grey"
|
|
|
|
@click=${() => self._tunnel(connection.tunnel.id, connection.pubkey)}
|
|
|
|
>
|
|
|
|
Connect
|
|
|
|
</button>
|
2023-09-06 18:48:06 -04:00
|
|
|
<tf-user id=${connection.pubkey} .users=${this.users}></tf-user> 📡
|
2022-11-12 22:36:48 -05:00
|
|
|
</li>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
2022-11-08 22:51:31 -05:00
|
|
|
render_broadcast(connection) {
|
|
|
|
return html`
|
|
|
|
<li>
|
2024-02-24 11:09:34 -05:00
|
|
|
<button
|
|
|
|
class="w3-button w3-dark-grey"
|
|
|
|
@click=${() => tfrpc.rpc.connect(connection)}
|
|
|
|
>
|
|
|
|
Connect
|
|
|
|
</button>
|
2022-11-08 22:51:31 -05:00
|
|
|
<tf-user id=${connection.pubkey} .users=${this.users}></tf-user>
|
|
|
|
${this.render_connection_summary(connection)}
|
|
|
|
</li>
|
2023-03-29 18:02:12 -04:00
|
|
|
`;
|
2022-11-08 22:51:31 -05:00
|
|
|
}
|
|
|
|
|
2023-01-17 19:57:54 -05:00
|
|
|
async forget_stored_connection(connection) {
|
|
|
|
await tfrpc.rpc.forgetStoredConnection(connection);
|
|
|
|
this.stored_connections = (await tfrpc.rpc.getStoredConnections()) || [];
|
|
|
|
}
|
|
|
|
|
2023-09-06 18:48:06 -04:00
|
|
|
render_connection(connection) {
|
|
|
|
return html`
|
2024-02-24 11:09:34 -05:00
|
|
|
<button
|
|
|
|
class="w3-button w3-dark-grey"
|
|
|
|
@click=${() => tfrpc.rpc.closeConnection(connection.id)}
|
|
|
|
>
|
|
|
|
Close
|
|
|
|
</button>
|
2023-09-06 18:48:06 -04:00
|
|
|
<tf-user id=${connection.id} .users=${this.users}></tf-user>
|
2024-02-24 11:09:34 -05:00
|
|
|
${connection.tunnel !== undefined
|
|
|
|
? '🚇'
|
|
|
|
: html`(${connection.host}:${connection.port})`}
|
2023-09-06 18:48:06 -04:00
|
|
|
<ul>
|
2024-02-24 11:09:34 -05:00
|
|
|
${this.connections
|
|
|
|
.filter((x) => x.tunnel === this.connections.indexOf(connection))
|
|
|
|
.map((x) => html`<li>${this.render_connection(x)}</li>`)}
|
2023-09-06 18:48:06 -04:00
|
|
|
${this.render_room_peers(connection.id)}
|
|
|
|
</ul>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
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`
|
2024-01-12 21:55:52 -05:00
|
|
|
<div class="w3-container">
|
|
|
|
<h2>New Connection</h2>
|
|
|
|
<textarea class="w3-input w3-dark-grey" id="code"></textarea>
|
2024-02-24 11:09:34 -05:00
|
|
|
<button
|
|
|
|
class="w3-button w3-dark-grey"
|
|
|
|
@click=${() =>
|
|
|
|
tfrpc.rpc.connect(self.renderRoot.getElementById('code').value)}
|
|
|
|
>
|
|
|
|
Connect
|
|
|
|
</button>
|
2024-01-12 21:55:52 -05:00
|
|
|
<h2>Broadcasts</h2>
|
|
|
|
<ul>
|
2024-02-24 11:09:34 -05:00
|
|
|
${this.broadcasts
|
|
|
|
.filter((x) => x.address)
|
|
|
|
.map((x) => self.render_broadcast(x))}
|
2024-01-12 21:55:52 -05:00
|
|
|
</ul>
|
|
|
|
<h2>Connections</h2>
|
|
|
|
<ul>
|
2024-02-24 11:09:34 -05:00
|
|
|
${this.connections
|
|
|
|
.filter((x) => x.tunnel === undefined)
|
|
|
|
.map((x) => html` <li>${this.render_connection(x)}</li> `)}
|
2024-01-12 21:55:52 -05:00
|
|
|
</ul>
|
|
|
|
<h2>Stored Connections (WIP)</h2>
|
|
|
|
<ul>
|
2024-02-24 11:09:34 -05:00
|
|
|
${this.stored_connections.map(
|
|
|
|
(x) => html`
|
|
|
|
<li>
|
|
|
|
<button
|
|
|
|
class="w3-button w3-dark-grey"
|
|
|
|
@click=${() => self.forget_stored_connection(x)}
|
|
|
|
>
|
|
|
|
Forget
|
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
class="w3-button w3-dark-grey"
|
|
|
|
@click=${() => tfrpc.rpc.connect(x)}
|
|
|
|
>
|
|
|
|
Connect
|
|
|
|
</button>
|
|
|
|
${x.address}:${x.port}
|
|
|
|
<tf-user id=${x.pubkey} .users=${self.users}></tf-user>
|
|
|
|
</li>
|
|
|
|
`
|
|
|
|
)}
|
2024-01-12 21:55:52 -05:00
|
|
|
</ul>
|
|
|
|
<h2>Local Accounts</h2>
|
|
|
|
<ul>
|
2024-02-24 11:09:34 -05:00
|
|
|
${this.identities.map(
|
|
|
|
(x) =>
|
|
|
|
html`<li><tf-user id=${x} .users=${this.users}></tf-user></li>`
|
|
|
|
)}
|
2024-01-12 21:55:52 -05:00
|
|
|
</ul>
|
2022-11-19 21:09:52 -05:00
|
|
|
</div>
|
2022-09-11 13:42:41 -04:00
|
|
|
`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-24 11:09:34 -05:00
|
|
|
customElements.define('tf-tab-connections', TfTabConnectionsElement);
|