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