diff --git a/static/js/index.js b/static/js/index.js index 4d9c3d3..e570dde 100644 --- a/static/js/index.js +++ b/static/js/index.js @@ -18,7 +18,26 @@ window.app = Vue.createApp({ privateKey: null } }, - wsConnection: null + wsConnection: null, + nostrStatus: { + connected: false, + error: null, + relays_connected: 0, + relays_total: 0 + } + } + }, + computed: { + nostrStatusColor: function () { + if (this.nostrStatus.connected) { + return 'green' + } else if (this.nostrStatus.warning) { + return 'orange' + } + return 'red' + }, + nostrStatusLabel: function () { + return 'Connect' } }, methods: { @@ -198,18 +217,82 @@ window.app = Vue.createApp({ }) } }, + checkNostrStatus: async function (showNotification = false) { + try { + const response = await fetch('/nostrclient/api/v1/relays') + const body = await response.json() + + if (response.status === 200) { + const relaysConnected = body.filter(r => r.connected).length + if (body.length === 0) { + this.nostrStatus = { + connected: false, + error: 'No relays configured in Nostr Client', + relays_connected: 0, + relays_total: 0, + warning: true + } + } else { + this.nostrStatus = { + connected: true, + error: null, + relays_connected: relaysConnected, + relays_total: body.length + } + } + } else { + this.nostrStatus = { + connected: false, + error: body.detail, + relays_connected: 0, + relays_total: 0 + } + } + + if (showNotification) { + this.$q.notify({ + timeout: 3000, + type: this.nostrStatus.connected ? 'positive' : 'warning', + message: this.nostrStatus.connected ? 'Connected' : 'Disconnected', + caption: this.nostrStatus.error || undefined + }) + } + } catch (error) { + console.error('Failed to check nostr status:', error) + this.nostrStatus = { + connected: false, + error: error.message, + relays_connected: 0, + relays_total: 0 + } + if (showNotification) { + this.$q.notify({ + timeout: 5000, + type: 'negative', + message: this.nostrStatus.error + }) + } + } + }, restartNostrConnection: async function () { LNbits.utils .confirmDialog( - 'Are you sure you want to reconnect to the nostrcient extension?' + 'Are you sure you want to reconnect to the nostrclient extension?' ) .onOk(async () => { try { + this.$q.notify({ + timeout: 2000, + type: 'info', + message: 'Reconnecting...' + }) await LNbits.api.request( 'PUT', '/nostrmarket/api/v1/restart', this.g.user.wallets[0].adminkey ) + // Check status after restart (give time for websocket to reconnect) + setTimeout(() => this.checkNostrStatus(true), 3000) } catch (error) { LNbits.utils.notifyApiError(error) } @@ -300,6 +383,7 @@ window.app = Vue.createApp({ }, created: async function () { await this.getMerchant() + await this.checkNostrStatus() setInterval(async () => { if ( !this.wsConnection || diff --git a/templates/nostrmarket/index.html b/templates/nostrmarket/index.html index ed9a39f..9f89808 100644 --- a/templates/nostrmarket/index.html +++ b/templates/nostrmarket/index.html @@ -278,16 +278,68 @@
- - - Restart the connection to the nostrclient extension - - + + + + + + + Restart Connection + + Reconnect to the nostrclient extension + + + + + + + + + Check Status + + Check connection to nostrclient + + + + + + + + Status: + + + + Relays:  + + of + + connected + + + + + +