From dd7d13f41bcaa78afd1b474f34769554d31b1e00 Mon Sep 17 00:00:00 2001 From: padreug Date: Wed, 6 Aug 2025 00:03:11 +0200 Subject: [PATCH] refactor: Improve chat component layout and message handling - Ensure the chat header is always present to maintain layout consistency, even when no peer is selected. - Update message display logic to only scroll to the bottom when new messages arrive, enhancing user experience. - Refactor message input and display sections for better responsiveness and usability across devices. --- src/components/nostr/ChatComponent.vue | 144 +++++++++++++------------ 1 file changed, 77 insertions(+), 67 deletions(-) diff --git a/src/components/nostr/ChatComponent.vue b/src/components/nostr/ChatComponent.vue index 3b00cc2..2374a9d 100644 --- a/src/components/nostr/ChatComponent.vue +++ b/src/components/nostr/ChatComponent.vue @@ -192,66 +192,66 @@
-
- -
-
- - - {{ getPeerInitials(selectedPeer) }} - -
-

{{ selectedPeer.username || 'Unknown User' }}

-

- {{ formatPubkey(selectedPeer.pubkey) }} + +

+
+ + + {{ getPeerInitials(selectedPeer) }} + +
+

{{ selectedPeer.username || 'Unknown User' }}

+

+ {{ formatPubkey(selectedPeer.pubkey) }} +

+
+
+
+
+ + + +
+
+
+

{{ message.content }}

+

+ {{ formatTime(message.created_at) }}

+ +
+ - - -
-
-
-

{{ message.content }}

-

- {{ formatTime(message.created_at) }} -

-
-
-
-
- - - -
-
- - -
-
+ +
+
+ + +
@@ -300,9 +300,12 @@ interface ChatMessage { const peers = ref([]) const selectedPeer = ref(null) const messageInput = ref('') -const messagesEndRef = ref(null) + const isLoading = ref(false) const showChat = ref(false) +const messagesScrollArea = ref(null) +const messagesContainer = ref(null) +const scrollTarget = ref(null) // Mobile detection const isMobile = ref(false) @@ -400,10 +403,8 @@ const selectPeer = async (peer: Peer) => { // Subscribe to messages from this peer await subscribeToPeer(peer.pubkey) - // Scroll to bottom after messages load - nextTick(() => { - scrollToBottom() - }) + // Don't scroll when selecting a peer - let user see existing messages + // Only scroll when new messages arrive } const sendMessage = async () => { @@ -423,9 +424,15 @@ const sendMessage = async () => { } const scrollToBottom = () => { - if (messagesEndRef.value) { - messagesEndRef.value.scrollIntoView({ behavior: 'smooth' }) - } + nextTick(() => { + if (scrollTarget.value) { + // Scroll to the hidden target element at the bottom + scrollTarget.value.scrollIntoView({ + behavior: 'smooth', + block: 'end' + }) + } + }) } const formatPubkey = (pubkey: string) => { @@ -466,9 +473,12 @@ onUnmounted(() => { }) // Watch for new messages and scroll to bottom -watch(currentMessages, () => { - nextTick(() => { - scrollToBottom() - }) +watch(currentMessages, (newMessages, oldMessages) => { + // Only scroll if messages were added (not just peer selection) + if (newMessages.length > 0 && oldMessages && newMessages.length > oldMessages.length) { + nextTick(() => { + scrollToBottom() + }) + } }) \ No newline at end of file