fixed contacts list sorting

This commit is contained in:
slawk0
2024-12-08 16:07:28 +01:00
parent 17ee9d1fcf
commit eedae59646
4 changed files with 69 additions and 8 deletions

View File

@@ -80,9 +80,13 @@ function ContactsList({
}
const sortedContacts = [...contactsList].sort((a, b) => {
// First, sort by read status
if (a.read !== b.read) {
return a.read ? 1 : -1;
}
if (a.read && b.read) {
return -a.last_active?.localeCompare(b.last_active);
}
return 0;
});

View File

@@ -118,9 +118,17 @@ function MessagesArea({
user_id: msg.sender_id,
conversation_id: msg.conversation_id,
type: 'direct',
last_active: new Date().toString(),
},
];
} else if (existingContact) {
return prevContacts.map((contact) =>
contact.conversation_id === msg.conversation_id
? { ...contact, last_active: new Date().toString() }
: contact,
);
}
return prevContacts;
});
@@ -137,12 +145,43 @@ function MessagesArea({
user_id: msg.sender_id,
type: 'direct',
conversation_id: msg.conversation_id,
last_active: new Date().toString(),
},
false,
);
}
if (msg.conversation_id == currentContact?.conversation_id) {
messageHandler(msg);
} else {
if (msg.conversation_id == currentContact?.conversation_id) {
messageHandler(msg);
setContactsList((prevContacts) => {
// Find if contact already exists
const existingContact = prevContacts.find(
(c) => c.conversation_id === msg.conversation_id,
);
if (!existingContact) {
sendContact(msg.sender);
return [
...prevContacts,
{
username: msg.sender,
read: false,
id: msg.message_id,
user_id: msg.sender_id,
conversation_id: msg.conversation_id,
type: 'direct',
last_active: new Date().toString(),
},
];
} else if (existingContact) {
return prevContacts.map((contact) =>
contact.conversation_id === msg.conversation_id
? { ...contact, last_active: new Date().toString() }
: contact,
);
}
return prevContacts;
});
}
}
});
@@ -188,7 +227,7 @@ function MessagesArea({
title={`${new Intl.DateTimeFormat('en-Gb', {
timeStyle: 'medium',
dateStyle: 'short',
}).format(msg.sent_at)}`}
})?.format(msg.sent_at)}`}
>
{msg.sender}: {msg.message}
</span>

View File

@@ -30,6 +30,7 @@ export type ContactsProps = {
id: number;
type: 'direct' | 'group';
conversation_id: number;
last_active: string;
};
function Chat() {

View File

@@ -176,6 +176,7 @@ async function insertMessage(
attachmentUrls,
]);
console.log("insertmessageresult: ", result.rows);
updateConversationLastActive(conversation_id);
return result.rows[0];
} catch (e) {
console.error("Failed to insert message ", e);
@@ -433,10 +434,11 @@ async function insertContact(userUsername, receiverUsername, read) {
WHEN ci.user_id = (SELECT sender_id FROM user_ids) THEN (SELECT username FROM Accounts WHERE user_id = (SELECT receiver_id FROM user_ids))
ELSE (SELECT username FROM Accounts WHERE user_id = (SELECT sender_id FROM user_ids))
END AS username,
c.last_active,
ci.read,
ci.last_active,
'direct' AS type
FROM contact_insert ci;
FROM contact_insert ci
JOIN Conversations c ON ci.conversation_id = c.conversation_id;
`;
try {
@@ -458,7 +460,7 @@ async function getContacts(user_id) {
c.contact_id AS id,
a2.user_id AS user_id,
a2.username AS username,
c.last_active,
conv.last_active,
c.conversation_id,
conv.conversation_type AS type,
c.read
@@ -469,7 +471,7 @@ async function getContacts(user_id) {
WHERE c.user_id = $1
AND conv.conversation_type = 'direct'
AND a2.user_id != $1
ORDER BY c.last_active DESC;
ORDER BY conv.last_active DESC;
`;
const groupsQuery = `
@@ -591,6 +593,21 @@ async function updateContactLastActive(user_id, contact_id) {
}
}
async function updateConversationLastActive(conversation_id) {
//TODO CHECK IF USER IS MEMBER OF THAT CONVERSATION
const query = `
UPDATE Conversations
SET last_active = NOW()
WHERE conversation_id = $1
`;
try {
await client.query(query, [conversation_id]);
console.log("Successfully update conversation last active time");
} catch (e) {
console.error("Failed to update conversation last active time ", e);
}
}
async function getConversationsForUser(user_id) {
const query = `
SELECT conversation_id