Update the signer section in place instead of reloading the page
Disconnecting, cancelling and (when a key already exists) connecting now swap the Nostr Connect section via Turbo Streams, so the page's relay/profile/media lookups are not re-fetched. A replace still reloads because it changes the key, and a first-time connect reloads to reveal the pubkey and metadata.
This commit is contained in:
9 files changed
+189
-72
No files matched your search
@@ -33,7 +33,8 @@
|
||||
<div data-controller="nostr-signer-connect"
|
||||
data-nostr-signer-connect-status-url-value="<%= @status_url %>"
|
||||
data-nostr-signer-connect-replace-url-value="<%= @replace_url %>"
|
||||
data-nostr-signer-connect-destroy-url-value="<%= @destroy_url %>">
|
||||
data-nostr-signer-connect-destroy-url-value="<%= @destroy_url %>"
|
||||
data-nostr-signer-connect-section-url-value="<%= @section_url %>">
|
||||
<% unless @pending_session.bunker? %>
|
||||
<div data-nostr-signer-connect-target="details">
|
||||
<div class="<%= @title.present? ? "mt-4" : "" %> hidden justify-center sm:flex"
|
||||
@@ -131,7 +132,7 @@
|
||||
|
||||
<details class="mt-4">
|
||||
<summary class="cursor-pointer text-gray-600">Prefer a bunker:// URI?</summary>
|
||||
<%= form_with url: @create_url, method: :post, local: true, class: "mt-3" do %>
|
||||
<%= form_with url: @create_url, method: :post, class: "mt-3" do %>
|
||||
<p>
|
||||
<%= text_field_tag :bunker_uri, nil,
|
||||
placeholder: "bunker://…",
|
||||
|
||||
@@ -3,7 +3,8 @@ require "rqrcode"
|
||||
class NostrSignerConnectComponent < ViewComponent::Base
|
||||
def initialize(pending_session: nil, active_session: nil, create_url:, destroy_url: nil,
|
||||
status_url: nil, replace_url: nil, title: "Nostr Connect", description: nil,
|
||||
show_disconnect: true, modal: false, collapsible: false, flush: false)
|
||||
show_disconnect: true, modal: false, collapsible: false, flush: false,
|
||||
section_url: nil)
|
||||
@pending_session = pending_session
|
||||
@active_session = active_session
|
||||
@create_url = create_url
|
||||
@@ -16,6 +17,7 @@ class NostrSignerConnectComponent < ViewComponent::Base
|
||||
@modal = modal
|
||||
@collapsible = collapsible
|
||||
@flush = flush
|
||||
@section_url = section_url
|
||||
end
|
||||
|
||||
def section_class
|
||||
|
||||
@@ -37,6 +37,10 @@ module Settings
|
||||
|
||||
respond_to do |format|
|
||||
format.html { redirect_to setting_path(:nostr) }
|
||||
format.turbo_stream do
|
||||
@pending_signer_session = signer_session
|
||||
render turbo_stream: turbo_stream.replace("nostr-signer-section", partial: "settings/nostr_signer")
|
||||
end
|
||||
format.json do
|
||||
render json: {
|
||||
token: signer_session.token,
|
||||
@@ -75,6 +79,15 @@ module Settings
|
||||
end
|
||||
end
|
||||
|
||||
def section
|
||||
respond_to do |format|
|
||||
format.turbo_stream do
|
||||
render turbo_stream: turbo_stream.replace("nostr-signer-section", partial: "settings/nostr_signer")
|
||||
end
|
||||
format.html { redirect_to setting_path(:nostr) }
|
||||
end
|
||||
end
|
||||
|
||||
def replace
|
||||
signer_session = NostrSignerSession.find_by_token(params[:token])
|
||||
return render_error("Unknown connection", :not_found) if signer_session.nil? ||
|
||||
@@ -97,8 +110,15 @@ module Settings
|
||||
session.delete(:nostr_signer_token)
|
||||
|
||||
respond_to do |format|
|
||||
format.html { redirect_to setting_path(:nostr), flash: { success: "Remote signer disconnected" } }
|
||||
format.html { redirect_to setting_path(:nostr) }
|
||||
format.json { render json: { ok: true } }
|
||||
format.turbo_stream do
|
||||
if current_user.nostr_pubkey.present?
|
||||
render turbo_stream: turbo_stream.replace("nostr-signer-section", partial: "settings/nostr_signer")
|
||||
else
|
||||
redirect_to setting_path(:nostr)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ const MAX_ATTEMPTS = 160
|
||||
|
||||
export default class extends Controller {
|
||||
static targets = [ "status", "error", "authWrapper", "authLink", "details", "retry", "signerHint", "qr", "qrToggle", "replace", "replaceExisting", "replaceSigner" ]
|
||||
static values = { statusUrl: String, replaceUrl: String, destroyUrl: String }
|
||||
static values = { statusUrl: String, replaceUrl: String, destroyUrl: String, sectionUrl: String }
|
||||
|
||||
connect () {
|
||||
this.attempts = 0
|
||||
@@ -36,8 +36,7 @@ export default class extends Controller {
|
||||
}
|
||||
|
||||
if (data.status === "connected") {
|
||||
this.statusTarget.textContent = "Connected. Redirecting…"
|
||||
window.location.href = data.redirect_url || window.location.href
|
||||
this.finishConnection(data.redirect_url)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -157,8 +156,8 @@ export default class extends Controller {
|
||||
}
|
||||
|
||||
if (data.status === "connected") {
|
||||
this.statusTarget.classList.remove("hidden")
|
||||
this.statusTarget.textContent = "Connected. Redirecting…"
|
||||
// Replacing links a different key, so the pubkey field, profile and
|
||||
// media server lists elsewhere on the page are stale — full reload.
|
||||
window.location.reload()
|
||||
return
|
||||
}
|
||||
@@ -178,10 +177,51 @@ export default class extends Controller {
|
||||
})
|
||||
}
|
||||
} catch (error) {
|
||||
// Ignore; reload below reflects whatever state the server is in.
|
||||
// Ignore; the section update below reflects whatever state the server is in.
|
||||
}
|
||||
|
||||
window.location.reload()
|
||||
this.finishConnection()
|
||||
}
|
||||
|
||||
// After the connection is established (or cancelled), update just the signer
|
||||
// section so the page's Nostr metadata isn't re-fetched from relays. Falls
|
||||
// back to a full reload when there is no section to update (login flow) or
|
||||
// when the account had no key yet (the pubkey field and metadata need a fresh
|
||||
// page).
|
||||
finishConnection (redirectUrl) {
|
||||
if (!this.hasSectionUrlValue || !this.sectionUrlValue) {
|
||||
if (this.hasStatusTarget) this.statusTarget.textContent = "Connected. Redirecting…"
|
||||
window.location.href = redirectUrl || window.location.href
|
||||
return
|
||||
}
|
||||
|
||||
if (!this.hasAccountKey()) {
|
||||
window.location.reload()
|
||||
return
|
||||
}
|
||||
|
||||
if (this.hasStatusTarget) this.statusTarget.textContent = "Connected. Updating…"
|
||||
this.refreshSection()
|
||||
}
|
||||
|
||||
hasAccountKey () {
|
||||
return Boolean(document.querySelector('[data-controller~="nostr-metadata"]'))
|
||||
}
|
||||
|
||||
async refreshSection () {
|
||||
try {
|
||||
const response = await fetch(this.sectionUrlValue, {
|
||||
credentials: "include",
|
||||
headers: { "Accept": "text/vnd.turbo-stream.html" }
|
||||
})
|
||||
if (!response.ok || response.redirected) {
|
||||
window.location.reload()
|
||||
return
|
||||
}
|
||||
window.Turbo.renderStreamMessage(await response.text())
|
||||
} catch (error) {
|
||||
window.location.reload()
|
||||
}
|
||||
}
|
||||
|
||||
showAuthUrl (url) {
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import { Controller } from "@hotwired/stimulus"
|
||||
|
||||
// Mirrors the signer section's connection state onto the document, so the
|
||||
// `data-remote-signer` flag stays correct when the section is replaced in place
|
||||
// (turbo stream) instead of via a full page load.
|
||||
export default class extends Controller {
|
||||
static values = { connected: Boolean }
|
||||
|
||||
connect () {
|
||||
document.documentElement.dataset.remoteSigner = this.connectedValue ? "true" : "false"
|
||||
}
|
||||
}
|
||||
@@ -47,66 +47,7 @@
|
||||
</p>
|
||||
<% end %>
|
||||
|
||||
<% if nostr_remote_signer_available? && @pending_signer_session.present? %>
|
||||
<div data-controller="modal"
|
||||
data-modal-open-value="true"
|
||||
data-action="turbo:load->modal#open">
|
||||
<%= render ModalComponent.new(
|
||||
show_close_button: false,
|
||||
dismissible: false,
|
||||
card_class: "w-96 max-w-[85vw]"
|
||||
) do %>
|
||||
<%= render NostrSignerConnectComponent.new(
|
||||
pending_session: @pending_signer_session,
|
||||
create_url: nostr_signer_settings_path,
|
||||
destroy_url: nostr_signer_settings_path,
|
||||
status_url: nostr_signer_status_settings_path(token: @pending_signer_session.token),
|
||||
replace_url: nostr_signer_replace_settings_path(token: @pending_signer_session.token),
|
||||
title: "Nostr Connect",
|
||||
modal: true
|
||||
) %>
|
||||
<% end %>
|
||||
</div>
|
||||
<% elsif current_user.nostr_pubkey.present? %>
|
||||
<% if nostr_remote_signer_available? %>
|
||||
<%= render NostrSignerConnectComponent.new(
|
||||
active_session: active_nostr_signer_session,
|
||||
create_url: nostr_signer_settings_path,
|
||||
destroy_url: nostr_signer_settings_path,
|
||||
collapsible: true
|
||||
) %>
|
||||
<% end %>
|
||||
<% else %>
|
||||
<div class="mt-8 flex flex-col gap-8 sm:flex-row">
|
||||
<div class="flex-1">
|
||||
<h4 class="mb-0">Browser extension</h4>
|
||||
<p class="mt-3 hidden" data-settings--nostr-pubkey-target="extensionDescription">
|
||||
Use a Nostr-signing browser extension, like Alby or nos2x
|
||||
</p>
|
||||
<p class="mt-3" data-settings--nostr-pubkey-target="extensionUnavailable">
|
||||
No browser extension detected. Install a Nostr signer like Alby to use this option.
|
||||
</p>
|
||||
<div class="mt-4">
|
||||
<button class="btn-md btn-gray w-full sm:w-auto" disabled
|
||||
data-settings--nostr-pubkey-target="setPubkey"
|
||||
data-action="settings--nostr-pubkey#setPubkey">
|
||||
Connect browser extension
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<% if nostr_remote_signer_available? %>
|
||||
<div class="flex-1">
|
||||
<%= render NostrSignerConnectComponent.new(
|
||||
active_session: active_nostr_signer_session,
|
||||
create_url: nostr_signer_settings_path,
|
||||
destroy_url: nostr_signer_settings_path,
|
||||
flush: true
|
||||
) %>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
<% end %>
|
||||
<%= render "nostr_signer" %>
|
||||
</section>
|
||||
|
||||
<% if current_user.nostr_pubkey.present? %>
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
<div id="nostr-signer-section"
|
||||
data-controller="nostr-signer-status"
|
||||
data-nostr-signer-status-connected-value="<%= active_nostr_signer_session.present? %>">
|
||||
<% if nostr_remote_signer_available? && @pending_signer_session.present? %>
|
||||
<div data-controller="modal"
|
||||
data-modal-open-value="true"
|
||||
data-action="turbo:load->modal#open">
|
||||
<%= render ModalComponent.new(
|
||||
show_close_button: false,
|
||||
dismissible: false,
|
||||
card_class: "w-96 max-w-[85vw]"
|
||||
) do %>
|
||||
<%= render NostrSignerConnectComponent.new(
|
||||
pending_session: @pending_signer_session,
|
||||
create_url: nostr_signer_settings_path,
|
||||
destroy_url: nostr_signer_settings_path,
|
||||
status_url: nostr_signer_status_settings_path(token: @pending_signer_session.token),
|
||||
replace_url: nostr_signer_replace_settings_path(token: @pending_signer_session.token),
|
||||
title: "Nostr Connect",
|
||||
modal: true,
|
||||
section_url: nostr_signer_section_settings_path
|
||||
) %>
|
||||
<% end %>
|
||||
</div>
|
||||
<% elsif current_user.nostr_pubkey.present? %>
|
||||
<% if nostr_remote_signer_available? %>
|
||||
<%= render NostrSignerConnectComponent.new(
|
||||
active_session: active_nostr_signer_session,
|
||||
create_url: nostr_signer_settings_path,
|
||||
destroy_url: nostr_signer_settings_path,
|
||||
collapsible: true
|
||||
) %>
|
||||
<% end %>
|
||||
<% else %>
|
||||
<div class="mt-8 flex flex-col gap-8 sm:flex-row">
|
||||
<div class="flex-1">
|
||||
<h4 class="mb-0">Browser extension</h4>
|
||||
<p class="mt-3 hidden" data-settings--nostr-pubkey-target="extensionDescription">
|
||||
Use a Nostr-signing browser extension, like Alby or nos2x
|
||||
</p>
|
||||
<p class="mt-3" data-settings--nostr-pubkey-target="extensionUnavailable">
|
||||
No browser extension detected. Install a Nostr signer like Alby to use this option.
|
||||
</p>
|
||||
<div class="mt-4">
|
||||
<button class="btn-md btn-gray w-full sm:w-auto" disabled
|
||||
data-settings--nostr-pubkey-target="setPubkey"
|
||||
data-action="settings--nostr-pubkey#setPubkey">
|
||||
Connect browser extension
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<% if nostr_remote_signer_available? %>
|
||||
<div class="flex-1">
|
||||
<%= render NostrSignerConnectComponent.new(
|
||||
active_session: active_nostr_signer_session,
|
||||
create_url: nostr_signer_settings_path,
|
||||
destroy_url: nostr_signer_settings_path,
|
||||
flush: true
|
||||
) %>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
@@ -79,6 +79,7 @@ Rails.application.routes.draw do
|
||||
post 'nostr_publish', to: 'settings/nostr_events#create', as: :nostr_publish
|
||||
post 'nostr_signer', to: 'settings/nostr_signers#create', as: :nostr_signer
|
||||
get 'nostr_signer/status', to: 'settings/nostr_signers#status', as: :nostr_signer_status
|
||||
get 'nostr_signer/section', to: 'settings/nostr_signers#section', as: :nostr_signer_section
|
||||
post 'nostr_signer/replace', to: 'settings/nostr_signers#replace', as: :nostr_signer_replace
|
||||
delete 'nostr_signer', to: 'settings/nostr_signers#destroy'
|
||||
post 'nostr_sign', to: 'settings/nostr_signatures#create', as: :nostr_sign
|
||||
|
||||
@@ -44,6 +44,15 @@ RSpec.describe "Settings::NostrSigners", type: :request do
|
||||
expect(data["uri"]).to start_with("nostrconnect://")
|
||||
end
|
||||
|
||||
it "replaces the signer section with the pending modal for turbo streams" do
|
||||
post nostr_signer_settings_path, headers: { "Accept" => "text/vnd.turbo-stream.html" }
|
||||
|
||||
expect(response.media_type).to eq("text/vnd.turbo-stream.html")
|
||||
expect(response.body).to include('target="nostr-signer-section"')
|
||||
expect(response.body).to include("nostrconnect://")
|
||||
expect(response.body).to include('data-modal-open-value="true"')
|
||||
end
|
||||
|
||||
it "renders the QR code and status poll inside an open modal" do
|
||||
post nostr_signer_settings_path, as: :json
|
||||
|
||||
@@ -83,6 +92,15 @@ RSpec.describe "Settings::NostrSigners", type: :request do
|
||||
end
|
||||
end
|
||||
|
||||
describe "GET /settings/nostr_signer/section" do
|
||||
it "replaces the signer section for turbo streams" do
|
||||
get nostr_signer_section_settings_path, headers: { "Accept" => "text/vnd.turbo-stream.html" }
|
||||
|
||||
expect(response.media_type).to eq("text/vnd.turbo-stream.html")
|
||||
expect(response.body).to include('target="nostr-signer-section"')
|
||||
end
|
||||
end
|
||||
|
||||
describe "GET /settings/nostr_signer/status" do
|
||||
it "stores the verified pubkey in LDAP once" do
|
||||
session = create_verified_session
|
||||
@@ -247,5 +265,22 @@ RSpec.describe "Settings::NostrSigners", type: :request do
|
||||
|
||||
expect(signer_session.reload.status).to eq("disconnecting")
|
||||
end
|
||||
|
||||
it "replaces the signer section for turbo streams when a key is linked" do
|
||||
allow_any_instance_of(User).to receive(:nostr_pubkey).and_return(user_pubkey)
|
||||
create_connected_session
|
||||
|
||||
delete nostr_signer_settings_path, headers: { "Accept" => "text/vnd.turbo-stream.html" }
|
||||
|
||||
expect(response.media_type).to eq("text/vnd.turbo-stream.html")
|
||||
expect(response.body).to include('target="nostr-signer-section"')
|
||||
expect(response.body).to include("Connect a signer app")
|
||||
end
|
||||
|
||||
it "redirects for turbo streams when no key is linked" do
|
||||
delete nostr_signer_settings_path, headers: { "Accept" => "text/vnd.turbo-stream.html" }
|
||||
|
||||
expect(response).to redirect_to(setting_path(:nostr))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user