improve subscribe to messages

This commit is contained in:
padreug 2025-02-12 01:42:04 +01:00
parent 32b0bf7247
commit 8b3f1aa14b
3 changed files with 45 additions and 4 deletions

View file

@ -1,5 +1,5 @@
<script setup lang="ts">
import { ref, computed, nextTick, onMounted, watch } from 'vue'
import { ref, computed, nextTick, onMounted, watch, onUnmounted } from 'vue'
import { useNostrStore } from '@/stores/nostr'
import { npubToHex } from '@/lib/nostr'
import type { DirectMessage } from '@/types/nostr'
@ -79,6 +79,25 @@ onMounted(async () => {
}
})
// Add cleanup on unmount
onUnmounted(() => {
if (nostrStore.activeChat) {
nostrStore.unsubscribeFromMessages()
}
})
// Watch for changes in activeChat
watch(() => nostrStore.activeChat, async (newChat) => {
if (newChat) {
try {
await nostrStore.subscribeToMessages()
} catch (err) {
console.error('Failed to subscribe to messages:', err)
error.value = 'Failed to connect to chat. Please try again later.'
}
}
})
function scrollToBottom() {
if (messagesEndRef.value) {
messagesEndRef.value.scrollIntoView({ behavior: 'smooth' })