tildefriends/apps/ssb/tf-tab-connections.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

191 lines
4.9 KiB
JavaScript
Raw Permalink Normal View History

import {LitElement, html} from './lit-all.min.js';
import * as tfrpc from '/static/tfrpc.js';
import {styles} from './tf-styles.js';
class TfTabConnectionsElement extends LitElement {
static get properties() {
return {
broadcasts: {type: Array},
identities: {type: Array},
connections: {type: Array},
stored_connections: {type: Array},
users: {type: Object},
};
}
static styles = styles;
constructor() {
super();
let self = this;
this.broadcasts = [];
this.identities = [];
this.connections = [];
this.stored_connections = [];
this.users = {};
2024-02-24 11:09:34 -05:00
tfrpc.rpc.getAllIdentities().then(function (identities) {
self.identities = identities || [];
});
2024-02-24 11:09:34 -05:00
tfrpc.rpc.getStoredConnections().then(function (connections) {
self.stored_connections = connections || [];
});
}
render_connection_summary(connection) {
if (connection.address && connection.port) {
2024-04-13 20:07:39 -04:00
return html`<div>
<small>${connection.address}:${connection.port}</small>
</div>`;
} else if (connection.tunnel) {
return html`<div>room peer</div>`;
} else {
return JSON.stringify(connection);
}
}
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);
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)}`)}`;
}
}
async _tunnel(portal, target) {
return tfrpc.rpc.createTunnel(portal, target);
}
render_room_peer(connection) {
let self = this;
return html`
<li>
2024-02-24 11:09:34 -05:00
<button
2024-04-04 20:35:09 -04:00
class="w3-button w3-theme-d1"
2024-02-24 11:09:34 -05:00
@click=${() => self._tunnel(connection.tunnel.id, connection.pubkey)}
>
Connect
</button>
<tf-user id=${connection.pubkey} .users=${this.users}></tf-user> 📡
</li>
`;
}
render_broadcast(connection) {
return html`
<li class="w3-bar" style="overflow: hidden; overflow-wrap: nowrap">
2024-02-24 11:09:34 -05:00
<button
class="w3-bar-item w3-button w3-theme-d1"
2024-02-24 11:09:34 -05:00
@click=${() => tfrpc.rpc.connect(connection)}
>
Connect
</button>
<div class="w3-bar-item">
<tf-user id=${connection.pubkey} .users=${this.users}></tf-user>
${this.render_connection_summary(connection)}
</div>
</li>
`;
}
async forget_stored_connection(connection) {
await tfrpc.rpc.forgetStoredConnection(connection);
this.stored_connections = (await tfrpc.rpc.getStoredConnections()) || [];
}
render_connection(connection) {
return html`
2024-02-24 11:09:34 -05:00
<button
2024-04-04 20:35:09 -04:00
class="w3-button w3-theme-d1"
2024-02-24 11:09:34 -05:00
@click=${() => tfrpc.rpc.closeConnection(connection.id)}
>
Close
</button>
<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})`}
<div>${connection.requests.map(x => html`
<span class="w3-tag w3-small">${x.request_number > 0 ? '🟩' : '🟥'} ${x.name}</span>
`)}</div>
<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>`)}
${this.render_room_peers(connection.id)}
</ul>
`;
}
render() {
let self = this;
return html`
<div class="w3-container" style="box-sizing: border-box">
<h2>New Connection</h2>
2024-04-04 20:35:09 -04:00
<textarea class="w3-input w3-theme-d1" id="code"></textarea>
2024-02-24 11:09:34 -05:00
<button
2024-04-04 20:35:09 -04:00
class="w3-button w3-theme-d1"
2024-02-24 11:09:34 -05:00
@click=${() =>
tfrpc.rpc.connect(self.renderRoot.getElementById('code').value)}
>
Connect
</button>
<h2>Broadcasts</h2>
<ul class="w3-ul w3-border">
2024-02-24 11:09:34 -05:00
${this.broadcasts
.filter((x) => x.address)
.map((x) => self.render_broadcast(x))}
</ul>
<h2>Connections</h2>
<ul class="w3-ul w3-border">
2024-02-24 11:09:34 -05:00
${this.connections
.filter((x) => x.tunnel === undefined)
2024-04-13 20:07:39 -04:00
.map(
(x) => html`
<li class="w3-bar">${this.render_connection(x)}</li>
`
)}
</ul>
<h2>Stored Connections</h2>
<ul class="w3-ul w3-border">
2024-02-24 11:09:34 -05:00
${this.stored_connections.map(
(x) => html`
<li class="w3-bar">
2024-02-24 11:09:34 -05:00
<button
class="w3-bar-item w3-button w3-theme-d1"
2024-02-24 11:09:34 -05:00
@click=${() => self.forget_stored_connection(x)}
>
Forget
</button>
<button
class="w3-bar-item w3-button w3-theme-d1"
2024-02-24 11:09:34 -05:00
@click=${() => tfrpc.rpc.connect(x)}
>
Connect
</button>
<div class="w3-bar-item">
<tf-user id=${x.pubkey} .users=${self.users}></tf-user>
<div><small>${x.address}:${x.port}</small></div>
</div>
2024-02-24 11:09:34 -05:00
</li>
`
)}
</ul>
<h2>Local Accounts</h2>
<ul class="w3-ul w3-border">
2024-02-24 11:09:34 -05:00
${this.identities.map(
(x) =>
html`<li class="w3-bar">
<tf-user id=${x} .users=${this.users}></tf-user>
</li>`
2024-02-24 11:09:34 -05:00
)}
</ul>
</div>
`;
}
}
2024-02-24 11:09:34 -05:00
customElements.define('tf-tab-connections', TfTabConnectionsElement);