From 8d2d430faf990c1d0cbe137a04db02013b1eccaf Mon Sep 17 00:00:00 2001 From: DJ2LS <75909252+DJ2LS@users.noreply.github.com> Date: Tue, 1 Apr 2025 10:37:47 +0200 Subject: [PATCH] sort messages --- .../src/components/chat_conversations.vue | 23 +++++++- freedata_gui/src/components/chat_messages.vue | 11 ++-- freedata_gui/src/js/messagesHandler.js | 55 +++++++++++-------- 3 files changed, 58 insertions(+), 31 deletions(-) diff --git a/freedata_gui/src/components/chat_conversations.vue b/freedata_gui/src/components/chat_conversations.vue index 207cc36c..2303076c 100644 --- a/freedata_gui/src/components/chat_conversations.vue +++ b/freedata_gui/src/components/chat_conversations.vue @@ -127,12 +127,29 @@ function getDateTime(input) { date = new Date(input); } - const hours = date.getHours().toString().padStart(2, '0'); - const minutes = date.getMinutes().toString().padStart(2, '0'); - return `${hours}:${minutes}`; + const now = new Date(); + const isSameDay = date.getDate() === now.getDate() && + date.getMonth() === now.getMonth() && + date.getFullYear() === now.getFullYear(); + + if (isSameDay) { + // Use the browser's locale to format time only + return date.toLocaleTimeString(undefined, { hour: '2-digit', minute: '2-digit' }); + } else { + // Use the browser's locale to format both date and time + const datePart = date.toLocaleDateString(undefined, { day: '2-digit', month: '2-digit', year: 'numeric' }); + //const timePart = date.toLocaleTimeString(undefined, { hour: '2-digit', minute: '2-digit' }); + //return `${datePart} ${timePart}`; + return `${datePart}`; + } } + + + + + function newChat() { let callsign = newChatCall.value; callsign = callsign.toUpperCase().trim(); diff --git a/freedata_gui/src/components/chat_messages.vue b/freedata_gui/src/components/chat_messages.vue index 80da0869..0c5ff285 100644 --- a/freedata_gui/src/components/chat_messages.vue +++ b/freedata_gui/src/components/chat_messages.vue @@ -47,14 +47,15 @@ setActivePinia(pinia); const chat = useChatStore(pinia); function getDate(timestampRaw) { - // Parsing the timestamp and returning the date part as YYYY-MM-DD const date = new Date(timestampRaw); - const year = date.getFullYear(); - const month = (date.getMonth() + 1).toString().padStart(2, '0'); - const day = date.getDate().toString().padStart(2, '0'); - return `${year}-${month}-${day}`; + return date.toLocaleDateString(undefined, { + year: 'numeric', + month: '2-digit', + day: '2-digit' + }); } + function showDateSeparator(index, currentTimestamp, messages) { if (index === 0) return true; // Always show the date for the first message diff --git a/freedata_gui/src/js/messagesHandler.js b/freedata_gui/src/js/messagesHandler.js index 17f396ff..7d7412eb 100644 --- a/freedata_gui/src/js/messagesHandler.js +++ b/freedata_gui/src/js/messagesHandler.js @@ -41,39 +41,48 @@ export async function processFreedataMessages(data) { */ function createCallsignListFromAPI(data) { const callsignList = {}; - chatStore.totalUnreadMessages = 0; data.messages.forEach((message) => { - let callsign = - message.direction === "receive" ? message.origin : message.destination; + const callsign = message.direction === "receive" ? message.origin : message.destination; - if ( - !callsignList[callsign] || - callsignList[callsign].timestamp < message.timestamp - ) { - let unreadCounter = 0; - - if (callsignList[callsign]) { - unreadCounter = callsignList[callsign].unread_messages; - } - - if (!message.is_read) { - unreadCounter++; - chatStore.totalUnreadMessages++; - } + // Increment global unread count if the message is not read. + if (!message.is_read) { + chatStore.totalUnreadMessages++; + } + // Create or update the callsign entry. + if (!callsignList[callsign]) { callsignList[callsign] = { timestamp: message.timestamp, body: message.body, - unread_messages: unreadCounter, + unread_messages: !message.is_read ? 1 : 0, }; - } else if (!message.is_read) { - chatStore.totalUnreadMessages++; + } else { + // Update the unread count if the message is unread. + if (!message.is_read) { + callsignList[callsign].unread_messages++; + } + // Update stored details if this message is newer. + if (new Date(message.timestamp) > new Date(callsignList[callsign].timestamp)) { + callsignList[callsign].timestamp = message.timestamp; + callsignList[callsign].body = message.body; + } } }); - return callsignList; + // Get the keys sorted in descending order by timestamp. + const sortedKeys = Object.keys(callsignList).sort( + (a, b) => new Date(callsignList[b].timestamp) - new Date(callsignList[a].timestamp) + ); + + // Rebuild the object with keys in sorted order. + const sortedCallsignList = {}; + sortedKeys.forEach((key) => { + sortedCallsignList[key] = callsignList[key]; + }); + + return sortedCallsignList; } /** @@ -85,8 +94,8 @@ function createSortedMessagesList(data) { const callsignMessages = {}; data.messages.forEach((message) => { - let callsign = - message.direction === "receive" ? message.origin : message.destination; + // Determine the callsign based on message direction. + const callsign = message.direction === "receive" ? message.origin : message.destination; if (!callsignMessages[callsign]) { callsignMessages[callsign] = [];