mirror of https://github.com/DJ2LS/FreeDATA.git
sort messages
parent
befe368fd3
commit
8d2d430faf
|
@ -127,12 +127,29 @@ function getDateTime(input) {
|
||||||
date = new Date(input);
|
date = new Date(input);
|
||||||
}
|
}
|
||||||
|
|
||||||
const hours = date.getHours().toString().padStart(2, '0');
|
const now = new Date();
|
||||||
const minutes = date.getMinutes().toString().padStart(2, '0');
|
const isSameDay = date.getDate() === now.getDate() &&
|
||||||
return `${hours}:${minutes}`;
|
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() {
|
function newChat() {
|
||||||
let callsign = newChatCall.value;
|
let callsign = newChatCall.value;
|
||||||
callsign = callsign.toUpperCase().trim();
|
callsign = callsign.toUpperCase().trim();
|
||||||
|
|
|
@ -47,14 +47,15 @@ setActivePinia(pinia);
|
||||||
const chat = useChatStore(pinia);
|
const chat = useChatStore(pinia);
|
||||||
|
|
||||||
function getDate(timestampRaw) {
|
function getDate(timestampRaw) {
|
||||||
// Parsing the timestamp and returning the date part as YYYY-MM-DD
|
|
||||||
const date = new Date(timestampRaw);
|
const date = new Date(timestampRaw);
|
||||||
const year = date.getFullYear();
|
return date.toLocaleDateString(undefined, {
|
||||||
const month = (date.getMonth() + 1).toString().padStart(2, '0');
|
year: 'numeric',
|
||||||
const day = date.getDate().toString().padStart(2, '0');
|
month: '2-digit',
|
||||||
return `${year}-${month}-${day}`;
|
day: '2-digit'
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function showDateSeparator(index, currentTimestamp, messages) {
|
function showDateSeparator(index, currentTimestamp, messages) {
|
||||||
if (index === 0) return true; // Always show the date for the first message
|
if (index === 0) return true; // Always show the date for the first message
|
||||||
|
|
||||||
|
|
|
@ -41,39 +41,48 @@ export async function processFreedataMessages(data) {
|
||||||
*/
|
*/
|
||||||
function createCallsignListFromAPI(data) {
|
function createCallsignListFromAPI(data) {
|
||||||
const callsignList = {};
|
const callsignList = {};
|
||||||
|
|
||||||
chatStore.totalUnreadMessages = 0;
|
chatStore.totalUnreadMessages = 0;
|
||||||
|
|
||||||
data.messages.forEach((message) => {
|
data.messages.forEach((message) => {
|
||||||
let callsign =
|
const callsign = message.direction === "receive" ? message.origin : message.destination;
|
||||||
message.direction === "receive" ? message.origin : message.destination;
|
|
||||||
|
|
||||||
if (
|
// Increment global unread count if the message is not read.
|
||||||
!callsignList[callsign] ||
|
if (!message.is_read) {
|
||||||
callsignList[callsign].timestamp < message.timestamp
|
chatStore.totalUnreadMessages++;
|
||||||
) {
|
}
|
||||||
let unreadCounter = 0;
|
|
||||||
|
|
||||||
if (callsignList[callsign]) {
|
|
||||||
unreadCounter = callsignList[callsign].unread_messages;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!message.is_read) {
|
|
||||||
unreadCounter++;
|
|
||||||
chatStore.totalUnreadMessages++;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Create or update the callsign entry.
|
||||||
|
if (!callsignList[callsign]) {
|
||||||
callsignList[callsign] = {
|
callsignList[callsign] = {
|
||||||
timestamp: message.timestamp,
|
timestamp: message.timestamp,
|
||||||
body: message.body,
|
body: message.body,
|
||||||
unread_messages: unreadCounter,
|
unread_messages: !message.is_read ? 1 : 0,
|
||||||
};
|
};
|
||||||
} else if (!message.is_read) {
|
} else {
|
||||||
chatStore.totalUnreadMessages++;
|
// 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 = {};
|
const callsignMessages = {};
|
||||||
|
|
||||||
data.messages.forEach((message) => {
|
data.messages.forEach((message) => {
|
||||||
let callsign =
|
// Determine the callsign based on message direction.
|
||||||
message.direction === "receive" ? message.origin : message.destination;
|
const callsign = message.direction === "receive" ? message.origin : message.destination;
|
||||||
|
|
||||||
if (!callsignMessages[callsign]) {
|
if (!callsignMessages[callsign]) {
|
||||||
callsignMessages[callsign] = [];
|
callsignMessages[callsign] = [];
|
||||||
|
|
Loading…
Reference in New Issue