refactor: Simplify Nostr connection management and enhance store integration
- Refactor useNostr composable to utilize a centralized Pinia store for connection state and client management. - Update NostrFeed and App components to leverage the new store-based approach for relay configuration and client instantiation. - Remove direct relay URL handling from components, improving maintainability and consistency across the application.
This commit is contained in:
parent
0324cf8ec5
commit
236a8a59b9
5 changed files with 85 additions and 38 deletions
|
|
@ -1,5 +1,7 @@
|
|||
import { defineStore } from 'pinia'
|
||||
import { ref } from 'vue'
|
||||
import { NostrClient } from '@/lib/nostr/client'
|
||||
import { config } from '@/lib/config'
|
||||
|
||||
// Define an interface for the account object
|
||||
interface NostrAccount {
|
||||
|
|
@ -8,16 +10,67 @@ interface NostrAccount {
|
|||
}
|
||||
|
||||
export const useNostrStore = defineStore('nostr', () => {
|
||||
// Connection state
|
||||
const isConnected = ref(false)
|
||||
const relayUrls = ref<string[]>([])
|
||||
const isConnecting = ref(false)
|
||||
const error = ref<Error | null>(null)
|
||||
|
||||
// Configuration
|
||||
const relayUrls = ref<string[]>(config.nostr.relays)
|
||||
const account = ref<NostrAccount | null>(null)
|
||||
|
||||
// Singleton client instance
|
||||
let client: NostrClient | null = null
|
||||
|
||||
// Get or create client instance
|
||||
function getClient(): NostrClient {
|
||||
if (!client) {
|
||||
client = new NostrClient({ relays: relayUrls.value })
|
||||
}
|
||||
return client
|
||||
}
|
||||
|
||||
// Connection management
|
||||
async function connect(): Promise<void> {
|
||||
try {
|
||||
error.value = null
|
||||
isConnecting.value = true
|
||||
|
||||
const nostrClient = getClient()
|
||||
await nostrClient.connect()
|
||||
|
||||
isConnected.value = nostrClient.isConnected
|
||||
} catch (err) {
|
||||
error.value = err instanceof Error ? err : new Error('Failed to connect')
|
||||
isConnected.value = false
|
||||
throw err
|
||||
} finally {
|
||||
isConnecting.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function disconnect(): void {
|
||||
if (client) {
|
||||
client.disconnect()
|
||||
client = null
|
||||
}
|
||||
isConnected.value = false
|
||||
isConnecting.value = false
|
||||
error.value = null
|
||||
}
|
||||
|
||||
// Configuration setters
|
||||
function setConnected(value: boolean) {
|
||||
isConnected.value = value
|
||||
}
|
||||
|
||||
function setRelayUrls(urls: string[]) {
|
||||
relayUrls.value = urls
|
||||
// Recreate client with new relays
|
||||
if (client) {
|
||||
client.disconnect()
|
||||
client = null
|
||||
}
|
||||
}
|
||||
|
||||
function setAccount(nostrAccount: NostrAccount | null) {
|
||||
|
|
@ -25,9 +78,17 @@ export const useNostrStore = defineStore('nostr', () => {
|
|||
}
|
||||
|
||||
return {
|
||||
// State
|
||||
isConnected,
|
||||
isConnecting,
|
||||
error,
|
||||
relayUrls,
|
||||
account,
|
||||
|
||||
// Actions
|
||||
connect,
|
||||
disconnect,
|
||||
getClient,
|
||||
setConnected,
|
||||
setRelayUrls,
|
||||
setAccount,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue