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,38 +1,24 @@
|
|||
import { ref } from 'vue'
|
||||
import { NostrClient, type NostrClientConfig } from '@/lib/nostr/client'
|
||||
import { storeToRefs } from 'pinia'
|
||||
import { useNostrStore } from '@/stores/nostr'
|
||||
import type { NostrClientConfig } from '@/lib/nostr/client'
|
||||
|
||||
export function useNostr(config: NostrClientConfig) {
|
||||
const client = new NostrClient(config)
|
||||
const isConnected = ref(false)
|
||||
const isConnecting = ref(false)
|
||||
const error = ref<Error | null>(null)
|
||||
|
||||
async function connect() {
|
||||
try {
|
||||
error.value = null
|
||||
isConnecting.value = true
|
||||
await client.connect()
|
||||
isConnected.value = client.isConnected
|
||||
} catch (err) {
|
||||
error.value = err instanceof Error ? err : new Error('Failed to connect')
|
||||
isConnected.value = false
|
||||
} finally {
|
||||
isConnecting.value = false
|
||||
}
|
||||
export function useNostr(config?: NostrClientConfig) {
|
||||
const store = useNostrStore()
|
||||
|
||||
// If custom relays are provided, update the store
|
||||
if (config?.relays) {
|
||||
store.setRelayUrls(config.relays)
|
||||
}
|
||||
|
||||
function disconnect() {
|
||||
client.disconnect()
|
||||
isConnected.value = false
|
||||
isConnecting.value = false
|
||||
error.value = null
|
||||
}
|
||||
// Return reactive refs from the store
|
||||
const { isConnected, isConnecting, error } = storeToRefs(store)
|
||||
|
||||
return {
|
||||
isConnected,
|
||||
isConnecting,
|
||||
error,
|
||||
connect,
|
||||
disconnect
|
||||
connect: store.connect,
|
||||
disconnect: store.disconnect,
|
||||
getClient: store.getClient
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue