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