web-app/src/components/nostr/ChatComponent.vue
padreug 0b62418310 feat: Add Nostr chat integration for LNBits users
- Introduce a new chat system that allows LNBits users to communicate via Nostr relays.
- Implement ChatComponent for real-time messaging, peer selection, and message display.
- Create useNostrChat composable to manage Nostr relay connections, message encryption, and user authentication.
- Develop ChatPage to serve as the main interface for the chat feature.
- Add API endpoints for retrieving current user and public keys for peer messaging.
- Ensure secure communication with encryption and admin-only access to private keys.
2025-08-10 10:50:14 +02:00

291 lines
No EOL
8.2 KiB
Vue

<template>
<div class="flex flex-col h-full">
<!-- Header -->
<div class="flex items-center justify-between p-4 border-b">
<div class="flex items-center space-x-3">
<h2 class="text-lg font-semibold">Chat</h2>
<Badge v-if="isConnected" variant="default" class="text-xs">
Connected
</Badge>
<Badge v-else variant="secondary" class="text-xs">
Disconnected
</Badge>
</div>
<Button @click="refreshPeers" :disabled="isLoading" size="sm">
<RefreshCw v-if="isLoading" class="h-4 w-4 animate-spin" />
<RefreshCw v-else class="h-4 w-4" />
Refresh Peers
</Button>
</div>
<!-- Main Content -->
<div class="flex flex-1 overflow-hidden">
<!-- Peer List -->
<div class="w-80 border-r bg-muted/30">
<div class="p-4 border-b">
<h3 class="font-medium">Peers ({{ peers.length }})</h3>
</div>
<ScrollArea class="h-full">
<div class="p-2 space-y-1">
<div
v-for="peer in peers"
:key="peer.user_id"
@click="selectPeer(peer)"
:class="[
'flex items-center space-x-3 p-3 rounded-lg cursor-pointer transition-colors',
selectedPeer?.user_id === peer.user_id
? 'bg-primary text-primary-foreground'
: 'hover:bg-muted'
]"
>
<Avatar class="h-8 w-8">
<AvatarImage v-if="getPeerAvatar(peer)" :src="getPeerAvatar(peer)!" />
<AvatarFallback>{{ getPeerInitials(peer) }}</AvatarFallback>
</Avatar>
<div class="flex-1 min-w-0">
<p class="text-sm font-medium truncate">
{{ peer.username || 'Unknown User' }}
</p>
<p class="text-xs text-muted-foreground truncate">
{{ formatPubkey(peer.pubkey) }}
</p>
</div>
</div>
</div>
</ScrollArea>
</div>
<!-- Chat Area -->
<div class="flex-1 flex flex-col">
<div v-if="selectedPeer" class="flex-1 flex flex-col">
<!-- Chat Header -->
<div class="p-4 border-b">
<div class="flex items-center space-x-3">
<Avatar class="h-8 w-8">
<AvatarImage v-if="getPeerAvatar(selectedPeer)" :src="getPeerAvatar(selectedPeer)!" />
<AvatarFallback>{{ getPeerInitials(selectedPeer) }}</AvatarFallback>
</Avatar>
<div>
<h3 class="font-medium">{{ selectedPeer.username || 'Unknown User' }}</h3>
<p class="text-sm text-muted-foreground">
{{ formatPubkey(selectedPeer.pubkey) }}
</p>
</div>
</div>
</div>
<!-- Messages -->
<ScrollArea class="flex-1 p-4">
<div class="space-y-4">
<div
v-for="message in currentMessages"
:key="message.id"
:class="[
'flex',
message.sent ? 'justify-end' : 'justify-start'
]"
>
<div
:class="[
'max-w-xs lg:max-w-md px-4 py-2 rounded-lg',
message.sent
? 'bg-primary text-primary-foreground'
: 'bg-muted'
]"
>
<p class="text-sm">{{ message.content }}</p>
<p class="text-xs opacity-70 mt-1">
{{ formatTime(message.created_at) }}
</p>
</div>
</div>
</div>
<div ref="messagesEndRef" />
</ScrollArea>
<!-- Message Input -->
<div class="p-4 border-t">
<form @submit.prevent="sendMessage" class="flex space-x-2">
<Input
v-model="messageInput"
placeholder="Type a message..."
:disabled="!isConnected || !selectedPeer"
class="flex-1"
/>
<Button type="submit" :disabled="!isConnected || !selectedPeer || !messageInput.trim()">
<Send class="h-4 w-4" />
</Button>
</form>
</div>
</div>
<!-- No Peer Selected -->
<div v-else class="flex-1 flex items-center justify-center">
<div class="text-center">
<MessageSquare class="h-12 w-12 text-muted-foreground mx-auto mb-4" />
<h3 class="text-lg font-medium mb-2">No peer selected</h3>
<p class="text-muted-foreground">
Select a peer from the list to start chatting
</p>
</div>
</div>
</div>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, computed, onMounted, onUnmounted, nextTick, watch } from 'vue'
import { Send, RefreshCw, MessageSquare } from 'lucide-vue-next'
import { Button } from '@/components/ui/button'
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'
// Types
interface Peer {
user_id: string
username: string
pubkey: string
}
interface ChatMessage {
id: string
content: string
created_at: number
sent: boolean
}
// State
const peers = ref<Peer[]>([])
const selectedPeer = ref<Peer | null>(null)
const messageInput = ref('')
const messagesEndRef = ref<HTMLDivElement | null>(null)
const isLoading = ref(false)
// Nostr chat composable
const {
isConnected,
messages,
sendMessage: sendNostrMessage,
connect,
disconnect,
subscribeToPeer
} = useNostrChat()
// Computed
const currentMessages = computed(() => {
if (!selectedPeer.value) return []
return messages.value.get(selectedPeer.value.pubkey) || []
})
// Methods
const loadPeers = async () => {
try {
isLoading.value = true
const response = await fetch('/users/api/v1/nostr/pubkeys', {
headers: {
'Authorization': `Bearer ${localStorage.getItem('admin_token')}`
}
})
if (!response.ok) {
throw new Error('Failed to load peers')
}
const data = await response.json()
peers.value = data.map((peer: any) => ({
user_id: peer.user_id,
username: peer.username,
pubkey: peer.pubkey
}))
console.log(`Loaded ${peers.value.length} peers`)
} catch (error) {
console.error('Failed to load peers:', error)
} finally {
isLoading.value = false
}
}
const refreshPeers = () => {
loadPeers()
}
const selectPeer = async (peer: Peer) => {
selectedPeer.value = peer
messageInput.value = ''
// Subscribe to messages from this peer
await subscribeToPeer(peer.pubkey)
// Scroll to bottom after messages load
nextTick(() => {
scrollToBottom()
})
}
const sendMessage = async () => {
if (!selectedPeer.value || !messageInput.value.trim()) return
try {
await sendNostrMessage(selectedPeer.value.pubkey, messageInput.value)
messageInput.value = ''
// Scroll to bottom
nextTick(() => {
scrollToBottom()
})
} catch (error) {
console.error('Failed to send message:', error)
}
}
const scrollToBottom = () => {
if (messagesEndRef.value) {
messagesEndRef.value.scrollIntoView({ behavior: 'smooth' })
}
}
const formatPubkey = (pubkey: string) => {
return pubkey.slice(0, 8) + '...' + pubkey.slice(-8)
}
const formatTime = (timestamp: number) => {
return new Date(timestamp * 1000).toLocaleTimeString([], {
hour: '2-digit',
minute: '2-digit'
})
}
const getPeerAvatar = (peer: Peer) => {
// You can implement avatar logic here
return null
}
const getPeerInitials = (peer: Peer) => {
if (peer.username) {
return peer.username.slice(0, 2).toUpperCase()
}
return peer.pubkey.slice(0, 2).toUpperCase()
}
// Lifecycle
onMounted(async () => {
await connect()
await loadPeers()
})
onUnmounted(() => {
disconnect()
})
// Watch for new messages and scroll to bottom
watch(currentMessages, () => {
nextTick(() => {
scrollToBottom()
})
})
</script>