BlueGit

ict rusty-teams

Files

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

feed.rs

feed.rs 15.0 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";
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 { .. } | ComposerEvent::EditLast => {}
194 }
195 }
196
197 pub(super) fn stop_reply_typing(&mut self, cx: &mut Context<Self>) {
198 let Some(composer) = self.reply_composer.clone() else {
199 return;
200 };
201 if composer.update(cx, |composer, _| composer.stop_typing()) {
202 let root_id = self.reply_root.clone();
203 self.send_typing_in(root_id, false, cx);
204 }
205 }
206
207 pub(super) fn show_main_composer(&mut self, cx: &mut Context<Self>) {
208 if self.in_feed() {
209 self.new_post_open = true;
210 cx.notify();
211 }
212 }
213
214 pub(super) fn render_new_post(
215 &self,
216 window: &Window,
217 cx: &mut Context<Self>,
218 ) -> Option<AnyElement> {
219 if !self.in_feed() || self.pane != ChannelPane::Posts {
220 return None;
221 }
222 let proposal_card = self.render_proposal_card(cx);
223 if !self.new_post_open {
224 return Some(
225 v_flex()
226 .w_full()
227 .flex_none()
228 .children(proposal_card)
229 .child(
230 h_flex()
231 .w_full()
232 .flex_none()
233 .px(px(20.))
234 .pt(px(12.))
235 .pb(px(12.))
236 .child(
237 h_flex()
238 .id("new-post")
239 .h(px(32.))
240 .px(px(12.))
241 .gap(px(6.))
242 .items_center()
243 .rounded(px(8.))
244 .bg(theme::accent())
245 .text_color(theme::on_accent())
246 .text_size(px(13.))
247 .font_weight(FontWeight::SEMIBOLD)
248 .cursor_pointer()
249 .hover(|button| button.bg(theme::accent_text()))
250 .on_click(cx.listener(|this, _, window, cx| {
251 this.open_new_post(window, cx)
252 }))
253 .child(icon(IconName::Plus, 14., theme::on_accent()))
254 .child("New post"),
255 ),
256 )
257 .into_any_element(),
258 );
259 }
260 let editing = self.composer.read(cx).is_editing();
261 let focused = self
262 .subject
263 .read(cx)
264 .focus_handle(cx)
265 .contains_focused(window, cx);
266 let subject = (!editing).then(|| {
267 div().w_full().px(px(20.)).pt(px(12.)).child(
268 div()
269 .w_full()
270 .h(px(36.))
271 .px(px(12.))
272 .flex()
273 .items_center()
274 .rounded(px(10.))
275 .bg(theme::surface_raised())
276 .border_1()
277 .border_color(if focused {
278 theme::accent()
279 } else {
280 transparent_black()
281 })
282 .child(
283 div()
284 .w_full()
285 .child(Input::new(&self.subject).appearance(false).bordered(false)),
286 ),
287 )
288 });
289 Some(
290 v_flex()
291 .w_full()
292 .flex_none()
293 .children(subject)
294 .children(proposal_card)
295 .child(self.composer.clone())
296 .child(
297 h_flex()
298 .w_full()
299 .px(px(20.))
300 .pb(px(12.))
301 .justify_end()
302 .child(
303 Button::new("cancel-new-post")
304 .ghost()
305 .compact()
306 .label("Cancel")
307 .on_click(cx.listener(|this, _, window, cx| {
308 this.cancel_new_post(window, cx)
309 })),
310 ),
311 )
312 .into_any_element(),
313 )
314 }
315
316 pub(super) fn cancel_new_post(&mut self, window: &mut Window, cx: &mut Context<Self>) {
317 let editing = self.composer.read(cx).is_editing();
318 if editing {
319 self.composer
320 .update(cx, |composer, cx| composer.cancel_edit(window, cx));
321 }
322 self.close_new_post(cx);
323 }
324
325 pub(super) fn on_escape(&mut self, window: &mut Window, cx: &mut Context<Self>) -> bool {
326 if self.close_new_folder(cx) {
327 return true;
328 }
329 if self.reply_root.is_some() {
330 self.close_reply_editor(cx);
331 return true;
332 }
333 if self.new_post_open && self.in_feed() {
334 self.cancel_new_post(window, cx);
335 return true;
336 }
337 if matches!(
338 self.current.as_ref().map(|current| &current.mode),
339 Some(ViewMode::Thread(_))
340 ) && !self.draft_active
341 {
342 self.back_to_threads(cx);
343 return true;
344 }
345 false
346 }
347
348 pub(super) fn enter_thread_mode(&mut self, root_id: String, cx: &mut Context<Self>) {
349 let Some(current) = self.current.as_mut() else {
350 return;
351 };
352 if current.mode == ViewMode::ThreadList {
353 let stash = FeedStash {
354 scroller: self.scroller.clone(),
355 rows: self.rows.clone(),
356 near_top: self.feed_at_top,
357 };
358 self.feed_stash = Some(stash);
359 self.scroller = cx.new(|cx| MessageScrollerState::new(0, cx));
360 self.rows = Rc::new(RefCell::new(Vec::new()));
361 }
362 current.mode = ViewMode::Thread(root_id);
363 self.reply_root = None;
364 }
365
366 pub(super) fn enter_feed_mode(&mut self) -> bool {
367 let Some(current) = self.current.as_mut() else {
368 return false;
369 };
370 current.mode = ViewMode::ThreadList;
371 match self.feed_stash.take() {
372 Some(stash) => {
373 self.scroller = stash.scroller;
374 self.rows = stash.rows;
375 self.feed_at_top = stash.near_top;
376 true
377 }
378 None => false,
379 }
380 }
381
382 pub(super) fn open_thread(
383 &mut self,
384 root_id: String,
385 window: &mut Window,
386 cx: &mut Context<Self>,
387 ) {
388 self.stop_typing(cx);
389 self.stop_reply_typing(cx);
390 self.enter_thread_mode(root_id.clone(), cx);
391 self.reload_pending(cx);
392 self.rebuild(true, cx);
393 self.refresh_thread(root_id, cx);
394 self.composer
395 .update(cx, |composer, cx| composer.focus(window, cx));
396 }
397
398 pub(super) fn back_to_threads(&mut self, cx: &mut Context<Self>) {
399 self.stop_typing(cx);
400 let restored = self.enter_feed_mode();
401 self.reload_pending(cx);
402 self.rebuild(!restored, cx);
403 }
404
405 pub(super) fn refresh_thread(&mut self, root_id: String, cx: &mut Context<Self>) {
406 let state = self.app.read(cx);
407 if state.mode.demo || state.mode.read_only {
408 return;
409 }
410 let (Some(engine), Some(conversation_id)) = (state.engine.clone(), self.conversation_id())
411 else {
412 return;
413 };
414 let receiver = {
415 let (conversation_id, root_id) = (conversation_id.clone(), root_id.clone());
416 runtime::spawn(async move { engine.refresh_thread(&conversation_id, &root_id).await })
417 };
418 cx.spawn(async move |this, cx| {
419 let result = receiver.await;
420 this.update(cx, |this, cx| {
421 this.finish_thread_refresh(&conversation_id, &root_id, result, cx)
422 })
423 .ok();
424 })
425 .detach();
426 }
427
428 fn finish_thread_refresh(
429 &mut self,
430 conversation_id: &str,
431 root_id: &str,
432 result: Result<
433 teams_core::Result<teams_core::Delta>,
434 tokio::sync::oneshot::error::RecvError,
435 >,
436 cx: &mut Context<Self>,
437 ) {
438 let still_open = self.current.as_ref().is_some_and(|current| {
439 current.selection.conversation_id() == conversation_id
440 && current.mode == ViewMode::Thread(root_id.to_owned())
441 });
442 if !still_open {
443 return;
444 }
445 let failure = match result {
446 Ok(Ok(_)) => None,
447 Ok(Err(error)) => Some(short_error(&error)),
448 Err(error) => Some(short_error(&error)),
449 };
450 if let Some(failure) = failure {
451 self.notice = Some(format!("Could not load all replies: {failure}"));
452 }
453 self.rebuild(false, cx);
454 }
455}