Use naive_format_distance_from_now in Notifications

Nate Butler created

Change summary

crates/ui2/src/components/notifications_panel.rs | 38 ++++++++++++++---
crates/ui2/src/static_data.rs                    | 31 ++++++++++++++
2 files changed, 61 insertions(+), 8 deletions(-)

Detailed changes

crates/ui2/src/components/notifications_panel.rs 🔗

@@ -1,3 +1,4 @@
+use crate::utils::naive_format_distance_from_now;
 use crate::{
     h_stack, prelude::*, static_new_notification_items_2, v_stack, Avatar, Button, Icon,
     IconButton, IconElement, Label, LabelColor, LineHeightStyle, ListHeaderMeta, ListSeparator,
@@ -137,6 +138,7 @@ impl<V> Notification<V> {
     fn new(
         id: ElementId,
         message: SharedString,
+        date_received: NaiveDateTime,
         slot: ActorOrIcon,
         click_action: Option<ClickHandler<V>>,
     ) -> Self {
@@ -150,9 +152,7 @@ impl<V> Notification<V> {
 
         Self {
             id,
-            date_received: DateTime::parse_from_rfc3339("1969-07-20T00:00:00Z")
-                .unwrap()
-                .naive_local(),
+            date_received,
             message,
             meta: None,
             slot,
@@ -170,12 +170,14 @@ impl<V> Notification<V> {
     pub fn new_actor_message(
         id: impl Into<ElementId>,
         message: impl Into<SharedString>,
+        date_received: NaiveDateTime,
         actor: PublicActor,
         click_action: ClickHandler<V>,
     ) -> Self {
         Self::new(
             id.into(),
             message.into(),
+            date_received,
             ActorOrIcon::Actor(actor),
             Some(click_action),
         )
@@ -187,12 +189,14 @@ impl<V> Notification<V> {
     pub fn new_icon_message(
         id: impl Into<ElementId>,
         message: impl Into<SharedString>,
+        date_received: NaiveDateTime,
         icon: Icon,
         click_action: ClickHandler<V>,
     ) -> Self {
         Self::new(
             id.into(),
             message.into(),
+            date_received,
             ActorOrIcon::Icon(icon),
             Some(click_action),
         )
@@ -205,10 +209,18 @@ impl<V> Notification<V> {
     pub fn new_actor_with_actions(
         id: impl Into<ElementId>,
         message: impl Into<SharedString>,
+        date_received: NaiveDateTime,
         actor: PublicActor,
         actions: [NotificationAction<V>; 2],
     ) -> Self {
-        Self::new(id.into(), message.into(), ActorOrIcon::Actor(actor), None).actions(actions)
+        Self::new(
+            id.into(),
+            message.into(),
+            date_received,
+            ActorOrIcon::Actor(actor),
+            None,
+        )
+        .actions(actions)
     }
 
     /// Creates a new notification with an icon slot
@@ -218,10 +230,18 @@ impl<V> Notification<V> {
     pub fn new_icon_with_actions(
         id: impl Into<ElementId>,
         message: impl Into<SharedString>,
+        date_received: NaiveDateTime,
         icon: Icon,
         actions: [NotificationAction<V>; 2],
     ) -> Self {
-        Self::new(id.into(), message.into(), ActorOrIcon::Icon(icon), None).actions(actions)
+        Self::new(
+            id.into(),
+            message.into(),
+            date_received,
+            ActorOrIcon::Icon(icon),
+            None,
+        )
+        .actions(actions)
     }
 
     fn on_click(mut self, handler: ClickHandler<V>) -> Self {
@@ -303,9 +323,11 @@ impl<V> Notification<V> {
                                 h_stack()
                                     .gap_1()
                                     .child(
-                                        Label::new(
-                                            self.date_received.format("%m/%d/%Y").to_string(),
-                                        )
+                                        Label::new(naive_format_distance_from_now(
+                                            self.date_received,
+                                            true,
+                                            true,
+                                        ))
                                         .color(LabelColor::Muted),
                                     )
                                     .child(self.render_meta_items(cx)),

crates/ui2/src/static_data.rs 🔗

@@ -2,6 +2,7 @@ use std::path::PathBuf;
 use std::str::FromStr;
 use std::sync::Arc;
 
+use chrono::{DateTime, NaiveDateTime};
 use gpui2::{AppContext, ViewContext};
 use rand::Rng;
 use theme2::ActiveTheme;
@@ -332,12 +333,18 @@ pub fn static_new_notification_items_2<V: 'static>() -> Vec<Notification<V>> {
         Notification::new_icon_message(
             "notif-1",
             "You were mentioned in a note.",
+            DateTime::parse_from_rfc3339("2023-11-02T11:59:57Z")
+                .unwrap()
+                .naive_local(),
             Icon::AtSign,
             Arc::new(|_, _| {}),
         ),
         Notification::new_actor_with_actions(
             "notif-2",
             "as-cii sent you a contact request.",
+            DateTime::parse_from_rfc3339("2023-11-02T12:09:07Z")
+                .unwrap()
+                .naive_local(),
             PublicActor::new("as-cii", "http://github.com/as-cii.png?s=50"),
             [
                 NotificationAction::new(
@@ -355,12 +362,18 @@ pub fn static_new_notification_items_2<V: 'static>() -> Vec<Notification<V>> {
         Notification::new_icon_message(
             "notif-3",
             "You were mentioned #design.",
+            DateTime::parse_from_rfc3339("2023-11-02T12:09:07Z")
+                .unwrap()
+                .naive_local(),
             Icon::MessageBubbles,
             Arc::new(|_, _| {}),
         ),
         Notification::new_actor_with_actions(
             "notif-4",
             "as-cii sent you a contact request.",
+            DateTime::parse_from_rfc3339("2023-11-01T12:09:07Z")
+                .unwrap()
+                .naive_local(),
             PublicActor::new("as-cii", "http://github.com/as-cii.png?s=50"),
             [
                 NotificationAction::new(
@@ -378,12 +391,18 @@ pub fn static_new_notification_items_2<V: 'static>() -> Vec<Notification<V>> {
         Notification::new_icon_message(
             "notif-5",
             "You were mentioned in a note.",
+            DateTime::parse_from_rfc3339("2023-10-28T12:09:07Z")
+                .unwrap()
+                .naive_local(),
             Icon::AtSign,
             Arc::new(|_, _| {}),
         ),
         Notification::new_actor_with_actions(
             "notif-6",
             "as-cii sent you a contact request.",
+            DateTime::parse_from_rfc3339("2022-10-25T12:09:07Z")
+                .unwrap()
+                .naive_local(),
             PublicActor::new("as-cii", "http://github.com/as-cii.png?s=50"),
             [
                 NotificationAction::new(
@@ -401,12 +420,18 @@ pub fn static_new_notification_items_2<V: 'static>() -> Vec<Notification<V>> {
         Notification::new_icon_message(
             "notif-7",
             "You were mentioned in a note.",
+            DateTime::parse_from_rfc3339("2022-10-14T12:09:07Z")
+                .unwrap()
+                .naive_local(),
             Icon::AtSign,
             Arc::new(|_, _| {}),
         ),
         Notification::new_actor_with_actions(
             "notif-8",
             "as-cii sent you a contact request.",
+            DateTime::parse_from_rfc3339("2021-10-12T12:09:07Z")
+                .unwrap()
+                .naive_local(),
             PublicActor::new("as-cii", "http://github.com/as-cii.png?s=50"),
             [
                 NotificationAction::new(
@@ -424,12 +449,18 @@ pub fn static_new_notification_items_2<V: 'static>() -> Vec<Notification<V>> {
         Notification::new_icon_message(
             "notif-9",
             "You were mentioned in a note.",
+            DateTime::parse_from_rfc3339("2021-02-02T12:09:07Z")
+                .unwrap()
+                .naive_local(),
             Icon::AtSign,
             Arc::new(|_, _| {}),
         ),
         Notification::new_actor_with_actions(
             "notif-10",
             "as-cii sent you a contact request.",
+            DateTime::parse_from_rfc3339("1969-07-20T00:00:00Z")
+                .unwrap()
+                .naive_local(),
             PublicActor::new("as-cii", "http://github.com/as-cii.png?s=50"),
             [
                 NotificationAction::new(