| 1 | use std::rc::Rc; |
| 2 | |
| 3 | use gpui_kit::component::{ |
| 4 | button::{Button, ButtonVariants as _}, |
| 5 | h_flex, v_flex, |
| 6 | }; |
| 7 | use gpui_kit::*; |
| 8 | use teams_core::{ImageRef, LinkPreview}; |
| 9 | |
| 10 | use super::{ConversationView, HoverSlot, PENDING_KEY_PREFIX}; |
| 11 | use crate::app_state::AppState; |
| 12 | use crate::rows::{Delivery, MessageRow, PostRow}; |
| 13 | use crate::scheduled_rows::SCHEDULED_KEY_PREFIX; |
| 14 | use crate::views::adaptive_card::{ensure_cards_inputs, request_card_refreshes}; |
| 15 | use crate::views::composer::Composer; |
| 16 | use crate::views::message_row::{DeliveryActions, RowActions}; |
| 17 | use crate::views::post_card::{PostAction, PostActions}; |
| 18 | |
| 19 | pub(super) struct RenderEnv { |
| 20 | pub view: WeakEntity<ConversationView>, |
| 21 | pub app: Entity<AppState>, |
| 22 | pub highlighted: Option<String>, |
| 23 | pub drafting: bool, |
| 24 | pub reply_editor: Option<(String, Entity<Composer>)>, |
| 25 | } |
| 26 | |
| 27 | fn boxed(action: impl Fn(&mut App) + 'static) -> Box<dyn Fn(&mut App)> { |
| 28 | Box::new(action) |
| 29 | } |
| 30 | |
| 31 | impl RenderEnv { |
| 32 | fn request_missing_media(&self, message: &MessageRow, cx: &mut App) { |
| 33 | let senders: Vec<String> = (!message.own && !message.series.has_prev) |
| 34 | .then(|| message.sender_id.clone()) |
| 35 | .flatten() |
| 36 | .into_iter() |
| 37 | .chain( |
| 38 | message |
| 39 | .reactions |
| 40 | .iter() |
| 41 | .flat_map(|chip| chip.reactors.iter()) |
| 42 | .filter_map(|reactor| reactor.user_id.clone()), |
| 43 | ) |
| 44 | .filter(|sender| self.app.read(cx).directory.avatar(sender).is_none()) |
| 45 | .collect(); |
| 46 | if !senders.is_empty() { |
| 47 | let app = self.app.clone(); |
| 48 | cx.defer(move |cx| { |
| 49 | app.update(cx, |state, cx| state.request_avatars(senders, cx)); |
| 50 | }); |
| 51 | } |
| 52 | let missing_images: Vec<ImageRef> = message |
| 53 | .images |
| 54 | .iter() |
| 55 | .cloned() |
| 56 | .chain(message.link_preview.as_ref().and_then(LinkPreview::image)) |
| 57 | .filter(|image| self.app.read(cx).directory.image(&image.url).is_none()) |
| 58 | .collect(); |
| 59 | if !missing_images.is_empty() { |
| 60 | let app = self.app.clone(); |
| 61 | cx.defer(move |cx| { |
| 62 | app.update(cx, |state, cx| state.request_images(missing_images, cx)); |
| 63 | }); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | fn bot_identity( |
| 68 | &self, |
| 69 | message: &MessageRow, |
| 70 | cx: &mut App, |
| 71 | ) -> Option<crate::card_state::BotIdentity> { |
| 72 | let application_id = message.application_id.as_deref()?; |
| 73 | let known = self |
| 74 | .app |
| 75 | .read(cx) |
| 76 | .bot_identity(&message.conversation_id, application_id); |
| 77 | if known.is_none() { |
| 78 | let (app, conversation_id) = (self.app.clone(), message.conversation_id.clone()); |
| 79 | cx.defer(move |cx| { |
| 80 | app.update(cx, |state, cx| { |
| 81 | state.request_chat_apps(&conversation_id, cx) |
| 82 | }); |
| 83 | }); |
| 84 | } |
| 85 | known |
| 86 | } |
| 87 | |
| 88 | pub fn message_actions( |
| 89 | &self, |
| 90 | message: &MessageRow, |
| 91 | window: &mut Window, |
| 92 | cx: &mut App, |
| 93 | ) -> RowActions { |
| 94 | let view = &self.view; |
| 95 | let failed = matches!(message.delivery, Delivery::Failed(_)); |
| 96 | let retry = failed.then(|| { |
| 97 | let (view, key, handle) = (view.clone(), message.key.clone(), window.window_handle()); |
| 98 | boxed(move |cx| { |
| 99 | let (view, key) = (view.clone(), key.clone()); |
| 100 | handle |
| 101 | .update(cx, move |_, window, cx| { |
| 102 | view.update(cx, |this, cx| this.retry(&key, window, cx)) |
| 103 | .ok(); |
| 104 | }) |
| 105 | .ok(); |
| 106 | }) |
| 107 | }); |
| 108 | let scheduled = view.upgrade().and_then(|entity| { |
| 109 | entity |
| 110 | .read(cx) |
| 111 | .scheduled_row_actions(message, view, window, cx) |
| 112 | }); |
| 113 | let delete = failed.then(|| { |
| 114 | let (view, key) = (view.clone(), message.key.clone()); |
| 115 | boxed(move |cx| { |
| 116 | view.update(cx, |this, cx| this.delete_pending(&key, cx)) |
| 117 | .ok(); |
| 118 | }) |
| 119 | }); |
| 120 | self.request_missing_media(message, cx); |
| 121 | let is_real = |
| 122 | !self.drafting && !message.key.starts_with(PENDING_KEY_PREFIX) && scheduled.is_none(); |
| 123 | let hovered = is_real.then(|| { |
| 124 | let (view, key) = (view.clone(), message.key.clone()); |
| 125 | Rc::new(move |hovered: bool, cx: &mut App| { |
| 126 | view.update(cx, |this, cx| { |
| 127 | this.set_hover(HoverSlot::Row, &key, hovered, cx) |
| 128 | }) |
| 129 | .ok(); |
| 130 | }) as Rc<dyn Fn(bool, &mut App)> |
| 131 | }); |
| 132 | let (delivery, scheduled_menu, hovered) = match scheduled { |
| 133 | Some(actions) => (actions.delivery, actions.menu, Some(actions.hovered)), |
| 134 | None => ( |
| 135 | DeliveryActions { |
| 136 | retry, |
| 137 | delete, |
| 138 | send_now: None, |
| 139 | }, |
| 140 | None, |
| 141 | hovered, |
| 142 | ), |
| 143 | }; |
| 144 | let actionable = is_real && !message.deleted; |
| 145 | let (menu, react, reaction_controls) = view |
| 146 | .upgrade() |
| 147 | .filter(|_| actionable) |
| 148 | .map(|entity| { |
| 149 | let this = entity.read(cx); |
| 150 | let mine = message |
| 151 | .reactions |
| 152 | .iter() |
| 153 | .filter(|chip| chip.mine) |
| 154 | .map(|chip| chip.glyph()) |
| 155 | .collect(); |
| 156 | let refresh = |
| 157 | message |
| 158 | .adaptive_cards |
| 159 | .iter() |
| 160 | .enumerate() |
| 161 | .find_map(|(index, card)| { |
| 162 | card.refresh |
| 163 | .as_ref() |
| 164 | .map(|refresh| (index, refresh.action.clone())) |
| 165 | }); |
| 166 | let menu = |
| 167 | this.message_menu(&message.key, message.own, mine, refresh, view.clone(), cx); |
| 168 | let react = menu.react.clone(); |
| 169 | let visible = this.toolbar_visible(&message.key); |
| 170 | let controls = (!message.reactions.is_empty()) |
| 171 | .then(|| this.reaction_controls(&message.key, view.clone())); |
| 172 | (visible.then_some(menu), Some(react), controls) |
| 173 | }) |
| 174 | .unwrap_or((None, None, None)); |
| 175 | let reply = (is_real && !message.deleted).then(|| { |
| 176 | let (view, key, handle) = (view.clone(), message.key.clone(), window.window_handle()); |
| 177 | boxed(move |cx| { |
| 178 | let (view, key) = (view.clone(), key.clone()); |
| 179 | handle |
| 180 | .update(cx, move |_, window, cx| { |
| 181 | view.update(cx, |this, cx| this.begin_reply(&key, window, cx)) |
| 182 | .ok(); |
| 183 | }) |
| 184 | .ok(); |
| 185 | }) |
| 186 | }); |
| 187 | let files = (is_real && !message.deleted && !message.files.is_empty()) |
| 188 | .then(|| view.upgrade()) |
| 189 | .flatten() |
| 190 | .map(|entity| { |
| 191 | entity |
| 192 | .read(cx) |
| 193 | .file_actions(&message.key, &message.files, view.clone()) |
| 194 | }); |
| 195 | let bot = self.bot_identity(message, cx); |
| 196 | ensure_cards_inputs( |
| 197 | &self.app, |
| 198 | &message.adaptive_cards, |
| 199 | &message.conversation_id, |
| 200 | &message.key, |
| 201 | window, |
| 202 | cx, |
| 203 | ); |
| 204 | request_card_refreshes( |
| 205 | &self.app, |
| 206 | &message.adaptive_cards, |
| 207 | &message.conversation_id, |
| 208 | &message.key, |
| 209 | cx, |
| 210 | ); |
| 211 | RowActions { |
| 212 | bot, |
| 213 | delivery, |
| 214 | reply, |
| 215 | hovered, |
| 216 | menu, |
| 217 | scheduled_menu, |
| 218 | react, |
| 219 | reaction_controls, |
| 220 | files, |
| 221 | highlighted: self.highlighted.as_deref() == Some(message.key.as_str()), |
| 222 | saved: self |
| 223 | .app |
| 224 | .read(cx) |
| 225 | .is_saved(&message.conversation_id, &message.key), |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | fn open_by_root( |
| 230 | &self, |
| 231 | root_key: String, |
| 232 | run: fn(&mut ConversationView, String, &mut Window, &mut Context<ConversationView>), |
| 233 | ) -> PostAction { |
| 234 | let view = self.view.clone(); |
| 235 | Box::new(move |window, cx| { |
| 236 | view.update(cx, |this, cx| run(this, root_key.clone(), window, cx)) |
| 237 | .ok(); |
| 238 | }) |
| 239 | } |
| 240 | |
| 241 | pub fn post_actions(&self, post: &PostRow, window: &mut Window, cx: &mut App) -> PostActions { |
| 242 | let root_key = post.root.key.clone(); |
| 243 | let is_local = |
| 244 | root_key.starts_with(PENDING_KEY_PREFIX) || root_key.starts_with(SCHEDULED_KEY_PREFIX); |
| 245 | let open_thread = Some( |
| 246 | self.open_by_root(root_key.clone(), |this, root_key, window, cx| { |
| 247 | this.open_thread(root_key, window, cx) |
| 248 | }), |
| 249 | ); |
| 250 | let open_reply = (!is_local).then(|| { |
| 251 | self.open_by_root(root_key.clone(), |this, root_key, window, cx| { |
| 252 | this.open_reply_editor(root_key, None, window, cx) |
| 253 | }) |
| 254 | }); |
| 255 | let reply_editor = self |
| 256 | .reply_editor |
| 257 | .as_ref() |
| 258 | .filter(|(open_root, _)| *open_root == root_key) |
| 259 | .map(|(_, composer)| self.inline_reply_editor(composer)); |
| 260 | PostActions { |
| 261 | root: self.message_actions(&post.root, window, cx), |
| 262 | replies: post |
| 263 | .replies |
| 264 | .iter() |
| 265 | .map(|reply| self.message_actions(reply, window, cx)) |
| 266 | .collect(), |
| 267 | open_thread, |
| 268 | open_reply, |
| 269 | reply_editor, |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | fn inline_reply_editor(&self, composer: &Entity<Composer>) -> AnyElement { |
| 274 | let view = self.view.clone(); |
| 275 | v_flex() |
| 276 | .w_full() |
| 277 | .gap(px(6.)) |
| 278 | .child(composer.clone()) |
| 279 | .child( |
| 280 | h_flex().justify_end().child( |
| 281 | Button::new("cancel-inline-reply") |
| 282 | .ghost() |
| 283 | .compact() |
| 284 | .label("Cancel") |
| 285 | .on_click(move |_, _, cx| { |
| 286 | view.update(cx, |this, cx| this.close_reply_editor(cx)).ok(); |
| 287 | }), |
| 288 | ), |
| 289 | ) |
| 290 | .into_any_element() |
| 291 | } |
| 292 | } |