sort messages

pull/932/head
DJ2LS 2025-04-01 10:37:47 +02:00
parent befe368fd3
commit 8d2d430faf
3 changed files with 58 additions and 31 deletions

View File

@ -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();

View File

@ -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

View File

@ -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] = [];