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