web-app/src/modules/chat/composables/useChat.ts
padreug daa9656680 Implement LNbits integration in AuthService and enhance ChatComponent for improved user experience
- Refactor AuthService to integrate LNbits authentication, including fetching user data from the API and handling token validation.
- Update ChatComponent to reflect changes in peer management, replacing user_id with pubkey and username with name for better clarity.
- Enhance connection status indicators in ChatComponent for improved user feedback during chat initialization.
2025-09-05 02:48:47 +02:00

98 lines
No EOL
2.5 KiB
TypeScript

import { ref, computed } from 'vue'
import { injectService } from '@/core/di-container'
import type { ChatService } from '../services/chat-service'
import type { ChatPeer } from '../types'
// Service token for chat service
export const CHAT_SERVICE_TOKEN = Symbol('chatService')
export function useChat() {
const chatService = injectService<ChatService>(CHAT_SERVICE_TOKEN)
if (!chatService) {
throw new Error('ChatService not available. Make sure chat module is installed.')
}
const selectedPeer = ref<string | null>(null)
const isLoading = ref(false)
const error = ref<string | null>(null)
// Computed properties
const peers = computed(() => chatService.allPeers.value)
const totalUnreadCount = computed(() => chatService.totalUnreadCount.value)
const isReady = computed(() => chatService.isReady.value)
const currentMessages = computed(() => {
return selectedPeer.value ? chatService.getMessages(selectedPeer.value) : []
})
const currentPeer = computed(() => {
return selectedPeer.value ? chatService.getPeer(selectedPeer.value) : undefined
})
// Methods
const selectPeer = (peerPubkey: string) => {
selectedPeer.value = peerPubkey
chatService.markAsRead(peerPubkey)
}
const sendMessage = async (content: string) => {
if (!selectedPeer.value || !content.trim()) {
return
}
isLoading.value = true
error.value = null
try {
await chatService.sendMessage(selectedPeer.value, content.trim())
} catch (err) {
error.value = err instanceof Error ? err.message : 'Failed to send message'
console.error('Send message error:', err)
} finally {
isLoading.value = false
}
}
const addPeer = (pubkey: string, name?: string): ChatPeer => {
return chatService.addPeer(pubkey, name)
}
const markAsRead = (peerPubkey: string) => {
chatService.markAsRead(peerPubkey)
}
const refreshPeers = async () => {
isLoading.value = true
error.value = null
try {
await chatService.refreshPeers()
} catch (err) {
error.value = err instanceof Error ? err.message : 'Failed to refresh peers'
console.error('Failed to refresh peers:', err)
} finally {
isLoading.value = false
}
}
return {
// State
selectedPeer,
isLoading,
error,
// Computed
peers,
totalUnreadCount,
isReady,
currentMessages,
currentPeer,
// Methods
selectPeer,
sendMessage,
addPeer,
markAsRead,
refreshPeers
}
}