BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / conversation / feed.rs

feed.rs

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