| 1 | use std::collections::{HashMap, HashSet}; |
| 2 | use std::ops::Range; |
| 3 | use std::rc::Rc; |
| 4 | |
| 5 | use chrono::{DateTime, Local, NaiveDate, Offset, TimeZone, Utc}; |
| 6 | use gpui_kit::assets::IconName; |
| 7 | use gpui_kit::component::{h_flex, tooltip::Tooltip, v_flex}; |
| 8 | use gpui_kit::prelude::FluentBuilder as _; |
| 9 | use gpui_kit::*; |
| 10 | use store::Sidebar; |
| 11 | |
| 12 | use super::avatar::person_avatar; |
| 13 | use super::scroll_edge::{scroll_offset, with_top_edge}; |
| 14 | use super::sidebar::SIDEBAR_SCROLL_DIVIDER; |
| 15 | use super::widgets::{Corner, corner_badge, dot, icon}; |
| 16 | use crate::activity::{ActivityCenter, Entry, Filter, Kind}; |
| 17 | use crate::agent_api::{ |
| 18 | self, |
| 19 | pending::{PendingKind, PendingProposal, conversation_label as proposal_conversation_label}, |
| 20 | }; |
| 21 | use crate::app_state::{AppState, chat_title}; |
| 22 | use crate::chat_filter::{FEWER_FILTERS, MORE_FILTERS}; |
| 23 | use crate::data::is_one_on_one; |
| 24 | use crate::format; |
| 25 | use crate::people::resolve_names; |
| 26 | use crate::theme; |
| 27 | |
| 28 | const AVATAR_SIZE: f32 = 32.; |
| 29 | const KIND_MARKER_SIZE: f32 = 16.; |
| 30 | const BUTTON_SIZE: f32 = 32.; |
| 31 | const MARK_READ_SIZE: f32 = 20.; |
| 32 | const ROW_HEIGHT: f32 = 58.; |
| 33 | const EVENT_LINE_HEIGHT: f32 = 16.; |
| 34 | const HEADER_HEIGHT: f32 = 30.; |
| 35 | const EMPTY_HEIGHT: f32 = 84.; |
| 36 | const FALLBACK_VIEWPORT: f32 = 900.; |
| 37 | const VIEWPORT_MARGIN: f32 = 300.; |
| 38 | const FILTER_KEY: &str = "activity_filter"; |
| 39 | const PROPOSALS_LABEL: &str = "Agent proposals"; |
| 40 | pub const ACTIVITY_PROPOSALS_TAG: &str = "shell/islands/sidebar/activity-proposals"; |
| 41 | |
| 42 | pub enum ActivityPanelEvent { |
| 43 | OpenSettings, |
| 44 | Open { |
| 45 | conversation_id: String, |
| 46 | message_id: String, |
| 47 | link: Option<String>, |
| 48 | }, |
| 49 | } |
| 50 | |
| 51 | pub struct ActivityPanel { |
| 52 | app: Entity<AppState>, |
| 53 | activity: Entity<ActivityCenter>, |
| 54 | filter: Filter, |
| 55 | more_filters_open: bool, |
| 56 | scroll: ScrollHandle, |
| 57 | resolved_names: HashMap<String, String>, |
| 58 | looked_up: HashSet<String>, |
| 59 | _observations: [Subscription; 2], |
| 60 | } |
| 61 | |
| 62 | impl EventEmitter<ActivityPanelEvent> for ActivityPanel {} |
| 63 | |
| 64 | impl ActivityPanel { |
| 65 | pub fn activity(&self) -> Entity<ActivityCenter> { |
| 66 | self.activity.clone() |
| 67 | } |
| 68 | |
| 69 | pub fn new( |
| 70 | app: Entity<AppState>, |
| 71 | activity: Entity<ActivityCenter>, |
| 72 | cx: &mut Context<Self>, |
| 73 | ) -> Self { |
| 74 | let observations = [ |
| 75 | cx.observe(&app, |_, _, cx| cx.notify()), |
| 76 | cx.observe(&activity, |_, _, cx| cx.notify()), |
| 77 | ]; |
| 78 | let filter = app |
| 79 | .read(cx) |
| 80 | .store |
| 81 | .meta(FILTER_KEY) |
| 82 | .ok() |
| 83 | .flatten() |
| 84 | .and_then(|key| Filter::from_key(&key)) |
| 85 | .unwrap_or_default(); |
| 86 | ActivityPanel { |
| 87 | app, |
| 88 | activity, |
| 89 | filter, |
| 90 | more_filters_open: false, |
| 91 | scroll: ScrollHandle::new(), |
| 92 | resolved_names: HashMap::new(), |
| 93 | looked_up: HashSet::new(), |
| 94 | _observations: observations, |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | fn open(&mut self, id: &str, cx: &mut Context<Self>) { |
| 99 | let Some(entry) = self.activity.read(cx).feed().get(id) else { |
| 100 | return; |
| 101 | }; |
| 102 | let stored_reply = entry.reply_id.clone().filter(|reply_id| { |
| 103 | self.app |
| 104 | .read(cx) |
| 105 | .store |
| 106 | .messages_by_id(&entry.conversation_id, std::slice::from_ref(reply_id)) |
| 107 | .is_ok_and(|found| !found.is_empty()) |
| 108 | }); |
| 109 | let event = ActivityPanelEvent::Open { |
| 110 | conversation_id: entry.conversation_id.clone(), |
| 111 | message_id: stored_reply.unwrap_or_else(|| entry.message_id.clone()), |
| 112 | link: entry.link.clone(), |
| 113 | }; |
| 114 | self.activity |
| 115 | .update(cx, |activity, cx| activity.mark_read(id, cx)); |
| 116 | cx.emit(event); |
| 117 | } |
| 118 | |
| 119 | fn filter_pill(&self, filter: Filter, cx: &mut Context<Self>) -> impl IntoElement { |
| 120 | let selected = filter == self.filter; |
| 121 | div() |
| 122 | .id(ElementId::Name( |
| 123 | format!("activity-filter-{}", filter.label()).into(), |
| 124 | )) |
| 125 | .h(px(24.)) |
| 126 | .px(px(12.)) |
| 127 | .flex() |
| 128 | .flex_none() |
| 129 | .items_center() |
| 130 | .rounded_full() |
| 131 | .cursor_pointer() |
| 132 | .text_size(px(12.)) |
| 133 | .when(selected, |pill| { |
| 134 | pill.bg(theme::accent()).text_color(theme::on_accent()) |
| 135 | }) |
| 136 | .when(!selected, |pill| { |
| 137 | pill.bg(theme::surface_raised()) |
| 138 | .text_color(theme::text_soft()) |
| 139 | .hover(|pill| pill.bg(theme::border_strong())) |
| 140 | }) |
| 141 | .child(filter.label()) |
| 142 | .on_click(cx.listener(move |this, _, _, cx| { |
| 143 | this.filter = filter; |
| 144 | let _ = this.app.read(cx).store.set_meta(FILTER_KEY, filter.key()); |
| 145 | cx.notify(); |
| 146 | })) |
| 147 | } |
| 148 | |
| 149 | fn more_filters_button(&self, cx: &mut Context<Self>) -> impl IntoElement { |
| 150 | let open = self.more_filters_open; |
| 151 | let label = if open { FEWER_FILTERS } else { MORE_FILTERS }; |
| 152 | div() |
| 153 | .id("activity-filter-more") |
| 154 | .size(px(24.)) |
| 155 | .flex() |
| 156 | .flex_none() |
| 157 | .items_center() |
| 158 | .justify_center() |
| 159 | .rounded_full() |
| 160 | .cursor_pointer() |
| 161 | .bg(theme::surface_raised()) |
| 162 | .hover(|pill| pill.bg(theme::border_strong())) |
| 163 | .tooltip(move |window, cx| Tooltip::new(label).build(window, cx)) |
| 164 | .child(icon( |
| 165 | if open { |
| 166 | IconName::ChevronUp |
| 167 | } else { |
| 168 | IconName::ChevronDown |
| 169 | }, |
| 170 | 14., |
| 171 | theme::text_soft(), |
| 172 | )) |
| 173 | .on_click(cx.listener(|this, _, _, cx| { |
| 174 | this.more_filters_open = !this.more_filters_open; |
| 175 | cx.notify(); |
| 176 | })) |
| 177 | } |
| 178 | |
| 179 | fn header(&self, cx: &mut Context<Self>) -> Div { |
| 180 | let overflow_active = Filter::OVERFLOW.contains(&self.filter); |
| 181 | let mut pills: Vec<AnyElement> = Filter::PRIMARY |
| 182 | .into_iter() |
| 183 | .map(|filter| self.filter_pill(filter, cx).into_any_element()) |
| 184 | .collect(); |
| 185 | if self.more_filters_open || overflow_active { |
| 186 | pills.extend( |
| 187 | Filter::OVERFLOW |
| 188 | .into_iter() |
| 189 | .map(|filter| self.filter_pill(filter, cx).into_any_element()), |
| 190 | ); |
| 191 | } |
| 192 | if !overflow_active { |
| 193 | pills.push(self.more_filters_button(cx).into_any_element()); |
| 194 | } |
| 195 | v_flex() |
| 196 | .flex_none() |
| 197 | .child( |
| 198 | h_flex() |
| 199 | .h(px(52.)) |
| 200 | .pl(px(16.)) |
| 201 | .pr(px(12.)) |
| 202 | .gap(px(4.)) |
| 203 | .items_center() |
| 204 | .child( |
| 205 | div() |
| 206 | .flex_1() |
| 207 | .text_size(px(16.)) |
| 208 | .font_weight(FontWeight::BOLD) |
| 209 | .text_color(theme::text()) |
| 210 | .child("Activity"), |
| 211 | ) |
| 212 | .child( |
| 213 | div() |
| 214 | .id("activity-mark-all-read") |
| 215 | .px(px(8.)) |
| 216 | .h(px(BUTTON_SIZE)) |
| 217 | .flex() |
| 218 | .items_center() |
| 219 | .rounded(px(8.)) |
| 220 | .cursor_pointer() |
| 221 | .text_size(px(12.)) |
| 222 | .text_color(theme::accent_text()) |
| 223 | .hover(|button| button.bg(theme::surface_raised())) |
| 224 | .child("Mark all as read") |
| 225 | .on_click(cx.listener(|this, _, _, cx| { |
| 226 | this.activity |
| 227 | .update(cx, |activity, cx| activity.mark_all_read(cx)); |
| 228 | })), |
| 229 | ) |
| 230 | .child( |
| 231 | div() |
| 232 | .id("activity-settings") |
| 233 | .size(px(BUTTON_SIZE)) |
| 234 | .flex() |
| 235 | .flex_none() |
| 236 | .items_center() |
| 237 | .justify_center() |
| 238 | .rounded(px(8.)) |
| 239 | .cursor_pointer() |
| 240 | .hover(|button| button.bg(theme::surface_raised())) |
| 241 | .child(icon(IconName::Settings, 16., theme::text_muted())) |
| 242 | .on_click(cx.listener(|_, _, _, cx| { |
| 243 | cx.emit(ActivityPanelEvent::OpenSettings); |
| 244 | })), |
| 245 | ), |
| 246 | ) |
| 247 | .child( |
| 248 | h_flex() |
| 249 | .px(px(16.)) |
| 250 | .pb(px(12.)) |
| 251 | .flex_wrap() |
| 252 | .gap(px(6.)) |
| 253 | .children(pills), |
| 254 | ) |
| 255 | } |
| 256 | |
| 257 | fn row( |
| 258 | &self, |
| 259 | entry: &Entry, |
| 260 | sidebar: &Sidebar, |
| 261 | directory: &crate::data::Directory, |
| 262 | today: NaiveDate, |
| 263 | offset: chrono::FixedOffset, |
| 264 | panel: Entity<ActivityPanel>, |
| 265 | ) -> AnyElement { |
| 266 | let activity = self.activity.clone(); |
| 267 | let id = entry.id.clone(); |
| 268 | let actor = entry.latest_actor(); |
| 269 | let avatar = person_avatar( |
| 270 | directory, |
| 271 | actor.and_then(|actor| actor.user_id.as_deref()), |
| 272 | actor.map_or("", |actor| actor.display_name(&self.resolved_names)), |
| 273 | AVATAR_SIZE, |
| 274 | ); |
| 275 | let group = SharedString::from(format!("activity-row-group-{id}")); |
| 276 | row_frame(RowParts { |
| 277 | element_id: format!("activity-row-{id}"), |
| 278 | avatar: avatar.into_any_element(), |
| 279 | marker: kind_marker(entry, group.clone()), |
| 280 | group, |
| 281 | headline: headline(entry, sidebar, &self.resolved_names), |
| 282 | detail: detail(entry), |
| 283 | event_line: entry |
| 284 | .event_range |
| 285 | .map(|(start, end)| event_line(start, end, &Local)), |
| 286 | unread: !entry.read, |
| 287 | time: format::list_time_label(entry.updated_at, today, offset), |
| 288 | mark_read: Rc::new({ |
| 289 | let id = id.clone(); |
| 290 | move |cx: &mut App| activity.update(cx, |activity, cx| activity.mark_read(&id, cx)) |
| 291 | }), |
| 292 | }) |
| 293 | .on_click(move |_, _, cx| panel.update(cx, |this, cx| this.open(&id, cx))) |
| 294 | .into_any_element() |
| 295 | } |
| 296 | |
| 297 | fn proposal_row( |
| 298 | &self, |
| 299 | pending: &PendingProposal, |
| 300 | sidebar: &Sidebar, |
| 301 | today: NaiveDate, |
| 302 | offset: chrono::FixedOffset, |
| 303 | panel: Entity<ActivityPanel>, |
| 304 | ) -> AnyElement { |
| 305 | let group = SharedString::from(format!("activity-proposal-group-{}", pending.id)); |
| 306 | let label = proposal_conversation_label(sidebar, &pending.conversation_id); |
| 307 | let proposal_id = pending.id.clone(); |
| 308 | let opened = pending.clone(); |
| 309 | row_frame(RowParts { |
| 310 | element_id: format!("activity-proposal-{}", pending.id), |
| 311 | avatar: agent_avatar().into_any_element(), |
| 312 | marker: proposal_marker(pending, group.clone()), |
| 313 | group, |
| 314 | headline: pending.headline(label.as_deref()), |
| 315 | detail: pending.detail(), |
| 316 | event_line: None, |
| 317 | unread: !pending.seen, |
| 318 | time: format::list_time_label(pending.created_at, today, offset), |
| 319 | mark_read: Rc::new(move |cx: &mut App| agent_api::mark_proposal_seen(&proposal_id, cx)), |
| 320 | }) |
| 321 | .on_click(move |_, _, cx| panel.update(cx, |this, cx| this.open_proposal(&opened, cx))) |
| 322 | .into_any_element() |
| 323 | } |
| 324 | |
| 325 | fn open_proposal(&mut self, pending: &PendingProposal, cx: &mut Context<Self>) { |
| 326 | agent_api::mark_proposal_seen(&pending.id, cx); |
| 327 | cx.emit(ActivityPanelEvent::Open { |
| 328 | conversation_id: pending.conversation_id.clone(), |
| 329 | message_id: pending.jump_message_id().to_owned(), |
| 330 | link: None, |
| 331 | }); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | struct RowParts { |
| 336 | element_id: String, |
| 337 | group: SharedString, |
| 338 | avatar: AnyElement, |
| 339 | marker: Div, |
| 340 | headline: String, |
| 341 | detail: String, |
| 342 | event_line: Option<String>, |
| 343 | unread: bool, |
| 344 | time: String, |
| 345 | mark_read: MarkRead, |
| 346 | } |
| 347 | |
| 348 | type MarkRead = Rc<dyn Fn(&mut App)>; |
| 349 | |
| 350 | fn row_frame(parts: RowParts) -> Stateful<Div> { |
| 351 | let RowParts { |
| 352 | element_id, |
| 353 | group, |
| 354 | avatar, |
| 355 | marker, |
| 356 | headline, |
| 357 | detail, |
| 358 | event_line, |
| 359 | unread, |
| 360 | time, |
| 361 | mark_read, |
| 362 | } = parts; |
| 363 | let height = ROW_HEIGHT + event_line.as_ref().map_or(0., |_| EVENT_LINE_HEIGHT); |
| 364 | let selector = element_id.clone(); |
| 365 | h_flex() |
| 366 | .id(ElementId::Name(element_id.clone().into())) |
| 367 | .debug_selector(move || selector) |
| 368 | .group(group.clone()) |
| 369 | .h(px(height)) |
| 370 | .flex_none() |
| 371 | .mx(px(6.)) |
| 372 | .px(px(10.)) |
| 373 | .py(px(8.)) |
| 374 | .gap(px(12.)) |
| 375 | .items_start() |
| 376 | .rounded(px(8.)) |
| 377 | .cursor_pointer() |
| 378 | .hover(|row| row.bg(theme::row_hover())) |
| 379 | .child( |
| 380 | div() |
| 381 | .relative() |
| 382 | .flex_none() |
| 383 | .size(px(AVATAR_SIZE)) |
| 384 | .child(avatar) |
| 385 | .child(marker), |
| 386 | ) |
| 387 | .child( |
| 388 | v_flex() |
| 389 | .flex_1() |
| 390 | .min_w_0() |
| 391 | .gap(px(2.)) |
| 392 | .child( |
| 393 | div() |
| 394 | .truncate() |
| 395 | .text_size(px(13.)) |
| 396 | .font_weight(if unread { |
| 397 | FontWeight::SEMIBOLD |
| 398 | } else { |
| 399 | FontWeight::NORMAL |
| 400 | }) |
| 401 | .text_color(if unread { |
| 402 | theme::text() |
| 403 | } else { |
| 404 | theme::text_soft() |
| 405 | }) |
| 406 | .child(headline), |
| 407 | ) |
| 408 | .child( |
| 409 | div() |
| 410 | .truncate() |
| 411 | .text_size(px(12.)) |
| 412 | .text_color(if unread { |
| 413 | theme::text_soft() |
| 414 | } else { |
| 415 | theme::text_muted() |
| 416 | }) |
| 417 | .child(detail), |
| 418 | ) |
| 419 | .children(event_line.map(|line| { |
| 420 | div() |
| 421 | .truncate() |
| 422 | .text_size(px(12.)) |
| 423 | .text_color(theme::text_muted()) |
| 424 | .child(line) |
| 425 | })), |
| 426 | ) |
| 427 | .child( |
| 428 | v_flex() |
| 429 | .flex_none() |
| 430 | .items_end() |
| 431 | .child( |
| 432 | div() |
| 433 | .text_size(px(11.)) |
| 434 | .text_color(theme::text_muted()) |
| 435 | .child(time), |
| 436 | ) |
| 437 | .child(status_slot(element_id, unread, group, mark_read)), |
| 438 | ) |
| 439 | } |
| 440 | |
| 441 | fn agent_avatar() -> Div { |
| 442 | div() |
| 443 | .size(px(AVATAR_SIZE)) |
| 444 | .flex() |
| 445 | .items_center() |
| 446 | .justify_center() |
| 447 | .rounded_full() |
| 448 | .bg(theme::accent_soft()) |
| 449 | .child(icon(IconName::Bot, 16., theme::accent_text())) |
| 450 | } |
| 451 | |
| 452 | fn admits_proposal(filter: Filter, pending: &PendingProposal) -> bool { |
| 453 | match filter { |
| 454 | Filter::All => true, |
| 455 | Filter::Mentions => false, |
| 456 | Filter::Unread => !pending.seen, |
| 457 | Filter::Replies | Filter::Reactions | Filter::MissedCalls => false, |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | fn status_slot(element_id: String, unread: bool, group: SharedString, mark_read: MarkRead) -> Div { |
| 462 | let slot = div() |
| 463 | .h(px(MARK_READ_SIZE)) |
| 464 | .flex() |
| 465 | .flex_none() |
| 466 | .items_center() |
| 467 | .justify_end(); |
| 468 | if !unread { |
| 469 | return slot; |
| 470 | } |
| 471 | // Toggle visibility, never display: gpui reads hover in prepaint and paint from different frames. |
| 472 | slot.relative() |
| 473 | .w(px(MARK_READ_SIZE)) |
| 474 | .child( |
| 475 | div() |
| 476 | .flex() |
| 477 | .group_hover(group.clone(), |wrapper| wrapper.invisible()) |
| 478 | .mr(px(8.)) |
| 479 | .child(dot(8.)), |
| 480 | ) |
| 481 | .child( |
| 482 | div() |
| 483 | .id(ElementId::Name(format!("{element_id}-mark-read").into())) |
| 484 | .absolute() |
| 485 | .top_0() |
| 486 | .right_0() |
| 487 | .invisible() |
| 488 | .group_hover(group, |button| button.visible()) |
| 489 | .size(px(MARK_READ_SIZE)) |
| 490 | .flex() |
| 491 | .items_center() |
| 492 | .justify_center() |
| 493 | .rounded(px(6.)) |
| 494 | .bg(theme::surface_raised()) |
| 495 | .cursor_pointer() |
| 496 | .child(icon(IconName::Check, 12., theme::text())) |
| 497 | .tooltip(|window, cx| Tooltip::new("Mark as read").build(window, cx)) |
| 498 | .on_click(move |_, _, cx| { |
| 499 | cx.stop_propagation(); |
| 500 | mark_read(cx); |
| 501 | }), |
| 502 | ) |
| 503 | } |
| 504 | |
| 505 | fn marker_frame(row_group: SharedString) -> Div { |
| 506 | corner_badge(Corner::BottomRight, 4.) |
| 507 | .size(px(KIND_MARKER_SIZE)) |
| 508 | .flex() |
| 509 | .items_center() |
| 510 | .justify_center() |
| 511 | .rounded_full() |
| 512 | .border_2() |
| 513 | .border_color(theme::surface()) |
| 514 | .group_hover(row_group, |marker| marker.border_color(theme::row_hover())) |
| 515 | .bg(theme::surface_raised()) |
| 516 | .text_size(px(10.)) |
| 517 | } |
| 518 | |
| 519 | fn proposal_marker(pending: &PendingProposal, row_group: SharedString) -> Div { |
| 520 | let marker = marker_frame(row_group); |
| 521 | match &pending.kind { |
| 522 | PendingKind::Reply { .. } => { |
| 523 | marker.child(icon(IconName::MessageCircle, 10., theme::text_muted())) |
| 524 | } |
| 525 | PendingKind::Reaction { glyph, .. } => marker.child(glyph.clone()), |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | fn kind_marker(entry: &Entry, row_group: SharedString) -> Div { |
| 530 | let marker = marker_frame(row_group); |
| 531 | let muted_icon = |name: IconName| icon(name, 10., theme::text_muted()); |
| 532 | match entry.kind { |
| 533 | Kind::Mention => marker |
| 534 | .text_color(theme::accent_text()) |
| 535 | .font_weight(FontWeight::BOLD) |
| 536 | .child("@"), |
| 537 | Kind::Reaction => match entry.glyphs.first() { |
| 538 | Some(glyph) => marker.child(glyph.clone()), |
| 539 | None => marker.child(muted_icon(IconName::Heart)), |
| 540 | }, |
| 541 | Kind::MissedCall => marker.child(icon(IconName::PhoneOff, 10., theme::red())), |
| 542 | Kind::Reply => marker.child(muted_icon(IconName::Reply)), |
| 543 | Kind::Call => marker.child(muted_icon(IconName::Phone)), |
| 544 | Kind::Posted => marker.child(muted_icon(IconName::MessageSquare)), |
| 545 | Kind::Team => marker.child(muted_icon(IconName::Users)), |
| 546 | Kind::App => marker.child(muted_icon(IconName::AppWindow)), |
| 547 | Kind::Other => marker.child(muted_icon(IconName::Bell)), |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | fn headline(entry: &Entry, sidebar: &Sidebar, resolved_names: &HashMap<String, String>) -> String { |
| 552 | let actor = entry |
| 553 | .latest_actor() |
| 554 | .map(|actor| actor.display_name(resolved_names).to_owned()) |
| 555 | .unwrap_or_default(); |
| 556 | let names = if entry.count > 1 && matches!(entry.kind, Kind::Reaction | Kind::Reply) { |
| 557 | format!("{actor} +{}", entry.count - 1) |
| 558 | } else { |
| 559 | actor.clone() |
| 560 | }; |
| 561 | let channel = channel_of(sidebar, &entry.conversation_id); |
| 562 | let verb = match (entry.kind, entry.subtype.as_str()) { |
| 563 | (Kind::Mention, "everyone") => format!("{names} mentioned Everyone"), |
| 564 | (Kind::Mention, "channel") => match channel { |
| 565 | Some((_, channel_name)) => format!("{names} mentioned {channel_name}"), |
| 566 | None => format!("{names} mentioned the channel"), |
| 567 | }, |
| 568 | (Kind::Mention, "team") => match channel { |
| 569 | Some((team_name, _)) => format!("{names} mentioned {team_name}"), |
| 570 | None => format!("{names} mentioned the team"), |
| 571 | }, |
| 572 | (Kind::Mention, _) => format!("{names} mentioned you"), |
| 573 | (Kind::Reply, _) => reply_headline(entry, &names), |
| 574 | (Kind::Reaction, _) => format!("{names} reacted to your message"), |
| 575 | (Kind::MissedCall, _) => format!("Missed call from {actor}"), |
| 576 | (Kind::Call, "voicemail") => format!("Voicemail from {actor}"), |
| 577 | (Kind::Call, "missedMeetingNudge") => format!("{actor} asked you to join a meeting"), |
| 578 | (Kind::Call, _) => actor, |
| 579 | (Kind::Posted, "channelReplyMessage") => format!("{names} replied"), |
| 580 | (Kind::Posted, _) => format!("{names} posted a new message"), |
| 581 | (Kind::Team, "promotedToTeamAdmin") => { |
| 582 | return format!("{actor} made you an owner of {}", team_label(entry, channel)); |
| 583 | } |
| 584 | (Kind::Team, _) => { |
| 585 | return format!("{actor} added you to {}", team_label(entry, channel)); |
| 586 | } |
| 587 | (Kind::App, _) => match &entry.title { |
| 588 | Some(title) => return title.clone(), |
| 589 | None => entry.source_label.clone().unwrap_or(actor), |
| 590 | }, |
| 591 | (Kind::Other, _) => actor, |
| 592 | }; |
| 593 | match conversation_label(sidebar, &entry.conversation_id) { |
| 594 | Some(label) => format!("{verb} in {label}"), |
| 595 | None => verb, |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | fn event_line<Zone: TimeZone>(start: DateTime<Utc>, end: DateTime<Utc>, zone: &Zone) -> String |
| 600 | where |
| 601 | Zone::Offset: std::fmt::Display, |
| 602 | { |
| 603 | let (start, end) = (start.with_timezone(zone), end.with_timezone(zone)); |
| 604 | let end_format = if end.date_naive() == start.date_naive() { |
| 605 | "%H:%M" |
| 606 | } else { |
| 607 | "%b %-d, %H:%M" |
| 608 | }; |
| 609 | format!("{} - {}", start.format("%b %-d, %H:%M"), end.format(end_format)) |
| 610 | } |
| 611 | |
| 612 | fn reply_headline(entry: &Entry, names: &str) -> String { |
| 613 | match entry.activity_type.as_str() { |
| 614 | "reply" => format!("{names} replied to your post"), |
| 615 | "replyInChat" => format!("{names} replied to message"), |
| 616 | _ => format!("{names} replied"), |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | fn team_label(entry: &Entry, channel: Option<(&str, &str)>) -> String { |
| 621 | channel |
| 622 | .map(|(team_name, _)| team_name.to_owned()) |
| 623 | .or_else(|| entry.source_label.clone()) |
| 624 | .unwrap_or_else(|| "a team".to_owned()) |
| 625 | } |
| 626 | |
| 627 | fn channel_of<'a>(sidebar: &'a Sidebar, conversation_id: &str) -> Option<(&'a str, &'a str)> { |
| 628 | sidebar.teams.iter().find_map(|team| { |
| 629 | team.channels |
| 630 | .iter() |
| 631 | .find(|channel| channel.id == conversation_id) |
| 632 | .map(|channel| (team.team.name.as_str(), channel.name.as_str())) |
| 633 | }) |
| 634 | } |
| 635 | |
| 636 | fn detail(entry: &Entry) -> String { |
| 637 | match entry.kind { |
| 638 | Kind::Reaction if !entry.glyphs.is_empty() => { |
| 639 | format!("{} to: {}", entry.glyphs.join(" "), entry.preview) |
| 640 | } |
| 641 | Kind::MissedCall if entry.count > 1 => format!("{} missed calls", entry.count), |
| 642 | _ => entry.preview.clone(), |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | fn conversation_label(sidebar: &Sidebar, conversation_id: &str) -> Option<String> { |
| 647 | if let Some(chat) = sidebar.chats.iter().find(|chat| chat.id == conversation_id) { |
| 648 | return (!is_one_on_one(chat)).then(|| chat_title(chat)); |
| 649 | } |
| 650 | sidebar.teams.iter().find_map(|team| { |
| 651 | team.channels |
| 652 | .iter() |
| 653 | .find(|channel| channel.id == conversation_id) |
| 654 | .map(|channel| format!("{} > {}", team.team.name, channel.name)) |
| 655 | }) |
| 656 | } |
| 657 | |
| 658 | fn group_header(label: &'static str) -> Div { |
| 659 | div() |
| 660 | .h(px(HEADER_HEIGHT - 4.)) |
| 661 | .mt(px(4.)) |
| 662 | .px(px(16.)) |
| 663 | .flex() |
| 664 | .flex_none() |
| 665 | .items_center() |
| 666 | .text_size(px(11.)) |
| 667 | .font_weight(FontWeight::SEMIBOLD) |
| 668 | .text_color(theme::text_muted()) |
| 669 | .child(label) |
| 670 | } |
| 671 | |
| 672 | enum Slot<'a> { |
| 673 | Proposals(Vec<PendingProposal>), |
| 674 | Header(&'static str), |
| 675 | Row(&'a Entry), |
| 676 | Empty, |
| 677 | } |
| 678 | |
| 679 | impl Slot<'_> { |
| 680 | fn height(&self) -> f32 { |
| 681 | match self { |
| 682 | Slot::Proposals(proposals) => HEADER_HEIGHT + proposals.len() as f32 * ROW_HEIGHT, |
| 683 | Slot::Header(_) => HEADER_HEIGHT, |
| 684 | Slot::Row(entry) if entry.event_range.is_some() => ROW_HEIGHT + EVENT_LINE_HEIGHT, |
| 685 | Slot::Row(_) => ROW_HEIGHT, |
| 686 | Slot::Empty => EMPTY_HEIGHT, |
| 687 | } |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | #[derive(Debug, PartialEq)] |
| 692 | struct VisibleSlots { |
| 693 | range: Range<usize>, |
| 694 | before: f32, |
| 695 | after: f32, |
| 696 | } |
| 697 | |
| 698 | fn visible_slots(heights: &[f32], from: f32, to: f32) -> VisibleSlots { |
| 699 | let mut position = 0.; |
| 700 | let mut first = heights.len(); |
| 701 | let mut last = heights.len(); |
| 702 | let mut before = 0.; |
| 703 | for (index, height) in heights.iter().enumerate() { |
| 704 | let end = position + height; |
| 705 | if first == heights.len() && end >= from { |
| 706 | first = index; |
| 707 | before = position; |
| 708 | } |
| 709 | if position > to { |
| 710 | last = index; |
| 711 | break; |
| 712 | } |
| 713 | position = end; |
| 714 | } |
| 715 | if first == heights.len() { |
| 716 | before = position; |
| 717 | } |
| 718 | let last = last.max(first); |
| 719 | let after = heights[last..].iter().sum(); |
| 720 | VisibleSlots { |
| 721 | range: first..last, |
| 722 | before, |
| 723 | after, |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | impl Render for ActivityPanel { |
| 728 | fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 729 | let now: DateTime<Local> = Local::now(); |
| 730 | let (today, offset) = (now.date_naive(), now.offset().fix()); |
| 731 | let panel = cx.entity(); |
| 732 | let app = self.app.clone(); |
| 733 | let activity = self.activity.clone(); |
| 734 | let state = app.read(cx); |
| 735 | let sections = activity |
| 736 | .read(cx) |
| 737 | .feed() |
| 738 | .sections(self.filter, today, offset); |
| 739 | let mut slots: Vec<Slot> = Vec::new(); |
| 740 | let proposals: Vec<PendingProposal> = agent_api::pending_proposals(cx) |
| 741 | .into_iter() |
| 742 | .filter(|pending| admits_proposal(self.filter, pending)) |
| 743 | .collect(); |
| 744 | if !proposals.is_empty() { |
| 745 | slots.push(Slot::Proposals(proposals)); |
| 746 | } |
| 747 | for section in §ions { |
| 748 | slots.push(Slot::Header(section.bucket.label())); |
| 749 | slots.extend(section.entries.iter().map(|entry| Slot::Row(entry))); |
| 750 | } |
| 751 | if slots.is_empty() { |
| 752 | slots.push(Slot::Empty); |
| 753 | } |
| 754 | let heights: Vec<f32> = slots.iter().map(Slot::height).collect(); |
| 755 | let top = -f32::from(scroll_offset(&self.scroll).y); |
| 756 | let measured = f32::from(self.scroll.bounds().size.height); |
| 757 | let height = if measured > 1. { |
| 758 | measured |
| 759 | } else { |
| 760 | f32::from(window.viewport_size().height).max(FALLBACK_VIEWPORT) |
| 761 | }; |
| 762 | let visible = visible_slots(&heights, top - VIEWPORT_MARGIN, top + height + VIEWPORT_MARGIN); |
| 763 | let visible_slots = &slots[visible.range.clone()]; |
| 764 | let unresolved: Vec<String> = visible_slots |
| 765 | .iter() |
| 766 | .filter_map(|slot| match slot { |
| 767 | Slot::Row(entry) => Some(entry.unresolved_user_ids()), |
| 768 | _ => None, |
| 769 | }) |
| 770 | .flatten() |
| 771 | .filter(|user_id| !self.looked_up.contains(*user_id)) |
| 772 | .cloned() |
| 773 | .collect(); |
| 774 | if !unresolved.is_empty() { |
| 775 | self.looked_up.extend(unresolved.iter().cloned()); |
| 776 | self.resolved_names |
| 777 | .extend(resolve_names(state, &unresolved)); |
| 778 | } |
| 779 | let mut rows: Vec<AnyElement> = Vec::new(); |
| 780 | if visible.before > 0. { |
| 781 | rows.push(div().h(px(visible.before)).flex_none().into_any_element()); |
| 782 | } |
| 783 | for slot in visible_slots { |
| 784 | rows.push(match slot { |
| 785 | Slot::Proposals(proposals) => v_flex() |
| 786 | .debug_selector(|| ACTIVITY_PROPOSALS_TAG.into()) |
| 787 | .child(group_header(PROPOSALS_LABEL)) |
| 788 | .children(proposals.iter().map(|pending| { |
| 789 | self.proposal_row(pending, &state.sidebar, today, offset, panel.clone()) |
| 790 | })) |
| 791 | .into_any_element(), |
| 792 | Slot::Header(label) => group_header(label).into_any_element(), |
| 793 | Slot::Row(entry) => self.row( |
| 794 | entry, |
| 795 | &state.sidebar, |
| 796 | &state.directory, |
| 797 | today, |
| 798 | offset, |
| 799 | panel.clone(), |
| 800 | ), |
| 801 | Slot::Empty => div() |
| 802 | .h(px(EMPTY_HEIGHT)) |
| 803 | .flex() |
| 804 | .flex_none() |
| 805 | .items_center() |
| 806 | .justify_center() |
| 807 | .text_size(px(13.)) |
| 808 | .text_color(theme::text_muted()) |
| 809 | .child(self.filter.empty_label()) |
| 810 | .into_any_element(), |
| 811 | }); |
| 812 | } |
| 813 | if visible.after > 0. { |
| 814 | rows.push(div().h(px(visible.after)).flex_none().into_any_element()); |
| 815 | } |
| 816 | let list = v_flex() |
| 817 | .id("activity-list") |
| 818 | .flex_1() |
| 819 | .min_h_0() |
| 820 | .pb(px(12.)) |
| 821 | .overflow_y_scroll() |
| 822 | .track_scroll(&self.scroll) |
| 823 | .children(rows); |
| 824 | v_flex() |
| 825 | .flex_1() |
| 826 | .min_h_0() |
| 827 | .child(self.header(cx)) |
| 828 | .child(with_top_edge(list, &self.scroll, SIDEBAR_SCROLL_DIVIDER)) |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | #[cfg(test)] |
| 833 | mod tests { |
| 834 | use std::collections::HashMap; |
| 835 | use std::sync::Arc; |
| 836 | |
| 837 | use chrono::{Local, Offset, TimeZone, Utc}; |
| 838 | use gpui_kit::{AppContext as _, Modifiers, TestAppContext}; |
| 839 | use store::{ChatRecord, Sidebar, Store}; |
| 840 | |
| 841 | use super::{ |
| 842 | ActivityPanel, ROW_HEIGHT, VIEWPORT_MARGIN, VisibleSlots, detail, headline, visible_slots, |
| 843 | }; |
| 844 | use crate::activity::{ActivityCenter, Actor, Entry, Filter, Kind}; |
| 845 | use crate::app_state::{AppState, Mode}; |
| 846 | |
| 847 | fn entry(kind: Kind, count: u32) -> Entry { |
| 848 | Entry { |
| 849 | id: "1".into(), |
| 850 | conversation_id: "chat".into(), |
| 851 | kind, |
| 852 | message_id: "m1".into(), |
| 853 | actors: vec![Actor { |
| 854 | user_id: Some("u1".into()), |
| 855 | name: "Anna".into(), |
| 856 | }], |
| 857 | preview: "hello".into(), |
| 858 | glyphs: vec!["A".into(), "B".into()], |
| 859 | count, |
| 860 | updated_at: Utc.with_ymd_and_hms(2026, 10, 8, 9, 0, 0).unwrap(), |
| 861 | ..Entry::default() |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | fn with(kind: Kind, activity_type: &str, subtype: &str, count: u32) -> Entry { |
| 866 | Entry { |
| 867 | activity_type: activity_type.into(), |
| 868 | subtype: subtype.into(), |
| 869 | ..entry(kind, count) |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | #[test] |
| 874 | fn only_rows_near_the_viewport_are_built() { |
| 875 | let heights = vec![ROW_HEIGHT; 300]; |
| 876 | let viewport = 900.; |
| 877 | let at_top = visible_slots(&heights, -VIEWPORT_MARGIN, viewport + VIEWPORT_MARGIN); |
| 878 | assert_eq!( |
| 879 | at_top, |
| 880 | VisibleSlots { |
| 881 | range: 0..21, |
| 882 | before: 0., |
| 883 | after: 279. * ROW_HEIGHT, |
| 884 | } |
| 885 | ); |
| 886 | let scrolled = visible_slots( |
| 887 | &heights, |
| 888 | 5000. - VIEWPORT_MARGIN, |
| 889 | 5000. + viewport + VIEWPORT_MARGIN, |
| 890 | ); |
| 891 | assert_eq!(scrolled.range, 81..107); |
| 892 | assert_eq!(scrolled.before, 81. * ROW_HEIGHT); |
| 893 | assert_eq!(scrolled.after, 193. * ROW_HEIGHT); |
| 894 | let past_the_end = visible_slots(&heights, 90_000., 91_000.); |
| 895 | assert_eq!(past_the_end.range, 300..300); |
| 896 | assert_eq!(past_the_end.before, 300. * ROW_HEIGHT); |
| 897 | assert_eq!(past_the_end.after, 0.); |
| 898 | } |
| 899 | |
| 900 | #[test] |
| 901 | fn detail_follows_the_kind() { |
| 902 | assert_eq!(detail(&entry(Kind::Mention, 1)), "hello"); |
| 903 | assert_eq!(detail(&entry(Kind::Reaction, 1)), "A B to: hello"); |
| 904 | assert_eq!(detail(&entry(Kind::MissedCall, 3)), "3 missed calls"); |
| 905 | assert_eq!(detail(&entry(Kind::Reply, 2)), "hello"); |
| 906 | } |
| 907 | |
| 908 | #[test] |
| 909 | fn event_line_shows_the_meeting_time_in_local_time() { |
| 910 | let start = chrono::DateTime::from_timestamp_millis(1793865600000).unwrap(); |
| 911 | let end = chrono::DateTime::from_timestamp_millis(1793869200000).unwrap(); |
| 912 | let berlin = chrono::FixedOffset::east_opt(3600).unwrap(); |
| 913 | assert_eq!(super::event_line(start, end, &berlin), "Nov 5, 09:00 - 10:00"); |
| 914 | let next_day = chrono::DateTime::from_timestamp_millis(1793955600000).unwrap(); |
| 915 | assert_eq!(super::event_line(start, next_day, &berlin), "Nov 5, 09:00 - Nov 6, 10:00"); |
| 916 | } |
| 917 | |
| 918 | #[test] |
| 919 | fn headline_uses_the_teams_activity_texts() { |
| 920 | let sidebar = Sidebar { |
| 921 | chats: vec![ChatRecord { |
| 922 | id: "chat".into(), |
| 923 | kind: "oneOnOne".into(), |
| 924 | ..Default::default() |
| 925 | }], |
| 926 | teams: Vec::new(), |
| 927 | }; |
| 928 | let cases = [ |
| 929 | (with(Kind::Mention, "mentionInChat", "person", 1), "Anna mentioned you"), |
| 930 | (with(Kind::Mention, "mentionInChat", "everyone", 1), "Anna mentioned Everyone"), |
| 931 | (with(Kind::Reaction, "reactionInChat", "like", 1), "Anna reacted to your message"), |
| 932 | (with(Kind::Reaction, "reactionInChat", "like", 3), "Anna +2 reacted to your message"), |
| 933 | (with(Kind::Reply, "reply", "", 1), "Anna replied to your post"), |
| 934 | (with(Kind::Reply, "replyToReply", "", 1), "Anna replied"), |
| 935 | (with(Kind::MissedCall, "call", "missedCall", 1), "Missed call from Anna"), |
| 936 | (with(Kind::Call, "call", "missedMeetingNudge", 1), "Anna asked you to join a meeting"), |
| 937 | (with(Kind::Posted, "follow", "channelNewMessage", 1), "Anna posted a new message"), |
| 938 | ]; |
| 939 | for (entry, expected) in cases { |
| 940 | assert_eq!(headline(&entry, &sidebar, &HashMap::new()), expected); |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | #[test] |
| 945 | fn headline_names_channel_and_team_mentions() { |
| 946 | let sidebar = Sidebar { |
| 947 | chats: Vec::new(), |
| 948 | teams: vec![store::SidebarTeam { |
| 949 | team: store::TeamRecord { |
| 950 | id: "team".into(), |
| 951 | name: "Platform".into(), |
| 952 | }, |
| 953 | channels: vec![store::ChannelRecord { |
| 954 | id: "chat".into(), |
| 955 | team_id: "team".into(), |
| 956 | name: "Releases".into(), |
| 957 | membership_type: None, |
| 958 | last_message_at: None, |
| 959 | unread: false, |
| 960 | }], |
| 961 | hidden: false, |
| 962 | hidden_channel_ids: Vec::new(), |
| 963 | notifications: Default::default(), |
| 964 | mentions: Default::default(), |
| 965 | }], |
| 966 | }; |
| 967 | assert_eq!( |
| 968 | headline(&with(Kind::Mention, "mention", "channel", 1), &sidebar, &HashMap::new()), |
| 969 | "Anna mentioned Releases in Platform > Releases" |
| 970 | ); |
| 971 | assert_eq!( |
| 972 | headline(&with(Kind::Mention, "mention", "team", 1), &sidebar, &HashMap::new()), |
| 973 | "Anna mentioned Platform in Platform > Releases" |
| 974 | ); |
| 975 | assert_eq!( |
| 976 | headline( |
| 977 | &with(Kind::Team, "teamMembershipChange", "addedToTeam", 1), |
| 978 | &sidebar, |
| 979 | &HashMap::new() |
| 980 | ), |
| 981 | "Anna added you to Platform" |
| 982 | ); |
| 983 | } |
| 984 | |
| 985 | #[test] |
| 986 | fn headline_resolves_an_unknown_actor() { |
| 987 | let mut stale = with(Kind::Reaction, "reactionInChat", "like", 1); |
| 988 | stale.actors[0].name = "Unknown".into(); |
| 989 | let sidebar = Sidebar::default(); |
| 990 | let resolved = HashMap::from([("u1".to_owned(), "Anna".to_owned())]); |
| 991 | assert_eq!( |
| 992 | headline(&stale, &sidebar, &resolved), |
| 993 | "Anna reacted to your message" |
| 994 | ); |
| 995 | } |
| 996 | |
| 997 | #[gpui_kit::test] |
| 998 | fn row_sliding_under_a_resting_mouse_draws(cx: &mut TestAppContext) { |
| 999 | cx.update(gpui_kit::init); |
| 1000 | let store = Arc::new(Store::open_in_memory().unwrap()); |
| 1001 | crate::demo::seed(&store); |
| 1002 | let mode = Mode { |
| 1003 | demo: true, |
| 1004 | read_only: false, |
| 1005 | demo_sync: None, |
| 1006 | }; |
| 1007 | let app = cx.update(|cx| { |
| 1008 | cx.new(|_| { |
| 1009 | let mut state = AppState::new(store, mode); |
| 1010 | crate::demo::seed_directory(&mut state); |
| 1011 | state |
| 1012 | }) |
| 1013 | }); |
| 1014 | let (panel, cx) = cx.add_window_view(|window, cx| { |
| 1015 | let activity = cx.new(|cx| ActivityCenter::new(app.clone(), window, cx)); |
| 1016 | let mut panel = ActivityPanel::new(app.clone(), activity, cx); |
| 1017 | panel.filter = Filter::Unread; |
| 1018 | panel |
| 1019 | }); |
| 1020 | cx.update(|window, cx| window.draw(cx).clear(cx)); |
| 1021 | let activity = cx.update(|_, cx| panel.read(cx).activity.clone()); |
| 1022 | let first_unread = cx.update(|_, cx| { |
| 1023 | let now = Local::now(); |
| 1024 | activity |
| 1025 | .read(cx) |
| 1026 | .feed() |
| 1027 | .sections(Filter::Unread, now.date_naive(), now.offset().fix())[0] |
| 1028 | .entries[0] |
| 1029 | .id |
| 1030 | .clone() |
| 1031 | }); |
| 1032 | let selector: &'static str = format!("activity-row-{first_unread}").leak(); |
| 1033 | let first_row = cx.debug_bounds(selector).unwrap(); |
| 1034 | cx.simulate_mouse_move(first_row.center(), None, Modifiers::none()); |
| 1035 | cx.update(|window, cx| window.draw(cx).clear(cx)); |
| 1036 | cx.update(|_, cx| activity.update(cx, |activity, cx| activity.mark_read(&first_unread, cx))); |
| 1037 | cx.update(|window, cx| window.draw(cx).clear(cx)); |
| 1038 | } |
| 1039 | } |