BlueGit

ict rusty-teams

Files

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

feed.rs

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