BlueGit

ict rusty-teams

Files

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

activity_panel.rs

activity_panel.rs 17.3 KiB Download Raw
1use chrono::{DateTime, Local, NaiveDate, Offset};
2use gpui_kit::assets::IconName;
3use gpui_kit::component::{h_flex, tooltip::Tooltip, v_flex};
4use gpui_kit::prelude::FluentBuilder as _;
5use gpui_kit::*;
6use store::Sidebar;
7
8use super::avatar::person_avatar;
9use super::title_bar::TITLE_BAR_HEIGHT;
10use super::widgets::{dot, icon};
11use crate::activity::{ActivityCenter, Entry, Filter, Kind};
12use crate::app_state::{AppState, chat_title};
13use crate::data::is_one_on_one;
14use crate::format;
15use crate::theme;
16
17const PANEL_WIDTH: f32 = 420.;
18const PANEL_MAX_HEIGHT_RATIO: f32 = 0.7;
19const PANEL_MARGIN: f32 = 8.;
20const AVATAR_SIZE: f32 = 32.;
21const KIND_MARKER_SIZE: f32 = 16.;
22const BUTTON_SIZE: f32 = 28.;
23const MARK_READ_SIZE: f32 = 24.;
24const FILTER_KEY: &str = "activity_filter";
25
26pub enum ActivityPanelEvent {
27 Close,
28 OpenSettings,
29 Open {
30 conversation_id: String,
31 message_id: String,
32 },
33}
34
35pub struct ActivityPanel {
36 app: Entity<AppState>,
37 activity: Entity<ActivityCenter>,
38 filter: Filter,
39 focus_handle: FocusHandle,
40 scroll: ScrollHandle,
41 _observations: [Subscription; 2],
42}
43
44impl EventEmitter<ActivityPanelEvent> for ActivityPanel {}
45
46impl ActivityPanel {
47 pub fn new(
48 app: Entity<AppState>,
49 activity: Entity<ActivityCenter>,
50 window: &mut Window,
51 cx: &mut Context<Self>,
52 ) -> Self {
53 let focus_handle = cx.focus_handle();
54 window.focus(&focus_handle, cx);
55 let observations = [
56 cx.observe(&app, |_, _, cx| cx.notify()),
57 cx.observe(&activity, |_, _, cx| cx.notify()),
58 ];
59 let filter = app
60 .read(cx)
61 .store
62 .meta(FILTER_KEY)
63 .ok()
64 .flatten()
65 .and_then(|key| Filter::from_key(&key))
66 .unwrap_or_default();
67 ActivityPanel {
68 app,
69 activity,
70 filter,
71 focus_handle,
72 scroll: ScrollHandle::new(),
73 _observations: observations,
74 }
75 }
76
77 fn open(&mut self, id: i64, cx: &mut Context<Self>) {
78 let Some(entry) = self.activity.read(cx).feed().get(id) else {
79 return;
80 };
81 let event = ActivityPanelEvent::Open {
82 conversation_id: entry.conversation_id.clone(),
83 message_id: entry.message_id.clone(),
84 };
85 self.activity
86 .update(cx, |activity, cx| activity.mark_read(id, cx));
87 cx.emit(event);
88 }
89
90 fn on_key_down(&mut self, event: &KeyDownEvent, _: &mut Window, cx: &mut Context<Self>) {
91 if event.keystroke.key == "escape" {
92 cx.emit(ActivityPanelEvent::Close);
93 }
94 }
95
96 fn header(&self, cx: &mut Context<Self>) -> Div {
97 let pills = Filter::ALL.into_iter().map(|filter| {
98 let selected = filter == self.filter;
99 div()
100 .id(ElementId::Name(
101 format!("activity-filter-{}", filter.label()).into(),
102 ))
103 .h(px(24.))
104 .px(px(10.))
105 .flex()
106 .items_center()
107 .rounded_full()
108 .cursor_pointer()
109 .text_size(px(12.))
110 .when(selected, |pill| {
111 pill.bg(theme::accent())
112 .text_color(theme::on_accent())
113 .font_weight(FontWeight::SEMIBOLD)
114 })
115 .when(!selected, |pill| {
116 pill.bg(theme::surface_raised())
117 .text_color(theme::text_soft())
118 .hover(|pill| pill.bg(theme::border_strong()))
119 })
120 .child(filter.label())
121 .on_click(cx.listener(move |this, _, _, cx| {
122 this.filter = filter;
123 let _ = this.app.read(cx).store.set_meta(FILTER_KEY, filter.key());
124 cx.notify();
125 }))
126 });
127 v_flex()
128 .px(px(12.))
129 .pt(px(10.))
130 .pb(px(8.))
131 .gap(px(8.))
132 .border_b_1()
133 .border_color(theme::border())
134 .child(
135 h_flex()
136 .gap(px(8.))
137 .items_center()
138 .child(
139 div()
140 .flex_1()
141 .text_size(px(15.))
142 .font_weight(FontWeight::SEMIBOLD)
143 .text_color(theme::text())
144 .child("Activity"),
145 )
146 .child(
147 div()
148 .id("activity-mark-all-read")
149 .px(px(8.))
150 .h(px(BUTTON_SIZE))
151 .flex()
152 .items_center()
153 .rounded(px(6.))
154 .cursor_pointer()
155 .text_size(px(12.))
156 .text_color(theme::accent_text())
157 .hover(|button| button.bg(theme::surface_raised()))
158 .child("Mark all as read")
159 .on_click(cx.listener(|this, _, _, cx| {
160 this.activity
161 .update(cx, |activity, cx| activity.mark_all_read(cx));
162 })),
163 )
164 .child(
165 div()
166 .id("activity-settings")
167 .size(px(BUTTON_SIZE))
168 .flex()
169 .flex_none()
170 .items_center()
171 .justify_center()
172 .rounded(px(6.))
173 .cursor_pointer()
174 .hover(|button| button.bg(theme::surface_raised()))
175 .child(icon(IconName::Settings, 16., theme::text_muted()))
176 .on_click(cx.listener(|_, _, _, cx| {
177 cx.emit(ActivityPanelEvent::OpenSettings);
178 })),
179 ),
180 )
181 .child(h_flex().gap(px(6.)).children(pills))
182 }
183
184 fn row(
185 &self,
186 entry: &Entry,
187 sidebar: &Sidebar,
188 directory: &crate::data::Directory,
189 today: NaiveDate,
190 offset: chrono::FixedOffset,
191 panel: Entity<ActivityPanel>,
192 ) -> AnyElement {
193 let activity = self.activity.clone();
194 let id = entry.id;
195 let unread = !entry.read;
196 let actor = entry.latest_actor();
197 let avatar = person_avatar(
198 directory,
199 actor.and_then(|actor| actor.user_id.as_deref()),
200 actor.map_or("", |actor| actor.name.as_str()),
201 AVATAR_SIZE,
202 );
203 let group = SharedString::from(format!("activity-row-group-{id}"));
204 h_flex()
205 .id(ElementId::Name(format!("activity-row-{id}").into()))
206 .group(group.clone())
207 .mx(px(6.))
208 .px(px(6.))
209 .py(px(8.))
210 .gap(px(10.))
211 .items_start()
212 .rounded(px(6.))
213 .cursor_pointer()
214 .hover(|row| row.bg(theme::row_hover()))
215 .child(
216 div()
217 .relative()
218 .flex_none()
219 .size(px(AVATAR_SIZE))
220 .child(avatar)
221 .child(kind_marker(entry)),
222 )
223 .child(
224 v_flex()
225 .flex_1()
226 .min_w_0()
227 .gap(px(2.))
228 .child(
229 div()
230 .truncate()
231 .text_size(px(13.))
232 .font_weight(if unread {
233 FontWeight::SEMIBOLD
234 } else {
235 FontWeight::NORMAL
236 })
237 .text_color(if unread {
238 theme::text()
239 } else {
240 theme::text_soft()
241 })
242 .child(headline(entry, sidebar)),
243 )
244 .child(
245 div()
246 .truncate()
247 .text_size(px(12.))
248 .text_color(if unread {
249 theme::text_soft()
250 } else {
251 theme::text_muted()
252 })
253 .child(detail(entry)),
254 ),
255 )
256 .child(
257 v_flex()
258 .flex_none()
259 .items_end()
260 .gap(px(2.))
261 .child(
262 div()
263 .text_size(px(11.))
264 .text_color(theme::text_muted())
265 .child(format::list_time_label(entry.updated_at, today, offset)),
266 )
267 .child(status_slot(id, unread, group, activity)),
268 )
269 .on_click(move |_, _, cx| panel.update(cx, |this, cx| this.open(id, cx)))
270 .into_any_element()
271 }
272}
273
274fn status_slot(
275 id: i64,
276 unread: bool,
277 group: SharedString,
278 activity: Entity<ActivityCenter>,
279) -> Div {
280 let slot = div()
281 .h(px(MARK_READ_SIZE))
282 .flex()
283 .flex_none()
284 .items_center()
285 .justify_end();
286 if !unread {
287 return slot;
288 }
289 slot.child(
290 div()
291 .flex()
292 .group_hover(group.clone(), |wrapper| wrapper.hidden())
293 .child(dot(8.)),
294 )
295 .child(
296 div()
297 .id(ElementId::Name(format!("activity-mark-read-{id}").into()))
298 .hidden()
299 .group_hover(group, |button| button.flex())
300 .size(px(MARK_READ_SIZE))
301 .flex_none()
302 .items_center()
303 .justify_center()
304 .rounded(px(6.))
305 .border_1()
306 .border_color(theme::border_strong())
307 .bg(theme::surface_raised())
308 .cursor_pointer()
309 .child(icon(IconName::Check, 14., theme::text()))
310 .tooltip(|window, cx| Tooltip::new("Mark as read").build(window, cx))
311 .on_click(move |_, _, cx| {
312 cx.stop_propagation();
313 activity.update(cx, |activity, cx| activity.mark_read(id, cx));
314 }),
315 )
316}
317
318fn kind_marker(entry: &Entry) -> Div {
319 let marker = div()
320 .absolute()
321 .right(px(-4.))
322 .bottom(px(-4.))
323 .size(px(KIND_MARKER_SIZE))
324 .flex()
325 .items_center()
326 .justify_center()
327 .rounded_full()
328 .border_1()
329 .border_color(theme::surface())
330 .bg(theme::surface_raised())
331 .text_size(px(10.));
332 match entry.kind {
333 Kind::Messages => marker.child(icon(IconName::MessageCircle, 10., theme::text_muted())),
334 Kind::Mention => marker
335 .text_color(theme::accent_text())
336 .font_weight(FontWeight::BOLD)
337 .child("@"),
338 Kind::Reaction => marker.child(entry.glyphs.first().cloned().unwrap_or_default()),
339 }
340}
341
342fn headline(entry: &Entry, sidebar: &Sidebar) -> String {
343 let names = entry.names();
344 let verb = match entry.kind {
345 Kind::Messages => names,
346 Kind::Mention => format!("{names} mentioned you"),
347 Kind::Reaction => format!("{names} reacted"),
348 };
349 match conversation_label(sidebar, &entry.conversation_id) {
350 Some(label) => format!("{verb} in {label}"),
351 None => verb,
352 }
353}
354
355fn detail(entry: &Entry) -> String {
356 match entry.kind {
357 Kind::Messages if entry.count > 1 => {
358 format!("{} new messages: {}", entry.count, entry.preview)
359 }
360 Kind::Reaction => format!("{} to: {}", entry.glyphs.join(" "), entry.preview),
361 _ => entry.preview.clone(),
362 }
363}
364
365fn conversation_label(sidebar: &Sidebar, conversation_id: &str) -> Option<String> {
366 if let Some(chat) = sidebar.chats.iter().find(|chat| chat.id == conversation_id) {
367 return (!is_one_on_one(chat)).then(|| chat_title(chat));
368 }
369 sidebar.teams.iter().find_map(|team| {
370 team.channels
371 .iter()
372 .find(|channel| channel.id == conversation_id)
373 .map(|channel| format!("{} > {}", team.team.name, channel.name))
374 })
375}
376
377fn date_header(label: &'static str) -> AnyElement {
378 div()
379 .px(px(12.))
380 .pt(px(8.))
381 .pb(px(4.))
382 .text_size(px(11.))
383 .font_weight(FontWeight::SEMIBOLD)
384 .text_color(theme::text_muted())
385 .child(label)
386 .into_any_element()
387}
388
389impl Render for ActivityPanel {
390 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
391 let now: DateTime<Local> = Local::now();
392 let (today, offset) = (now.date_naive(), now.offset().fix());
393 let panel = cx.entity();
394 let app = self.app.clone();
395 let activity = self.activity.clone();
396 let state = app.read(cx);
397 let mut rows: Vec<AnyElement> = Vec::new();
398 for section in activity
399 .read(cx)
400 .feed()
401 .sections(self.filter, today, offset)
402 {
403 rows.push(date_header(section.bucket.label()));
404 for entry in section.entries {
405 rows.push(self.row(
406 entry,
407 &state.sidebar,
408 &state.directory,
409 today,
410 offset,
411 panel.clone(),
412 ));
413 }
414 }
415 if rows.is_empty() {
416 rows.push(
417 div()
418 .py(px(32.))
419 .flex()
420 .justify_center()
421 .text_size(px(13.))
422 .text_color(theme::text_muted())
423 .child(self.filter.empty_label())
424 .into_any_element(),
425 );
426 }
427 let max_height = f32::from(window.viewport_size().height) * PANEL_MAX_HEIGHT_RATIO;
428 div()
429 .id("activity-layer")
430 .absolute()
431 .top(px(TITLE_BAR_HEIGHT))
432 .left_0()
433 .right_0()
434 .bottom_0()
435 .occlude()
436 .flex()
437 .justify_end()
438 .items_start()
439 .p(px(PANEL_MARGIN))
440 .on_click(cx.listener(|_, _, _, cx| cx.emit(ActivityPanelEvent::Close)))
441 .child(
442 v_flex()
443 .id("activity-card")
444 .track_focus(&self.focus_handle)
445 .w(px(PANEL_WIDTH))
446 .max_h(px(max_height))
447 .rounded(px(10.))
448 .border_1()
449 .border_color(theme::border_strong())
450 .bg(theme::surface())
451 .shadow_lg()
452 .overflow_hidden()
453 .occlude()
454 .on_click(|_, _, cx| cx.stop_propagation())
455 .on_key_down(cx.listener(Self::on_key_down))
456 .child(self.header(cx))
457 .child(
458 v_flex()
459 .id("activity-list")
460 .flex_1()
461 .min_h_0()
462 .py(px(4.))
463 .overflow_y_scroll()
464 .track_scroll(&self.scroll)
465 .children(rows),
466 ),
467 )
468 }
469}
470
471#[cfg(test)]
472mod tests {
473 use chrono::{TimeZone, Utc};
474 use store::{ChatRecord, Sidebar};
475
476 use super::{detail, headline};
477 use crate::activity::{Actor, Entry, Kind};
478
479 fn entry(kind: Kind, count: u32) -> Entry {
480 Entry {
481 id: 1,
482 conversation_id: "chat".into(),
483 kind,
484 message_id: "m1".into(),
485 actors: vec![Actor {
486 user_id: Some("u1".into()),
487 name: "Anna".into(),
488 }],
489 preview: "hello".into(),
490 glyphs: vec!["A".into(), "B".into()],
491 count,
492 updated_at: Utc.with_ymd_and_hms(2026, 10, 8, 9, 0, 0).unwrap(),
493 read: false,
494 }
495 }
496
497 #[test]
498 fn detail_follows_the_kind() {
499 assert_eq!(detail(&entry(Kind::Messages, 1)), "hello");
500 assert_eq!(detail(&entry(Kind::Messages, 3)), "3 new messages: hello");
501 assert_eq!(detail(&entry(Kind::Mention, 1)), "hello");
502 assert_eq!(detail(&entry(Kind::Reaction, 1)), "A B to: hello");
503 }
504
505 #[test]
506 fn headline_names_the_actor_and_skips_direct_chat_context() {
507 let sidebar = Sidebar {
508 chats: vec![ChatRecord {
509 id: "chat".into(),
510 kind: "oneOnOne".into(),
511 ..Default::default()
512 }],
513 teams: Vec::new(),
514 };
515 assert_eq!(headline(&entry(Kind::Messages, 1), &sidebar), "Anna");
516 assert_eq!(
517 headline(&entry(Kind::Reaction, 1), &sidebar),
518 "Anna reacted"
519 );
520 assert_eq!(
521 headline(&entry(Kind::Mention, 1), &sidebar),
522 "Anna mentioned you"
523 );
524 }
525}