| 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 mcp::ReactionProposal; |
| 11 | use teams_core::{ImageRef, LinkPreview}; |
| 12 | |
| 13 | use super::{ConversationView, HoverSlot, PENDING_KEY_PREFIX}; |
| 14 | use crate::agent_api::agent_label; |
| 15 | use crate::agents::AgentRun; |
| 16 | use crate::app_state::AppState; |
| 17 | use crate::rows::{Delivery, MessageRow, PostRow, RecordingCard}; |
| 18 | use crate::scheduled_rows::SCHEDULED_KEY_PREFIX; |
| 19 | use crate::translation::TranslationLine; |
| 20 | use crate::views::adaptive_card::{ensure_cards_inputs, request_card_refreshes}; |
| 21 | use crate::views::attachments::ImageActions; |
| 22 | use crate::views::call_card::CallActions; |
| 23 | use crate::views::call_notice::RecapActions; |
| 24 | use crate::views::composer::Composer; |
| 25 | use crate::views::message_row::{DeliveryActions, RowActions, SharedRowAction}; |
| 26 | use crate::views::post_card::{PostAction, PostActions}; |
| 27 | use crate::views::reaction_proposal_pill::ReactionPill; |
| 28 | use crate::views::toolbar_placement::TimelineBounds; |
| 29 | use crate::views::translation_line::{LineAction, TranslationActions}; |
| 30 | |
| 31 | pub(super) struct RenderEnv { |
| 32 | pub view: WeakEntity<ConversationView>, |
| 33 | pub app: Entity<AppState>, |
| 34 | pub highlighted: Option<String>, |
| 35 | pub drafting: bool, |
| 36 | pub reply_editor: Option<(String, Entity<Composer>)>, |
| 37 | pub timeline: TimelineBounds, |
| 38 | pub agent_runs: Rc<RefCell<HashMap<String, AgentRun>>>, |
| 39 | pub reaction_proposals: Rc<RefCell<HashMap<String, ReactionProposal>>>, |
| 40 | } |
| 41 | |
| 42 | fn boxed(action: impl Fn(&mut App) + 'static) -> Box<dyn Fn(&mut App)> { |
| 43 | Box::new(action) |
| 44 | } |
| 45 | |
| 46 | impl RenderEnv { |
| 47 | fn request_missing_media(&self, message: &MessageRow, cx: &mut App) { |
| 48 | let senders: Vec<String> = (!message.own && !message.series.has_prev) |
| 49 | .then(|| message.sender_id.clone()) |
| 50 | .flatten() |
| 51 | .into_iter() |
| 52 | .chain( |
| 53 | message |
| 54 | .reactions |
| 55 | .iter() |
| 56 | .flat_map(|chip| chip.reactors.iter()) |
| 57 | .filter_map(|reactor| reactor.user_id.clone()), |
| 58 | ) |
| 59 | .filter(|sender| self.app.read(cx).directory.avatar(sender).is_none()) |
| 60 | .collect(); |
| 61 | if !senders.is_empty() { |
| 62 | let app = self.app.clone(); |
| 63 | cx.defer(move |cx| { |
| 64 | app.update(cx, |state, cx| state.request_avatars(senders, cx)); |
| 65 | }); |
| 66 | } |
| 67 | let missing_images: Vec<ImageRef> = message |
| 68 | .images |
| 69 | .iter() |
| 70 | .cloned() |
| 71 | .chain(message.link_preview.as_ref().and_then(LinkPreview::image)) |
| 72 | .chain( |
| 73 | message |
| 74 | .call |
| 75 | .as_ref() |
| 76 | .and_then(|call| call.recap.as_ref()?.recording.thumbnail.clone()), |
| 77 | ) |
| 78 | .filter(|image| self.app.read(cx).directory.image(&image.url).is_none()) |
| 79 | .collect(); |
| 80 | if !missing_images.is_empty() { |
| 81 | let app = self.app.clone(); |
| 82 | cx.defer(move |cx| { |
| 83 | app.update(cx, |state, cx| state.request_images(missing_images, cx)); |
| 84 | }); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | fn reaction_pill(&self, message: &MessageRow) -> Option<ReactionPill> { |
| 89 | let proposal = self |
| 90 | .reaction_proposals |
| 91 | .borrow() |
| 92 | .get(&message.key) |
| 93 | .cloned()?; |
| 94 | let (accept_view, discard_view) = (self.view.clone(), self.view.clone()); |
| 95 | let (accept_key, discard_key) = (message.key.clone(), message.key.clone()); |
| 96 | let (emoji, discard_id, accept_id) = ( |
| 97 | proposal.emoji.clone(), |
| 98 | proposal.id.clone(), |
| 99 | proposal.id.clone(), |
| 100 | ); |
| 101 | Some(ReactionPill { |
| 102 | agent: agent_label(proposal.agent.as_deref()), |
| 103 | emoji: proposal.emoji, |
| 104 | accept: Rc::new(move |cx| { |
| 105 | accept_view |
| 106 | .update(cx, |this, cx| { |
| 107 | this.accept_reaction(&accept_key, &accept_id, &emoji, cx) |
| 108 | }) |
| 109 | .ok(); |
| 110 | }), |
| 111 | discard: Rc::new(move |cx| { |
| 112 | discard_view |
| 113 | .update(cx, |this, cx| { |
| 114 | this.discard_reaction(&discard_key, &discard_id, cx) |
| 115 | }) |
| 116 | .ok(); |
| 117 | }), |
| 118 | }) |
| 119 | } |
| 120 | |
| 121 | fn bot_identity( |
| 122 | &self, |
| 123 | message: &MessageRow, |
| 124 | cx: &mut App, |
| 125 | ) -> Option<crate::card_state::BotIdentity> { |
| 126 | let application_id = message.application_id.as_deref()?; |
| 127 | let known = self |
| 128 | .app |
| 129 | .read(cx) |
| 130 | .bot_identity(&message.conversation_id, application_id); |
| 131 | if known.is_none() { |
| 132 | let (app, conversation_id) = (self.app.clone(), message.conversation_id.clone()); |
| 133 | cx.defer(move |cx| { |
| 134 | app.update(cx, |state, cx| { |
| 135 | state.request_chat_apps(&conversation_id, cx) |
| 136 | }); |
| 137 | }); |
| 138 | } |
| 139 | known |
| 140 | } |
| 141 | |
| 142 | pub fn message_actions( |
| 143 | &self, |
| 144 | message: &MessageRow, |
| 145 | window: &mut Window, |
| 146 | cx: &mut App, |
| 147 | ) -> RowActions { |
| 148 | let view = &self.view; |
| 149 | let failed = matches!(message.delivery, Delivery::Failed(_)); |
| 150 | let retry = failed.then(|| { |
| 151 | let (view, key, handle) = (view.clone(), message.key.clone(), window.window_handle()); |
| 152 | boxed(move |cx| { |
| 153 | let (view, key) = (view.clone(), key.clone()); |
| 154 | handle |
| 155 | .update(cx, move |_, window, cx| { |
| 156 | view.update(cx, |this, cx| this.retry(&key, window, cx)) |
| 157 | .ok(); |
| 158 | }) |
| 159 | .ok(); |
| 160 | }) |
| 161 | }); |
| 162 | let scheduled = view.upgrade().and_then(|entity| { |
| 163 | entity |
| 164 | .read(cx) |
| 165 | .scheduled_row_actions(message, view, window, cx) |
| 166 | }); |
| 167 | let delete = failed.then(|| { |
| 168 | let (view, key) = (view.clone(), message.key.clone()); |
| 169 | boxed(move |cx| { |
| 170 | view.update(cx, |this, cx| this.delete_pending(&key, cx)) |
| 171 | .ok(); |
| 172 | }) |
| 173 | }); |
| 174 | self.request_missing_media(message, cx); |
| 175 | let is_real = !self.drafting |
| 176 | && !message.key.starts_with(PENDING_KEY_PREFIX) |
| 177 | && scheduled.is_none() |
| 178 | && message.call.is_none(); |
| 179 | let hovered = is_real.then(|| { |
| 180 | let (view, key) = (view.clone(), message.key.clone()); |
| 181 | Rc::new(move |hovered: bool, cx: &mut App| { |
| 182 | view.update(cx, |this, cx| { |
| 183 | this.set_hover(HoverSlot::Row, &key, hovered, cx) |
| 184 | }) |
| 185 | .ok(); |
| 186 | }) as Rc<dyn Fn(bool, &mut App)> |
| 187 | }); |
| 188 | let (delivery, scheduled_menu, hovered) = match scheduled { |
| 189 | Some(actions) => (actions.delivery, actions.menu, Some(actions.hovered)), |
| 190 | None => ( |
| 191 | DeliveryActions { |
| 192 | retry, |
| 193 | delete, |
| 194 | send_now: None, |
| 195 | }, |
| 196 | None, |
| 197 | hovered, |
| 198 | ), |
| 199 | }; |
| 200 | let actionable = is_real && !message.deleted; |
| 201 | let (menu, react, reaction_controls) = view |
| 202 | .upgrade() |
| 203 | .filter(|_| actionable) |
| 204 | .map(|entity| { |
| 205 | let this = entity.read(cx); |
| 206 | let mine = message |
| 207 | .reactions |
| 208 | .iter() |
| 209 | .filter(|chip| chip.mine) |
| 210 | .map(|chip| chip.glyph()) |
| 211 | .collect(); |
| 212 | let refresh = |
| 213 | message |
| 214 | .adaptive_cards |
| 215 | .iter() |
| 216 | .enumerate() |
| 217 | .find_map(|(index, card)| { |
| 218 | card.refresh |
| 219 | .as_ref() |
| 220 | .map(|refresh| (index, refresh.action.clone())) |
| 221 | }); |
| 222 | let menu = this.message_menu(message, mine, refresh, view.clone(), cx); |
| 223 | let react = menu.react.clone(); |
| 224 | let visible = this.toolbar_visible(&message.key); |
| 225 | let controls = (!message.reactions.is_empty()) |
| 226 | .then(|| this.reaction_controls(&message.key, view.clone())); |
| 227 | (visible.then_some(menu), Some(react), controls) |
| 228 | }) |
| 229 | .unwrap_or((None, None, None)); |
| 230 | let reply = (is_real && !message.deleted).then(|| { |
| 231 | let (view, key, handle) = (view.clone(), message.key.clone(), window.window_handle()); |
| 232 | boxed(move |cx| { |
| 233 | let (view, key) = (view.clone(), key.clone()); |
| 234 | handle |
| 235 | .update(cx, move |_, window, cx| { |
| 236 | view.update(cx, |this, cx| this.begin_reply(&key, window, cx)) |
| 237 | .ok(); |
| 238 | }) |
| 239 | .ok(); |
| 240 | }) |
| 241 | }); |
| 242 | let files = (is_real && !message.deleted && !message.files.is_empty()) |
| 243 | .then(|| view.upgrade()) |
| 244 | .flatten() |
| 245 | .map(|entity| { |
| 246 | entity |
| 247 | .read(cx) |
| 248 | .file_actions(&message.key, &message.files, view.clone()) |
| 249 | }); |
| 250 | let open_image = (is_real && !message.deleted && !message.images.is_empty()).then(|| { |
| 251 | let (open_view, open_key) = (view.clone(), message.key.clone()); |
| 252 | let (share_view, share_key) = (view.clone(), message.key.clone()); |
| 253 | ImageActions { |
| 254 | open: Rc::new(move |url: &str, cx: &mut App| { |
| 255 | open_view |
| 256 | .update(cx, |this, cx| this.open_image_viewer(&open_key, url, cx)) |
| 257 | .ok(); |
| 258 | }), |
| 259 | share: Some(Rc::new(move |cx: &mut App| { |
| 260 | share_view |
| 261 | .update(cx, |this, cx| this.forward_message(&share_key, cx)) |
| 262 | .ok(); |
| 263 | })), |
| 264 | } |
| 265 | }); |
| 266 | let bot = self.bot_identity(message, cx); |
| 267 | let actionable_run = (is_real && !message.deleted) |
| 268 | .then(|| self.agent_runs.borrow().get(&message.key).cloned()) |
| 269 | .flatten(); |
| 270 | let reaction_proposal = (is_real && !message.deleted) |
| 271 | .then(|| self.reaction_pill(message)) |
| 272 | .flatten(); |
| 273 | let translation = (is_real && !message.deleted) |
| 274 | .then(|| self.translation_actions(message)) |
| 275 | .flatten(); |
| 276 | ensure_cards_inputs( |
| 277 | &self.app, |
| 278 | &message.adaptive_cards, |
| 279 | &message.conversation_id, |
| 280 | &message.key, |
| 281 | window, |
| 282 | cx, |
| 283 | ); |
| 284 | request_card_refreshes( |
| 285 | &self.app, |
| 286 | &message.adaptive_cards, |
| 287 | &message.conversation_id, |
| 288 | &message.key, |
| 289 | cx, |
| 290 | ); |
| 291 | RowActions { |
| 292 | bot, |
| 293 | delivery, |
| 294 | reply, |
| 295 | hovered, |
| 296 | menu, |
| 297 | scheduled_menu, |
| 298 | react, |
| 299 | reaction_controls, |
| 300 | files, |
| 301 | open_image, |
| 302 | highlighted: self.highlighted.as_deref() == Some(message.key.as_str()), |
| 303 | saved: self |
| 304 | .app |
| 305 | .read(cx) |
| 306 | .is_saved(&message.conversation_id, &message.key), |
| 307 | pinned: self |
| 308 | .app |
| 309 | .read(cx) |
| 310 | .is_pinned(&message.conversation_id, &message.key), |
| 311 | translation, |
| 312 | timeline: self.timeline.clone(), |
| 313 | agent_run: actionable_run, |
| 314 | call: self.call_actions(message, cx), |
| 315 | reaction_proposal, |
| 316 | undo_delete: self.undo_delete_action(message, is_real, view, cx), |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | fn undo_delete_action( |
| 321 | &self, |
| 322 | message: &MessageRow, |
| 323 | is_real: bool, |
| 324 | view: &WeakEntity<ConversationView>, |
| 325 | cx: &App, |
| 326 | ) -> SharedRowAction { |
| 327 | let restorable = is_real |
| 328 | && self.app.read(cx).restorable_deletes.can_undo( |
| 329 | &message.conversation_id, |
| 330 | &message.key, |
| 331 | message.own, |
| 332 | message.deleted, |
| 333 | ); |
| 334 | restorable.then(|| { |
| 335 | let (view, key) = (view.clone(), message.key.clone()); |
| 336 | Rc::new(move |cx: &mut App| { |
| 337 | view.update(cx, |this, cx| this.undo_delete(&key, cx)).ok(); |
| 338 | }) as Rc<dyn Fn(&mut App)> |
| 339 | }) |
| 340 | } |
| 341 | |
| 342 | fn call_actions(&self, message: &MessageRow, cx: &App) -> CallActions { |
| 343 | let Some(call) = &message.call else { |
| 344 | return CallActions::none(); |
| 345 | }; |
| 346 | let app = self.app.clone(); |
| 347 | let conversation_id = message.conversation_id.clone(); |
| 348 | let call_back = call.missed.then(|| { |
| 349 | let (app, conversation_id) = (app.clone(), conversation_id.clone()); |
| 350 | boxed(move |cx| { |
| 351 | app.update(cx, |state, cx| state.start_chat_call(&conversation_id, cx)); |
| 352 | }) |
| 353 | }); |
| 354 | let joinable = call.started && app.read(cx).running_meeting(&conversation_id).is_some(); |
| 355 | let join = joinable.then(|| { |
| 356 | boxed(move |cx| { |
| 357 | app.update(cx, |state, cx| { |
| 358 | state.join_meeting_call(&conversation_id, cx) |
| 359 | }); |
| 360 | }) |
| 361 | }); |
| 362 | let open = |recording: &RecordingCard| { |
| 363 | let (view, message_id, url) = ( |
| 364 | self.view.clone(), |
| 365 | recording.message_id.clone(), |
| 366 | recording.url.clone(), |
| 367 | ); |
| 368 | boxed(move |cx| { |
| 369 | view.update(cx, |this, cx| { |
| 370 | this.open_recording(&message_id, url.as_deref(), cx) |
| 371 | }) |
| 372 | .ok(); |
| 373 | }) |
| 374 | }; |
| 375 | let recording = call.recap.as_ref().map(|recap| &recap.recording); |
| 376 | let share = recording |
| 377 | .and_then(|recording| recording.url.clone()) |
| 378 | .map(|url| { |
| 379 | boxed(move |cx| cx.write_to_clipboard(ClipboardItem::new_string(url.clone()))) |
| 380 | }); |
| 381 | CallActions { |
| 382 | call_back, |
| 383 | join, |
| 384 | recap: RecapActions { |
| 385 | view_recap: recording.map(open), |
| 386 | open_recording: recording.map(open), |
| 387 | transcript: recording.map(open), |
| 388 | share, |
| 389 | }, |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | fn translation_actions(&self, message: &MessageRow) -> Option<TranslationActions> { |
| 394 | let line = message.translation.as_ref()?; |
| 395 | let (app, conversation_id, key) = ( |
| 396 | self.app.clone(), |
| 397 | message.conversation_id.clone(), |
| 398 | message.key.clone(), |
| 399 | ); |
| 400 | let translate: LineAction = { |
| 401 | let (app, conversation_id, key) = (app.clone(), conversation_id.clone(), key.clone()); |
| 402 | Rc::new(move |cx: &mut App| { |
| 403 | app.update(cx, |state, cx| { |
| 404 | state.translate_message(&conversation_id, &key, cx) |
| 405 | }); |
| 406 | }) |
| 407 | }; |
| 408 | let never = match line { |
| 409 | TranslationLine::Offer { language_code, .. } => { |
| 410 | let language_code = language_code.clone(); |
| 411 | Some(Rc::new(move |cx: &mut App| { |
| 412 | app.update(cx, |state, cx| { |
| 413 | state.never_translate_language(&language_code, cx) |
| 414 | }); |
| 415 | }) as LineAction) |
| 416 | } |
| 417 | _ => None, |
| 418 | }; |
| 419 | Some(TranslationActions { translate, never }) |
| 420 | } |
| 421 | |
| 422 | fn open_by_root( |
| 423 | &self, |
| 424 | root_key: String, |
| 425 | run: fn(&mut ConversationView, String, &mut Window, &mut Context<ConversationView>), |
| 426 | ) -> PostAction { |
| 427 | let view = self.view.clone(); |
| 428 | Box::new(move |window, cx| { |
| 429 | view.update(cx, |this, cx| run(this, root_key.clone(), window, cx)) |
| 430 | .ok(); |
| 431 | }) |
| 432 | } |
| 433 | |
| 434 | pub fn post_actions(&self, post: &PostRow, window: &mut Window, cx: &mut App) -> PostActions { |
| 435 | let root_key = post.root.key.clone(); |
| 436 | let is_local = |
| 437 | root_key.starts_with(PENDING_KEY_PREFIX) || root_key.starts_with(SCHEDULED_KEY_PREFIX); |
| 438 | let open_thread = Some( |
| 439 | self.open_by_root(root_key.clone(), |this, root_key, window, cx| { |
| 440 | this.open_thread(root_key, window, cx) |
| 441 | }), |
| 442 | ); |
| 443 | let open_reply = (!is_local).then(|| { |
| 444 | self.open_by_root(root_key.clone(), |this, root_key, window, cx| { |
| 445 | this.open_reply_editor(root_key, None, window, cx) |
| 446 | }) |
| 447 | }); |
| 448 | let reply_editor = self |
| 449 | .reply_editor |
| 450 | .as_ref() |
| 451 | .filter(|(open_root, _)| *open_root == root_key) |
| 452 | .map(|(_, composer)| self.inline_reply_editor(composer)); |
| 453 | PostActions { |
| 454 | root: self.message_actions(&post.root, window, cx), |
| 455 | replies: post |
| 456 | .replies |
| 457 | .iter() |
| 458 | .map(|reply| self.message_actions(reply, window, cx)) |
| 459 | .collect(), |
| 460 | open_thread, |
| 461 | open_reply, |
| 462 | reply_editor, |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | fn inline_reply_editor(&self, composer: &Entity<Composer>) -> AnyElement { |
| 467 | let view = self.view.clone(); |
| 468 | v_flex() |
| 469 | .w_full() |
| 470 | .gap(px(6.)) |
| 471 | .child(composer.clone()) |
| 472 | .child( |
| 473 | h_flex().justify_end().child( |
| 474 | Button::new("cancel-inline-reply") |
| 475 | .ghost() |
| 476 | .compact() |
| 477 | .label("Cancel") |
| 478 | .on_click(move |_, _, cx| { |
| 479 | view.update(cx, |this, cx| this.close_reply_editor(cx)).ok(); |
| 480 | }), |
| 481 | ), |
| 482 | ) |
| 483 | .into_any_element() |
| 484 | } |
| 485 | } |