BlueGit

ict rusty-teams

Files

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

feed.rs

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