BlueGit

ict rusty-teams

Files

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

feed.rs

feed.rs 15.7 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 composer.update(cx, |composer, cx| {
207 if changed {
208 composer.set_text("", window, cx);
209 }
210 composer.set_reply(quote, cx);
211 composer.focus(window, cx);
212 });
213 self.remeasure_posts(&[Some(root_id), previous], cx);
214 }
215
216 pub(super) fn close_reply_editor(&mut self, cx: &mut Context<Self>) {
217 if let Some(root_id) = self.reply_root.take() {
218 self.remeasure_posts(&[Some(root_id)], cx);
219 }
220 }
221
222 fn remeasure_posts(&mut self, root_ids: &[Option<String>], cx: &mut Context<Self>) {
223 let indices: Vec<usize> = self
224 .rows
225 .borrow()
226 .iter()
227 .enumerate()
228 .filter(|(_, row)| {
229 matches!(row, Row::Post(post) if root_ids.iter().flatten().any(|id| *id == post.root.key))
230 })
231 .map(|(index, _)| index)
232 .collect();
233 self.scroller.update(cx, |scroller, cx| {
234 for index in indices {
235 scroller.remeasure_items(index..index + 1, cx);
236 }
237 });
238 cx.notify();
239 }
240
241 fn on_reply_event(
242 &mut self,
243 _: &Entity<Composer>,
244 event: &ComposerEvent,
245 window: &mut Window,
246 cx: &mut Context<Self>,
247 ) {
248 self.report_composer_activity(event, cx);
249 match event {
250 ComposerEvent::Submit(outgoing) => {
251 let Some(root_id) = self.reply_root.clone() else {
252 return;
253 };
254 self.close_reply_editor(cx);
255 self.send_in_thread(root_id, (**outgoing).clone(), window, cx);
256 }
257 ComposerEvent::Typing(active) => {
258 let root_id = self.reply_root.clone();
259 self.send_typing_in(root_id, *active, cx);
260 }
261 ComposerEvent::Edited => self.keep_reply_editor_in_view(window, cx),
262 ComposerEvent::Schedule { .. }
263 | ComposerEvent::EditLast
264 | ComposerEvent::FocusMessages
265 | ComposerEvent::ClosePost
266 | ComposerEvent::RestorePost(_) => {}
267 }
268 }
269
270 pub(super) fn stop_reply_typing(&mut self, cx: &mut Context<Self>) {
271 let Some(composer) = self.reply_composer.clone() else {
272 return;
273 };
274 if composer.update(cx, |composer, _| composer.stop_typing()) {
275 let root_id = self.reply_root.clone();
276 self.send_typing_in(root_id, false, cx);
277 }
278 }
279
280 pub(super) fn show_main_composer(&mut self, cx: &mut Context<Self>) {
281 if self.in_feed() {
282 self.mark_new_post_open(cx);
283 }
284 }
285
286 pub(super) fn render_new_post(&self, cx: &mut Context<Self>) -> Option<AnyElement> {
287 if !self.in_feed() || self.pane != ChannelPane::Posts {
288 return None;
289 }
290 let proposal_card = self
291 .render_proposal_card(cx)
292 .map(|card| div().w_full().pt(px(FEED_CARD_TOP)).child(card));
293 if !self.new_post_open {
294 let view = cx.entity().downgrade();
295 let announce_view = view.clone();
296 let open: PostAction = Box::new(move |window, cx| {
297 view.update(cx, |this, cx| this.open_new_post(window, cx))
298 .ok();
299 });
300 let announce: PostAction = Box::new(move |window, cx| {
301 announce_view
302 .update(cx, |this, cx| this.open_new_announcement(window, cx))
303 .ok();
304 });
305 return Some(
306 v_flex()
307 .w_full()
308 .flex_none()
309 .children(proposal_card)
310 .child(div().w_full().flex_none().px(px(20.)).py(px(12.)).child(
311 post_in_channel_card(&self.app.read(cx).directory, open, announce),
312 ))
313 .into_any_element(),
314 );
315 }
316 let editing = self.composer.read(cx).is_editing();
317 let cancel = editing.then(|| {
318 h_flex()
319 .w_full()
320 .px(px(20.))
321 .pb(px(12.))
322 .justify_end()
323 .child(
324 Button::new("cancel-new-post")
325 .ghost()
326 .compact()
327 .label(tr("common.cancel"))
328 .on_click(
329 cx.listener(|this, _, window, cx| this.cancel_new_post(window, cx)),
330 ),
331 )
332 });
333 Some(
334 v_flex()
335 .w_full()
336 .flex_none()
337 .children(proposal_card)
338 .child(div().w_full().pt(px(12.)).child(self.composer.clone()))
339 .children(cancel)
340 .into_any_element(),
341 )
342 }
343
344 pub(super) fn cancel_new_post(&mut self, window: &mut Window, cx: &mut Context<Self>) {
345 let editing = self.composer.read(cx).is_editing();
346 if editing {
347 self.composer
348 .update(cx, |composer, cx| composer.cancel_edit(window, cx));
349 }
350 self.close_new_post(cx);
351 }
352
353 pub(super) fn on_escape(&mut self, window: &mut Window, cx: &mut Context<Self>) -> bool {
354 if self.close_new_folder(cx) {
355 return true;
356 }
357 if self.reply_root.is_some() {
358 self.close_reply_editor(cx);
359 return true;
360 }
361 if self.new_post_open && self.in_feed() {
362 self.cancel_new_post(window, cx);
363 return true;
364 }
365 if matches!(
366 self.current.as_ref().map(|current| &current.mode),
367 Some(ViewMode::Thread(_))
368 ) && !self.draft_active
369 {
370 self.back_to_threads(cx);
371 return true;
372 }
373 false
374 }
375
376 pub(super) fn enter_thread_mode(&mut self, root_id: String, cx: &mut Context<Self>) {
377 let Some(current) = self.current.as_mut() else {
378 return;
379 };
380 if current.mode == ViewMode::ThreadList {
381 let stash = FeedStash {
382 scroller: self.scroller.clone(),
383 rows: self.rows.clone(),
384 near_top: self.feed_at_top,
385 };
386 self.feed_stash = Some(stash);
387 self.scroller = cx.new(|cx| MessageScrollerState::new(0, cx));
388 self.rows = Rc::new(RefCell::new(Vec::new()));
389 }
390 current.mode = ViewMode::Thread(root_id);
391 self.reply_root = None;
392 }
393
394 pub(super) fn enter_feed_mode(&mut self) -> bool {
395 let Some(current) = self.current.as_mut() else {
396 return false;
397 };
398 current.mode = ViewMode::ThreadList;
399 match self.feed_stash.take() {
400 Some(stash) => {
401 self.scroller = stash.scroller;
402 self.rows = stash.rows;
403 self.feed_at_top = stash.near_top;
404 true
405 }
406 None => false,
407 }
408 }
409
410 pub(super) fn open_thread(
411 &mut self,
412 root_id: String,
413 window: &mut Window,
414 cx: &mut Context<Self>,
415 ) {
416 self.stop_typing(cx);
417 self.stop_reply_typing(cx);
418 self.enter_thread_mode(root_id.clone(), cx);
419 self.reload_pending(cx);
420 self.rebuild(true, cx);
421 self.refresh_thread(root_id, cx);
422 self.composer
423 .update(cx, |composer, cx| composer.focus(window, cx));
424 }
425
426 pub(super) fn back_to_threads(&mut self, cx: &mut Context<Self>) {
427 self.stop_typing(cx);
428 let restored = self.enter_feed_mode();
429 self.reload_pending(cx);
430 self.rebuild(!restored, cx);
431 }
432
433 pub(super) fn refresh_thread(&mut self, root_id: String, cx: &mut Context<Self>) {
434 let state = self.app.read(cx);
435 if state.mode.demo || state.mode.read_only {
436 return;
437 }
438 let (Some(engine), Some(conversation_id)) = (state.engine.clone(), self.conversation_id())
439 else {
440 return;
441 };
442 let receiver = {
443 let (conversation_id, root_id) = (conversation_id.clone(), root_id.clone());
444 runtime::spawn(async move { engine.refresh_thread(&conversation_id, &root_id).await })
445 };
446 cx.spawn(async move |this, cx| {
447 let result = receiver.await;
448 this.update(cx, |this, cx| {
449 this.finish_thread_refresh(&conversation_id, &root_id, result, cx)
450 })
451 .ok();
452 })
453 .detach();
454 }
455
456 fn finish_thread_refresh(
457 &mut self,
458 conversation_id: &str,
459 root_id: &str,
460 result: Result<
461 teams_core::Result<teams_core::Delta>,
462 tokio::sync::oneshot::error::RecvError,
463 >,
464 cx: &mut Context<Self>,
465 ) {
466 let still_open = self.current.as_ref().is_some_and(|current| {
467 current.selection.conversation_id() == conversation_id
468 && current.mode == ViewMode::Thread(root_id.to_owned())
469 });
470 if !still_open {
471 return;
472 }
473 let failure = match result {
474 Ok(Ok(_)) => None,
475 Ok(Err(error)) => Some(short_error(&error)),
476 Err(error) => Some(short_error(&error)),
477 };
478 if let Some(failure) = failure {
479 self.notice = Some(format!("Could not load all replies: {failure}"));
480 }
481 self.rebuild(false, cx);
482 }
483}