BlueGit

ict rusty-teams

Files

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

feed.rs

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