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:
parent
855a003962
commit
2dec184c42
5 changed files with 160 additions and 209 deletions
37
src/App.vue
37
src/App.vue
|
|
@ -8,28 +8,39 @@ import { Toaster } from '@/components/ui/sonner'
|
|||
import 'vue-sonner/style.css'
|
||||
import { auth } from '@/composables/useAuth'
|
||||
import { useMarketPreloader } from '@/composables/useMarketPreloader'
|
||||
import { useNostrChatPreloader } from '@/composables/useNostrChatPreloader'
|
||||
import { nostrChat } from '@/composables/useNostrChat'
|
||||
import { toast } from 'vue-sonner'
|
||||
|
||||
const route = useRoute()
|
||||
const showLoginDialog = ref(false)
|
||||
|
||||
// Initialize preloaders
|
||||
// Initialize preloader
|
||||
const marketPreloader = useMarketPreloader()
|
||||
const chatPreloader = useNostrChatPreloader()
|
||||
|
||||
// Hide navbar on login page
|
||||
const showNavbar = computed(() => {
|
||||
return route.path !== '/login'
|
||||
})
|
||||
|
||||
function handleLoginSuccess() {
|
||||
async function handleLoginSuccess() {
|
||||
showLoginDialog.value = false
|
||||
toast.success('Welcome back!')
|
||||
|
||||
// Trigger preloading after successful login
|
||||
marketPreloader.preloadMarket()
|
||||
chatPreloader.preloadChat()
|
||||
|
||||
// Connect to chat
|
||||
if (!nostrChat.isConnected.value) {
|
||||
try {
|
||||
await nostrChat.connect()
|
||||
|
||||
// Load peers and subscribe to all for notifications
|
||||
const peers = await nostrChat.loadPeers()
|
||||
await nostrChat.subscribeToAllPeersForNotifications(peers)
|
||||
} catch (error) {
|
||||
console.error('Failed to initialize chat:', error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(async () => {
|
||||
|
|
@ -42,15 +53,23 @@ onMounted(async () => {
|
|||
})
|
||||
|
||||
// Watch for authentication changes and trigger preloading
|
||||
watch(() => auth.isAuthenticated.value, (isAuthenticated) => {
|
||||
watch(() => auth.isAuthenticated.value, async (isAuthenticated) => {
|
||||
if (isAuthenticated) {
|
||||
if (!marketPreloader.isPreloaded.value) {
|
||||
console.log('User authenticated, triggering market preload...')
|
||||
marketPreloader.preloadMarket()
|
||||
}
|
||||
if (!chatPreloader.isPreloaded.value) {
|
||||
console.log('User authenticated, triggering chat preload...')
|
||||
chatPreloader.preloadChat()
|
||||
if (!nostrChat.isConnected.value) {
|
||||
console.log('User authenticated, connecting to chat...')
|
||||
try {
|
||||
await nostrChat.connect()
|
||||
|
||||
// Load peers and subscribe to all for notifications
|
||||
const peers = await nostrChat.loadPeers()
|
||||
await nostrChat.subscribeToAllPeersForNotifications(peers)
|
||||
} catch (error) {
|
||||
console.error('Failed to initialize chat:', error)
|
||||
}
|
||||
}
|
||||
}
|
||||
}, { immediate: true })
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue