Add better interface

This commit is contained in:
Tobias Baunbaek
2024-01-29 15:26:28 +01:00
parent 3cbd6212c3
commit f45f5ad337
2 changed files with 75 additions and 21 deletions

View File

@@ -2,6 +2,7 @@
import http from 'http' import http from 'http'
import { WebSocketServer } from 'ws' import { WebSocketServer } from 'ws'
import { Session } from '@holepunchto/pear-inspect' import { Session } from '@holepunchto/pear-inspect'
import b4a from 'b4a'
customElements.define('devtools-inspector', class extends HTMLElement { customElements.define('devtools-inspector', class extends HTMLElement {
constructor () { constructor () {
@@ -17,24 +18,66 @@ customElements.define('devtools-inspector', class extends HTMLElement {
#add-key-error { #add-key-error {
color: red; color: red;
} }
#no-apps.hidden {
display: none;
}
#code-installation {
white-space: pre-line;
padding-bottom: 1rem;
font-size: 10px;
}
.app {
display: flex;
align-items: center;
}
.app .title {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.app:hover .remove {
display: block;
}
.app .remove {
cursor: pointer;
padding-right: 10px;
margin-left: calc(-1rem - 10px);
width: 1rem;
display: none;
}
</style> </style>
<h1>Inspector</h1> <h1>Inspector</h1>
<div> <div>
This acts as a link between chrome://inspect and debugging Pear apps Use DevTools to debug your Pear apps
</div> </div>
<ol> <ol>
<li>Run a pear app with "pear dev . --inspect"</li> <li>npm install pear-inspect in your app</li>
<li>The outputted key should be added here</li> <li>
Add this code to your app:
<div id="code-installation">
import inspector from 'inspector'
import { Inspector } from 'pear-inspect'
const pearInspector = await new Inspector({ inspector }).enable()
const { publicKey } = await pearInspector.enable()
console.log('Pear Inspector key:', publicKey.toString('hex'))
</div>
</li>
<li>Running your program and the Pear Inspector key will be logged</li>
<li>Paste the logged key here</li>
<li>In Chrome, open chrome://inspect and the app should appear under Targets</li> <li>In Chrome, open chrome://inspect and the app should appear under Targets</li>
</ol> </ol>
<div> <div>
<form id="add-key-form"> <form id="add-key-form">
<input id="add-key-input" type="text" placeholder="Id given when running pear with --inspect"/> <input id="add-key-input" type="text" placeholder="Pear Inspector key"/>
<p id="add-key-error"></p> <p id="add-key-error"></p>
</form> </form>
</div> </div>
<h3 id="no-apps">No apps availble. Add a key above, to start debugging.</h3> <h2>Apps</h2>
<h3 id="no-apps">No apps added. Add a key above, to start debugging.</h3>
<div id="apps"></div> <div id="apps"></div>
</div> </div>
` `
@@ -48,20 +91,20 @@ customElements.define('devtools-inspector', class extends HTMLElement {
this.noAppsElem = this.root.querySelector('#no-apps') this.noAppsElem = this.root.querySelector('#no-apps')
this.apps = new Map() this.apps = new Map()
this.addKeyInputElem.addEventListener('keypress', e => {
this.addKeyErrorElem.textContent = ''
})
this.addKeyFormElem.addEventListener('submit', e => { this.addKeyFormElem.addEventListener('submit', e => {
e.preventDefault() e.preventDefault()
const publicKey = this.addKeyInputElem.value const publicKey = this.addKeyInputElem.value
if (publicKey.length === 0) {
this.addKeyErrorElem.textContent = ''
return
}
if (publicKey.length !== 64) { if (publicKey.length !== 64) {
this.addKeyErrorElem.textContent = 'Key needs to be 64 characters long' this.addKeyErrorElem.textContent = 'Key needs to be 64 characters long'
return return
} }
const sessionId = generateUuid() const sessionId = generateUuid()
const inspectorSession = new Session({ publicKey: Buffer.from(publicKey, 'hex') }) const inspectorSession = new Session({ publicKey: b4a.from(publicKey, 'hex') })
const app = { const app = {
publicKey, publicKey,
title: '', title: '',
@@ -153,17 +196,28 @@ customElements.define('devtools-inspector', class extends HTMLElement {
} }
renderApps () { renderApps () {
const content = [...this.apps.values()].map(app => ` this.appsElem.replaceChildren(...[...this.apps].map(([sessionId, app]) => {
<div> const div = document.createElement('div')
<div>Title: ${app.title}</div> div.innerHTML = `
<div>URL: ${app.url}</div> <div class="app">
<div>Public key: ${app.publicKey}</div> <div class="remove" data-session-id="${sessionId}">✕</div>
<div>Connected: ${app.connected ? 'Yes' : 'No'}</div> <div class="title">${app.title} (${app.url})</div>
</div> </div>
`) `
this.appsElem.innerHTML = content div.querySelector('.remove').addEventListener('click', () => {
this.apps.delete(sessionId)
this.renderApps()
})
return div
}))
if (this.apps.size > 0) {
this.noAppsElem.classList.add('hidden')
} else {
this.noAppsElem.classList.remove('hidden')
}
if (this.apps.size > 0) this.noAppsElem.remove()
} }
load () { load () {

View File

@@ -10,8 +10,8 @@
} }
}, },
"dependencies": { "dependencies": {
"bare-path": "^2.1.0",
"@holepunchto/pear-inspect": "^2.0.0", "@holepunchto/pear-inspect": "^2.0.0",
"bare-path": "^2.1.0",
"redhat-overpass-font": "^1.0.0", "redhat-overpass-font": "^1.0.0",
"ws": "^8.16.0" "ws": "^8.16.0"
}, },