BlueGit

ict rusty-teams

Files

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

feed.rs

d4d204e5 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| InputState::new(window, cx).placeholder(crate::views::composer::SUBJECT_PLACEHOLDER))
35}
36
37impl ConversationView {
38 pub(super) fn in_feed(&self) -> bool {
39 !self.draft_active
40 && self
41 .current
42 .as_ref()
43 .is_some_and(|current| current.mode == ViewMode::ThreadList)
44 }
45
46 pub fn start_channel_post(&mut self, window: &mut Window, cx: &mut Context<Self>) {
47 if self.in_feed() {
48 self.open_new_post(window, cx);
49 }
50 }
51
52 pub(super) fn open_new_post(&mut self, window: &mut Window, cx: &mut Context<Self>) {
53 self.mark_new_post_open(cx);
54 self.composer
55 .update(cx, |composer, cx| composer.focus(window, cx));
56 }
57
58 pub(super) fn open_new_announcement(&mut self, window: &mut Window, cx: &mut Context<Self>) {
59 self.mark_new_post_open(cx);
60 self.composer
61 .update(cx, |composer, cx| composer.begin_announcement(window, cx));
62 }
63
64 fn mark_new_post_open(&mut self, cx: &mut Context<Self>) {
65 self.new_post_open = true;
66 let subject = self.subject.clone();
67 self.composer.update(cx, |composer, cx| {
68 composer.set_post_layout(Some(subject), cx)
69 });
70 cx.notify();
71 }
72
73 pub(super) fn restore_new_post(
74 &mut self,
75 subject: Option<String>,
76 window: &mut Window,
77 cx: &mut Context<Self>,
78 ) {
79 if !self.in_feed() {
80 return;
81 }
82 self.restore_subject(subject.as_deref(), window, cx);
83 self.mark_new_post_open(cx);
84 self.composer.update(cx, |composer, cx| {
85 composer.sync_subject_placeholder(window, cx)
86 });
87 }
88
89 pub(super) fn close_new_post(&mut self, cx: &mut Context<Self>) {
90 self.composer
91 .update(cx, |composer, cx| composer.set_post_layout(None, cx));
92 if self.new_post_open {
93 self.new_post_open = false;
94 cx.notify();
95 }
96 }
97
98 pub(super) fn take_subject(
99 &mut self,
100 window: &mut Window,
101 cx: &mut Context<Self>,
102 ) -> Option<String> {
103 let subject = self.subject.read(cx).value().trim().to_owned();
104 self.subject
105 .update(cx, |input, cx| input.set_value("", window, cx));
106 (!subject.is_empty()).then_some(subject)
107 }
108
109 pub(super) fn restore_subject(
110 &mut self,
111 subject: Option<&str>,
112 window: &mut Window,
113 cx: &mut Context<Self>,
114 ) {
115 let subject = subject.unwrap_or_default().to_owned();
116 self.subject
117 .update(cx, |input, cx| input.set_value(subject, window, cx));
118 }
119
120 pub(super) fn has_subject(&self, cx: &App) -> bool {
121 !self.subject.read(cx).value().trim().is_empty()
122 }
123
124 pub(super) fn on_subject_event(
125 &mut self,
126 _: &Entity<InputState>,
127 event: &InputEvent,
128 window: &mut Window,
129 cx: &mut Context<Self>,
130 ) {
131 match event {
132 InputEvent::PressEnter { .. } => self
133 .composer
134 .update(cx, |composer, cx| composer.focus(window, cx)),
135 InputEvent::Change => self
136 .composer
137 .update(cx, |composer, cx| composer.schedule_draft_save(cx)),
138 _ => {}
139 }
140 }
141
142 fn reply_composer(&mut self, window: &mut Window, cx: &mut Context<Self>) -> Entity<Composer> {
143 if let Some(composer) = &self.reply_composer {
144 return composer.clone();
145 }
146 let composer = cx.new(|cx| Composer::new(self.app.clone(), window, cx).inline());
147 let subscription = cx.subscribe_in(&composer, window, Self::on_reply_event);
148 self._subscriptions.push(subscription);
149 self.reply_composer = Some(composer.clone());
150 self.sync_reply_composer(window, cx);
151 composer
152 }
153
154 fn keep_reply_editor_in_view(&mut self, window: &mut Window, cx: &mut Context<Self>) {
155 let Some(root_id) = self.reply_root.clone() else {
156 return;
157 };
158 if !self.in_feed() {
159 return;
160 }
161 let view = cx.entity().downgrade();
162 window.on_next_frame(move |_, cx| {
163 view.update(cx, |this, cx| {
164 let index = this.rows.borrow().iter().position(|row| row.key() == root_id);
165 if let Some(index) = index {
166 this.scroller
167 .update(cx, |scroller, cx| scroller.reveal_item_bottom(index, cx));
168 }
169 })
170 .ok();
171 });
172 }
173
174 pub(super) fn sync_reply_composer(&mut self, window: &mut Window, cx: &mut Context<Self>) {
175 self.reply_root = None;
176 let (Some(composer), Some(conversation_id)) =
177 (self.reply_composer.clone(), self.conversation_id())
178 else {
179 return;
180 };
181 composer.update(cx, |composer, cx| {
182 composer.set_conversation(&conversation_id, window, cx);
183 composer.set_placeholder(REPLY_PLACEHOLDER, window, cx);
184 composer.set_text("", window, cx);
185 composer.set_reply(None, cx);
186 });
187 }
188
189 pub(super) fn open_reply_editor(
190 &mut self,
191 root_id: String,
192 quote: Option<ReplyPreview>,
193 window: &mut Window,
194 cx: &mut Context<Self>,
195 ) {
196 let composer = self.reply_composer(window, cx);
197 let previous = self.reply_root.replace(root_id.clone());
198 let changed = previous.as_deref() != Some(root_id.as_str());
199 composer.update(cx, |composer, cx| {
200 if changed {
201 composer.set_text("", window, cx);
202 }
203 composer.set_reply(quote, cx);
204 composer.focus(window, cx);
205 });
206 self.remeasure_posts(&[Some(root_id), previous], cx);
207 }
208
209 pub(super) fn close_reply_editor(&mut self, cx: &mut Context<Self>) {
210 if let Some(root_id) = self.reply_root.take() {
211 self.remeasure_posts(&[Some(root_id)], cx);
212 }
213 }
214
215 fn remeasure_posts(&mut self, root_ids: &[Option<String>], cx: &mut Context<Self>) {
216 let indices: Vec<usize> = self
217 .rows
218 .borrow()
219 .iter()
220 .enumerate()
221 .filter(|(_, row)| {
222 matches!(row, Row::Post(post) if root_ids.iter().flatten().any(|id| *id == post.root.key))
223 })
224 .map(|(index, _)| index)
225 .collect();
226 self.scroller.update(cx, |scroller, cx| {
227 for index in indices {
228 scroller.remeasure_items(index..index + 1, cx);
229 }
230 });
231 cx.notify();
232 }
233
234 fn on_reply_event(
235 &mut self,
236 _: &Entity<Composer>,
237 event: &ComposerEvent,
238 window: &mut Window,
239 cx: &mut Context<Self>,
240 ) {
241 self.report_composer_activity(event, cx);
242 match event {
243 ComposerEvent::Submit(outgoing) => {
244 let Some(root_id) = self.reply_root.clone() else {
245 return;
246 };
247 self.close_reply_editor(cx);
248 self.send_in_thread(root_id, (**outgoing).clone(), window, cx);
249 }
250 ComposerEvent::Typing(active) => {
251 let root_id = self.reply_root.clone();
252 self.send_typing_in(root_id, *active, cx);
253 }
254 ComposerEvent::Edited => self.keep_reply_editor_in_view(window, cx),
255 ComposerEvent::Schedule { .. }
256 | ComposerEvent::EditLast
257 | ComposerEvent::FocusMessages
258 | ComposerEvent::ClosePost
259 | ComposerEvent::RestorePost(_) => {}
260 }
261 }
262
263 pub(super) fn stop_reply_typing(&mut self, cx: &mut Context<Self>) {
264 let Some(composer) = self.reply_composer.clone() else {
265 return;
266 };
267 if composer.update(cx, |composer, _| composer.stop_typing()) {
268 let root_id = self.reply_root.clone();
269 self.send_typing_in(root_id, false, cx);
270 }
271 }
272
273 pub(super) fn show_main_composer(&mut self, cx: &mut Context<Self>) {
274 if self.in_feed() {
275 self.mark_new_post_open(cx);
276 }
277 }
278
279 pub(super) fn render_new_post(&self, cx: &mut Context<Self>) -> Option<AnyElement> {
280 if !self.in_feed() || self.pane != ChannelPane::Posts {
281 return None;
282 }
283 let proposal_card = self
284 .render_proposal_card(cx)
285 .map(|card| div().w_full().pt(px(FEED_CARD_TOP)).child(card));
286 if !self.new_post_open {
287 let view = cx.entity().downgrade();
288 let announce_view = view.clone();
289 let open: PostAction = Box::new(move |window, cx| {
290 view.update(cx, |this, cx| this.open_new_post(window, cx)).ok();
291 });
292 let announce: PostAction = Box::new(move |window, cx| {
293 announce_view
294 .update(cx, |this, cx| this.open_new_announcement(window, cx))
295 .ok();
296 });
297 return Some(
298 v_flex()
299 .w_full()
300 .flex_none()
301 .children(proposal_card)
302 .child(
303 div()
304 .w_full()
305 .flex_none()
306 .px(px(20.))
307 .py(px(12.))
308 .child(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("Cancel")
325 .on_click(cx.listener(|this, _, window, cx| {
326 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}