refactor: Replace Nostr chat preloader with a singleton pattern for improved state management

- Remove the useNostrChatPreloader composable and integrate its functionality into the useNostrChat composable, streamlining chat data handling.
- Update App.vue and ChatComponent to utilize the new singleton instance for managing chat connections and peer subscriptions.
- Enhance Navbar and ChatComponent to reflect changes in unread message tracking and peer management, improving user experience.
- Ensure proper error handling and logging during chat connection and peer loading processes.
This commit is contained in:
padreug 2025-08-09 15:19:52 +02:00
parent 855a003962
commit 2dec184c42
5 changed files with 160 additions and 209 deletions

View file

@ -13,8 +13,8 @@
Disconnected
</Badge>
<!-- Total unread count -->
<Badge v-if="getTotalUnreadCount() > 0" variant="destructive" class="text-xs">
{{ getTotalUnreadCount() }} unread
<Badge v-if="totalUnreadCount > 0" variant="destructive" class="text-xs">
{{ totalUnreadCount }} unread
</Badge>
</div>
<Button @click="refreshPeers" :disabled="isLoading" size="sm">
@ -198,8 +198,8 @@
Disconnected
</Badge>
<!-- Total unread count -->
<Badge v-if="getTotalUnreadCount() > 0" variant="destructive" class="text-xs">
{{ getTotalUnreadCount() }} unread
<Badge v-if="totalUnreadCount > 0" variant="destructive" class="text-xs">
{{ totalUnreadCount }} unread
</Badge>
</div>
<Button @click="refreshPeers" :disabled="isLoading" size="sm">
@ -375,8 +375,8 @@ import { Input } from '@/components/ui/input'
import { Badge } from '@/components/ui/badge'
import { ScrollArea } from '@/components/ui/scroll-area'
import { Avatar, AvatarImage, AvatarFallback } from '@/components/ui/avatar'
import { useNostrChat } from '@/composables/useNostrChat'
import { useNostrChatPreloader } from '@/composables/useNostrChatPreloader'
import { nostrChat } from '@/composables/useNostrChat'
import { useFuzzySearch } from '@/composables/useFuzzySearch'
// Types
@ -386,15 +386,12 @@ interface Peer {
pubkey: string
}
// Initialize preloader and chat
const chatPreloader = useNostrChatPreloader()
// State
const peers = computed(() => chatPreloader.peers.value)
const peers = computed(() => nostrChat.peers.value)
const selectedPeer = ref<Peer | null>(null)
const messageInput = ref('')
const isLoading = computed(() => chatPreloader.isPreloading.value)
const isLoading = ref(false)
const showChat = ref(false)
const messagesScrollArea = ref<HTMLElement | null>(null)
const messagesContainer = ref<HTMLElement | null>(null)
@ -403,7 +400,7 @@ const scrollTarget = ref<HTMLElement | null>(null)
// Mobile detection
const isMobile = ref(false)
// Nostr chat composable
// Nostr chat composable (singleton)
const {
isConnected,
messages,
@ -414,9 +411,9 @@ const {
onMessageAdded,
markMessagesAsRead,
getUnreadCount,
getTotalUnreadCount,
totalUnreadCount,
getLatestMessageTimestamp
} = useNostrChat()
} = nostrChat
// Computed
const currentMessages = computed(() => {
@ -505,7 +502,14 @@ const goBackToPeers = () => {
const refreshPeers = async () => {
console.log('Refreshing peers and chat data...')
await chatPreloader.preloadChat()
isLoading.value = true
try {
await nostrChat.loadPeers()
} catch (error) {
console.error('Failed to refresh peers:', error)
} finally {
isLoading.value = false
}
}
const selectPeer = async (peer: Peer) => {
@ -600,22 +604,22 @@ onMounted(async () => {
}
}
console.log('Chat component mounted - checking if preloader has data...')
console.log('Chat component mounted - checking connection state...')
// If chat is already preloaded, we're good to go
if (chatPreloader.isPreloaded.value) {
console.log('Chat data was preloaded, connecting to chat...')
await connect()
console.log('Chat connected successfully')
} else if (!chatPreloader.isPreloading.value) {
// If not preloaded and not currently preloading, trigger preload
console.log('Chat data not preloaded, triggering preload...')
await chatPreloader.preloadChat()
// If not connected, connect
if (!isConnected.value) {
console.log('Not connected, connecting to chat...')
await connect()
} else {
// Currently preloading, just connect
console.log('Chat is currently preloading, just connecting...')
await connect()
console.log('Already connected to chat')
}
// If no peers loaded, load them
if (peers.value.length === 0) {
console.log('No peers loaded, loading peers...')
await nostrChat.loadPeers()
} else {
console.log('Peers already loaded:', peers.value.length)
}
})