BlueGit

ict rusty-teams

Files

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

activity_panel.rs

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