Files
akkounts/app/javascript/controllers/nostr_metadata_controller.js
raucao 355d0a2fb0
CI / Test (pull_request) Successful in 47s
Release Drafter / Update release notes draft (pull_request) Successful in 3s
Add a "Just a moment..." title to the metadata loading state
The title sits above the rotating status messages and disappears with
the loading state; the status messages no longer carry their own
ellipses.
2026-11-19 10:58:35 +04:00

116 lines
3.0 KiB
JavaScript

import { Controller } from "@hotwired/stimulus"
const REQUEST_TIMEOUT = 60000
const REFRESH_ATTEMPTS = 5
const REFRESH_DELAY = 2000
const STEP_FOR_KIND = { 10002: "relays", 0: "profile", 10063: "blossom" }
const MESSAGES = {
relays: "Looking up your relay list",
profile: "Fetching your profile",
blossom: "Fetching your media server list"
}
const WAITING_MESSAGE = "Waiting for your changes to appear"
export default class extends Controller {
static targets = [ "status", "loading", "content", "retry" ]
static values = { url: String }
connect () {
this.runStep("relays")
}
refresh (event) {
const detail = event.detail || {}
this.contentTarget.classList.add("hidden")
this.contentTarget.innerHTML = ""
this.loadingTarget.classList.remove("hidden")
this.runStep("relays", {
message: "Saved. Updating",
expectedKind: detail.kind,
expectedId: detail.eventId,
attempts: 0
})
}
retry () {
this.runStep("relays")
}
async runStep (step, options = {}) {
this.hideRetry()
const message = options.message || MESSAGES[step]
if (message) {
this.statusTarget.textContent = message
}
try {
const res = await this.request(step)
if (!res.ok) {
throw new Error(`HTTP ${res.status}`)
}
if (step === "render") {
const html = await res.text()
this.loadingTarget.classList.add("hidden")
this.contentTarget.classList.remove("hidden")
this.contentTarget.innerHTML = html
} else {
const data = await res.json()
if (this.awaitingUpdate(step, data, options)) {
const attempts = (options.attempts || 0) + 1
setTimeout(
() => this.runStep(step, { ...options, message: WAITING_MESSAGE, attempts }),
REFRESH_DELAY
)
return
}
if (data.next) {
this.runStep(data.next, { ...options, message: null })
}
}
} catch (error) {
console.warn("Nostr metadata fetch failed:", error.message)
this.statusTarget.textContent = "Something went wrong. You can try again."
this.showRetry()
}
}
async request (step) {
const controller = new AbortController()
const timer = setTimeout(() => controller.abort(), REQUEST_TIMEOUT)
const headers = (step === "render")
? { "Accept": "text/html" }
: { "Accept": "application/json" }
try {
return await fetch(`${this.urlValue}?step=${step}`, {
headers,
signal: controller.signal
})
} finally {
clearTimeout(timer)
}
}
awaitingUpdate (step, data, options) {
return step === (STEP_FOR_KIND[options.expectedKind] || "relays") &&
Boolean(options.expectedId) &&
data.event_id !== options.expectedId &&
(options.attempts || 0) < REFRESH_ATTEMPTS
}
showRetry () {
if (this.hasRetryTarget) this.retryTarget.classList.remove("hidden")
}
hideRetry () {
if (this.hasRetryTarget) this.retryTarget.classList.add("hidden")
}
}