import { html, LitElement } from "./lib/lit.min.js"; import { unixNow, newExpirationValue, formatBytes } from "./utils.js"; export class ListForm extends LitElement { static properties = { pubkey: { state: true }, blobs: { state: true }, status: { state: true, type: String }, }; createRenderRoot() { return this; } _auth = null; async submit(e) { e.preventDefault(); const formData = new FormData(e.target); this.pubkey = formData.get("pubkey") || (await window.nostr?.getPublicKey()); if (!this.pubkey) return; this.status = "Signing..."; this._auth = await window.nostr.signEvent({ kind: 24242, content: "List Blobs", created_at: unixNow(), tags: [ ["t", "list"], ["expiration", newExpirationValue()], ["server", new URL(location.protocol + "//" + location.hostname).toString()], ], }); this.status = "Fetching..."; this.blobs = await fetch("/list/" + this.pubkey, { headers: { authorization: "Nostr " + btoa(JSON.stringify(this._auth)) }, }).then((res) => res.json()); this.status = undefined; } async refresh() { this.blobs = await fetch("/list/" + this.pubkey, { headers: { authorization: "Nostr " + btoa(JSON.stringify(this._auth)) }, }).then((res) => res.json()); } inputChange(e) { this.pubkey = e.target.files[0]; } async pubkeyFromExtension(e) { e.preventDefault(); this.pubkey = await window.nostr.getPublicKey(); } async deleteBlob(blob) { const auth = await window.nostr.signEvent({ kind: 24242, content: "Delete Item", created_at: unixNow(), tags: [ ["t", "delete"], ["x", blob.sha256], ["expiration", newExpirationValue()], ], }); await fetch("/" + blob.sha256, { method: "DELETE", headers: { authorization: "Nostr " + btoa(JSON.stringify(auth)) }, }).then(async (res) => { if (res.ok) { alert("Blob deleted"); await this.refresh(); } else alert(await res.text()); }); } renderResults() { return html`
sha256 | Type | Size | |
---|---|---|---|
${blob.sha256} | ${blob.type} | ${formatBytes(blob.size)} | { e.preventDefault(); this.deleteBlob(blob); }}" > Delete |
${this.status}
`; } else if (this.blobs) { if (this.blobs.length === 0) return html`Set pubkey
`; return this.renderResults(); } return html`Set pubkey
`; } render() { if (this.status) { return html`${this.status}
`; } return html`