From 50242981a51140f7ad8d52fc1eda6d652db024c5 Mon Sep 17 00:00:00 2001 From: reya Date: Tue, 18 Feb 2025 17:04:19 +0700 Subject: [PATCH] feat: sort inbox by time after added new messages --- crates/chats/src/registry.rs | 8 +++++++- crates/chats/src/room.rs | 4 ++++ crates/common/src/last_seen.rs | 1 + 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/crates/chats/src/registry.rs b/crates/chats/src/registry.rs index 2d693fb..9167dd2 100644 --- a/crates/chats/src/registry.rs +++ b/crates/chats/src/registry.rs @@ -75,7 +75,7 @@ impl ChatRegistry { fn new(_cx: &mut Context) -> Self { Self { - rooms: Vec::with_capacity(5), + rooms: vec![], is_loading: true, } } @@ -186,6 +186,12 @@ impl ChatRegistry { }); cx.notify(); }); + + // Re sort rooms by last seen + self.rooms + .sort_by_key(|room| Reverse(room.read(cx).last_seen())); + + cx.notify(); } else { let room = cx.new(|cx| Room::parse(&event, cx)); self.rooms.insert(0, room); diff --git a/crates/chats/src/room.rs b/crates/chats/src/room.rs index 20b3c9d..b553c01 100644 --- a/crates/chats/src/room.rs +++ b/crates/chats/src/room.rs @@ -124,6 +124,10 @@ impl Room { } } + pub fn last_seen(&self) -> &LastSeen { + &self.last_seen + } + /// Get all public keys from current room pub fn pubkeys(&self) -> Vec { let mut pubkeys: Vec<_> = self.members.iter().map(|m| m.public_key()).collect(); diff --git a/crates/common/src/last_seen.rs b/crates/common/src/last_seen.rs index 972a714..456ef2f 100644 --- a/crates/common/src/last_seen.rs +++ b/crates/common/src/last_seen.rs @@ -2,6 +2,7 @@ use chrono::{Datelike, Local, TimeZone}; use gpui::SharedString; use nostr_sdk::prelude::*; +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct LastSeen(pub Timestamp); impl LastSeen {