feat: Implement message addition callback and enhance scrolling behavior
- Introduce a callback for when new messages are added, allowing for automatic scrolling to the bottom of the chat when relevant peers are selected. - Update the ChatComponent to reference the scrolling area correctly and improve the logic for scrolling to the latest messages. - Enhance console logging for better debugging and tracking of message flow and scrolling actions.
This commit is contained in:
parent
7bef56f630
commit
55e051146e
2 changed files with 53 additions and 6 deletions
|
|
@ -89,7 +89,7 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Messages -->
|
<!-- Messages -->
|
||||||
<ScrollArea class="flex-1 p-4">
|
<ScrollArea class="flex-1 p-4" ref="messagesScrollArea">
|
||||||
<div class="space-y-4">
|
<div class="space-y-4">
|
||||||
<div
|
<div
|
||||||
v-for="message in currentMessages"
|
v-for="message in currentMessages"
|
||||||
|
|
@ -329,7 +329,8 @@ const {
|
||||||
sendMessage: sendNostrMessage,
|
sendMessage: sendNostrMessage,
|
||||||
connect,
|
connect,
|
||||||
disconnect,
|
disconnect,
|
||||||
subscribeToPeer
|
subscribeToPeer,
|
||||||
|
onMessageAdded
|
||||||
} = useNostrChat()
|
} = useNostrChat()
|
||||||
|
|
||||||
// Computed
|
// Computed
|
||||||
|
|
@ -427,13 +428,17 @@ const sendMessage = async () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const scrollToBottom = () => {
|
const scrollToBottom = () => {
|
||||||
|
console.log('scrollToBottom called')
|
||||||
nextTick(() => {
|
nextTick(() => {
|
||||||
if (scrollTarget.value) {
|
if (scrollTarget.value) {
|
||||||
// Scroll to the hidden target element at the bottom
|
console.log('Found scrollTarget, scrolling to bottom')
|
||||||
|
// Use scrollIntoView on the target element
|
||||||
scrollTarget.value.scrollIntoView({
|
scrollTarget.value.scrollIntoView({
|
||||||
behavior: 'smooth',
|
behavior: 'smooth',
|
||||||
block: 'end'
|
block: 'end'
|
||||||
})
|
})
|
||||||
|
} else {
|
||||||
|
console.log('No scrollTarget found')
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -466,6 +471,17 @@ onMounted(async () => {
|
||||||
checkMobile()
|
checkMobile()
|
||||||
window.addEventListener('resize', checkMobile)
|
window.addEventListener('resize', checkMobile)
|
||||||
|
|
||||||
|
// Set up message callback
|
||||||
|
onMessageAdded.value = (peerPubkey: string) => {
|
||||||
|
console.log('Message added callback triggered for peer:', peerPubkey)
|
||||||
|
if (selectedPeer.value && selectedPeer.value.pubkey === peerPubkey) {
|
||||||
|
console.log('Triggering scroll for current peer')
|
||||||
|
nextTick(() => {
|
||||||
|
scrollToBottom()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
await connect()
|
await connect()
|
||||||
await loadPeers()
|
await loadPeers()
|
||||||
})
|
})
|
||||||
|
|
@ -477,8 +493,15 @@ onUnmounted(() => {
|
||||||
|
|
||||||
// Watch for new messages and scroll to bottom
|
// Watch for new messages and scroll to bottom
|
||||||
watch(currentMessages, (newMessages, oldMessages) => {
|
watch(currentMessages, (newMessages, oldMessages) => {
|
||||||
// Only scroll if messages were added (not just peer selection)
|
console.log('Messages changed:', {
|
||||||
if (newMessages.length > 0 && oldMessages && newMessages.length > oldMessages.length) {
|
newLength: newMessages.length,
|
||||||
|
oldLength: oldMessages?.length,
|
||||||
|
isNewMessage: !oldMessages || newMessages.length > oldMessages.length
|
||||||
|
})
|
||||||
|
|
||||||
|
// Scroll to bottom when new messages are added
|
||||||
|
if (newMessages.length > 0 && (!oldMessages || newMessages.length > oldMessages.length)) {
|
||||||
|
console.log('Triggering scroll to bottom')
|
||||||
nextTick(() => {
|
nextTick(() => {
|
||||||
scrollToBottom()
|
scrollToBottom()
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,9 @@ export function useNostrChat() {
|
||||||
const pool = ref<SimplePool | null>(null)
|
const pool = ref<SimplePool | null>(null)
|
||||||
const processedMessageIds = ref(new Set<string>())
|
const processedMessageIds = ref(new Set<string>())
|
||||||
|
|
||||||
|
// Callback for when messages change
|
||||||
|
const onMessageAdded = ref<((peerPubkey: string) => void) | null>(null)
|
||||||
|
|
||||||
// Computed
|
// Computed
|
||||||
const isLoggedIn = computed(() => !!currentUser.value)
|
const isLoggedIn = computed(() => !!currentUser.value)
|
||||||
|
|
||||||
|
|
@ -328,11 +331,23 @@ export function useNostrChat() {
|
||||||
messages.value.set(conversationKey, [])
|
messages.value.set(conversationKey, [])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!messages.value.has(conversationKey)) {
|
||||||
|
messages.value.set(conversationKey, [])
|
||||||
|
}
|
||||||
|
|
||||||
messages.value.get(conversationKey)!.push(message)
|
messages.value.get(conversationKey)!.push(message)
|
||||||
|
|
||||||
// Sort messages by timestamp
|
// Sort messages by timestamp
|
||||||
messages.value.get(conversationKey)!.sort((a, b) => a.created_at - b.created_at)
|
messages.value.get(conversationKey)!.sort((a, b) => a.created_at - b.created_at)
|
||||||
|
|
||||||
|
// Force reactivity by triggering a change
|
||||||
|
messages.value = new Map(messages.value)
|
||||||
|
|
||||||
|
// Trigger callback if set
|
||||||
|
if (onMessageAdded.value) {
|
||||||
|
onMessageAdded.value(conversationKey)
|
||||||
|
}
|
||||||
|
|
||||||
console.log('Messages for conversation:', messages.value.get(conversationKey)?.map(m => ({
|
console.log('Messages for conversation:', messages.value.get(conversationKey)?.map(m => ({
|
||||||
id: m.id,
|
id: m.id,
|
||||||
sent: m.sent,
|
sent: m.sent,
|
||||||
|
|
@ -411,6 +426,14 @@ export function useNostrChat() {
|
||||||
// Sort messages by timestamp
|
// Sort messages by timestamp
|
||||||
messages.value.get(peerPubkey)!.sort((a, b) => a.created_at - b.created_at)
|
messages.value.get(peerPubkey)!.sort((a, b) => a.created_at - b.created_at)
|
||||||
|
|
||||||
|
// Force reactivity by triggering a change
|
||||||
|
messages.value = new Map(messages.value)
|
||||||
|
|
||||||
|
// Trigger callback if set
|
||||||
|
if (onMessageAdded.value) {
|
||||||
|
onMessageAdded.value(peerPubkey)
|
||||||
|
}
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to send message:', error)
|
console.error('Failed to send message:', error)
|
||||||
throw error
|
throw error
|
||||||
|
|
@ -440,6 +463,7 @@ export function useNostrChat() {
|
||||||
sendMessage,
|
sendMessage,
|
||||||
getMessages,
|
getMessages,
|
||||||
clearMessages,
|
clearMessages,
|
||||||
loadCurrentUser
|
loadCurrentUser,
|
||||||
|
onMessageAdded
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue