Files
akkounts/app/javascript/controllers/nostr_image_upload_controller.js
raucao be133af6fa
CI / Test (pull_request) Successful in 49s
Release Drafter / Update release notes draft (pull_request) Successful in 37s
Upload avatar and header images from the Nostr profile editor
The editor gains edit affordances over the avatar and header image and an upload panel that stores the image (previewing the server-cropped result) and records it as pending. Saving the profile signs a Blossom upload token, flushes the pending images, merges the returned URLs into the kind-0 content, and keeps pending state until the publish succeeds so a retry re-flushes.
2026-11-19 21:58:12 +04:00

131 lines
3.7 KiB
JavaScript

import { Controller } from "@hotwired/stimulus"
import { csrfToken } from "lib/csrf"
const ALLOWED_TYPES = [ "image/jpeg", "image/png", "image/webp", "image/gif" ]
const TITLES = { picture: "Avatar", banner: "Header image" }
export default class extends Controller {
static targets = [ "panel", "title", "description", "file", "preview", "error", "submit" ]
static values = {
field: String,
storeUrl: String,
maxBytes: { type: Number, default: 0 },
pictureMaxBytes: Number,
bannerMaxBytes: Number,
descriptions: { type: Object, default: {} }
}
static outlets = [ "nostr-profile-editor" ]
open (event) {
this.fieldValue = event.params.field
this.opener = event.currentTarget
this.maxBytesValue = this.fieldValue === "banner" ? this.bannerMaxBytesValue : this.pictureMaxBytesValue
if (this.hasTitleTarget) this.titleTarget.textContent = TITLES[this.fieldValue] || "Image"
if (this.hasDescriptionTarget) this.descriptionTarget.textContent = this.descriptionsValue[this.fieldValue] || ""
this.clearError()
this.resetFile()
this.panelTarget.classList.remove("hidden")
}
close () {
this.panelTarget.classList.add("hidden")
this.clearError()
this.resetFile()
this.setBusy(false)
if (this.opener && typeof this.opener.focus === "function") this.opener.focus()
this.opener = null
}
closeBackground (event) {
if (event.target === this.panelTarget) this.close()
}
preview () {
const file = this.selectedFile()
if (!file || !this.hasPreviewTarget) return
if (this.objectUrl) URL.revokeObjectURL(this.objectUrl)
this.objectUrl = URL.createObjectURL(file)
this.previewTarget.src = this.objectUrl
this.previewTarget.classList.remove("hidden")
}
async save (event) {
event.preventDefault()
this.clearError()
const file = this.selectedFile()
if (!file) { this.showError("Please choose an image."); return }
if (!ALLOWED_TYPES.includes(file.type)) { this.showError("Unsupported image type."); return }
if (this.maxBytesValue && file.size > this.maxBytesValue) { this.showError("This image is too large."); return }
this.setBusy(true)
try {
const response = await fetch(this.storeUrlValue, {
method: "POST",
credentials: "include",
headers: { "Accept": "application/json", "X-CSRF-Token": csrfToken() },
body: this.formData(file)
})
const data = await response.json().catch(() => ({}))
if (!response.ok) throw new Error(data.error || `Upload failed (HTTP ${response.status})`)
if (this.hasNostrProfileEditorOutlet) {
this.nostrProfileEditorOutlet.setPending(this.fieldValue, data.sha256, data.previewUrl)
}
this.close()
} catch (error) {
this.showError(error.message)
} finally {
this.setBusy(false)
}
}
resetFile () {
if (this.hasFileTarget) this.fileTarget.value = ""
if (this.objectUrl) {
URL.revokeObjectURL(this.objectUrl)
this.objectUrl = null
}
if (this.hasPreviewTarget) {
this.previewTarget.removeAttribute("src")
this.previewTarget.classList.add("hidden")
}
}
selectedFile () {
return this.hasFileTarget ? this.fileTarget.files[0] : null
}
formData (file) {
const form = new FormData()
form.append("field", this.fieldValue)
form.append("file", file)
return form
}
setBusy (busy) {
if (!this.hasSubmitTarget) return
this.submitTarget.disabled = busy
}
showError (message) {
if (!this.hasErrorTarget) return
this.errorTarget.textContent = message
this.errorTarget.classList.remove("hidden")
}
clearError () {
if (!this.hasErrorTarget) return
this.errorTarget.textContent = ""
this.errorTarget.classList.add("hidden")
}
}