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