| 
									
										
										
										
											2022-09-11 17:42:41 +00:00
										 |  |  | import {LitElement, html} from './lit-all.min.js'; | 
					
						
							|  |  |  | import * as tfrpc from '/static/tfrpc.js'; | 
					
						
							| 
									
										
										
										
											2024-01-13 02:55:52 +00:00
										 |  |  | import {styles} from './tf-styles.js'; | 
					
						
							| 
									
										
										
										
											2022-09-11 17:42:41 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class TfTabConnectionsElement extends LitElement { | 
					
						
							|  |  |  | 	static get properties() { | 
					
						
							|  |  |  | 		return { | 
					
						
							|  |  |  | 			broadcasts: {type: Array}, | 
					
						
							|  |  |  | 			identities: {type: Array}, | 
					
						
							| 
									
										
										
										
											2024-05-11 09:58:24 -04:00
										 |  |  | 			my_identities: {type: Array}, | 
					
						
							| 
									
										
										
										
											2022-09-11 17:42:41 +00:00
										 |  |  | 			connections: {type: Array}, | 
					
						
							| 
									
										
										
										
											2023-01-18 00:57:54 +00:00
										 |  |  | 			stored_connections: {type: Array}, | 
					
						
							| 
									
										
										
										
											2022-09-11 17:42:41 +00:00
										 |  |  | 			users: {type: Object}, | 
					
						
							| 
									
										
										
										
											2024-05-11 09:33:38 -04:00
										 |  |  | 			server_identity: {type: String}, | 
					
						
							| 
									
										
										
										
											2023-03-29 22:02:12 +00:00
										 |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2022-09-11 17:42:41 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-13 02:55:52 +00:00
										 |  |  | 	static styles = styles; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-14 19:23:01 -04:00
										 |  |  | 	static k_broadcast_emojis = { | 
					
						
							|  |  |  | 		discovery: '🏓', | 
					
						
							|  |  |  | 		room: '🚪', | 
					
						
							|  |  |  | 		peer_exchange: '🕸', | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-11 17:42:41 +00:00
										 |  |  | 	constructor() { | 
					
						
							|  |  |  | 		super(); | 
					
						
							|  |  |  | 		let self = this; | 
					
						
							|  |  |  | 		this.broadcasts = []; | 
					
						
							|  |  |  | 		this.identities = []; | 
					
						
							| 
									
										
										
										
											2024-05-11 09:58:24 -04:00
										 |  |  | 		this.my_identities = []; | 
					
						
							| 
									
										
										
										
											2022-09-11 17:42:41 +00:00
										 |  |  | 		this.connections = []; | 
					
						
							| 
									
										
										
										
											2023-01-18 00:57:54 +00:00
										 |  |  | 		this.stored_connections = []; | 
					
						
							| 
									
										
										
										
											2022-09-11 17:42:41 +00:00
										 |  |  | 		this.users = {}; | 
					
						
							| 
									
										
										
										
											2024-05-11 09:58:24 -04:00
										 |  |  | 		tfrpc.rpc.getIdentities().then(function (identities) { | 
					
						
							|  |  |  | 			self.my_identities = identities || []; | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2024-02-24 11:09:34 -05:00
										 |  |  | 		tfrpc.rpc.getAllIdentities().then(function (identities) { | 
					
						
							| 
									
										
										
										
											2022-09-11 17:42:41 +00:00
										 |  |  | 			self.identities = identities || []; | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2024-02-24 11:09:34 -05:00
										 |  |  | 		tfrpc.rpc.getStoredConnections().then(function (connections) { | 
					
						
							| 
									
										
										
										
											2023-01-18 00:57:54 +00:00
										 |  |  | 			self.stored_connections = connections || []; | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2024-05-11 09:33:38 -04:00
										 |  |  | 		tfrpc.rpc.getServerIdentity().then(function (identity) { | 
					
						
							|  |  |  | 			self.server_identity = identity; | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2022-09-11 17:42:41 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-09 01:47:47 +00:00
										 |  |  | 	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>`; | 
					
						
							| 
									
										
										
										
											2022-11-09 03:51:31 +00:00
										 |  |  | 		} else if (connection.tunnel) { | 
					
						
							| 
									
										
										
										
											2024-04-13 19:29:31 -04:00
										 |  |  | 			return html`<div>room peer</div>`; | 
					
						
							| 
									
										
										
										
											2022-11-09 01:47:47 +00:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			return JSON.stringify(connection); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-09 03:51:31 +00: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-09 03:51:31 +00: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-09 03:51:31 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-13 03:36:48 +00:00
										 |  |  | 	async _tunnel(portal, target) { | 
					
						
							| 
									
										
										
										
											2023-01-17 23:10:17 +00:00
										 |  |  | 		return tfrpc.rpc.createTunnel(portal, target); | 
					
						
							| 
									
										
										
										
											2022-11-13 03:36:48 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	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> | 
					
						
							| 
									
										
										
										
											2023-09-06 22:48:06 +00:00
										 |  |  | 				<tf-user id=${connection.pubkey} .users=${this.users}></tf-user> 📡 | 
					
						
							| 
									
										
										
										
											2022-11-13 03:36:48 +00:00
										 |  |  | 			</li> | 
					
						
							|  |  |  | 		`;
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-09 03:51:31 +00:00
										 |  |  | 	render_broadcast(connection) { | 
					
						
							|  |  |  | 		return html`
 | 
					
						
							| 
									
										
										
										
											2024-04-13 19:29:31 -04:00
										 |  |  | 			<li class="w3-bar" style="overflow: hidden; overflow-wrap: nowrap"> | 
					
						
							| 
									
										
										
										
											2024-02-24 11:09:34 -05:00
										 |  |  | 				<button | 
					
						
							| 
									
										
										
										
											2024-04-13 19:29:31 -04:00
										 |  |  | 					class="w3-bar-item w3-button w3-theme-d1" | 
					
						
							| 
									
										
										
										
											2024-02-24 11:09:34 -05:00
										 |  |  | 					@click=${() => tfrpc.rpc.connect(connection)} | 
					
						
							|  |  |  | 				> | 
					
						
							|  |  |  | 					Connect | 
					
						
							|  |  |  | 				</button> | 
					
						
							| 
									
										
										
										
											2024-04-13 19:29:31 -04:00
										 |  |  | 				<div class="w3-bar-item"> | 
					
						
							| 
									
										
										
										
											2024-08-14 19:23:01 -04:00
										 |  |  | 					${TfTabConnectionsElement.k_broadcast_emojis[connection.origin]} | 
					
						
							| 
									
										
										
										
											2024-04-13 19:29:31 -04:00
										 |  |  | 					<tf-user id=${connection.pubkey} .users=${this.users}></tf-user> | 
					
						
							|  |  |  | 					${this.render_connection_summary(connection)} | 
					
						
							|  |  |  | 				</div> | 
					
						
							| 
									
										
										
										
											2022-11-09 03:51:31 +00:00
										 |  |  | 			</li> | 
					
						
							| 
									
										
										
										
											2023-03-29 22:02:12 +00:00
										 |  |  | 		`;
 | 
					
						
							| 
									
										
										
										
											2022-11-09 03:51:31 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-18 00:57:54 +00:00
										 |  |  | 	async forget_stored_connection(connection) { | 
					
						
							|  |  |  | 		await tfrpc.rpc.forgetStoredConnection(connection); | 
					
						
							|  |  |  | 		this.stored_connections = (await tfrpc.rpc.getStoredConnections()) || []; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-06 22:48:06 +00:00
										 |  |  | 	render_connection(connection) { | 
					
						
							| 
									
										
										
										
											2024-05-12 08:23:34 -04:00
										 |  |  | 		let requests = Object.values( | 
					
						
							|  |  |  | 			connection.requests.reduce(function (accumulator, value) { | 
					
						
							|  |  |  | 				let key = `${value.name}:${Math.sign(value.request_number)}`; | 
					
						
							|  |  |  | 				if (!accumulator[key]) { | 
					
						
							|  |  |  | 					accumulator[key] = Object.assign({count: 0}, value); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				accumulator[key].count++; | 
					
						
							|  |  |  | 				return accumulator; | 
					
						
							|  |  |  | 			}, {}) | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2023-09-06 22:48:06 +00:00
										 |  |  | 		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> | 
					
						
							| 
									
										
										
										
											2023-09-06 22:48:06 +00: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})`} | 
					
						
							| 
									
										
										
										
											2024-05-12 08:23:34 -04:00
										 |  |  | 			<div> | 
					
						
							|  |  |  | 				${requests.map( | 
					
						
							|  |  |  | 					(x) => html`
 | 
					
						
							|  |  |  | 						<span class="w3-tag w3-small" | 
					
						
							|  |  |  | 							>${x.request_number > 0 ? '🟩' : '🟥'} ${x.name} | 
					
						
							|  |  |  | 							<span | 
					
						
							|  |  |  | 								class="w3-badge w3-white" | 
					
						
							|  |  |  | 								style=${x.count > 1 ? undefined : 'display: none'} | 
					
						
							|  |  |  | 								>${x.count}</span | 
					
						
							|  |  |  | 							></span | 
					
						
							|  |  |  | 						> | 
					
						
							|  |  |  | 					`
 | 
					
						
							|  |  |  | 				)} | 
					
						
							|  |  |  | 			</div> | 
					
						
							| 
									
										
										
										
											2023-09-06 22:48:06 +00: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 22:48:06 +00:00
										 |  |  | 				${this.render_room_peers(connection.id)} | 
					
						
							|  |  |  | 			</ul> | 
					
						
							|  |  |  | 		`;
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-11 17:42:41 +00:00
										 |  |  | 	render() { | 
					
						
							| 
									
										
										
										
											2022-11-09 01:47:47 +00:00
										 |  |  | 		let self = this; | 
					
						
							| 
									
										
										
										
											2022-09-11 17:42:41 +00:00
										 |  |  | 		return html`
 | 
					
						
							| 
									
										
										
										
											2024-04-13 19:29:31 -04:00
										 |  |  | 			<div class="w3-container" style="box-sizing: border-box"> | 
					
						
							| 
									
										
										
										
											2024-01-13 02:55:52 +00:00
										 |  |  | 				<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> | 
					
						
							| 
									
										
										
										
											2024-01-13 02:55:52 +00:00
										 |  |  | 				<h2>Broadcasts</h2> | 
					
						
							| 
									
										
										
										
											2024-04-13 19:29:31 -04:00
										 |  |  | 				<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))} | 
					
						
							| 
									
										
										
										
											2024-01-13 02:55:52 +00:00
										 |  |  | 				</ul> | 
					
						
							|  |  |  | 				<h2>Connections</h2> | 
					
						
							| 
									
										
										
										
											2024-04-13 19:29:31 -04:00
										 |  |  | 				<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> | 
					
						
							|  |  |  | 							`
 | 
					
						
							|  |  |  | 						)} | 
					
						
							| 
									
										
										
										
											2024-01-13 02:55:52 +00:00
										 |  |  | 				</ul> | 
					
						
							| 
									
										
										
										
											2024-04-13 19:29:31 -04:00
										 |  |  | 				<h2>Stored Connections</h2> | 
					
						
							|  |  |  | 				<ul class="w3-ul w3-border"> | 
					
						
							| 
									
										
										
										
											2024-02-24 11:09:34 -05:00
										 |  |  | 					${this.stored_connections.map( | 
					
						
							|  |  |  | 						(x) => html`
 | 
					
						
							| 
									
										
										
										
											2024-04-13 19:29:31 -04:00
										 |  |  | 							<li class="w3-bar"> | 
					
						
							| 
									
										
										
										
											2024-02-24 11:09:34 -05:00
										 |  |  | 								<button | 
					
						
							| 
									
										
										
										
											2024-04-13 19:29:31 -04:00
										 |  |  | 									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 | 
					
						
							| 
									
										
										
										
											2024-04-13 19:29:31 -04:00
										 |  |  | 									class="w3-bar-item w3-button w3-theme-d1" | 
					
						
							| 
									
										
										
										
											2024-02-24 11:09:34 -05:00
										 |  |  | 									@click=${() => tfrpc.rpc.connect(x)} | 
					
						
							|  |  |  | 								> | 
					
						
							|  |  |  | 									Connect | 
					
						
							|  |  |  | 								</button> | 
					
						
							| 
									
										
										
										
											2024-04-13 19:29:31 -04:00
										 |  |  | 								<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> | 
					
						
							|  |  |  | 						`
 | 
					
						
							|  |  |  | 					)} | 
					
						
							| 
									
										
										
										
											2024-01-13 02:55:52 +00:00
										 |  |  | 				</ul> | 
					
						
							|  |  |  | 				<h2>Local Accounts</h2> | 
					
						
							| 
									
										
										
										
											2024-04-13 19:29:31 -04:00
										 |  |  | 				<ul class="w3-ul w3-border"> | 
					
						
							| 
									
										
										
										
											2024-02-24 11:09:34 -05:00
										 |  |  | 					${this.identities.map( | 
					
						
							|  |  |  | 						(x) => | 
					
						
							| 
									
										
										
										
											2024-04-13 19:29:31 -04:00
										 |  |  | 							html`<li class="w3-bar">
 | 
					
						
							| 
									
										
										
										
											2024-05-12 07:48:34 -04:00
										 |  |  | 								${x == this.server_identity | 
					
						
							|  |  |  | 									? html`<span class="w3-tag w3-medium w3-round w3-theme-l1"
 | 
					
						
							|  |  |  | 											>🖥 local server</span | 
					
						
							|  |  |  | 										>`
 | 
					
						
							|  |  |  | 									: undefined} | 
					
						
							|  |  |  | 								${this.my_identities.indexOf(x) != -1 | 
					
						
							|  |  |  | 									? html`<span class="w3-tag w3-medium w3-round w3-theme-d1"
 | 
					
						
							|  |  |  | 											>😎 you</span | 
					
						
							|  |  |  | 										>`
 | 
					
						
							|  |  |  | 									: undefined} | 
					
						
							| 
									
										
										
										
											2024-04-13 19:29:31 -04:00
										 |  |  | 								<tf-user id=${x} .users=${this.users}></tf-user> | 
					
						
							|  |  |  | 							</li>` | 
					
						
							| 
									
										
										
										
											2024-02-24 11:09:34 -05:00
										 |  |  | 					)} | 
					
						
							| 
									
										
										
										
											2024-01-13 02:55:52 +00:00
										 |  |  | 				</ul> | 
					
						
							| 
									
										
										
										
											2022-11-20 02:09:52 +00:00
										 |  |  | 			</div> | 
					
						
							| 
									
										
										
										
											2022-09-11 17:42:41 +00:00
										 |  |  | 		`;
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 11:09:34 -05:00
										 |  |  | customElements.define('tf-tab-connections', TfTabConnectionsElement); |