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
|
|
@ -39,6 +39,9 @@ export function useNostrChat() {
|
|||
const currentUser = ref<{ pubkey: string; prvkey: string } | null>(null)
|
||||
const pool = ref<SimplePool | null>(null)
|
||||
const processedMessageIds = ref(new Set<string>())
|
||||
|
||||
// Callback for when messages change
|
||||
const onMessageAdded = ref<((peerPubkey: string) => void) | null>(null)
|
||||
|
||||
// Computed
|
||||
const isLoggedIn = computed(() => !!currentUser.value)
|
||||
|
|
@ -328,11 +331,23 @@ export function useNostrChat() {
|
|||
messages.value.set(conversationKey, [])
|
||||
}
|
||||
|
||||
if (!messages.value.has(conversationKey)) {
|
||||
messages.value.set(conversationKey, [])
|
||||
}
|
||||
|
||||
messages.value.get(conversationKey)!.push(message)
|
||||
|
||||
// Sort messages by timestamp
|
||||
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 => ({
|
||||
id: m.id,
|
||||
sent: m.sent,
|
||||
|
|
@ -410,6 +425,14 @@ export function useNostrChat() {
|
|||
|
||||
// Sort messages by timestamp
|
||||
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) {
|
||||
console.error('Failed to send message:', error)
|
||||
|
|
@ -440,6 +463,7 @@ export function useNostrChat() {
|
|||
sendMessage,
|
||||
getMessages,
|
||||
clearMessages,
|
||||
loadCurrentUser
|
||||
loadCurrentUser,
|
||||
onMessageAdded
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue