| 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::views::call_notice::RecapActions; |
| 19 | use crate::scheduled_rows::SCHEDULED_KEY_PREFIX; |
| 20 | use crate::translation::TranslationLine; |
| 21 | use crate::views::adaptive_card::{ensure_cards_inputs, request_card_refreshes}; |
| 22 | use crate::views::attachments::ImageActions; |
| 23 | use crate::views::call_card::CallActions; |
| 24 | use crate::views::composer::Composer; |
| 25 | use crate::views::message_row::{DeliveryActions, RowActions}; |
| 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| this.accept_reaction(&accept_key, &accept_id, &emoji, cx)) |
| 107 | .ok(); |
| 108 | }), |
| 109 | discard: Rc::new(move |cx| { |
| 110 | discard_view |
| 111 | .update(cx, |this, cx| this.discard_reaction(&discard_key, &discard_id, cx)) |
| 112 | .ok(); |
| 113 | }), |
| 114 | }) |
| 115 | } |
| 116 | |
| 117 | fn bot_identity( |
| 118 | &self, |
| 119 | message: &MessageRow, |
| 120 | cx: &mut App, |
| 121 | ) -> Option<crate::card_state::BotIdentity> { |
| 122 | let application_id = message.application_id.as_deref()?; |
| 123 | let known = self |
| 124 | .app |
| 125 | .read(cx) |
| 126 | .bot_identity(&message.conversation_id, application_id); |
| 127 | if known.is_none() { |
| 128 | let (app, conversation_id) = (self.app.clone(), message.conversation_id.clone()); |
| 129 | cx.defer(move |cx| { |
| 130 | app.update(cx, |state, cx| { |
| 131 | state.request_chat_apps(&conversation_id, cx) |
| 132 | }); |
| 133 | }); |
| 134 | } |
| 135 | known |
| 136 | } |
| 137 | |
| 138 | pub fn message_actions( |
| 139 | &self, |
| 140 | message: &MessageRow, |
| 141 | window: &mut Window, |
| 142 | cx: &mut App, |
| 143 | ) -> RowActions { |
| 144 | let view = &self.view; |
| 145 | let failed = matches!(message.delivery, Delivery::Failed(_)); |
| 146 | let retry = failed.then(|| { |
| 147 | let (view, key, handle) = (view.clone(), message.key.clone(), window.window_handle()); |
| 148 | boxed(move |cx| { |
| 149 | let (view, key) = (view.clone(), key.clone()); |
| 150 | handle |
| 151 | .update(cx, move |_, window, cx| { |
| 152 | view.update(cx, |this, cx| this.retry(&key, window, cx)) |
| 153 | .ok(); |
| 154 | }) |
| 155 | .ok(); |
| 156 | }) |
| 157 | }); |
| 158 | let scheduled = view.upgrade().and_then(|entity| { |
| 159 | entity |
| 160 | .read(cx) |
| 161 | .scheduled_row_actions(message, view, window, cx) |
| 162 | }); |
| 163 | let delete = failed.then(|| { |
| 164 | let (view, key) = (view.clone(), message.key.clone()); |
| 165 | boxed(move |cx| { |
| 166 | view.update(cx, |this, cx| this.delete_pending(&key, cx)) |
| 167 | .ok(); |
| 168 | }) |
| 169 | }); |
| 170 | self.request_missing_media(message, cx); |
| 171 | let is_real = !self.drafting |
| 172 | && !message.key.starts_with(PENDING_KEY_PREFIX) |
| 173 | && scheduled.is_none() |
| 174 | && message.call.is_none(); |
| 175 | let hovered = is_real.then(|| { |
| 176 | let (view, key) = (view.clone(), message.key.clone()); |
| 177 | Rc::new(move |hovered: bool, cx: &mut App| { |
| 178 | view.update(cx, |this, cx| { |
| 179 | this.set_hover(HoverSlot::Row, &key, hovered, cx) |
| 180 | }) |
| 181 | .ok(); |
| 182 | }) as Rc<dyn Fn(bool, &mut App)> |
| 183 | }); |
| 184 | let (delivery, scheduled_menu, hovered) = match scheduled { |
| 185 | Some(actions) => (actions.delivery, actions.menu, Some(actions.hovered)), |
| 186 | None => ( |
| 187 | DeliveryActions { |
| 188 | retry, |
| 189 | delete, |
| 190 | send_now: None, |
| 191 | }, |
| 192 | None, |
| 193 | hovered, |
| 194 | ), |
| 195 | }; |
| 196 | let actionable = is_real && !message.deleted; |
| 197 | let (menu, react, reaction_controls) = view |
| 198 | .upgrade() |
| 199 | .filter(|_| actionable) |
| 200 | .map(|entity| { |
| 201 | let this = entity.read(cx); |
| 202 | let mine = message |
| 203 | .reactions |
| 204 | .iter() |
| 205 | .filter(|chip| chip.mine) |
| 206 | .map(|chip| chip.glyph()) |
| 207 | .collect(); |
| 208 | let refresh = |
| 209 | message |
| 210 | .adaptive_cards |
| 211 | .iter() |
| 212 | .enumerate() |
| 213 | .find_map(|(index, card)| { |
| 214 | card.refresh |
| 215 | .as_ref() |
| 216 | .map(|refresh| (index, refresh.action.clone())) |
| 217 | }); |
| 218 | let menu = this.message_menu(message, mine, refresh, view.clone(), cx); |
| 219 | let react = menu.react.clone(); |
| 220 | let visible = this.toolbar_visible(&message.key); |
| 221 | let controls = (!message.reactions.is_empty()) |
| 222 | .then(|| this.reaction_controls(&message.key, view.clone())); |
| 223 | (visible.then_some(menu), Some(react), controls) |
| 224 | }) |
| 225 | .unwrap_or((None, None, None)); |
| 226 | let reply = (is_real && !message.deleted).then(|| { |
| 227 | let (view, key, handle) = (view.clone(), message.key.clone(), window.window_handle()); |
| 228 | boxed(move |cx| { |
| 229 | let (view, key) = (view.clone(), key.clone()); |
| 230 | handle |
| 231 | .update(cx, move |_, window, cx| { |
| 232 | view.update(cx, |this, cx| this.begin_reply(&key, window, cx)) |
| 233 | .ok(); |
| 234 | }) |
| 235 | .ok(); |
| 236 | }) |
| 237 | }); |
| 238 | let files = (is_real && !message.deleted && !message.files.is_empty()) |
| 239 | .then(|| view.upgrade()) |
| 240 | .flatten() |
| 241 | .map(|entity| { |
| 242 | entity |
| 243 | .read(cx) |
| 244 | .file_actions(&message.key, &message.files, view.clone()) |
| 245 | }); |
| 246 | let open_image = (is_real && !message.deleted && !message.images.is_empty()).then(|| { |
| 247 | let (open_view, open_key) = (view.clone(), message.key.clone()); |
| 248 | let (share_view, share_key) = (view.clone(), message.key.clone()); |
| 249 | ImageActions { |
| 250 | open: Rc::new(move |url: &str, cx: &mut App| { |
| 251 | open_view |
| 252 | .update(cx, |this, cx| this.open_image_viewer(&open_key, url, cx)) |
| 253 | .ok(); |
| 254 | }), |
| 255 | share: Some(Rc::new(move |cx: &mut App| { |
| 256 | share_view |
| 257 | .update(cx, |this, cx| this.forward_message(&share_key, cx)) |
| 258 | .ok(); |
| 259 | })), |
| 260 | } |
| 261 | }); |
| 262 | let bot = self.bot_identity(message, cx); |
| 263 | let actionable_run = (is_real && !message.deleted) |
| 264 | .then(|| self.agent_runs.borrow().get(&message.key).cloned()) |
| 265 | .flatten(); |
| 266 | let reaction_proposal = (is_real && !message.deleted) |
| 267 | .then(|| self.reaction_pill(message)) |
| 268 | .flatten(); |
| 269 | let translation = (is_real && !message.deleted) |
| 270 | .then(|| self.translation_actions(message)) |
| 271 | .flatten(); |
| 272 | ensure_cards_inputs( |
| 273 | &self.app, |
| 274 | &message.adaptive_cards, |
| 275 | &message.conversation_id, |
| 276 | &message.key, |
| 277 | window, |
| 278 | cx, |
| 279 | ); |
| 280 | request_card_refreshes( |
| 281 | &self.app, |
| 282 | &message.adaptive_cards, |
| 283 | &message.conversation_id, |
| 284 | &message.key, |
| 285 | cx, |
| 286 | ); |
| 287 | RowActions { |
| 288 | bot, |
| 289 | delivery, |
| 290 | reply, |
| 291 | hovered, |
| 292 | menu, |
| 293 | scheduled_menu, |
| 294 | react, |
| 295 | reaction_controls, |
| 296 | files, |
| 297 | open_image, |
| 298 | highlighted: self.highlighted.as_deref() == Some(message.key.as_str()), |
| 299 | saved: self |
| 300 | .app |
| 301 | .read(cx) |
| 302 | .is_saved(&message.conversation_id, &message.key), |
| 303 | translation, |
| 304 | timeline: self.timeline.clone(), |
| 305 | agent_run: actionable_run, |
| 306 | call: self.call_actions(message, cx), |
| 307 | reaction_proposal, |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | fn call_actions(&self, message: &MessageRow, cx: &App) -> CallActions { |
| 312 | let Some(call) = &message.call else { |
| 313 | return CallActions::none(); |
| 314 | }; |
| 315 | let app = self.app.clone(); |
| 316 | let conversation_id = message.conversation_id.clone(); |
| 317 | let call_back = call.missed.then(|| { |
| 318 | let (app, conversation_id) = (app.clone(), conversation_id.clone()); |
| 319 | boxed(move |cx| { |
| 320 | app.update(cx, |state, cx| state.start_chat_call(&conversation_id, cx)); |
| 321 | }) |
| 322 | }); |
| 323 | let joinable = call.started && app.read(cx).running_meeting(&conversation_id).is_some(); |
| 324 | let join = joinable.then(|| { |
| 325 | boxed(move |cx| { |
| 326 | app.update(cx, |state, cx| { |
| 327 | state.join_meeting_call(&conversation_id, cx) |
| 328 | }); |
| 329 | }) |
| 330 | }); |
| 331 | let open = |recording: &RecordingCard| { |
| 332 | let (view, message_id, url) = ( |
| 333 | self.view.clone(), |
| 334 | recording.message_id.clone(), |
| 335 | recording.url.clone(), |
| 336 | ); |
| 337 | boxed(move |cx| { |
| 338 | view.update(cx, |this, cx| { |
| 339 | this.open_recording(&message_id, url.as_deref(), cx) |
| 340 | }) |
| 341 | .ok(); |
| 342 | }) |
| 343 | }; |
| 344 | let recording = call.recap.as_ref().map(|recap| &recap.recording); |
| 345 | let share = recording.and_then(|recording| recording.url.clone()).map(|url| { |
| 346 | boxed(move |cx| cx.write_to_clipboard(ClipboardItem::new_string(url.clone()))) |
| 347 | }); |
| 348 | CallActions { |
| 349 | call_back, |
| 350 | join, |
| 351 | recap: RecapActions { |
| 352 | view_recap: recording.map(open), |
| 353 | open_recording: recording.map(open), |
| 354 | transcript: recording.map(open), |
| 355 | share, |
| 356 | }, |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | fn translation_actions(&self, message: &MessageRow) -> Option<TranslationActions> { |
| 361 | let line = message.translation.as_ref()?; |
| 362 | let (app, conversation_id, key) = ( |
| 363 | self.app.clone(), |
| 364 | message.conversation_id.clone(), |
| 365 | message.key.clone(), |
| 366 | ); |
| 367 | let translate: LineAction = { |
| 368 | let (app, conversation_id, key) = (app.clone(), conversation_id.clone(), key.clone()); |
| 369 | Rc::new(move |cx: &mut App| { |
| 370 | app.update(cx, |state, cx| { |
| 371 | state.translate_message(&conversation_id, &key, cx) |
| 372 | }); |
| 373 | }) |
| 374 | }; |
| 375 | let never = match line { |
| 376 | TranslationLine::Offer { language_code, .. } => { |
| 377 | let language_code = language_code.clone(); |
| 378 | Some(Rc::new(move |cx: &mut App| { |
| 379 | app.update(cx, |state, cx| { |
| 380 | state.never_translate_language(&language_code, cx) |
| 381 | }); |
| 382 | }) as LineAction) |
| 383 | } |
| 384 | _ => None, |
| 385 | }; |
| 386 | Some(TranslationActions { translate, never }) |
| 387 | } |
| 388 | |
| 389 | fn open_by_root( |
| 390 | &self, |
| 391 | root_key: String, |
| 392 | run: fn(&mut ConversationView, String, &mut Window, &mut Context<ConversationView>), |
| 393 | ) -> PostAction { |
| 394 | let view = self.view.clone(); |
| 395 | Box::new(move |window, cx| { |
| 396 | view.update(cx, |this, cx| run(this, root_key.clone(), window, cx)) |
| 397 | .ok(); |
| 398 | }) |
| 399 | } |
| 400 | |
| 401 | pub fn post_actions(&self, post: &PostRow, window: &mut Window, cx: &mut App) -> PostActions { |
| 402 | let root_key = post.root.key.clone(); |
| 403 | let is_local = |
| 404 | root_key.starts_with(PENDING_KEY_PREFIX) || root_key.starts_with(SCHEDULED_KEY_PREFIX); |
| 405 | let open_thread = Some( |
| 406 | self.open_by_root(root_key.clone(), |this, root_key, window, cx| { |
| 407 | this.open_thread(root_key, window, cx) |
| 408 | }), |
| 409 | ); |
| 410 | let open_reply = (!is_local).then(|| { |
| 411 | self.open_by_root(root_key.clone(), |this, root_key, window, cx| { |
| 412 | this.open_reply_editor(root_key, None, window, cx) |
| 413 | }) |
| 414 | }); |
| 415 | let reply_editor = self |
| 416 | .reply_editor |
| 417 | .as_ref() |
| 418 | .filter(|(open_root, _)| *open_root == root_key) |
| 419 | .map(|(_, composer)| self.inline_reply_editor(composer)); |
| 420 | PostActions { |
| 421 | root: self.message_actions(&post.root, window, cx), |
| 422 | replies: post |
| 423 | .replies |
| 424 | .iter() |
| 425 | .map(|reply| self.message_actions(reply, window, cx)) |
| 426 | .collect(), |
| 427 | open_thread, |
| 428 | open_reply, |
| 429 | reply_editor, |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | fn inline_reply_editor(&self, composer: &Entity<Composer>) -> AnyElement { |
| 434 | let view = self.view.clone(); |
| 435 | v_flex() |
| 436 | .w_full() |
| 437 | .gap(px(6.)) |
| 438 | .child(composer.clone()) |
| 439 | .child( |
| 440 | h_flex().justify_end().child( |
| 441 | Button::new("cancel-inline-reply") |
| 442 | .ghost() |
| 443 | .compact() |
| 444 | .label("Cancel") |
| 445 | .on_click(move |_, _, cx| { |
| 446 | view.update(cx, |this, cx| this.close_reply_editor(cx)).ok(); |
| 447 | }), |
| 448 | ), |
| 449 | ) |
| 450 | .into_any_element() |
| 451 | } |
| 452 | } |