BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / quick_view_pane.rs

quick_view_pane.rs

quick_view_pane.rs 14.1 KiB Download Raw
1use std::collections::HashMap;
2
3use app_base::format::Zone;
4use chrono::{DateTime, Local, NaiveDate, Utc};
5use gpui_kit::assets::IconName;
6use gpui_kit::component::scroll::ScrollableElement as _;
7use gpui_kit::component::{h_flex, tooltip::Tooltip, v_flex};
8use gpui_kit::prelude::FluentBuilder as _;
9use gpui_kit::*;
10use teams_core::SavedMessage;
11
12use super::avatar::person_avatar;
13use super::scroll_edge::with_top_edge;
14use super::widgets::icon;
15use crate::activity::{ActivityCenter, Entry, Kind};
16use store::Sidebar;
17
18use crate::app_state::{AppState, QuickView, selection_for, selection_title};
19use crate::format;
20use crate::local_previews::LocalPreview;
21use crate::theme;
22
23const QUICK_VIEW_SCROLL_DIVIDER: &str = "shell/islands/main/quick-view-divider-scroll";
24const HEADER_HEIGHT: f32 = 60.;
25const HEADER_PADDING: f32 = 20.;
26const AVATAR_SIZE: f32 = 32.;
27const BUTTON_SIZE: f32 = 24.;
28const PREVIEW_LINES: usize = 2;
29const LIST_MAX_WIDTH: f32 = 860.;
30const UNKNOWN_AUTHOR: &str = "Unknown";
31
32#[derive(Debug, Clone, PartialEq, Eq)]
33pub struct QuickRow {
34 pub conversation_id: String,
35 pub message_id: Option<String>,
36 pub author_id: Option<String>,
37 pub author: String,
38 pub place: String,
39 pub time: String,
40 pub preview: String,
41 pub saved: Option<SavedMessage>,
42}
43
44fn place_of(sidebar: &Sidebar, conversation_id: &str) -> Option<String> {
45 selection_for(sidebar, conversation_id).map(|selection| selection_title(sidebar, &selection))
46}
47
48fn time_label(time: DateTime<Utc>, today: NaiveDate, offset: Zone) -> String {
49 format::list_time_label(time, today, offset)
50}
51
52pub fn mention_rows(
53 entries: &[Entry],
54 sidebar: &Sidebar,
55 today: NaiveDate,
56 offset: Zone,
57) -> Vec<QuickRow> {
58 let mut mentions: Vec<&Entry> = entries
59 .iter()
60 .filter(|entry| entry.kind == Kind::Mention)
61 .collect();
62 mentions.sort_by(|left, right| right.updated_at.cmp(&left.updated_at));
63 mentions
64 .into_iter()
65 .map(|entry| {
66 let actor = entry.actors.first();
67 QuickRow {
68 conversation_id: entry.conversation_id.clone(),
69 message_id: Some(entry.message_id.clone()).filter(|id| !id.is_empty()),
70 author_id: actor.and_then(|actor| actor.user_id.clone()),
71 author: actor
72 .map(|actor| actor.name.clone())
73 .unwrap_or_else(|| UNKNOWN_AUTHOR.to_owned()),
74 place: place_of(sidebar, &entry.conversation_id)
75 .or_else(|| entry.source_label.clone())
76 .unwrap_or_default(),
77 time: time_label(entry.updated_at, today, offset),
78 preview: entry.preview.clone(),
79 saved: None,
80 }
81 })
82 .collect()
83}
84
85pub fn draft_rows(previews: &HashMap<String, LocalPreview>, sidebar: &Sidebar) -> Vec<QuickRow> {
86 let mut rows: Vec<QuickRow> = previews
87 .iter()
88 .filter_map(|(conversation_id, preview)| {
89 let LocalPreview::Draft(text) = preview else {
90 return None;
91 };
92 let place = place_of(sidebar, conversation_id)?;
93 Some(QuickRow {
94 conversation_id: conversation_id.clone(),
95 message_id: None,
96 author_id: None,
97 author: place.clone(),
98 place: String::new(),
99 time: String::new(),
100 preview: text.clone(),
101 saved: None,
102 })
103 })
104 .collect();
105 rows.sort_by(|left, right| left.author.to_lowercase().cmp(&right.author.to_lowercase()));
106 rows
107}
108
109pub fn saved_rows(
110 items: &[SavedMessage],
111 sidebar: &Sidebar,
112 today: NaiveDate,
113 offset: Zone,
114) -> Vec<QuickRow> {
115 items
116 .iter()
117 .map(|item| QuickRow {
118 conversation_id: item.conversation_id.clone(),
119 message_id: Some(item.message_id.clone()),
120 author_id: item.author_id.clone(),
121 author: item
122 .author_name
123 .clone()
124 .unwrap_or_else(|| UNKNOWN_AUTHOR.to_owned()),
125 place: place_of(sidebar, &item.conversation_id)
126 .or_else(|| item.topic.clone())
127 .unwrap_or_default(),
128 time: time_label(item.saved_at, today, offset),
129 preview: item.preview.clone(),
130 saved: Some(item.clone()),
131 })
132 .collect()
133}
134
135pub enum QuickViewEvent {
136 Open {
137 conversation_id: String,
138 message_id: Option<String>,
139 },
140}
141
142pub struct QuickViewPane {
143 app: Entity<AppState>,
144 activity: Entity<ActivityCenter>,
145 scroll: ScrollHandle,
146 _observations: [Subscription; 2],
147}
148
149impl EventEmitter<QuickViewEvent> for QuickViewPane {}
150
151impl QuickViewPane {
152 pub fn new(
153 app: Entity<AppState>,
154 activity: Entity<ActivityCenter>,
155 cx: &mut Context<Self>,
156 ) -> Self {
157 let observations = [
158 cx.observe(&app, |_, _, cx| cx.notify()),
159 cx.observe(&activity, |_, _, cx| cx.notify()),
160 ];
161 QuickViewPane {
162 app,
163 activity,
164 scroll: ScrollHandle::new(),
165 _observations: observations,
166 }
167 }
168
169 fn rows(&self, view: QuickView, cx: &App) -> Vec<QuickRow> {
170 let state = self.app.read(cx);
171 let offset = Zone::Local;
172 let today = Local::now().date_naive();
173 match view {
174 QuickView::Mentions => mention_rows(
175 self.activity.read(cx).feed().entries(),
176 &state.sidebar,
177 today,
178 offset,
179 ),
180 QuickView::Drafts => draft_rows(&state.local_previews, &state.sidebar),
181 QuickView::Saved => saved_rows(state.saved.items(), &state.sidebar, today, offset),
182 }
183 }
184
185 fn header(&self, view: QuickView, count: usize) -> Div {
186 h_flex()
187 .h(px(HEADER_HEIGHT))
188 .flex_none()
189 .px(px(HEADER_PADDING))
190 .gap(px(8.))
191 .items_center()
192 .child(
193 div()
194 .text_size(px(16.))
195 .font_weight(FontWeight::BOLD)
196 .text_color(theme::text())
197 .child(view.title()),
198 )
199 .when(count > 0, |header| {
200 header.child(
201 div()
202 .text_size(px(12.))
203 .text_color(theme::text_muted())
204 .child(count.to_string()),
205 )
206 })
207 }
208
209 fn row(&self, row: &QuickRow, index: usize, cx: &Context<Self>) -> AnyElement {
210 let app = self.app.clone();
211 let group = SharedString::from(format!("quick-row-group-{index}"));
212 let (conversation_id, message_id) = (row.conversation_id.clone(), row.message_id.clone());
213 let unsave = row.saved.clone().map(|item| {
214 div()
215 .id(ElementId::Name(format!("quick-unsave-{index}").into()))
216 .absolute()
217 .right(px(8.))
218 .top(px(8.))
219 .size(px(BUTTON_SIZE))
220 .flex()
221 .items_center()
222 .justify_center()
223 .rounded(px(6.))
224 .cursor_pointer()
225 .invisible()
226 .group_hover(group.clone(), |button| button.visible())
227 .hover(|button| button.bg(theme::popup_hover()))
228 .child(icon(IconName::BookmarkOff, 14., theme::text_muted()))
229 .tooltip(|window, cx| Tooltip::new("Unsave").build(window, cx))
230 .on_click(move |_, _, cx| {
231 cx.stop_propagation();
232 let entry = item.clone();
233 app.update(cx, |state, cx| state.set_saved(entry, false, cx));
234 })
235 });
236 let directory = &self.app.read(cx).directory;
237 h_flex()
238 .id(ElementId::Name(format!("quick-row-{index}").into()))
239 .group(group.clone())
240 .relative()
241 .w_full()
242 .p(px(12.))
243 .gap(px(12.))
244 .items_start()
245 .rounded(px(8.))
246 .cursor_pointer()
247 .hover(|row| row.bg(theme::row_hover()))
248 .child(div().flex_none().size(px(AVATAR_SIZE)).child(person_avatar(
249 directory,
250 row.author_id.as_deref(),
251 &row.author,
252 AVATAR_SIZE,
253 )))
254 .child(
255 v_flex()
256 .flex_1()
257 .min_w_0()
258 .gap(px(2.))
259 .child(
260 h_flex()
261 .gap(px(8.))
262 .items_baseline()
263 .child(
264 div()
265 .flex_none()
266 .text_size(px(14.))
267 .font_weight(FontWeight::SEMIBOLD)
268 .text_color(theme::text())
269 .child(row.author.clone()),
270 )
271 .child(
272 div()
273 .flex_1()
274 .min_w_0()
275 .truncate()
276 .text_size(px(12.))
277 .text_color(theme::text_muted())
278 .child(row.place.clone()),
279 )
280 .child(
281 div()
282 .flex_none()
283 .pr(px(BUTTON_SIZE + 8.))
284 .text_size(px(12.))
285 .text_color(theme::text_muted())
286 .child(row.time.clone()),
287 ),
288 )
289 .child(
290 div()
291 .line_clamp(PREVIEW_LINES)
292 .text_ellipsis()
293 .text_size(px(14.))
294 .line_height(relative(1.4))
295 .text_color(theme::text_soft())
296 .child(row.preview.clone()),
297 ),
298 )
299 .children(unsave)
300 .on_click(cx.listener(move |_, _, _, cx| {
301 cx.emit(QuickViewEvent::Open {
302 conversation_id: conversation_id.clone(),
303 message_id: message_id.clone(),
304 });
305 }))
306 .into_any_element()
307 }
308}
309
310impl Render for QuickViewPane {
311 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
312 let view = self.app.read(cx).quick_view.unwrap_or(QuickView::Saved);
313 let rows = self.rows(view, cx);
314 let mut list: Vec<AnyElement> = rows
315 .iter()
316 .enumerate()
317 .map(|(index, row)| self.row(row, index, cx))
318 .collect();
319 if list.is_empty() {
320 list.push(
321 div()
322 .py(px(32.))
323 .px(px(24.))
324 .flex()
325 .justify_center()
326 .text_center()
327 .text_size(px(13.))
328 .text_color(theme::text_muted())
329 .child(view.empty_text())
330 .into_any_element(),
331 );
332 }
333 v_flex()
334 .debug_selector(|| "shell/islands/main".into())
335 .flex_1()
336 .w_full()
337 .min_w_0()
338 .h_full()
339 .rounded(px(10.))
340 .bg(theme::surface())
341 .overflow_hidden()
342 .child(self.header(view, rows.len()))
343 .child(div().w_full().h(px(1.)).flex_none().bg(theme::border()))
344 .child(with_top_edge(
345 v_flex()
346 .id("quick-view-list")
347 .flex_1()
348 .min_h_0()
349 .px(px(HEADER_PADDING - 12.))
350 .py(px(8.))
351 .overflow_y_scrollbar()
352 .track_scroll(&self.scroll)
353 .child(v_flex().w_full().max_w(px(LIST_MAX_WIDTH)).children(list)),
354 &self.scroll,
355 QUICK_VIEW_SCROLL_DIVIDER,
356 ))
357 }
358}
359
360#[cfg(test)]
361mod tests {
362 use std::collections::HashMap;
363
364 use chrono::{FixedOffset, NaiveDate, TimeZone, Utc};
365 use store::Sidebar;
366
367 use super::{draft_rows, mention_rows};
368 use crate::activity::{Actor, Entry, Kind};
369 use crate::local_previews::LocalPreview;
370
371 fn entry(id: &str, kind: Kind, minute: u32) -> Entry {
372 Entry {
373 id: id.to_owned(),
374 conversation_id: "19:chat".to_owned(),
375 kind,
376 message_id: format!("m-{id}"),
377 actors: vec![Actor {
378 user_id: Some("ada".to_owned()),
379 name: "Ada".to_owned(),
380 }],
381 preview: "@Dana look".to_owned(),
382 updated_at: Utc.with_ymd_and_hms(2026, 11, 8, 9, minute, 0).unwrap(),
383 ..Entry::default()
384 }
385 }
386
387 #[test]
388 fn mentions_list_only_mentions_newest_first() {
389 let entries = [
390 entry("a", Kind::Mention, 1),
391 entry("b", Kind::Reaction, 2),
392 entry("c", Kind::Mention, 3),
393 ];
394 let offset = FixedOffset::east_opt(0).unwrap().into();
395 let today = NaiveDate::from_ymd_opt(2026, 11, 8).unwrap();
396 let rows = mention_rows(&entries, &Sidebar::default(), today, offset);
397 let ids: Vec<_> = rows
398 .iter()
399 .map(|row| row.message_id.clone().unwrap())
400 .collect();
401 assert_eq!(ids, ["m-c", "m-a"]);
402 assert_eq!(rows[0].author, "Ada");
403 }
404
405 #[test]
406 fn drafts_skip_unsent_and_unknown_conversations() {
407 let previews = HashMap::from([
408 ("19:gone".to_owned(), LocalPreview::Draft("lost".to_owned())),
409 ("19:chat".to_owned(), LocalPreview::NotSent),
410 ]);
411 assert!(draft_rows(&previews, &Sidebar::default()).is_empty());
412 }
413}