BlueGit

ict rusty-teams

Files

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

feed.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
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::notice::short_error;
15use crate::rows::Row;
16use crate::runtime;
17use crate::views::channel_tabs::ChannelPane;
18use crate::views::composer::{Composer, ComposerEvent, ReplyPreview};
19use crate::views::post_card::{PostAction, post_in_channel_card};
20
21const FEED_CARD_TOP: f32 = 12.;
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
290 .render_proposal_card(cx)
291 .map(|card| div().w_full().pt(px(FEED_CARD_TOP)).child(card));
292 if !self.new_post_open {
293 let view = cx.entity().downgrade();
294 let announce_view = view.clone();
295 let open: PostAction = Box::new(move |window, cx| {
296 view.update(cx, |this, cx| this.open_new_post(window, cx))
297 .ok();
298 });
299 let announce: PostAction = Box::new(move |window, cx| {
300 announce_view
301 .update(cx, |this, cx| this.open_new_announcement(window, cx))
302 .ok();
303 });
304 return Some(
305 v_flex()
306 .w_full()
307 .flex_none()
308 .children(proposal_card)
309 .child(div().w_full().flex_none().px(px(20.)).py(px(12.)).child(
310 post_in_channel_card(&self.app.read(cx).directory, open, announce),
311 ))
312 .into_any_element(),
313 );
314 }
315 let editing = self.composer.read(cx).is_editing();
316 let cancel = editing.then(|| {
317 h_flex()
318 .w_full()
319 .px(px(20.))
320 .pb(px(12.))
321 .justify_end()
322 .child(
323 Button::new("cancel-new-post")
324 .ghost()
325 .compact()
326 .label("Cancel")
327 .on_click(
328 cx.listener(|this, _, window, cx| this.cancel_new_post(window, cx)),
329 ),
330 )
331 });
332 Some(
333 v_flex()
334 .w_full()
335 .flex_none()
336 .children(proposal_card)
337 .child(div().w_full().pt(px(12.)).child(self.composer.clone()))
338 .children(cancel)
339 .into_any_element(),
340 )
341 }
342
343 pub(super) fn cancel_new_post(&mut self, window: &mut Window, cx: &mut Context<Self>) {
344 let editing = self.composer.read(cx).is_editing();
345 if editing {
346 self.composer
347 .update(cx, |composer, cx| composer.cancel_edit(window, cx));
348 }
349 self.close_new_post(cx);
350 }
351
352 pub(super) fn on_escape(&mut self, window: &mut Window, cx: &mut Context<Self>) -> bool {
353 if self.close_new_folder(cx) {
354 return true;
355 }
356 if self.reply_root.is_some() {
357 self.close_reply_editor(cx);
358 return true;
359 }
360 if self.new_post_open && self.in_feed() {
361 self.cancel_new_post(window, cx);
362 return true;
363 }
364 if matches!(
365 self.current.as_ref().map(|current| &current.mode),
366 Some(ViewMode::Thread(_))
367 ) && !self.draft_active
368 {
369 self.back_to_threads(cx);
370 return true;
371 }
372 false
373 }
374
375 pub(super) fn enter_thread_mode(&mut self, root_id: String, cx: &mut Context<Self>) {
376 let Some(current) = self.current.as_mut() else {
377 return;
378 };
379 if current.mode == ViewMode::ThreadList {
380 let stash = FeedStash {
381 scroller: self.scroller.clone(),
382 rows: self.rows.clone(),
383 near_top: self.feed_at_top,
384 };
385 self.feed_stash = Some(stash);
386 self.scroller = cx.new(|cx| MessageScrollerState::new(0, cx));
387 self.rows = Rc::new(RefCell::new(Vec::new()));
388 }
389 current.mode = ViewMode::Thread(root_id);
390 self.reply_root = None;
391 }
392
393 pub(super) fn enter_feed_mode(&mut self) -> bool {
394 let Some(current) = self.current.as_mut() else {
395 return false;
396 };
397 current.mode = ViewMode::ThreadList;
398 match self.feed_stash.take() {
399 Some(stash) => {
400 self.scroller = stash.scroller;
401 self.rows = stash.rows;
402 self.feed_at_top = stash.near_top;
403 true
404 }
405 None => false,
406 }
407 }
408
409 pub(super) fn open_thread(
410 &mut self,
411 root_id: String,
412 window: &mut Window,
413 cx: &mut Context<Self>,
414 ) {
415 self.stop_typing(cx);
416 self.stop_reply_typing(cx);
417 self.enter_thread_mode(root_id.clone(), cx);
418 self.reload_pending(cx);
419 self.rebuild(true, cx);
420 self.refresh_thread(root_id, cx);
421 self.composer
422 .update(cx, |composer, cx| composer.focus(window, cx));
423 }
424
425 pub(super) fn back_to_threads(&mut self, cx: &mut Context<Self>) {
426 self.stop_typing(cx);
427 let restored = self.enter_feed_mode();
428 self.reload_pending(cx);
429 self.rebuild(!restored, cx);
430 }
431
432 pub(super) fn refresh_thread(&mut self, root_id: String, cx: &mut Context<Self>) {
433 let state = self.app.read(cx);
434 if state.mode.demo || state.mode.read_only {
435 return;
436 }
437 let (Some(engine), Some(conversation_id)) = (state.engine.clone(), self.conversation_id())
438 else {
439 return;
440 };
441 let receiver = {
442 let (conversation_id, root_id) = (conversation_id.clone(), root_id.clone());
443 runtime::spawn(async move { engine.refresh_thread(&conversation_id, &root_id).await })
444 };
445 cx.spawn(async move |this, cx| {
446 let result = receiver.await;
447 this.update(cx, |this, cx| {
448 this.finish_thread_refresh(&conversation_id, &root_id, result, cx)
449 })
450 .ok();
451 })
452 .detach();
453 }
454
455 fn finish_thread_refresh(
456 &mut self,
457 conversation_id: &str,
458 root_id: &str,
459 result: Result<
460 teams_core::Result<teams_core::Delta>,
461 tokio::sync::oneshot::error::RecvError,
462 >,
463 cx: &mut Context<Self>,
464 ) {
465 let still_open = self.current.as_ref().is_some_and(|current| {
466 current.selection.conversation_id() == conversation_id
467 && current.mode == ViewMode::Thread(root_id.to_owned())
468 });
469 if !still_open {
470 return;
471 }
472 let failure = match result {
473 Ok(Ok(_)) => None,
474 Ok(Err(error)) => Some(short_error(&error)),
475 Err(error) => Some(short_error(&error)),
476 };
477 if let Some(failure) = failure {
478 self.notice = Some(format!("Could not load all replies: {failure}"));
479 }
480 self.rebuild(false, cx);
481 }
482}