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:
parent
be93965e13
commit
5eb46e96c3
11 changed files with 169 additions and 26 deletions
21
src/stores/messages.ts
Normal file
21
src/stores/messages.ts
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
// Separate message handling into its own store
|
||||
export const useMessageStore = defineStore('messages', () => {
|
||||
const messages = ref<Map<string, DirectMessage[]>>(new Map())
|
||||
const processedIds = ref(new Set<string>())
|
||||
|
||||
const addMessage = async (pubkey: string, message: DirectMessage) => {
|
||||
if (processedIds.value.has(message.id)) return
|
||||
|
||||
processedIds.value.add(message.id)
|
||||
const userMessages = messages.value.get(pubkey) || []
|
||||
messages.value.set(pubkey, [...userMessages, message].sort((a, b) =>
|
||||
a.created_at - b.created_at
|
||||
))
|
||||
}
|
||||
|
||||
return {
|
||||
messages,
|
||||
processedIds,
|
||||
addMessage
|
||||
}
|
||||
})
|
||||
|
|
@ -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,
|
||||
}
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue