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