| 1 | use std::collections::{HashMap, HashSet}; |
| 2 | use std::rc::Rc; |
| 3 | |
| 4 | use chrono::{DateTime, Local, NaiveDate, Offset}; |
| 5 | use gpui_kit::assets::IconName; |
| 6 | use gpui_kit::component::{h_flex, tooltip::Tooltip, v_flex}; |
| 7 | use gpui_kit::prelude::FluentBuilder as _; |
| 8 | use gpui_kit::*; |
| 9 | use store::Sidebar; |
| 10 | |
| 11 | use super::avatar::person_avatar; |
| 12 | use super::scroll_edge::with_top_edge; |
| 13 | use super::sidebar::SIDEBAR_SCROLL_DIVIDER; |
| 14 | use super::widgets::{Corner, corner_badge, dot, icon}; |
| 15 | use crate::activity::{ActivityCenter, Entry, Filter, Kind}; |
| 16 | use crate::agent_api::{ |
| 17 | self, |
| 18 | pending::{PendingKind, PendingProposal, conversation_label as proposal_conversation_label}, |
| 19 | }; |
| 20 | use crate::app_state::{AppState, chat_title}; |
| 21 | use crate::data::is_one_on_one; |
| 22 | use crate::format; |
| 23 | use crate::people::resolve_names; |
| 24 | use crate::theme; |
| 25 | |
| 26 | const AVATAR_SIZE: f32 = 32.; |
| 27 | const KIND_MARKER_SIZE: f32 = 16.; |
| 28 | const BUTTON_SIZE: f32 = 32.; |
| 29 | const MARK_READ_SIZE: f32 = 20.; |
| 30 | const FILTER_KEY: &str = "activity_filter"; |
| 31 | const PROPOSALS_LABEL: &str = "Agent proposals"; |
| 32 | pub const ACTIVITY_PROPOSALS_TAG: &str = "shell/islands/sidebar/activity-proposals"; |
| 33 | |
| 34 | pub enum ActivityPanelEvent { |
| 35 | OpenSettings, |
| 36 | Open { |
| 37 | conversation_id: String, |
| 38 | message_id: String, |
| 39 | }, |
| 40 | } |
| 41 | |
| 42 | pub struct ActivityPanel { |
| 43 | app: Entity<AppState>, |
| 44 | activity: Entity<ActivityCenter>, |
| 45 | filter: Filter, |
| 46 | scroll: ScrollHandle, |
| 47 | resolved_names: HashMap<String, String>, |
| 48 | looked_up: HashSet<String>, |
| 49 | _observations: [Subscription; 2], |
| 50 | } |
| 51 | |
| 52 | impl EventEmitter<ActivityPanelEvent> for ActivityPanel {} |
| 53 | |
| 54 | impl ActivityPanel { |
| 55 | pub fn activity(&self) -> Entity<ActivityCenter> { |
| 56 | self.activity.clone() |
| 57 | } |
| 58 | |
| 59 | pub fn new( |
| 60 | app: Entity<AppState>, |
| 61 | activity: Entity<ActivityCenter>, |
| 62 | cx: &mut Context<Self>, |
| 63 | ) -> Self { |
| 64 | let observations = [ |
| 65 | cx.observe(&app, |_, _, cx| cx.notify()), |
| 66 | cx.observe(&activity, |_, _, cx| cx.notify()), |
| 67 | ]; |
| 68 | let filter = app |
| 69 | .read(cx) |
| 70 | .store |
| 71 | .meta(FILTER_KEY) |
| 72 | .ok() |
| 73 | .flatten() |
| 74 | .and_then(|key| Filter::from_key(&key)) |
| 75 | .unwrap_or_default(); |
| 76 | ActivityPanel { |
| 77 | app, |
| 78 | activity, |
| 79 | filter, |
| 80 | scroll: ScrollHandle::new(), |
| 81 | resolved_names: HashMap::new(), |
| 82 | looked_up: HashSet::new(), |
| 83 | _observations: observations, |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | fn open(&mut self, id: &str, cx: &mut Context<Self>) { |
| 88 | let Some(entry) = self.activity.read(cx).feed().get(id) else { |
| 89 | return; |
| 90 | }; |
| 91 | let event = ActivityPanelEvent::Open { |
| 92 | conversation_id: entry.conversation_id.clone(), |
| 93 | message_id: entry.message_id.clone(), |
| 94 | }; |
| 95 | self.activity |
| 96 | .update(cx, |activity, cx| activity.mark_read(id, cx)); |
| 97 | cx.emit(event); |
| 98 | } |
| 99 | |
| 100 | fn header(&self, cx: &mut Context<Self>) -> Div { |
| 101 | let pills = Filter::ALL.into_iter().map(|filter| { |
| 102 | let selected = filter == self.filter; |
| 103 | div() |
| 104 | .id(ElementId::Name( |
| 105 | format!("activity-filter-{}", filter.label()).into(), |
| 106 | )) |
| 107 | .h(px(24.)) |
| 108 | .px(px(12.)) |
| 109 | .flex() |
| 110 | .items_center() |
| 111 | .rounded_full() |
| 112 | .cursor_pointer() |
| 113 | .text_size(px(12.)) |
| 114 | .when(selected, |pill| { |
| 115 | pill.bg(theme::accent()).text_color(theme::on_accent()) |
| 116 | }) |
| 117 | .when(!selected, |pill| { |
| 118 | pill.bg(theme::surface_raised()) |
| 119 | .text_color(theme::text_soft()) |
| 120 | .hover(|pill| pill.bg(theme::border_strong())) |
| 121 | }) |
| 122 | .child(filter.label()) |
| 123 | .on_click(cx.listener(move |this, _, _, cx| { |
| 124 | this.filter = filter; |
| 125 | let _ = this.app.read(cx).store.set_meta(FILTER_KEY, filter.key()); |
| 126 | cx.notify(); |
| 127 | })) |
| 128 | }); |
| 129 | v_flex() |
| 130 | .flex_none() |
| 131 | .child( |
| 132 | h_flex() |
| 133 | .h(px(52.)) |
| 134 | .pl(px(16.)) |
| 135 | .pr(px(12.)) |
| 136 | .gap(px(4.)) |
| 137 | .items_center() |
| 138 | .child( |
| 139 | div() |
| 140 | .flex_1() |
| 141 | .text_size(px(16.)) |
| 142 | .font_weight(FontWeight::BOLD) |
| 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(8.)) |
| 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(8.)) |
| 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().px(px(16.)).pb(px(12.)).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.clone(); |
| 195 | let actor = entry.latest_actor(); |
| 196 | let avatar = person_avatar( |
| 197 | directory, |
| 198 | actor.and_then(|actor| actor.user_id.as_deref()), |
| 199 | actor.map_or("", |actor| actor.display_name(&self.resolved_names)), |
| 200 | AVATAR_SIZE, |
| 201 | ); |
| 202 | let group = SharedString::from(format!("activity-row-group-{id}")); |
| 203 | row_frame(RowParts { |
| 204 | element_id: format!("activity-row-{id}"), |
| 205 | avatar: avatar.into_any_element(), |
| 206 | marker: kind_marker(entry, group.clone()), |
| 207 | group, |
| 208 | headline: headline(entry, sidebar, &self.resolved_names), |
| 209 | detail: detail(entry), |
| 210 | unread: !entry.read, |
| 211 | time: format::list_time_label(entry.updated_at, today, offset), |
| 212 | mark_read: Rc::new({ |
| 213 | let id = id.clone(); |
| 214 | move |cx: &mut App| activity.update(cx, |activity, cx| activity.mark_read(&id, cx)) |
| 215 | }), |
| 216 | }) |
| 217 | .on_click(move |_, _, cx| panel.update(cx, |this, cx| this.open(&id, cx))) |
| 218 | .into_any_element() |
| 219 | } |
| 220 | |
| 221 | fn proposal_row( |
| 222 | &self, |
| 223 | pending: &PendingProposal, |
| 224 | sidebar: &Sidebar, |
| 225 | today: NaiveDate, |
| 226 | offset: chrono::FixedOffset, |
| 227 | panel: Entity<ActivityPanel>, |
| 228 | ) -> AnyElement { |
| 229 | let group = SharedString::from(format!("activity-proposal-group-{}", pending.id)); |
| 230 | let label = proposal_conversation_label(sidebar, &pending.conversation_id); |
| 231 | let proposal_id = pending.id.clone(); |
| 232 | let opened = pending.clone(); |
| 233 | row_frame(RowParts { |
| 234 | element_id: format!("activity-proposal-{}", pending.id), |
| 235 | avatar: agent_avatar().into_any_element(), |
| 236 | marker: proposal_marker(pending, group.clone()), |
| 237 | group, |
| 238 | headline: pending.headline(label.as_deref()), |
| 239 | detail: pending.detail(), |
| 240 | unread: !pending.seen, |
| 241 | time: format::list_time_label(pending.created_at, today, offset), |
| 242 | mark_read: Rc::new(move |cx: &mut App| agent_api::mark_proposal_seen(&proposal_id, cx)), |
| 243 | }) |
| 244 | .on_click(move |_, _, cx| panel.update(cx, |this, cx| this.open_proposal(&opened, cx))) |
| 245 | .into_any_element() |
| 246 | } |
| 247 | |
| 248 | fn open_proposal(&mut self, pending: &PendingProposal, cx: &mut Context<Self>) { |
| 249 | agent_api::mark_proposal_seen(&pending.id, cx); |
| 250 | cx.emit(ActivityPanelEvent::Open { |
| 251 | conversation_id: pending.conversation_id.clone(), |
| 252 | message_id: pending.jump_message_id().to_owned(), |
| 253 | }); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | struct RowParts { |
| 258 | element_id: String, |
| 259 | group: SharedString, |
| 260 | avatar: AnyElement, |
| 261 | marker: Div, |
| 262 | headline: String, |
| 263 | detail: String, |
| 264 | unread: bool, |
| 265 | time: String, |
| 266 | mark_read: MarkRead, |
| 267 | } |
| 268 | |
| 269 | type MarkRead = Rc<dyn Fn(&mut App)>; |
| 270 | |
| 271 | fn row_frame(parts: RowParts) -> Stateful<Div> { |
| 272 | let RowParts { |
| 273 | element_id, |
| 274 | group, |
| 275 | avatar, |
| 276 | marker, |
| 277 | headline, |
| 278 | detail, |
| 279 | unread, |
| 280 | time, |
| 281 | mark_read, |
| 282 | } = parts; |
| 283 | let selector = element_id.clone(); |
| 284 | h_flex() |
| 285 | .id(ElementId::Name(element_id.clone().into())) |
| 286 | .debug_selector(move || selector) |
| 287 | .group(group.clone()) |
| 288 | .mx(px(6.)) |
| 289 | .px(px(10.)) |
| 290 | .py(px(8.)) |
| 291 | .gap(px(12.)) |
| 292 | .items_start() |
| 293 | .rounded(px(8.)) |
| 294 | .cursor_pointer() |
| 295 | .hover(|row| row.bg(theme::row_hover())) |
| 296 | .child( |
| 297 | div() |
| 298 | .relative() |
| 299 | .flex_none() |
| 300 | .size(px(AVATAR_SIZE)) |
| 301 | .child(avatar) |
| 302 | .child(marker), |
| 303 | ) |
| 304 | .child( |
| 305 | v_flex() |
| 306 | .flex_1() |
| 307 | .min_w_0() |
| 308 | .gap(px(2.)) |
| 309 | .child( |
| 310 | div() |
| 311 | .truncate() |
| 312 | .text_size(px(13.)) |
| 313 | .font_weight(if unread { |
| 314 | FontWeight::SEMIBOLD |
| 315 | } else { |
| 316 | FontWeight::NORMAL |
| 317 | }) |
| 318 | .text_color(if unread { |
| 319 | theme::text() |
| 320 | } else { |
| 321 | theme::text_soft() |
| 322 | }) |
| 323 | .child(headline), |
| 324 | ) |
| 325 | .child( |
| 326 | div() |
| 327 | .truncate() |
| 328 | .text_size(px(12.)) |
| 329 | .text_color(if unread { |
| 330 | theme::text_soft() |
| 331 | } else { |
| 332 | theme::text_muted() |
| 333 | }) |
| 334 | .child(detail), |
| 335 | ), |
| 336 | ) |
| 337 | .child( |
| 338 | v_flex() |
| 339 | .flex_none() |
| 340 | .items_end() |
| 341 | .child( |
| 342 | div() |
| 343 | .text_size(px(11.)) |
| 344 | .text_color(theme::text_muted()) |
| 345 | .child(time), |
| 346 | ) |
| 347 | .child(status_slot(element_id, unread, group, mark_read)), |
| 348 | ) |
| 349 | } |
| 350 | |
| 351 | fn agent_avatar() -> Div { |
| 352 | div() |
| 353 | .size(px(AVATAR_SIZE)) |
| 354 | .flex() |
| 355 | .items_center() |
| 356 | .justify_center() |
| 357 | .rounded_full() |
| 358 | .bg(theme::accent_soft()) |
| 359 | .child(icon(IconName::Bot, 16., theme::accent_text())) |
| 360 | } |
| 361 | |
| 362 | fn admits_proposal(filter: Filter, pending: &PendingProposal) -> bool { |
| 363 | match filter { |
| 364 | Filter::All => true, |
| 365 | Filter::Mentions => false, |
| 366 | Filter::Unread => !pending.seen, |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | fn status_slot(element_id: String, unread: bool, group: SharedString, mark_read: MarkRead) -> Div { |
| 371 | let slot = div() |
| 372 | .h(px(MARK_READ_SIZE)) |
| 373 | .flex() |
| 374 | .flex_none() |
| 375 | .items_center() |
| 376 | .justify_end(); |
| 377 | if !unread { |
| 378 | return slot; |
| 379 | } |
| 380 | // Toggle visibility, never display: gpui reads hover in prepaint and paint from different frames. |
| 381 | slot.relative() |
| 382 | .w(px(MARK_READ_SIZE)) |
| 383 | .child( |
| 384 | div() |
| 385 | .flex() |
| 386 | .group_hover(group.clone(), |wrapper| wrapper.invisible()) |
| 387 | .mr(px(8.)) |
| 388 | .child(dot(8.)), |
| 389 | ) |
| 390 | .child( |
| 391 | div() |
| 392 | .id(ElementId::Name(format!("{element_id}-mark-read").into())) |
| 393 | .absolute() |
| 394 | .top_0() |
| 395 | .right_0() |
| 396 | .invisible() |
| 397 | .group_hover(group, |button| button.visible()) |
| 398 | .size(px(MARK_READ_SIZE)) |
| 399 | .flex() |
| 400 | .items_center() |
| 401 | .justify_center() |
| 402 | .rounded(px(6.)) |
| 403 | .bg(theme::surface_raised()) |
| 404 | .cursor_pointer() |
| 405 | .child(icon(IconName::Check, 12., theme::text())) |
| 406 | .tooltip(|window, cx| Tooltip::new("Mark as read").build(window, cx)) |
| 407 | .on_click(move |_, _, cx| { |
| 408 | cx.stop_propagation(); |
| 409 | mark_read(cx); |
| 410 | }), |
| 411 | ) |
| 412 | } |
| 413 | |
| 414 | fn marker_frame(row_group: SharedString) -> Div { |
| 415 | corner_badge(Corner::BottomRight, 4.) |
| 416 | .size(px(KIND_MARKER_SIZE)) |
| 417 | .flex() |
| 418 | .items_center() |
| 419 | .justify_center() |
| 420 | .rounded_full() |
| 421 | .border_2() |
| 422 | .border_color(theme::surface()) |
| 423 | .group_hover(row_group, |marker| marker.border_color(theme::row_hover())) |
| 424 | .bg(theme::surface_raised()) |
| 425 | .text_size(px(10.)) |
| 426 | } |
| 427 | |
| 428 | fn proposal_marker(pending: &PendingProposal, row_group: SharedString) -> Div { |
| 429 | let marker = marker_frame(row_group); |
| 430 | match &pending.kind { |
| 431 | PendingKind::Reply { .. } => { |
| 432 | marker.child(icon(IconName::MessageCircle, 10., theme::text_muted())) |
| 433 | } |
| 434 | PendingKind::Reaction { glyph, .. } => marker.child(glyph.clone()), |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | fn kind_marker(entry: &Entry, row_group: SharedString) -> Div { |
| 439 | let marker = marker_frame(row_group); |
| 440 | let muted_icon = |name: IconName| icon(name, 10., theme::text_muted()); |
| 441 | match entry.kind { |
| 442 | Kind::Mention => marker |
| 443 | .text_color(theme::accent_text()) |
| 444 | .font_weight(FontWeight::BOLD) |
| 445 | .child("@"), |
| 446 | Kind::Reaction => match entry.glyphs.first() { |
| 447 | Some(glyph) => marker.child(glyph.clone()), |
| 448 | None => marker.child(muted_icon(IconName::Heart)), |
| 449 | }, |
| 450 | Kind::MissedCall => marker.child(icon(IconName::PhoneOff, 10., theme::red())), |
| 451 | Kind::Reply => marker.child(muted_icon(IconName::Reply)), |
| 452 | Kind::Call => marker.child(muted_icon(IconName::Phone)), |
| 453 | Kind::Posted => marker.child(muted_icon(IconName::MessageSquare)), |
| 454 | Kind::Team => marker.child(muted_icon(IconName::Users)), |
| 455 | Kind::App => marker.child(muted_icon(IconName::AppWindow)), |
| 456 | Kind::Other => marker.child(muted_icon(IconName::Bell)), |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | fn headline(entry: &Entry, sidebar: &Sidebar, resolved_names: &HashMap<String, String>) -> String { |
| 461 | let actor = entry |
| 462 | .latest_actor() |
| 463 | .map(|actor| actor.display_name(resolved_names).to_owned()) |
| 464 | .unwrap_or_default(); |
| 465 | let names = if entry.count > 1 && matches!(entry.kind, Kind::Reaction | Kind::Reply) { |
| 466 | format!("{actor} +{}", entry.count - 1) |
| 467 | } else { |
| 468 | actor.clone() |
| 469 | }; |
| 470 | let channel = channel_of(sidebar, &entry.conversation_id); |
| 471 | let verb = match (entry.kind, entry.subtype.as_str()) { |
| 472 | (Kind::Mention, "everyone") => format!("{names} mentioned Everyone"), |
| 473 | (Kind::Mention, "channel") => match channel { |
| 474 | Some((_, channel_name)) => format!("{names} mentioned {channel_name}"), |
| 475 | None => format!("{names} mentioned the channel"), |
| 476 | }, |
| 477 | (Kind::Mention, "team") => match channel { |
| 478 | Some((team_name, _)) => format!("{names} mentioned {team_name}"), |
| 479 | None => format!("{names} mentioned the team"), |
| 480 | }, |
| 481 | (Kind::Mention, _) => format!("{names} mentioned you"), |
| 482 | (Kind::Reply, _) => reply_headline(entry, &names), |
| 483 | (Kind::Reaction, _) => format!("{names} reacted to your message"), |
| 484 | (Kind::MissedCall, _) => format!("Missed call from {actor}"), |
| 485 | (Kind::Call, "voicemail") => format!("Voicemail from {actor}"), |
| 486 | (Kind::Call, "missedMeetingNudge") => format!("{actor} asked you to join a meeting"), |
| 487 | (Kind::Call, _) => actor, |
| 488 | (Kind::Posted, "channelReplyMessage") => format!("{names} replied"), |
| 489 | (Kind::Posted, _) => format!("{names} posted a new message"), |
| 490 | (Kind::Team, "promotedToTeamAdmin") => { |
| 491 | return format!("{actor} made you an owner of {}", team_label(entry, channel)); |
| 492 | } |
| 493 | (Kind::Team, _) => { |
| 494 | return format!("{actor} added you to {}", team_label(entry, channel)); |
| 495 | } |
| 496 | (Kind::App, _) => entry.source_label.clone().unwrap_or(actor), |
| 497 | (Kind::Other, _) => actor, |
| 498 | }; |
| 499 | match conversation_label(sidebar, &entry.conversation_id) { |
| 500 | Some(label) => format!("{verb} in {label}"), |
| 501 | None => verb, |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | fn reply_headline(entry: &Entry, names: &str) -> String { |
| 506 | match entry.activity_type.as_str() { |
| 507 | "reply" => format!("{names} replied to your post"), |
| 508 | "replyInChat" => format!("{names} replied to message"), |
| 509 | _ => format!("{names} replied"), |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | fn team_label(entry: &Entry, channel: Option<(&str, &str)>) -> String { |
| 514 | channel |
| 515 | .map(|(team_name, _)| team_name.to_owned()) |
| 516 | .or_else(|| entry.source_label.clone()) |
| 517 | .unwrap_or_else(|| "a team".to_owned()) |
| 518 | } |
| 519 | |
| 520 | fn channel_of<'a>(sidebar: &'a Sidebar, conversation_id: &str) -> Option<(&'a str, &'a str)> { |
| 521 | sidebar.teams.iter().find_map(|team| { |
| 522 | team.channels |
| 523 | .iter() |
| 524 | .find(|channel| channel.id == conversation_id) |
| 525 | .map(|channel| (team.team.name.as_str(), channel.name.as_str())) |
| 526 | }) |
| 527 | } |
| 528 | |
| 529 | fn detail(entry: &Entry) -> String { |
| 530 | match entry.kind { |
| 531 | Kind::Reaction if !entry.glyphs.is_empty() => { |
| 532 | format!("{} to: {}", entry.glyphs.join(" "), entry.preview) |
| 533 | } |
| 534 | Kind::MissedCall if entry.count > 1 => format!("{} missed calls", entry.count), |
| 535 | _ => entry.preview.clone(), |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | fn conversation_label(sidebar: &Sidebar, conversation_id: &str) -> Option<String> { |
| 540 | if let Some(chat) = sidebar.chats.iter().find(|chat| chat.id == conversation_id) { |
| 541 | return (!is_one_on_one(chat)).then(|| chat_title(chat)); |
| 542 | } |
| 543 | sidebar.teams.iter().find_map(|team| { |
| 544 | team.channels |
| 545 | .iter() |
| 546 | .find(|channel| channel.id == conversation_id) |
| 547 | .map(|channel| format!("{} > {}", team.team.name, channel.name)) |
| 548 | }) |
| 549 | } |
| 550 | |
| 551 | fn group_header(label: &'static str) -> Div { |
| 552 | div() |
| 553 | .h(px(26.)) |
| 554 | .mt(px(4.)) |
| 555 | .px(px(16.)) |
| 556 | .flex() |
| 557 | .flex_none() |
| 558 | .items_center() |
| 559 | .text_size(px(11.)) |
| 560 | .font_weight(FontWeight::SEMIBOLD) |
| 561 | .text_color(theme::text_muted()) |
| 562 | .child(label) |
| 563 | } |
| 564 | |
| 565 | impl Render for ActivityPanel { |
| 566 | fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 567 | let now: DateTime<Local> = Local::now(); |
| 568 | let (today, offset) = (now.date_naive(), now.offset().fix()); |
| 569 | let panel = cx.entity(); |
| 570 | let app = self.app.clone(); |
| 571 | let activity = self.activity.clone(); |
| 572 | let state = app.read(cx); |
| 573 | let sections = activity |
| 574 | .read(cx) |
| 575 | .feed() |
| 576 | .sections(self.filter, today, offset); |
| 577 | let unresolved: Vec<String> = sections |
| 578 | .iter() |
| 579 | .flat_map(|section| section.entries.iter()) |
| 580 | .flat_map(|entry| entry.unresolved_user_ids()) |
| 581 | .filter(|user_id| !self.looked_up.contains(*user_id)) |
| 582 | .cloned() |
| 583 | .collect(); |
| 584 | if !unresolved.is_empty() { |
| 585 | self.looked_up.extend(unresolved.iter().cloned()); |
| 586 | self.resolved_names |
| 587 | .extend(resolve_names(state, &unresolved)); |
| 588 | } |
| 589 | let mut rows: Vec<AnyElement> = Vec::new(); |
| 590 | let proposal_rows: Vec<AnyElement> = agent_api::pending_proposals(cx) |
| 591 | .iter() |
| 592 | .filter(|pending| admits_proposal(self.filter, pending)) |
| 593 | .map(|pending| self.proposal_row(pending, &state.sidebar, today, offset, panel.clone())) |
| 594 | .collect(); |
| 595 | if !proposal_rows.is_empty() { |
| 596 | rows.push( |
| 597 | v_flex() |
| 598 | .debug_selector(|| ACTIVITY_PROPOSALS_TAG.into()) |
| 599 | .child(group_header(PROPOSALS_LABEL)) |
| 600 | .children(proposal_rows) |
| 601 | .into_any_element(), |
| 602 | ); |
| 603 | } |
| 604 | for section in sections { |
| 605 | rows.push(group_header(section.bucket.label()).into_any_element()); |
| 606 | for entry in section.entries { |
| 607 | rows.push(self.row( |
| 608 | entry, |
| 609 | &state.sidebar, |
| 610 | &state.directory, |
| 611 | today, |
| 612 | offset, |
| 613 | panel.clone(), |
| 614 | )); |
| 615 | } |
| 616 | } |
| 617 | if rows.is_empty() { |
| 618 | rows.push( |
| 619 | div() |
| 620 | .py(px(32.)) |
| 621 | .flex() |
| 622 | .justify_center() |
| 623 | .text_size(px(13.)) |
| 624 | .text_color(theme::text_muted()) |
| 625 | .child(self.filter.empty_label()) |
| 626 | .into_any_element(), |
| 627 | ); |
| 628 | } |
| 629 | let list = v_flex() |
| 630 | .id("activity-list") |
| 631 | .flex_1() |
| 632 | .min_h_0() |
| 633 | .pb(px(12.)) |
| 634 | .overflow_y_scroll() |
| 635 | .track_scroll(&self.scroll) |
| 636 | .children(rows); |
| 637 | v_flex() |
| 638 | .flex_1() |
| 639 | .min_h_0() |
| 640 | .child(self.header(cx)) |
| 641 | .child(with_top_edge(list, &self.scroll, SIDEBAR_SCROLL_DIVIDER)) |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | #[cfg(test)] |
| 646 | mod tests { |
| 647 | use std::collections::HashMap; |
| 648 | use std::sync::Arc; |
| 649 | |
| 650 | use chrono::{Local, Offset, TimeZone, Utc}; |
| 651 | use gpui_kit::{AppContext as _, Modifiers, TestAppContext}; |
| 652 | use store::{ChatRecord, Sidebar, Store}; |
| 653 | |
| 654 | use super::{ActivityPanel, detail, headline}; |
| 655 | use crate::activity::{ActivityCenter, Actor, Entry, Filter, Kind}; |
| 656 | use crate::app_state::{AppState, Mode}; |
| 657 | |
| 658 | fn entry(kind: Kind, count: u32) -> Entry { |
| 659 | Entry { |
| 660 | id: "1".into(), |
| 661 | conversation_id: "chat".into(), |
| 662 | kind, |
| 663 | message_id: "m1".into(), |
| 664 | actors: vec![Actor { |
| 665 | user_id: Some("u1".into()), |
| 666 | name: "Anna".into(), |
| 667 | }], |
| 668 | preview: "hello".into(), |
| 669 | glyphs: vec!["A".into(), "B".into()], |
| 670 | count, |
| 671 | updated_at: Utc.with_ymd_and_hms(2026, 10, 8, 9, 0, 0).unwrap(), |
| 672 | ..Entry::default() |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | fn with(kind: Kind, activity_type: &str, subtype: &str, count: u32) -> Entry { |
| 677 | Entry { |
| 678 | activity_type: activity_type.into(), |
| 679 | subtype: subtype.into(), |
| 680 | ..entry(kind, count) |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | #[test] |
| 685 | fn detail_follows_the_kind() { |
| 686 | assert_eq!(detail(&entry(Kind::Mention, 1)), "hello"); |
| 687 | assert_eq!(detail(&entry(Kind::Reaction, 1)), "A B to: hello"); |
| 688 | assert_eq!(detail(&entry(Kind::MissedCall, 3)), "3 missed calls"); |
| 689 | assert_eq!(detail(&entry(Kind::Reply, 2)), "hello"); |
| 690 | } |
| 691 | |
| 692 | #[test] |
| 693 | fn headline_uses_the_teams_activity_texts() { |
| 694 | let sidebar = Sidebar { |
| 695 | chats: vec![ChatRecord { |
| 696 | id: "chat".into(), |
| 697 | kind: "oneOnOne".into(), |
| 698 | ..Default::default() |
| 699 | }], |
| 700 | teams: Vec::new(), |
| 701 | }; |
| 702 | let cases = [ |
| 703 | (with(Kind::Mention, "mentionInChat", "person", 1), "Anna mentioned you"), |
| 704 | (with(Kind::Mention, "mentionInChat", "everyone", 1), "Anna mentioned Everyone"), |
| 705 | (with(Kind::Reaction, "reactionInChat", "like", 1), "Anna reacted to your message"), |
| 706 | (with(Kind::Reaction, "reactionInChat", "like", 3), "Anna +2 reacted to your message"), |
| 707 | (with(Kind::Reply, "reply", "", 1), "Anna replied to your post"), |
| 708 | (with(Kind::Reply, "replyToReply", "", 1), "Anna replied"), |
| 709 | (with(Kind::MissedCall, "call", "missedCall", 1), "Missed call from Anna"), |
| 710 | (with(Kind::Call, "call", "missedMeetingNudge", 1), "Anna asked you to join a meeting"), |
| 711 | (with(Kind::Posted, "follow", "channelNewMessage", 1), "Anna posted a new message"), |
| 712 | ]; |
| 713 | for (entry, expected) in cases { |
| 714 | assert_eq!(headline(&entry, &sidebar, &HashMap::new()), expected); |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | #[test] |
| 719 | fn headline_names_channel_and_team_mentions() { |
| 720 | let sidebar = Sidebar { |
| 721 | chats: Vec::new(), |
| 722 | teams: vec![store::SidebarTeam { |
| 723 | team: store::TeamRecord { |
| 724 | id: "team".into(), |
| 725 | name: "Platform".into(), |
| 726 | }, |
| 727 | channels: vec![store::ChannelRecord { |
| 728 | id: "chat".into(), |
| 729 | team_id: "team".into(), |
| 730 | name: "Releases".into(), |
| 731 | membership_type: None, |
| 732 | last_message_at: None, |
| 733 | unread: false, |
| 734 | }], |
| 735 | hidden: false, |
| 736 | hidden_channel_ids: Vec::new(), |
| 737 | notifications: Default::default(), |
| 738 | mentions: Default::default(), |
| 739 | }], |
| 740 | }; |
| 741 | assert_eq!( |
| 742 | headline(&with(Kind::Mention, "mention", "channel", 1), &sidebar, &HashMap::new()), |
| 743 | "Anna mentioned Releases in Platform > Releases" |
| 744 | ); |
| 745 | assert_eq!( |
| 746 | headline(&with(Kind::Mention, "mention", "team", 1), &sidebar, &HashMap::new()), |
| 747 | "Anna mentioned Platform in Platform > Releases" |
| 748 | ); |
| 749 | assert_eq!( |
| 750 | headline( |
| 751 | &with(Kind::Team, "teamMembershipChange", "addedToTeam", 1), |
| 752 | &sidebar, |
| 753 | &HashMap::new() |
| 754 | ), |
| 755 | "Anna added you to Platform" |
| 756 | ); |
| 757 | } |
| 758 | |
| 759 | #[test] |
| 760 | fn headline_resolves_an_unknown_actor() { |
| 761 | let mut stale = with(Kind::Reaction, "reactionInChat", "like", 1); |
| 762 | stale.actors[0].name = "Unknown".into(); |
| 763 | let sidebar = Sidebar::default(); |
| 764 | let resolved = HashMap::from([("u1".to_owned(), "Anna".to_owned())]); |
| 765 | assert_eq!( |
| 766 | headline(&stale, &sidebar, &resolved), |
| 767 | "Anna reacted to your message" |
| 768 | ); |
| 769 | } |
| 770 | |
| 771 | #[gpui_kit::test] |
| 772 | fn row_sliding_under_a_resting_mouse_draws(cx: &mut TestAppContext) { |
| 773 | cx.update(gpui_kit::init); |
| 774 | let store = Arc::new(Store::open_in_memory().unwrap()); |
| 775 | crate::demo::seed(&store); |
| 776 | let mode = Mode { |
| 777 | demo: true, |
| 778 | read_only: false, |
| 779 | demo_sync: None, |
| 780 | }; |
| 781 | let app = cx.update(|cx| { |
| 782 | cx.new(|_| { |
| 783 | let mut state = AppState::new(store, mode); |
| 784 | crate::demo::seed_directory(&mut state); |
| 785 | state |
| 786 | }) |
| 787 | }); |
| 788 | let (panel, cx) = cx.add_window_view(|window, cx| { |
| 789 | let activity = cx.new(|cx| ActivityCenter::new(app.clone(), window, cx)); |
| 790 | let mut panel = ActivityPanel::new(app.clone(), activity, cx); |
| 791 | panel.filter = Filter::Unread; |
| 792 | panel |
| 793 | }); |
| 794 | cx.update(|window, cx| window.draw(cx).clear(cx)); |
| 795 | let activity = cx.update(|_, cx| panel.read(cx).activity.clone()); |
| 796 | let first_unread = cx.update(|_, cx| { |
| 797 | let now = Local::now(); |
| 798 | activity |
| 799 | .read(cx) |
| 800 | .feed() |
| 801 | .sections(Filter::Unread, now.date_naive(), now.offset().fix())[0] |
| 802 | .entries[0] |
| 803 | .id |
| 804 | .clone() |
| 805 | }); |
| 806 | let selector: &'static str = format!("activity-row-{first_unread}").leak(); |
| 807 | let first_row = cx.debug_bounds(selector).unwrap(); |
| 808 | cx.simulate_mouse_move(first_row.center(), None, Modifiers::none()); |
| 809 | cx.update(|window, cx| window.draw(cx).clear(cx)); |
| 810 | cx.update(|_, cx| activity.update(cx, |activity, cx| activity.mark_read(&first_unread, cx))); |
| 811 | cx.update(|window, cx| window.draw(cx).clear(cx)); |
| 812 | } |
| 813 | } |