From 611cef6ca044ef348a37dffdba9698dd5f4537ce Mon Sep 17 00:00:00 2001 From: reya Date: Mon, 23 Dec 2024 12:47:03 +0700 Subject: [PATCH] wip: refactor --- crates/app/src/main.rs | 9 +--- crates/app/src/views/dock/chat/room.rs | 67 +++++++++++++++++++++++--- 2 files changed, 63 insertions(+), 13 deletions(-) diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index 4bb21e7..eed7ec7 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -250,13 +250,8 @@ async fn main() { let metadata = async_cx .background_executor() .spawn(async move { - if let Ok(metadata) = - client.database().metadata(event.pubkey).await - { - metadata - } else { - None - } + (client.database().metadata(event.pubkey).await) + .unwrap_or_default() }) .await; diff --git a/crates/app/src/views/dock/chat/room.rs b/crates/app/src/views/dock/chat/room.rs index 86feefe..d35043a 100644 --- a/crates/app/src/views/dock/chat/room.rs +++ b/crates/app/src/views/dock/chat/room.rs @@ -2,16 +2,18 @@ use coop_ui::{ button::{Button, ButtonVariants}, input::{InputEvent, TextInput}, theme::ActiveTheme, - v_flex, Icon, IconName, + v_flex, Icon, IconName, StyledExt, }; use gpui::*; use itertools::Itertools; use nostr_sdk::prelude::*; +use prelude::FluentBuilder; use std::{collections::HashMap, sync::Arc}; use crate::{ get_client, states::chat::{ChatRegistry, Room}, + utils::{ago, show_npub}, }; #[derive(Clone, Debug, IntoElement)] @@ -39,8 +41,63 @@ impl MessageItem { } impl RenderOnce for MessageItem { - fn render(self, _cx: &mut WindowContext) -> impl IntoElement { - div().flex().flex_col().text_sm().child(self.content) + fn render(self, cx: &mut WindowContext) -> impl IntoElement { + let ago = ago(self.created_at.as_u64()); + let fallback_name = show_npub(self.author, 16); + + div() + .flex() + .gap_3() + .w_full() + .p_2() + .child(div().flex_shrink_0().map(|this| { + if let Some(metadata) = self.metadata.clone() { + if let Some(picture) = metadata.picture { + this.child( + img(picture) + .size_8() + .rounded_full() + .object_fit(ObjectFit::Cover), + ) + } else { + this.child(img("brand/avatar.png").size_8().rounded_full()) + } + } else { + this.child(img("brand/avatar.png").size_8().rounded_full()) + } + })) + .child( + div() + .flex() + .flex_col() + .flex_initial() + .overflow_hidden() + .child( + div() + .flex() + .items_baseline() + .gap_2() + .text_xs() + .child(div().font_semibold().map(|this| { + if let Some(metadata) = self.metadata { + if let Some(display_name) = metadata.display_name { + this.child(display_name) + } else { + this.child(fallback_name) + } + } else { + this.child(fallback_name) + } + })) + .child(div().child(ago).text_color(cx.theme().muted_foreground)), + ) + .child( + div() + .text_sm() + .text_color(cx.theme().foreground) + .child(self.content), + ), + ) } } @@ -278,14 +335,12 @@ impl Render for ChatRoom { fn render(&mut self, cx: &mut gpui::ViewContext) -> impl IntoElement { v_flex() .size_full() - .child(list(self.list.clone()).size_full().flex_1()) + .child(list(self.list.clone()).flex_1()) .child( div() .flex_shrink_0() .w_full() .h_12() - .border_t_1() - .border_color(cx.theme().border.opacity(0.7)) .flex() .items_center() .gap_2()