use super::style::{Tokens, radius, size, space, text}; use crate::lang::w; use common::call::ForwardMessages; use common::model::Id; use gpui_kit::{ AppContext, Context, Entity, InteractiveElement, IntoElement, ParentElement, Render, StatefulInteractiveElement, Styled, Window, component::{ WindowExt, input::{InputEvent, InputState}, }, div, px, }; use super::{ Yumush, clickable, community_name, composer::Composer, widget::{compact_field, search_text, spawn_done}, }; pub struct Forward { yumush: Entity, selected: Vec, filter: Entity, keep_authors: bool, } impl Forward { fn new( yumush: Entity, selected: Vec, window: &mut Window, cx: &mut Context, ) -> Self { let filter = cx.new(|cx| { let mut state = InputState::new(window, cx); state.set_placeholder(w().forward.filter, window, cx); state }); cx.subscribe_in(&filter, window, |_, _, event, _, cx| { if matches!(event, InputEvent::Change) { cx.notify(); } }) .detach(); let keep_authors = selected.len() > 1; Self { yumush, selected, filter, keep_authors, } } } impl Render for Forward { fn render(&mut self, _: &mut Window, cx: &mut Context) -> impl IntoElement { let t = Tokens::of(cx); let query = search_text(&self.filter, cx); let entity = cx.entity(); let keep_authors = self.keep_authors; let rows: Vec<_> = { let yumush = self.yumush.read(cx); yumush .chat .communities() .iter() .filter(|community| community_name(community).to_lowercase().contains(&query)) .enumerate() .map(|(index, community)| { let community_id = community.id; let selected = self.selected.clone(); let forward_entity = self.yumush.clone(); clickable(("forward_to", index), t, move |window, cx| { let selected = selected.clone(); forward_entity.update(cx, |yumush, cx| { yumush.forward_to(selected, community_id, keep_authors, window, cx) }); }) .h(px(size::CONTROL_SM)) .px(px(space::SM)) .flex() .items_center() .gap(px(space::SM)) .rounded(px(radius::MD)) .child(yumush.community_badge( community, px(size::ICON), super::text(text::CAPTION), t, )) .child( div() .flex_1() .min_w_0() .truncate() .child(community_name(community).to_string()), ) }) .collect() }; div() .flex() .flex_col() .gap(px(space::MD)) .child(compact_field(t, &self.filter)) .child(super::parts::labeled_toggle( "keep_authors", w().forward.keep_authors, keep_authors, t, move |_, cx| { entity.update(cx, |this, cx| { this.keep_authors = !this.keep_authors; cx.notify(); }); }, )) .child( div() .id("forward_list") .max_h(px(size::POPOVER)) .flex() .flex_col() .gap(px(space::XXS)) .overflow_y_scroll() .children(rows), ) } } impl Yumush { pub(super) fn open_forward( &mut self, selected: Vec, window: &mut Window, cx: &mut Context, ) { if selected.is_empty() { return; } let yumush = cx.entity(); let forward = cx.new(|cx| Forward::new(yumush, selected, window, cx)); window.open_dialog(cx, move |dialog, _, _| { dialog .title(w().forward.forward) .w(px(size::SHEET)) .child(forward.clone()) }); } pub(super) fn forward_to( &mut self, selected: Vec, community_id: Id, keep_authors: bool, window: &mut Window, cx: &mut Context, ) { window.close_dialog(cx); if matches!(self.chat.composer, Composer::Select { .. }) { self.chat.composer = Composer::Compose { reply: None }; } let ids: Vec = self .chat .session .messages .iter() .filter(|message| selected.contains(&message.id)) .map(|message| message.id) .collect(); let network = self.network.clone(); spawn_done(window, cx, async move { network .call(ForwardMessages { message_ids: ids, community_id, snippet: keep_authors, }) .await }); cx.notify(); } }