refactor: improve nostr connection and message handling

- Add WebSocket manager class for better connection handling
- Split message handling into separate store
- Add encryption service class
- Create chat composable for reusable chat logic
- Add error handling service
- Add connection status indicators throughout app
- Add message persistence service
- Improve subscription reliability with EOSE handling
- Add connection state management
- Hide status text on mobile for better space usage

These changes improve code organization, reliability, and user experience by:
- Better separation of concerns
- More robust error handling
- Clearer connection status feedback
- Improved message persistence
- More maintainable WebSocket management
- Better mobile responsiveness

Breaking changes:
- Message handling moved to separate store
- WebSocket connections now managed through NostrWebSocketManager
- Encryption now handled through NostrEncryption service
This commit is contained in:
padreug 2025-02-15 00:26:11 +01:00
parent be93965e13
commit 5eb46e96c3
11 changed files with 169 additions and 26 deletions

View file

@ -48,23 +48,23 @@ async function withTimeout<T>(promise: Promise<T>, timeoutMs: number = 10000): P
])
}
// Add to state
const connectionStatus = ref<'connected' | 'connecting' | 'disconnected'>('disconnected')
// Update in connect function
async function connectToRelay(url: string) {
connectionStatus.value = 'connecting'
console.log(`Attempting to connect to relay: ${url}`)
const relay = window.NostrTools.relayInit(url)
try {
console.log(`Initializing connection to ${url}...`)
await withTimeout(relay.connect())
console.log(`Successfully connected to ${url}`)
connectionStatus.value = 'connected'
return relay
} catch (err) {
console.error(`Failed to connect to ${url}:`, err)
if (err instanceof Error) {
console.error('Error details:', {
message: err.message,
name: err.name,
stack: err.stack
})
}
connectionStatus.value = 'disconnected'
return null
}
}
@ -519,6 +519,7 @@ export const useNostrStore = defineStore('nostr', () => {
sendMessage,
subscribeToMessages,
unsubscribeFromMessages,
loadProfiles
loadProfiles,
connectionStatus,
}
})