BlueGit

ict rusty-teams

Files

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

feed.rs

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