You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.2 KiB
49 lines
1.2 KiB
import { Controller } from "@hotwired/stimulus"; |
|
|
|
/** |
|
* After first paint, refreshes Nostr magazine indices (server-side, ≤5s) and swaps header/body HTML. |
|
*/ |
|
export default class extends Controller { |
|
static targets = ["headerNav", "pageBody"]; |
|
static values = { |
|
page: String, |
|
slug: String, |
|
url: String, |
|
}; |
|
|
|
connect() { |
|
this.sync(); |
|
} |
|
|
|
async sync() { |
|
const base = this.urlValue || "/ux/magazine-sync"; |
|
const params = new URLSearchParams(); |
|
params.set("page", this.pageValue || "article"); |
|
const slug = this.slugValue || ""; |
|
if (slug !== "") { |
|
params.set("slug", slug); |
|
} |
|
const url = `${base}?${params.toString()}`; |
|
try { |
|
const res = await fetch(url, { |
|
headers: { Accept: "application/json" }, |
|
credentials: "same-origin", |
|
}); |
|
if (!res.ok) { |
|
return; |
|
} |
|
const data = await res.json(); |
|
if (!data.ok) { |
|
return; |
|
} |
|
if (this.hasHeaderNavTarget && data.header) { |
|
this.headerNavTarget.outerHTML = data.header; |
|
} |
|
if (this.hasPageBodyTarget && data.body) { |
|
this.pageBodyTarget.outerHTML = data.body; |
|
} |
|
} catch { |
|
/* ignore network errors */ |
|
} |
|
} |
|
}
|
|
|