| 1 | use std::cell::RefCell; |
| 2 | use std::rc::Rc; |
| 3 | |
| 4 | use gpui_kit::component::{ |
| 5 | button::{Button, ButtonVariants as _}, |
| 6 | h_flex, |
| 7 | input::{InputEvent, InputState}, |
| 8 | message_scroller::MessageScrollerState, |
| 9 | v_flex, |
| 10 | }; |
| 11 | use gpui_kit::*; |
| 12 | |
| 13 | use super::{ConversationView, ViewMode}; |
| 14 | use crate::notice::short_error; |
| 15 | use crate::rows::Row; |
| 16 | use crate::runtime; |
| 17 | use crate::views::channel_tabs::ChannelPane; |
| 18 | use crate::views::composer::{Composer, ComposerEvent, ReplyPreview}; |
| 19 | use crate::views::post_card::{PostAction, post_in_channel_card}; |
| 20 | |
| 21 | const REPLY_PLACEHOLDER: &str = "Reply in thread"; |
| 22 | |
| 23 | pub(super) struct FeedStash { |
| 24 | scroller: Entity<MessageScrollerState>, |
| 25 | rows: Rc<RefCell<Vec<Row>>>, |
| 26 | near_top: bool, |
| 27 | } |
| 28 | |
| 29 | pub(super) fn subject_input( |
| 30 | window: &mut Window, |
| 31 | cx: &mut Context<ConversationView>, |
| 32 | ) -> Entity<InputState> { |
| 33 | cx.new(|cx| InputState::new(window, cx).placeholder(crate::views::composer::SUBJECT_PLACEHOLDER)) |
| 34 | } |
| 35 | |
| 36 | impl ConversationView { |
| 37 | pub(super) fn in_feed(&self) -> bool { |
| 38 | !self.draft_active |
| 39 | && self |
| 40 | .current |
| 41 | .as_ref() |
| 42 | .is_some_and(|current| current.mode == ViewMode::ThreadList) |
| 43 | } |
| 44 | |
| 45 | pub fn start_channel_post(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 46 | if self.in_feed() { |
| 47 | self.open_new_post(window, cx); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | pub(super) fn open_new_post(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 52 | self.mark_new_post_open(cx); |
| 53 | self.composer |
| 54 | .update(cx, |composer, cx| composer.focus(window, cx)); |
| 55 | } |
| 56 | |
| 57 | pub(super) fn open_new_announcement(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 58 | self.mark_new_post_open(cx); |
| 59 | self.composer |
| 60 | .update(cx, |composer, cx| composer.begin_announcement(window, cx)); |
| 61 | } |
| 62 | |
| 63 | fn mark_new_post_open(&mut self, cx: &mut Context<Self>) { |
| 64 | self.new_post_open = true; |
| 65 | let subject = self.subject.clone(); |
| 66 | self.composer.update(cx, |composer, cx| { |
| 67 | composer.set_post_layout(Some(subject), cx) |
| 68 | }); |
| 69 | cx.notify(); |
| 70 | } |
| 71 | |
| 72 | pub(super) fn restore_new_post( |
| 73 | &mut self, |
| 74 | subject: Option<String>, |
| 75 | window: &mut Window, |
| 76 | cx: &mut Context<Self>, |
| 77 | ) { |
| 78 | if !self.in_feed() { |
| 79 | return; |
| 80 | } |
| 81 | self.restore_subject(subject.as_deref(), window, cx); |
| 82 | self.mark_new_post_open(cx); |
| 83 | self.composer.update(cx, |composer, cx| { |
| 84 | composer.sync_subject_placeholder(window, cx) |
| 85 | }); |
| 86 | } |
| 87 | |
| 88 | pub(super) fn close_new_post(&mut self, cx: &mut Context<Self>) { |
| 89 | self.composer |
| 90 | .update(cx, |composer, cx| composer.set_post_layout(None, cx)); |
| 91 | if self.new_post_open { |
| 92 | self.new_post_open = false; |
| 93 | cx.notify(); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | pub(super) fn take_subject( |
| 98 | &mut self, |
| 99 | window: &mut Window, |
| 100 | cx: &mut Context<Self>, |
| 101 | ) -> Option<String> { |
| 102 | let subject = self.subject.read(cx).value().trim().to_owned(); |
| 103 | self.subject |
| 104 | .update(cx, |input, cx| input.set_value("", window, cx)); |
| 105 | (!subject.is_empty()).then_some(subject) |
| 106 | } |
| 107 | |
| 108 | pub(super) fn restore_subject( |
| 109 | &mut self, |
| 110 | subject: Option<&str>, |
| 111 | window: &mut Window, |
| 112 | cx: &mut Context<Self>, |
| 113 | ) { |
| 114 | let subject = subject.unwrap_or_default().to_owned(); |
| 115 | self.subject |
| 116 | .update(cx, |input, cx| input.set_value(subject, window, cx)); |
| 117 | } |
| 118 | |
| 119 | pub(super) fn has_subject(&self, cx: &App) -> bool { |
| 120 | !self.subject.read(cx).value().trim().is_empty() |
| 121 | } |
| 122 | |
| 123 | pub(super) fn on_subject_event( |
| 124 | &mut self, |
| 125 | _: &Entity<InputState>, |
| 126 | event: &InputEvent, |
| 127 | window: &mut Window, |
| 128 | cx: &mut Context<Self>, |
| 129 | ) { |
| 130 | match event { |
| 131 | InputEvent::PressEnter { .. } => self |
| 132 | .composer |
| 133 | .update(cx, |composer, cx| composer.focus(window, cx)), |
| 134 | InputEvent::Change => self |
| 135 | .composer |
| 136 | .update(cx, |composer, cx| composer.schedule_draft_save(cx)), |
| 137 | _ => {} |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | fn reply_composer(&mut self, window: &mut Window, cx: &mut Context<Self>) -> Entity<Composer> { |
| 142 | if let Some(composer) = &self.reply_composer { |
| 143 | return composer.clone(); |
| 144 | } |
| 145 | let composer = cx.new(|cx| Composer::new(self.app.clone(), window, cx).inline()); |
| 146 | let subscription = cx.subscribe_in(&composer, window, Self::on_reply_event); |
| 147 | self._subscriptions.push(subscription); |
| 148 | self.reply_composer = Some(composer.clone()); |
| 149 | self.sync_reply_composer(window, cx); |
| 150 | composer |
| 151 | } |
| 152 | |
| 153 | fn keep_reply_editor_in_view(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 154 | let Some(root_id) = self.reply_root.clone() else { |
| 155 | return; |
| 156 | }; |
| 157 | if !self.in_feed() { |
| 158 | return; |
| 159 | } |
| 160 | let view = cx.entity().downgrade(); |
| 161 | window.on_next_frame(move |_, cx| { |
| 162 | view.update(cx, |this, cx| { |
| 163 | let index = this.rows.borrow().iter().position(|row| row.key() == root_id); |
| 164 | if let Some(index) = index { |
| 165 | this.scroller |
| 166 | .update(cx, |scroller, cx| scroller.reveal_item_bottom(index, cx)); |
| 167 | } |
| 168 | }) |
| 169 | .ok(); |
| 170 | }); |
| 171 | } |
| 172 | |
| 173 | pub(super) fn sync_reply_composer(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 174 | self.reply_root = None; |
| 175 | let (Some(composer), Some(conversation_id)) = |
| 176 | (self.reply_composer.clone(), self.conversation_id()) |
| 177 | else { |
| 178 | return; |
| 179 | }; |
| 180 | composer.update(cx, |composer, cx| { |
| 181 | composer.set_conversation(&conversation_id, window, cx); |
| 182 | composer.set_placeholder(REPLY_PLACEHOLDER, window, cx); |
| 183 | composer.set_text("", window, cx); |
| 184 | composer.set_reply(None, cx); |
| 185 | }); |
| 186 | } |
| 187 | |
| 188 | pub(super) fn open_reply_editor( |
| 189 | &mut self, |
| 190 | root_id: String, |
| 191 | quote: Option<ReplyPreview>, |
| 192 | window: &mut Window, |
| 193 | cx: &mut Context<Self>, |
| 194 | ) { |
| 195 | let composer = self.reply_composer(window, cx); |
| 196 | let previous = self.reply_root.replace(root_id.clone()); |
| 197 | let changed = previous.as_deref() != Some(root_id.as_str()); |
| 198 | composer.update(cx, |composer, cx| { |
| 199 | if changed { |
| 200 | composer.set_text("", window, cx); |
| 201 | } |
| 202 | composer.set_reply(quote, cx); |
| 203 | composer.focus(window, cx); |
| 204 | }); |
| 205 | self.remeasure_posts(&[Some(root_id), previous], cx); |
| 206 | } |
| 207 | |
| 208 | pub(super) fn close_reply_editor(&mut self, cx: &mut Context<Self>) { |
| 209 | if let Some(root_id) = self.reply_root.take() { |
| 210 | self.remeasure_posts(&[Some(root_id)], cx); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | fn remeasure_posts(&mut self, root_ids: &[Option<String>], cx: &mut Context<Self>) { |
| 215 | let indices: Vec<usize> = self |
| 216 | .rows |
| 217 | .borrow() |
| 218 | .iter() |
| 219 | .enumerate() |
| 220 | .filter(|(_, row)| { |
| 221 | matches!(row, Row::Post(post) if root_ids.iter().flatten().any(|id| *id == post.root.key)) |
| 222 | }) |
| 223 | .map(|(index, _)| index) |
| 224 | .collect(); |
| 225 | self.scroller.update(cx, |scroller, cx| { |
| 226 | for index in indices { |
| 227 | scroller.remeasure_items(index..index + 1, cx); |
| 228 | } |
| 229 | }); |
| 230 | cx.notify(); |
| 231 | } |
| 232 | |
| 233 | fn on_reply_event( |
| 234 | &mut self, |
| 235 | _: &Entity<Composer>, |
| 236 | event: &ComposerEvent, |
| 237 | window: &mut Window, |
| 238 | cx: &mut Context<Self>, |
| 239 | ) { |
| 240 | self.report_composer_activity(event, cx); |
| 241 | match event { |
| 242 | ComposerEvent::Submit(outgoing) => { |
| 243 | let Some(root_id) = self.reply_root.clone() else { |
| 244 | return; |
| 245 | }; |
| 246 | self.close_reply_editor(cx); |
| 247 | self.send_in_thread(root_id, (**outgoing).clone(), window, cx); |
| 248 | } |
| 249 | ComposerEvent::Typing(active) => { |
| 250 | let root_id = self.reply_root.clone(); |
| 251 | self.send_typing_in(root_id, *active, cx); |
| 252 | } |
| 253 | ComposerEvent::Edited => self.keep_reply_editor_in_view(window, cx), |
| 254 | ComposerEvent::Schedule { .. } |
| 255 | | ComposerEvent::EditLast |
| 256 | | ComposerEvent::FocusMessages |
| 257 | | ComposerEvent::ClosePost |
| 258 | | ComposerEvent::RestorePost(_) => {} |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | pub(super) fn stop_reply_typing(&mut self, cx: &mut Context<Self>) { |
| 263 | let Some(composer) = self.reply_composer.clone() else { |
| 264 | return; |
| 265 | }; |
| 266 | if composer.update(cx, |composer, _| composer.stop_typing()) { |
| 267 | let root_id = self.reply_root.clone(); |
| 268 | self.send_typing_in(root_id, false, cx); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | pub(super) fn show_main_composer(&mut self, cx: &mut Context<Self>) { |
| 273 | if self.in_feed() { |
| 274 | self.mark_new_post_open(cx); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | pub(super) fn render_new_post(&self, cx: &mut Context<Self>) -> Option<AnyElement> { |
| 279 | if !self.in_feed() || self.pane != ChannelPane::Posts { |
| 280 | return None; |
| 281 | } |
| 282 | let proposal_card = self.render_proposal_card(cx); |
| 283 | if !self.new_post_open { |
| 284 | let view = cx.entity().downgrade(); |
| 285 | let announce_view = view.clone(); |
| 286 | let open: PostAction = Box::new(move |window, cx| { |
| 287 | view.update(cx, |this, cx| this.open_new_post(window, cx)).ok(); |
| 288 | }); |
| 289 | let announce: PostAction = Box::new(move |window, cx| { |
| 290 | announce_view |
| 291 | .update(cx, |this, cx| this.open_new_announcement(window, cx)) |
| 292 | .ok(); |
| 293 | }); |
| 294 | return Some( |
| 295 | v_flex() |
| 296 | .w_full() |
| 297 | .flex_none() |
| 298 | .children(proposal_card) |
| 299 | .child( |
| 300 | div() |
| 301 | .w_full() |
| 302 | .flex_none() |
| 303 | .px(px(20.)) |
| 304 | .py(px(12.)) |
| 305 | .child(post_in_channel_card(&self.app.read(cx).directory, open, announce)), |
| 306 | ) |
| 307 | .into_any_element(), |
| 308 | ); |
| 309 | } |
| 310 | let editing = self.composer.read(cx).is_editing(); |
| 311 | let cancel = editing.then(|| { |
| 312 | h_flex() |
| 313 | .w_full() |
| 314 | .px(px(20.)) |
| 315 | .pb(px(12.)) |
| 316 | .justify_end() |
| 317 | .child( |
| 318 | Button::new("cancel-new-post") |
| 319 | .ghost() |
| 320 | .compact() |
| 321 | .label("Cancel") |
| 322 | .on_click(cx.listener(|this, _, window, cx| { |
| 323 | this.cancel_new_post(window, cx) |
| 324 | })), |
| 325 | ) |
| 326 | }); |
| 327 | Some( |
| 328 | v_flex() |
| 329 | .w_full() |
| 330 | .flex_none() |
| 331 | .children(proposal_card) |
| 332 | .child(div().w_full().pt(px(12.)).child(self.composer.clone())) |
| 333 | .children(cancel) |
| 334 | .into_any_element(), |
| 335 | ) |
| 336 | } |
| 337 | |
| 338 | pub(super) fn cancel_new_post(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 339 | let editing = self.composer.read(cx).is_editing(); |
| 340 | if editing { |
| 341 | self.composer |
| 342 | .update(cx, |composer, cx| composer.cancel_edit(window, cx)); |
| 343 | } |
| 344 | self.close_new_post(cx); |
| 345 | } |
| 346 | |
| 347 | pub(super) fn on_escape(&mut self, window: &mut Window, cx: &mut Context<Self>) -> bool { |
| 348 | if self.close_new_folder(cx) { |
| 349 | return true; |
| 350 | } |
| 351 | if self.reply_root.is_some() { |
| 352 | self.close_reply_editor(cx); |
| 353 | return true; |
| 354 | } |
| 355 | if self.new_post_open && self.in_feed() { |
| 356 | self.cancel_new_post(window, cx); |
| 357 | return true; |
| 358 | } |
| 359 | if matches!( |
| 360 | self.current.as_ref().map(|current| ¤t.mode), |
| 361 | Some(ViewMode::Thread(_)) |
| 362 | ) && !self.draft_active |
| 363 | { |
| 364 | self.back_to_threads(cx); |
| 365 | return true; |
| 366 | } |
| 367 | false |
| 368 | } |
| 369 | |
| 370 | pub(super) fn enter_thread_mode(&mut self, root_id: String, cx: &mut Context<Self>) { |
| 371 | let Some(current) = self.current.as_mut() else { |
| 372 | return; |
| 373 | }; |
| 374 | if current.mode == ViewMode::ThreadList { |
| 375 | let stash = FeedStash { |
| 376 | scroller: self.scroller.clone(), |
| 377 | rows: self.rows.clone(), |
| 378 | near_top: self.feed_at_top, |
| 379 | }; |
| 380 | self.feed_stash = Some(stash); |
| 381 | self.scroller = cx.new(|cx| MessageScrollerState::new(0, cx)); |
| 382 | self.rows = Rc::new(RefCell::new(Vec::new())); |
| 383 | } |
| 384 | current.mode = ViewMode::Thread(root_id); |
| 385 | self.reply_root = None; |
| 386 | } |
| 387 | |
| 388 | pub(super) fn enter_feed_mode(&mut self) -> bool { |
| 389 | let Some(current) = self.current.as_mut() else { |
| 390 | return false; |
| 391 | }; |
| 392 | current.mode = ViewMode::ThreadList; |
| 393 | match self.feed_stash.take() { |
| 394 | Some(stash) => { |
| 395 | self.scroller = stash.scroller; |
| 396 | self.rows = stash.rows; |
| 397 | self.feed_at_top = stash.near_top; |
| 398 | true |
| 399 | } |
| 400 | None => false, |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | pub(super) fn open_thread( |
| 405 | &mut self, |
| 406 | root_id: String, |
| 407 | window: &mut Window, |
| 408 | cx: &mut Context<Self>, |
| 409 | ) { |
| 410 | self.stop_typing(cx); |
| 411 | self.stop_reply_typing(cx); |
| 412 | self.enter_thread_mode(root_id.clone(), cx); |
| 413 | self.reload_pending(cx); |
| 414 | self.rebuild(true, cx); |
| 415 | self.refresh_thread(root_id, cx); |
| 416 | self.composer |
| 417 | .update(cx, |composer, cx| composer.focus(window, cx)); |
| 418 | } |
| 419 | |
| 420 | pub(super) fn back_to_threads(&mut self, cx: &mut Context<Self>) { |
| 421 | self.stop_typing(cx); |
| 422 | let restored = self.enter_feed_mode(); |
| 423 | self.reload_pending(cx); |
| 424 | self.rebuild(!restored, cx); |
| 425 | } |
| 426 | |
| 427 | pub(super) fn refresh_thread(&mut self, root_id: String, cx: &mut Context<Self>) { |
| 428 | let state = self.app.read(cx); |
| 429 | if state.mode.demo || state.mode.read_only { |
| 430 | return; |
| 431 | } |
| 432 | let (Some(engine), Some(conversation_id)) = (state.engine.clone(), self.conversation_id()) |
| 433 | else { |
| 434 | return; |
| 435 | }; |
| 436 | let receiver = { |
| 437 | let (conversation_id, root_id) = (conversation_id.clone(), root_id.clone()); |
| 438 | runtime::spawn(async move { engine.refresh_thread(&conversation_id, &root_id).await }) |
| 439 | }; |
| 440 | cx.spawn(async move |this, cx| { |
| 441 | let result = receiver.await; |
| 442 | this.update(cx, |this, cx| { |
| 443 | this.finish_thread_refresh(&conversation_id, &root_id, result, cx) |
| 444 | }) |
| 445 | .ok(); |
| 446 | }) |
| 447 | .detach(); |
| 448 | } |
| 449 | |
| 450 | fn finish_thread_refresh( |
| 451 | &mut self, |
| 452 | conversation_id: &str, |
| 453 | root_id: &str, |
| 454 | result: Result< |
| 455 | teams_core::Result<teams_core::Delta>, |
| 456 | tokio::sync::oneshot::error::RecvError, |
| 457 | >, |
| 458 | cx: &mut Context<Self>, |
| 459 | ) { |
| 460 | let still_open = self.current.as_ref().is_some_and(|current| { |
| 461 | current.selection.conversation_id() == conversation_id |
| 462 | && current.mode == ViewMode::Thread(root_id.to_owned()) |
| 463 | }); |
| 464 | if !still_open { |
| 465 | return; |
| 466 | } |
| 467 | let failure = match result { |
| 468 | Ok(Ok(_)) => None, |
| 469 | Ok(Err(error)) => Some(short_error(&error)), |
| 470 | Err(error) => Some(short_error(&error)), |
| 471 | }; |
| 472 | if let Some(failure) = failure { |
| 473 | self.notice = Some(format!("Could not load all replies: {failure}")); |
| 474 | } |
| 475 | self.rebuild(false, cx); |
| 476 | } |
| 477 | } |