Format timestamps more tersely in the notification panel

Max Brunsfeld and Piotr created

Co-authored-by: Piotr <piotr@zed.dev>

Change summary

crates/collab_ui/src/chat_panel.rs         | 28 ++++++++++++++++++++++-
crates/collab_ui/src/collab_ui.rs          | 26 ----------------------
crates/collab_ui/src/notification_panel.rs | 28 +++++++++++++++++++++++
3 files changed, 53 insertions(+), 29 deletions(-)

Detailed changes

crates/collab_ui/src/chat_panel.rs 🔗

@@ -1,6 +1,5 @@
 use crate::{
-    channel_view::ChannelView, format_timestamp, is_channels_feature_enabled, render_avatar,
-    ChatPanelSettings,
+    channel_view::ChannelView, is_channels_feature_enabled, render_avatar, ChatPanelSettings,
 };
 use anyhow::Result;
 use call::ActiveCall;
@@ -874,6 +873,31 @@ impl Panel for ChatPanel {
     }
 }
 
+fn format_timestamp(
+    mut timestamp: OffsetDateTime,
+    mut now: OffsetDateTime,
+    local_timezone: UtcOffset,
+) -> String {
+    timestamp = timestamp.to_offset(local_timezone);
+    now = now.to_offset(local_timezone);
+
+    let today = now.date();
+    let date = timestamp.date();
+    let mut hour = timestamp.hour();
+    let mut part = "am";
+    if hour > 12 {
+        hour -= 12;
+        part = "pm";
+    }
+    if date == today {
+        format!("{:02}:{:02}{}", hour, timestamp.minute(), part)
+    } else if date.next_day() == Some(today) {
+        format!("yesterday at {:02}:{:02}{}", hour, timestamp.minute(), part)
+    } else {
+        format!("{:02}/{}/{}", date.month() as u32, date.day(), date.year())
+    }
+}
+
 fn render_icon_button<V: View>(style: &IconButton, svg_path: &'static str) -> impl Element<V> {
     Svg::new(svg_path)
         .with_color(style.color)

crates/collab_ui/src/collab_ui.rs 🔗

@@ -21,7 +21,6 @@ use gpui::{
 };
 use std::{rc::Rc, sync::Arc};
 use theme::AvatarStyle;
-use time::{OffsetDateTime, UtcOffset};
 use util::ResultExt;
 use workspace::AppState;
 
@@ -161,31 +160,6 @@ fn render_avatar<T: 'static>(
         .into_any()
 }
 
-fn format_timestamp(
-    mut timestamp: OffsetDateTime,
-    mut now: OffsetDateTime,
-    local_timezone: UtcOffset,
-) -> String {
-    timestamp = timestamp.to_offset(local_timezone);
-    now = now.to_offset(local_timezone);
-
-    let today = now.date();
-    let date = timestamp.date();
-    let mut hour = timestamp.hour();
-    let mut part = "am";
-    if hour > 12 {
-        hour -= 12;
-        part = "pm";
-    }
-    if date == today {
-        format!("{:02}:{:02}{}", hour, timestamp.minute(), part)
-    } else if date.next_day() == Some(today) {
-        format!("yesterday at {:02}:{:02}{}", hour, timestamp.minute(), part)
-    } else {
-        format!("{:02}/{}/{}", date.month() as u32, date.day(), date.year())
-    }
-}
-
 fn is_channels_feature_enabled(cx: &gpui::WindowContext<'_>) -> bool {
     cx.is_staff() || cx.has_flag::<ChannelsAlpha>()
 }

crates/collab_ui/src/notification_panel.rs 🔗

@@ -1,4 +1,4 @@
-use crate::{chat_panel::ChatPanel, format_timestamp, render_avatar, NotificationPanelSettings};
+use crate::{chat_panel::ChatPanel, render_avatar, NotificationPanelSettings};
 use anyhow::Result;
 use channel::ChannelStore;
 use client::{Client, Notification, User, UserStore};
@@ -848,3 +848,29 @@ impl workspace::notifications::Notification for NotificationToast {
         matches!(event, ToastEvent::Dismiss)
     }
 }
+
+fn format_timestamp(
+    mut timestamp: OffsetDateTime,
+    mut now: OffsetDateTime,
+    local_timezone: UtcOffset,
+) -> String {
+    timestamp = timestamp.to_offset(local_timezone);
+    now = now.to_offset(local_timezone);
+
+    let today = now.date();
+    let date = timestamp.date();
+    if date == today {
+        let difference = now - timestamp;
+        if difference >= Duration::from_secs(3600) {
+            format!("{}h", difference.whole_seconds() / 3600)
+        } else if difference >= Duration::from_secs(60) {
+            format!("{}m", difference.whole_seconds() / 60)
+        } else {
+            "just now".to_string()
+        }
+    } else if date.next_day() == Some(today) {
+        format!("yesterday")
+    } else {
+        format!("{:02}/{}/{}", date.month() as u32, date.day(), date.year())
+    }
+}