| 1 | use std::cell::Cell; |
| 2 | use std::collections::HashSet; |
| 3 | use std::time::Duration; |
| 4 | |
| 5 | use chrono::{Local, Offset, Utc}; |
| 6 | use gpui_kit::assets::IconName; |
| 7 | use gpui_kit::component::button::{Button, ButtonVariants as _}; |
| 8 | use gpui_kit::component::dialog::DialogFooter; |
| 9 | use gpui_kit::component::menu::{ContextMenuExt as _, DropdownMenu as _, PopupMenu, PopupMenuItem}; |
| 10 | use gpui_kit::component::{WindowExt as _, h_flex, tooltip::Tooltip, v_flex}; |
| 11 | use gpui_kit::prelude::FluentBuilder as _; |
| 12 | use gpui_kit::*; |
| 13 | use store::{ |
| 14 | ChannelNotificationLevel, ChannelNotifications, ChannelRecord, SidebarTeam, Store, TeamRecord, |
| 15 | }; |
| 16 | use teams_core::is_notes_chat; |
| 17 | |
| 18 | use super::activity_panel::ActivityPanel; |
| 19 | use super::avatar::{spec_avatar, square_avatar, with_presence}; |
| 20 | use super::calls_tab::calls_body; |
| 21 | use super::saved_panel::SavedPanel; |
| 22 | use super::scroll_edge::with_top_edge; |
| 23 | use super::section_dialog::{SectionNameTarget, open_section_name_dialog}; |
| 24 | use super::widgets::{count_badge, icon, page_header, unread_marker}; |
| 25 | use crate::agents::AgentsPanel; |
| 26 | use crate::app_state::{AppEvent, AppState, Selection}; |
| 27 | use crate::chat_actions::TITLE_LIMIT; |
| 28 | use crate::data::{Directory, is_pending_folder}; |
| 29 | use crate::format; |
| 30 | use crate::local_previews::LocalPreview; |
| 31 | use crate::notice::truncated; |
| 32 | use crate::sidebar_model::{ |
| 33 | AvatarSpec, ChannelOrderInput, ChatItem, DELETED_CHAT_PREVIEW, EMPTY_FOLDER_HINT, NameStyle, |
| 34 | NameWeight, Preview, Section, SectionInput, SectionKind, Step, build_sections, |
| 35 | channel_name_style, older_chats_trigger_resets, step_id, team_name_style, visible_channel_ids, |
| 36 | wants_older_chats, |
| 37 | }; |
| 38 | use crate::theme; |
| 39 | use app_ui_kit::layout::SIDEBAR_WIDTH; |
| 40 | |
| 41 | pub const SIDEBAR_SCROLL_DIVIDER: &str = "shell/islands/sidebar/divider-scroll"; |
| 42 | const COLLAPSED_TEAMS_META_KEY: &str = "sidebar_collapsed_teams"; |
| 43 | const ROW_HEIGHT: f32 = 54.; |
| 44 | const PROPOSAL_DOT_SIZE: f32 = 8.; |
| 45 | const PINNED_CHANNEL_HEIGHT: f32 = 48.; |
| 46 | const SECTION_HEIGHT: f32 = 30.; |
| 47 | const SECTION_GAP: f32 = 4.; |
| 48 | const HINT_HEIGHT: f32 = 26.; |
| 49 | const SEE_MORE_HEIGHT: f32 = 28.; |
| 50 | const LOADING_OLDER_LABEL: &str = "Loading older chats"; |
| 51 | const FALLBACK_VIEWPORT: f32 = 900.; |
| 52 | const CALLS_REFRESH: Duration = Duration::from_secs(30); |
| 53 | const VIEWPORT_MARGIN: f32 = 300.; |
| 54 | const AVATAR_SIZE: f32 = 36.; |
| 55 | const PINNED_LABEL: &str = "Pinned"; |
| 56 | const TEAMS_LABEL: &str = "Teams"; |
| 57 | const HIDDEN_TEAMS_LABEL: &str = "Hidden teams"; |
| 58 | const TEAM_ROW_HEIGHT: f32 = 40.; |
| 59 | const REVEAL_ROW_HEIGHT: f32 = 32.; |
| 60 | const NEW_CHAT_BUTTON_SIZE: f32 = 32.; |
| 61 | const MENU_ICON_SIZE: f32 = 15.; |
| 62 | const NOTIFICATION_ICON_SIZE: f32 = 13.; |
| 63 | |
| 64 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 65 | pub enum SidebarTab { |
| 66 | Activity, |
| 67 | Chats, |
| 68 | Channels, |
| 69 | Calls, |
| 70 | Saved, |
| 71 | Agents, |
| 72 | } |
| 73 | |
| 74 | impl SidebarTab { |
| 75 | fn title(self) -> &'static str { |
| 76 | match self { |
| 77 | SidebarTab::Activity => "Activity", |
| 78 | SidebarTab::Chats => "Chats", |
| 79 | SidebarTab::Channels => "Channels", |
| 80 | SidebarTab::Calls => "Calls", |
| 81 | SidebarTab::Saved => "Saved", |
| 82 | SidebarTab::Agents => "Agents", |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | #[derive(Clone)] |
| 88 | struct DraggedChat { |
| 89 | id: String, |
| 90 | title: String, |
| 91 | } |
| 92 | |
| 93 | struct DragGhost { |
| 94 | title: String, |
| 95 | } |
| 96 | |
| 97 | impl Render for DragGhost { |
| 98 | fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { |
| 99 | div() |
| 100 | .px(px(12.)) |
| 101 | .py(px(6.)) |
| 102 | .rounded(px(8.)) |
| 103 | .bg(theme::surface_raised()) |
| 104 | .border_1() |
| 105 | .border_color(theme::accent()) |
| 106 | .text_size(px(13.)) |
| 107 | .text_color(theme::text()) |
| 108 | .child(self.title.clone()) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | struct ChatMenuTarget { |
| 113 | id: String, |
| 114 | title: String, |
| 115 | folder_id: Option<String>, |
| 116 | unread: bool, |
| 117 | muted: bool, |
| 118 | is_group: bool, |
| 119 | member_count: usize, |
| 120 | } |
| 121 | |
| 122 | #[derive(Clone)] |
| 123 | struct MenuContext { |
| 124 | state: Entity<AppState>, |
| 125 | folders: Vec<(String, String)>, |
| 126 | } |
| 127 | |
| 128 | fn menu_icon(name: IconName) -> gpui_kit::component::Icon { |
| 129 | icon(name, MENU_ICON_SIZE, theme::text_muted()) |
| 130 | } |
| 131 | |
| 132 | fn chat_menu( |
| 133 | popup: PopupMenu, |
| 134 | window: &mut Window, |
| 135 | cx: &mut Context<PopupMenu>, |
| 136 | context: &MenuContext, |
| 137 | target: &ChatMenuTarget, |
| 138 | ) -> PopupMenu { |
| 139 | let mut popup = popup; |
| 140 | let current = target.folder_id.clone(); |
| 141 | let folders: Vec<(String, String)> = context |
| 142 | .folders |
| 143 | .iter() |
| 144 | .filter(|(folder_id, _)| current.as_ref() != Some(folder_id)) |
| 145 | .cloned() |
| 146 | .collect(); |
| 147 | if !folders.is_empty() { |
| 148 | let move_state = context.state.clone(); |
| 149 | let move_id = target.id.clone(); |
| 150 | popup = popup.submenu_with_icon( |
| 151 | Some(menu_icon(IconName::Folder)), |
| 152 | "Move to section", |
| 153 | window, |
| 154 | cx, |
| 155 | move |submenu, _, _| { |
| 156 | folders.iter().fold(submenu, |submenu, (folder_id, name)| { |
| 157 | let (state, chat_id, folder_id) = |
| 158 | (move_state.clone(), move_id.clone(), folder_id.clone()); |
| 159 | submenu.item(PopupMenuItem::new(name.clone()).on_click(move |_, _, cx| { |
| 160 | state.update(cx, |state, cx| { |
| 161 | state.move_chat(&chat_id, Some(&folder_id), cx) |
| 162 | }); |
| 163 | })) |
| 164 | }) |
| 165 | }, |
| 166 | ); |
| 167 | } |
| 168 | if current.is_some() { |
| 169 | let remove_state = context.state.clone(); |
| 170 | let remove_id = target.id.clone(); |
| 171 | popup = popup.item( |
| 172 | PopupMenuItem::new("Remove from section") |
| 173 | .icon(menu_icon(IconName::FolderMinus)) |
| 174 | .on_click(move |_, _, cx| { |
| 175 | remove_state.update(cx, |state, cx| state.move_chat(&remove_id, None, cx)); |
| 176 | }), |
| 177 | ); |
| 178 | } |
| 179 | let read_state = context.state.clone(); |
| 180 | let read_id = target.id.clone(); |
| 181 | let unread = target.unread; |
| 182 | let mute_state = context.state.clone(); |
| 183 | let mute_id = target.id.clone(); |
| 184 | let muted = target.muted; |
| 185 | let hide_state = context.state.clone(); |
| 186 | let hide_id = target.id.clone(); |
| 187 | let notes = is_notes_chat(&target.id); |
| 188 | popup = popup.separator().item( |
| 189 | PopupMenuItem::new(if unread { |
| 190 | "Mark as read" |
| 191 | } else { |
| 192 | "Mark as unread" |
| 193 | }) |
| 194 | .icon(menu_icon(if unread { |
| 195 | IconName::MailOpen |
| 196 | } else { |
| 197 | IconName::Mail |
| 198 | })) |
| 199 | .on_click(move |_, _, cx| { |
| 200 | read_state.update(cx, |state, cx| { |
| 201 | if unread { |
| 202 | state.mark_chat_read(&read_id, cx); |
| 203 | } else { |
| 204 | state.mark_chat_unread(&read_id, cx); |
| 205 | } |
| 206 | }); |
| 207 | }), |
| 208 | ); |
| 209 | if notes { |
| 210 | return popup; |
| 211 | } |
| 212 | popup = popup |
| 213 | .item( |
| 214 | PopupMenuItem::new(if muted { "Unmute" } else { "Mute" }) |
| 215 | .icon(menu_icon(if muted { |
| 216 | IconName::Bell |
| 217 | } else { |
| 218 | IconName::BellOff |
| 219 | })) |
| 220 | .on_click(move |_, _, cx| { |
| 221 | mute_state.update(cx, |state, cx| state.set_chat_muted(&mute_id, !muted, cx)); |
| 222 | }), |
| 223 | ) |
| 224 | .separator() |
| 225 | .item( |
| 226 | PopupMenuItem::new("Hide") |
| 227 | .icon(menu_icon(IconName::EyeOff)) |
| 228 | .on_click(move |_, _, cx| { |
| 229 | hide_state.update(cx, |state, cx| state.hide_chat(&hide_id, cx)); |
| 230 | }), |
| 231 | ); |
| 232 | if target.is_group { |
| 233 | let leave_state = context.state.clone(); |
| 234 | let leave_id = target.id.clone(); |
| 235 | let title = target.title.clone(); |
| 236 | let member_count = target.member_count; |
| 237 | popup = popup.item( |
| 238 | PopupMenuItem::element(|_, _| { |
| 239 | div().text_color(theme::red_soft()).child("Leave chat...") |
| 240 | }) |
| 241 | .icon(icon(IconName::LogOut, MENU_ICON_SIZE, theme::red_soft())) |
| 242 | .on_click(move |_, window, cx| { |
| 243 | confirm_leave(&leave_state, &leave_id, &title, member_count, window, cx); |
| 244 | }), |
| 245 | ); |
| 246 | } |
| 247 | popup |
| 248 | } |
| 249 | |
| 250 | #[derive(Clone)] |
| 251 | struct SectionMenuTarget { |
| 252 | id: String, |
| 253 | name: String, |
| 254 | user_created: bool, |
| 255 | previous_id: Option<String>, |
| 256 | next_id: Option<String>, |
| 257 | movable: bool, |
| 258 | read_only: bool, |
| 259 | } |
| 260 | |
| 261 | fn section_menu_targets( |
| 262 | sections: &[Section], |
| 263 | directory: &Directory, |
| 264 | read_only: bool, |
| 265 | ) -> Vec<SectionMenuTarget> { |
| 266 | let movable_ids: HashSet<&str> = directory |
| 267 | .folders |
| 268 | .iter() |
| 269 | .map(|folder| folder.id.as_str()) |
| 270 | .filter(|folder_id| !is_pending_folder(folder_id)) |
| 271 | .collect(); |
| 272 | let movable = |section: &Section| movable_ids.contains(section.id.as_str()); |
| 273 | sections |
| 274 | .iter() |
| 275 | .enumerate() |
| 276 | .map(|(index, section)| SectionMenuTarget { |
| 277 | id: section.id.clone(), |
| 278 | name: section.name.clone(), |
| 279 | user_created: section.kind == SectionKind::Folder && movable(section), |
| 280 | previous_id: sections[..index] |
| 281 | .iter() |
| 282 | .rev() |
| 283 | .find(|other| movable(other)) |
| 284 | .map(|other| other.id.clone()), |
| 285 | next_id: sections[index + 1..] |
| 286 | .iter() |
| 287 | .find(|other| movable(other)) |
| 288 | .map(|other| other.id.clone()), |
| 289 | movable: movable(section), |
| 290 | read_only, |
| 291 | }) |
| 292 | .collect() |
| 293 | } |
| 294 | |
| 295 | fn drop_target(section: &Section) -> Option<Option<String>> { |
| 296 | if is_pending_folder(§ion.id) { |
| 297 | return None; |
| 298 | } |
| 299 | Some( |
| 300 | matches!(section.kind, SectionKind::Favorites | SectionKind::Folder) |
| 301 | .then(|| section.id.clone()), |
| 302 | ) |
| 303 | } |
| 304 | |
| 305 | fn new_section_item(state: &Entity<AppState>, read_only: bool) -> PopupMenuItem { |
| 306 | let state = state.clone(); |
| 307 | PopupMenuItem::new("New section") |
| 308 | .icon(menu_icon(IconName::Plus)) |
| 309 | .disabled(read_only) |
| 310 | .on_click(move |_, window, cx| { |
| 311 | open_section_name_dialog(state.clone(), SectionNameTarget::New, window, cx); |
| 312 | }) |
| 313 | } |
| 314 | |
| 315 | fn move_section_item( |
| 316 | state: &Entity<AppState>, |
| 317 | label: &'static str, |
| 318 | glyph: IconName, |
| 319 | folder_id: &str, |
| 320 | target_id: Option<&String>, |
| 321 | read_only: bool, |
| 322 | ) -> PopupMenuItem { |
| 323 | let state = state.clone(); |
| 324 | let folder_id = folder_id.to_owned(); |
| 325 | let target_id = target_id.cloned(); |
| 326 | PopupMenuItem::new(label) |
| 327 | .icon(menu_icon(glyph)) |
| 328 | .disabled(read_only || target_id.is_none()) |
| 329 | .on_click(move |_, _, cx| { |
| 330 | if let Some(target_id) = &target_id { |
| 331 | state.update(cx, |state, cx| { |
| 332 | state.move_section(&folder_id, target_id, cx) |
| 333 | }); |
| 334 | } |
| 335 | }) |
| 336 | } |
| 337 | |
| 338 | fn section_menu( |
| 339 | popup: PopupMenu, |
| 340 | state: &Entity<AppState>, |
| 341 | target: &SectionMenuTarget, |
| 342 | ) -> PopupMenu { |
| 343 | let mut popup = popup; |
| 344 | if target.user_created { |
| 345 | let rename_state = state.clone(); |
| 346 | let rename_target = SectionNameTarget::Rename { |
| 347 | folder_id: target.id.clone(), |
| 348 | name: target.name.clone(), |
| 349 | }; |
| 350 | popup = popup.item( |
| 351 | PopupMenuItem::new("Rename section") |
| 352 | .icon(menu_icon(IconName::Pencil)) |
| 353 | .disabled(target.read_only) |
| 354 | .on_click(move |_, window, cx| { |
| 355 | open_section_name_dialog( |
| 356 | rename_state.clone(), |
| 357 | rename_target.clone(), |
| 358 | window, |
| 359 | cx, |
| 360 | ); |
| 361 | }), |
| 362 | ); |
| 363 | } |
| 364 | if target.movable { |
| 365 | popup = popup |
| 366 | .item(move_section_item( |
| 367 | state, |
| 368 | "Move up", |
| 369 | IconName::ArrowUp, |
| 370 | &target.id, |
| 371 | target.previous_id.as_ref(), |
| 372 | target.read_only, |
| 373 | )) |
| 374 | .item(move_section_item( |
| 375 | state, |
| 376 | "Move down", |
| 377 | IconName::ArrowDown, |
| 378 | &target.id, |
| 379 | target.next_id.as_ref(), |
| 380 | target.read_only, |
| 381 | )) |
| 382 | .separator(); |
| 383 | } |
| 384 | popup = popup.item(new_section_item(state, target.read_only)); |
| 385 | if target.user_created { |
| 386 | let delete_state = state.clone(); |
| 387 | let (folder_id, name) = (target.id.clone(), target.name.clone()); |
| 388 | popup = popup.separator().item( |
| 389 | PopupMenuItem::element(|_, _| { |
| 390 | div().text_color(theme::red_soft()).child("Delete section") |
| 391 | }) |
| 392 | .icon(icon(IconName::Trash, MENU_ICON_SIZE, theme::red_soft())) |
| 393 | .disabled(target.read_only) |
| 394 | .on_click(move |_, window, cx| { |
| 395 | confirm_delete_section(&delete_state, &folder_id, &name, window, cx); |
| 396 | }), |
| 397 | ); |
| 398 | } |
| 399 | popup |
| 400 | } |
| 401 | |
| 402 | fn confirm_delete_section( |
| 403 | state: &Entity<AppState>, |
| 404 | folder_id: &str, |
| 405 | name: &str, |
| 406 | window: &mut Window, |
| 407 | cx: &mut App, |
| 408 | ) { |
| 409 | let state = state.clone(); |
| 410 | let folder_id = folder_id.to_owned(); |
| 411 | let heading = format!("Delete section \"{}\"?", truncated(name, TITLE_LIMIT)); |
| 412 | window.open_alert_dialog(cx, move |alert, _, _| { |
| 413 | let state = state.clone(); |
| 414 | let folder_id = folder_id.clone(); |
| 415 | let cancel = Button::new("delete-section-cancel") |
| 416 | .label("Cancel") |
| 417 | .on_click(|_, window, cx| window.close_dialog(cx)); |
| 418 | let delete = Button::new("delete-section-confirm") |
| 419 | .label("Delete") |
| 420 | .danger() |
| 421 | .on_click(move |_, window, cx| { |
| 422 | state.update(cx, |state, cx| state.delete_section(&folder_id, cx)); |
| 423 | window.close_dialog(cx); |
| 424 | }); |
| 425 | alert |
| 426 | .title(heading.clone()) |
| 427 | .description("Chats in it move back to Chats.") |
| 428 | .footer(DialogFooter::new().child(cancel).child(delete)) |
| 429 | .on_ok(|_, _, _| false) |
| 430 | }); |
| 431 | } |
| 432 | |
| 433 | fn leave_description(member_count: usize) -> String { |
| 434 | let who = match member_count { |
| 435 | 0 | 1 => "The other members".to_owned(), |
| 436 | 2 => "The other member".to_owned(), |
| 437 | count => format!("The {} other members", count - 1), |
| 438 | }; |
| 439 | let verb = if member_count == 2 { "sees" } else { "see" }; |
| 440 | format!("{who} {verb} that you left. You can only come back if someone adds you.") |
| 441 | } |
| 442 | |
| 443 | pub(crate) fn confirm_leave( |
| 444 | state: &Entity<AppState>, |
| 445 | chat_id: &str, |
| 446 | title: &str, |
| 447 | member_count: usize, |
| 448 | window: &mut Window, |
| 449 | cx: &mut App, |
| 450 | ) { |
| 451 | let state = state.clone(); |
| 452 | let chat_id = chat_id.to_owned(); |
| 453 | let heading = format!("Leave \"{}\"?", truncated(title, TITLE_LIMIT)); |
| 454 | let description = leave_description(member_count); |
| 455 | window.open_alert_dialog(cx, move |alert, _, _| { |
| 456 | let state = state.clone(); |
| 457 | let chat_id = chat_id.clone(); |
| 458 | let cancel = Button::new("leave-cancel") |
| 459 | .label("Cancel") |
| 460 | .on_click(|_, window, cx| window.close_dialog(cx)); |
| 461 | let leave = Button::new("leave-confirm") |
| 462 | .label("Leave") |
| 463 | .danger() |
| 464 | .on_click(move |_, window, cx| { |
| 465 | state.update(cx, |state, cx| state.leave_chat(&chat_id, cx)); |
| 466 | window.close_dialog(cx); |
| 467 | }); |
| 468 | alert |
| 469 | .title(heading.clone()) |
| 470 | .description(description.clone()) |
| 471 | .footer(DialogFooter::new().child(cancel).child(leave)) |
| 472 | .on_ok(|_, _, _| false) |
| 473 | }); |
| 474 | } |
| 475 | |
| 476 | pub struct SidebarView { |
| 477 | state: Entity<AppState>, |
| 478 | tab: SidebarTab, |
| 479 | activity_panel: Entity<ActivityPanel>, |
| 480 | saved_panel: Entity<SavedPanel>, |
| 481 | agents_panel: Option<Entity<AgentsPanel>>, |
| 482 | collapsed_teams: HashSet<String>, |
| 483 | revealed_channel_teams: HashSet<String>, |
| 484 | hidden_teams_open: bool, |
| 485 | chats_scroll: ScrollHandle, |
| 486 | older_chats_trigger_top: Cell<Option<f32>>, |
| 487 | older_chats_seen: Cell<(bool, bool)>, |
| 488 | channels_scroll: ScrollHandle, |
| 489 | calls_scroll: ScrollHandle, |
| 490 | _subscription: Subscription, |
| 491 | _state_observation: Subscription, |
| 492 | _agent_observation: Option<Subscription>, |
| 493 | _runs_observation: Option<Subscription>, |
| 494 | } |
| 495 | |
| 496 | fn preview_text(preview: &Preview) -> Option<(String, bool)> { |
| 497 | match preview { |
| 498 | Preview::Empty => None, |
| 499 | Preview::Deleted => Some((DELETED_CHAT_PREVIEW.to_owned(), true)), |
| 500 | Preview::Event(text) => Some((text.clone(), true)), |
| 501 | Preview::Typing(text) => Some((text.clone(), true)), |
| 502 | Preview::Text { |
| 503 | prefix: Some(prefix), |
| 504 | text, |
| 505 | } => Some((format!("{prefix}: {text}"), false)), |
| 506 | Preview::Text { prefix: None, text } => Some((text.clone(), false)), |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | fn local_preview_row(preview: &LocalPreview, color: Hsla) -> Div { |
| 511 | let row = h_flex() |
| 512 | .flex_1() |
| 513 | .min_w_0() |
| 514 | .overflow_hidden() |
| 515 | .gap(px(4.)) |
| 516 | .items_center() |
| 517 | .text_size(px(12.5)) |
| 518 | .text_color(color); |
| 519 | match preview { |
| 520 | LocalPreview::Draft(text) => row |
| 521 | .child( |
| 522 | div() |
| 523 | .flex_none() |
| 524 | .text_color(theme::accent_text()) |
| 525 | .child("Draft:"), |
| 526 | ) |
| 527 | .child(div().truncate().child(text.clone())), |
| 528 | LocalPreview::NotSent => row.child( |
| 529 | div() |
| 530 | .flex_none() |
| 531 | .text_color(theme::red_soft()) |
| 532 | .child("Not sent"), |
| 533 | ), |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | impl SidebarView { |
| 538 | pub fn new( |
| 539 | state: Entity<AppState>, |
| 540 | activity_panel: Entity<ActivityPanel>, |
| 541 | saved_panel: Entity<SavedPanel>, |
| 542 | agents_panel: Option<Entity<AgentsPanel>>, |
| 543 | cx: &mut Context<Self>, |
| 544 | ) -> Self { |
| 545 | let subscription = cx.subscribe(&state, |this, state, event: &AppEvent, cx| { |
| 546 | if matches!(event, AppEvent::Selection) { |
| 547 | if state.read(cx).new_chat { |
| 548 | this.tab = SidebarTab::Chats; |
| 549 | } |
| 550 | this.expand_team_of_selection(cx); |
| 551 | } |
| 552 | if matches!( |
| 553 | event, |
| 554 | AppEvent::Sidebar |
| 555 | | AppEvent::CallHistory |
| 556 | | AppEvent::Selection |
| 557 | | AppEvent::Directory |
| 558 | | AppEvent::Typing |
| 559 | | AppEvent::LocalPreviews |
| 560 | ) { |
| 561 | cx.notify(); |
| 562 | } |
| 563 | }); |
| 564 | let state_observation = cx.observe(&state, |_, _, cx| cx.notify()); |
| 565 | let agent_observation = |
| 566 | crate::agent_api::handle(cx).map(|api| cx.observe(&api, |_, _, cx| cx.notify())); |
| 567 | let runs_observation = crate::agents::runs_handle(cx).map(|runs| { |
| 568 | cx.observe(&runs, |view, _, cx| { |
| 569 | if view.tab == SidebarTab::Agents && !crate::agents::agents_enabled(cx) { |
| 570 | view.open_tab(SidebarTab::Chats, cx); |
| 571 | } |
| 572 | }) |
| 573 | }); |
| 574 | let tab = if state.read(cx).start_on_channels { |
| 575 | SidebarTab::Channels |
| 576 | } else { |
| 577 | SidebarTab::Chats |
| 578 | }; |
| 579 | let collapsed_teams = load_collapsed_teams(&state.read(cx).store); |
| 580 | cx.spawn(async move |this, cx| { |
| 581 | loop { |
| 582 | cx.background_executor().timer(CALLS_REFRESH).await; |
| 583 | let alive = this.update(cx, |view, cx| { |
| 584 | if view.tab == SidebarTab::Calls { |
| 585 | cx.notify(); |
| 586 | } |
| 587 | }); |
| 588 | if alive.is_err() { |
| 589 | break; |
| 590 | } |
| 591 | } |
| 592 | }) |
| 593 | .detach(); |
| 594 | SidebarView { |
| 595 | state, |
| 596 | tab, |
| 597 | activity_panel, |
| 598 | saved_panel, |
| 599 | agents_panel, |
| 600 | collapsed_teams, |
| 601 | revealed_channel_teams: HashSet::new(), |
| 602 | hidden_teams_open: false, |
| 603 | chats_scroll: ScrollHandle::new(), |
| 604 | older_chats_trigger_top: Cell::new(None), |
| 605 | older_chats_seen: Cell::new((false, false)), |
| 606 | channels_scroll: ScrollHandle::new(), |
| 607 | calls_scroll: ScrollHandle::new(), |
| 608 | _subscription: subscription, |
| 609 | _state_observation: state_observation, |
| 610 | _agent_observation: agent_observation, |
| 611 | _runs_observation: runs_observation, |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | pub fn tab(&self) -> SidebarTab { |
| 616 | self.tab |
| 617 | } |
| 618 | |
| 619 | pub fn show_activity(&mut self, cx: &mut Context<Self>) { |
| 620 | self.open_tab(SidebarTab::Activity, cx); |
| 621 | } |
| 622 | |
| 623 | pub fn show_chats(&mut self, cx: &mut Context<Self>) { |
| 624 | self.open_tab(SidebarTab::Chats, cx); |
| 625 | } |
| 626 | |
| 627 | pub fn show_channels(&mut self, cx: &mut Context<Self>) { |
| 628 | self.open_tab(SidebarTab::Channels, cx); |
| 629 | } |
| 630 | |
| 631 | pub fn show_calls(&mut self, cx: &mut Context<Self>) { |
| 632 | self.open_tab(SidebarTab::Calls, cx); |
| 633 | } |
| 634 | |
| 635 | pub fn show_saved(&mut self, cx: &mut Context<Self>) { |
| 636 | self.open_tab(SidebarTab::Saved, cx); |
| 637 | } |
| 638 | |
| 639 | pub fn show_agents(&mut self, cx: &mut Context<Self>) { |
| 640 | self.open_tab(SidebarTab::Agents, cx); |
| 641 | } |
| 642 | |
| 643 | pub fn open_tab(&mut self, tab: SidebarTab, cx: &mut Context<Self>) { |
| 644 | if tab == SidebarTab::Agents && !crate::agents::agents_enabled(cx) { |
| 645 | return; |
| 646 | } |
| 647 | self.tab = tab; |
| 648 | match tab { |
| 649 | SidebarTab::Calls => self.state.update(cx, |state, cx| { |
| 650 | state.refresh_call_history(cx); |
| 651 | state.refresh_calendar(cx); |
| 652 | }), |
| 653 | SidebarTab::Saved => self.state.update(cx, |state, cx| state.refresh_saved(cx)), |
| 654 | _ => {} |
| 655 | } |
| 656 | cx.notify(); |
| 657 | } |
| 658 | |
| 659 | pub fn select_adjacent(&mut self, step: Step, cx: &mut Context<Self>) { |
| 660 | let selection = match self.tab { |
| 661 | SidebarTab::Chats => self |
| 662 | .state |
| 663 | .read(cx) |
| 664 | .adjacent_chat_id(step) |
| 665 | .map(Selection::Chat), |
| 666 | SidebarTab::Channels => self.adjacent_channel_id(step, cx).map(Selection::Channel), |
| 667 | SidebarTab::Activity | SidebarTab::Calls | SidebarTab::Saved | SidebarTab::Agents => { |
| 668 | None |
| 669 | } |
| 670 | }; |
| 671 | if let Some(selection) = selection { |
| 672 | self.state |
| 673 | .update(cx, |state, cx| state.select(selection, cx)); |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | fn adjacent_channel_id(&self, step: Step, cx: &mut Context<Self>) -> Option<String> { |
| 678 | let state = self.state.read(cx); |
| 679 | let selected_channel = match &state.selection { |
| 680 | Some(Selection::Channel(channel_id)) => Some(channel_id.as_str()), |
| 681 | _ => None, |
| 682 | }; |
| 683 | let input = ChannelOrderInput { |
| 684 | teams: &state.sidebar.teams, |
| 685 | pinned_ids: &state.directory.pinned_channels, |
| 686 | collapsed_teams: &self.collapsed_teams, |
| 687 | revealed_channel_teams: &self.revealed_channel_teams, |
| 688 | hidden_teams_open: self.hidden_teams_open, |
| 689 | selected_channel, |
| 690 | }; |
| 691 | step_id(&visible_channel_ids(&input), selected_channel, step) |
| 692 | } |
| 693 | |
| 694 | fn expand_team_of_selection(&mut self, cx: &mut Context<Self>) { |
| 695 | let state = self.state.read(cx); |
| 696 | let Some(Selection::Channel(channel_id)) = state.selection.clone() else { |
| 697 | return; |
| 698 | }; |
| 699 | let team_id = state |
| 700 | .sidebar |
| 701 | .teams |
| 702 | .iter() |
| 703 | .find(|entry| { |
| 704 | entry |
| 705 | .channels |
| 706 | .iter() |
| 707 | .any(|channel| channel.id == channel_id) |
| 708 | }) |
| 709 | .map(|entry| entry.team.id.clone()); |
| 710 | if let Some(team_id) = team_id |
| 711 | && self.collapsed_teams.remove(&team_id) |
| 712 | { |
| 713 | save_collapsed_teams(&self.state.read(cx).store, &self.collapsed_teams); |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | fn new_chat_button(&self, cx: &mut Context<Self>) -> impl IntoElement { |
| 718 | let active = self.state.read(cx).new_chat; |
| 719 | div() |
| 720 | .id("new-chat") |
| 721 | .size(px(NEW_CHAT_BUTTON_SIZE)) |
| 722 | .flex_none() |
| 723 | .flex() |
| 724 | .items_center() |
| 725 | .justify_center() |
| 726 | .rounded(px(8.)) |
| 727 | .bg(theme::surface_raised()) |
| 728 | .cursor_pointer() |
| 729 | .when(active, |button| button.bg(theme::popup_hover())) |
| 730 | .hover(|button| button.bg(theme::popup_hover())) |
| 731 | .tooltip(|window, cx| Tooltip::new("New chat (Ctrl+N)").build(window, cx)) |
| 732 | .child(icon(IconName::Pencil, 16., theme::text_soft())) |
| 733 | .on_click(cx.listener(|this, _, _, cx| { |
| 734 | this.state.update(cx, |state, cx| state.start_new_chat(cx)); |
| 735 | })) |
| 736 | } |
| 737 | |
| 738 | fn chat_list_menu_button(&self, cx: &mut Context<Self>) -> impl IntoElement { |
| 739 | let state = self.state.clone(); |
| 740 | let read_only = self.state.read(cx).mode.read_only; |
| 741 | Button::new("chat-list-more") |
| 742 | .icon(IconName::Ellipsis) |
| 743 | .ghost() |
| 744 | .size(px(NEW_CHAT_BUTTON_SIZE)) |
| 745 | .rounded(px(8.)) |
| 746 | .tooltip("More options") |
| 747 | .dropdown_menu(move |menu, _, _| menu.item(new_section_item(&state, read_only))) |
| 748 | } |
| 749 | |
| 750 | fn draft_row(&self) -> impl IntoElement { |
| 751 | h_flex() |
| 752 | .id("new-chat-draft") |
| 753 | .mx(px(6.)) |
| 754 | .h(px(ROW_HEIGHT)) |
| 755 | .flex_none() |
| 756 | .px(px(10.)) |
| 757 | .gap(px(12.)) |
| 758 | .items_center() |
| 759 | .rounded(px(8.)) |
| 760 | .bg(theme::surface_raised()) |
| 761 | .child( |
| 762 | div() |
| 763 | .size(px(AVATAR_SIZE)) |
| 764 | .flex_none() |
| 765 | .flex() |
| 766 | .items_center() |
| 767 | .justify_center() |
| 768 | .rounded_full() |
| 769 | .bg(theme::border_strong()) |
| 770 | .child(icon(IconName::Pencil, 16., theme::text())), |
| 771 | ) |
| 772 | .child( |
| 773 | v_flex() |
| 774 | .flex_1() |
| 775 | .min_w_0() |
| 776 | .gap(px(2.)) |
| 777 | .child( |
| 778 | div() |
| 779 | .truncate() |
| 780 | .text_size(px(14.)) |
| 781 | .text_color(theme::text()) |
| 782 | .child("New chat"), |
| 783 | ) |
| 784 | .child( |
| 785 | div() |
| 786 | .truncate() |
| 787 | .text_size(px(12.5)) |
| 788 | .text_color(theme::text_muted()) |
| 789 | .child("Draft"), |
| 790 | ), |
| 791 | ) |
| 792 | } |
| 793 | |
| 794 | fn section_header( |
| 795 | &self, |
| 796 | section: &Section, |
| 797 | menu: SectionMenuTarget, |
| 798 | cx: &mut Context<Self>, |
| 799 | ) -> impl IntoElement { |
| 800 | let drop_target = drop_target(section); |
| 801 | let toggle_id = section.id.clone(); |
| 802 | let menu_state = self.state.clone(); |
| 803 | h_flex() |
| 804 | .id(SharedString::from(format!("section-{}", section.id))) |
| 805 | .h(px(SECTION_HEIGHT)) |
| 806 | .mt(px(SECTION_GAP)) |
| 807 | .mx(px(6.)) |
| 808 | .px(px(10.)) |
| 809 | .gap(px(6.)) |
| 810 | .items_center() |
| 811 | .rounded(px(6.)) |
| 812 | .cursor_pointer() |
| 813 | .hover(|header| header.bg(theme::row_hover())) |
| 814 | .when_some(drop_target, |header, drop_target| { |
| 815 | header |
| 816 | .drag_over::<DraggedChat>(|style, _, _, _| { |
| 817 | style |
| 818 | .bg(theme::drop_background()) |
| 819 | .border_1() |
| 820 | .border_color(theme::accent()) |
| 821 | }) |
| 822 | .on_drop(cx.listener(move |this, dragged: &DraggedChat, _, cx| { |
| 823 | let target = drop_target.clone(); |
| 824 | this.state.update(cx, |state, cx| { |
| 825 | state.move_chat(&dragged.id, target.as_deref(), cx) |
| 826 | }); |
| 827 | })) |
| 828 | }) |
| 829 | .child(icon( |
| 830 | if section.collapsed { |
| 831 | IconName::ChevronRight |
| 832 | } else { |
| 833 | IconName::ChevronDown |
| 834 | }, |
| 835 | 12., |
| 836 | theme::text_muted(), |
| 837 | )) |
| 838 | .child( |
| 839 | div() |
| 840 | .flex_1() |
| 841 | .min_w_0() |
| 842 | .truncate() |
| 843 | .text_size(px(12.)) |
| 844 | .font_weight(FontWeight::SEMIBOLD) |
| 845 | .text_color(theme::text_soft()) |
| 846 | .child(section.name.clone()), |
| 847 | ) |
| 848 | .when(section.collapsed && section.unread_chats > 0, |header| { |
| 849 | header.child(count_badge(section.unread_chats, false)) |
| 850 | }) |
| 851 | .child( |
| 852 | div() |
| 853 | .text_size(px(11.)) |
| 854 | .text_color(theme::text_faint()) |
| 855 | .child(section.count.to_string()), |
| 856 | ) |
| 857 | .on_click(cx.listener(move |this, _, _, cx| { |
| 858 | let id = toggle_id.clone(); |
| 859 | this.state |
| 860 | .update(cx, |state, cx| state.toggle_collapsed(&id, cx)); |
| 861 | })) |
| 862 | .context_menu(move |popup, _, _| section_menu(popup, &menu_state, &menu)) |
| 863 | } |
| 864 | |
| 865 | fn see_more_row(&self, section: &Section, cx: &mut Context<Self>) -> impl IntoElement { |
| 866 | let toggle_id = section.id.clone(); |
| 867 | let label = if section.hidden_count > 0 { |
| 868 | format!("See more ({})", section.hidden_count) |
| 869 | } else { |
| 870 | "See less".to_owned() |
| 871 | }; |
| 872 | div() |
| 873 | .id(SharedString::from(format!("see-more-{}", section.id))) |
| 874 | .mx(px(6.)) |
| 875 | .pl(px(10. + AVATAR_SIZE + 12.)) |
| 876 | .h(px(SEE_MORE_HEIGHT)) |
| 877 | .flex() |
| 878 | .items_center() |
| 879 | .rounded(px(6.)) |
| 880 | .cursor_pointer() |
| 881 | .text_size(px(12.5)) |
| 882 | .text_color(theme::accent_text()) |
| 883 | .hover(|row| row.bg(theme::row_hover())) |
| 884 | .child(label) |
| 885 | .on_click(cx.listener(move |this, _, _, cx| { |
| 886 | let id = toggle_id.clone(); |
| 887 | this.state |
| 888 | .update(cx, |state, cx| state.toggle_list_expanded(&id, cx)); |
| 889 | })) |
| 890 | } |
| 891 | |
| 892 | fn chat_row( |
| 893 | &self, |
| 894 | item: &ChatItem, |
| 895 | selected: bool, |
| 896 | local_preview: Option<&LocalPreview>, |
| 897 | proposal_agent: Option<&'static str>, |
| 898 | directory: &Directory, |
| 899 | menu: &MenuContext, |
| 900 | ) -> AnyElement { |
| 901 | let ring = if selected { |
| 902 | theme::surface_raised() |
| 903 | } else { |
| 904 | theme::surface() |
| 905 | }; |
| 906 | let unread = item.unread.is_unread(); |
| 907 | let presence = item |
| 908 | .presence_user |
| 909 | .as_deref() |
| 910 | .map(|user_id| directory.presence_of(user_id)); |
| 911 | let avatar = spec_avatar(directory, &item.avatar, AVATAR_SIZE, ring); |
| 912 | let avatar = match presence { |
| 913 | Some(kind) => with_presence(avatar, kind, AVATAR_SIZE, ring).into_any_element(), |
| 914 | None => avatar, |
| 915 | }; |
| 916 | let name = div() |
| 917 | .flex_1() |
| 918 | .min_w_0() |
| 919 | .truncate() |
| 920 | .text_size(px(14.)) |
| 921 | .font_weight(if unread { |
| 922 | FontWeight::BOLD |
| 923 | } else { |
| 924 | FontWeight::NORMAL |
| 925 | }) |
| 926 | .text_color(theme::text()) |
| 927 | .child(item.title.clone()); |
| 928 | let time = h_flex() |
| 929 | .flex_none() |
| 930 | .gap(px(4.)) |
| 931 | .items_center() |
| 932 | .text_size(px(11.)) |
| 933 | .text_color(if unread { |
| 934 | theme::accent_text() |
| 935 | } else { |
| 936 | theme::text_muted() |
| 937 | }) |
| 938 | .child(item.time_label.clone()); |
| 939 | let preview_color = if matches!(item.preview, Preview::Typing(_)) { |
| 940 | theme::accent_text() |
| 941 | } else if unread { |
| 942 | theme::text_strong() |
| 943 | } else { |
| 944 | theme::text_muted() |
| 945 | }; |
| 946 | let preview = match (proposal_agent, local_preview) { |
| 947 | (Some(agent), _) => Some( |
| 948 | h_flex() |
| 949 | .flex_1() |
| 950 | .min_w_0() |
| 951 | .overflow_hidden() |
| 952 | .text_size(px(12.5)) |
| 953 | .text_color(theme::accent_text()) |
| 954 | .child(div().truncate().child(format!("Proposal from {agent}"))), |
| 955 | ), |
| 956 | (None, Some(local)) => Some(local_preview_row(local, preview_color)), |
| 957 | (None, None) => preview_text(&item.preview).map(|(text, italic)| { |
| 958 | h_flex() |
| 959 | .flex_1() |
| 960 | .min_w_0() |
| 961 | .overflow_hidden() |
| 962 | .gap(px(4.)) |
| 963 | .items_center() |
| 964 | .text_size(px(12.5)) |
| 965 | .when(italic, |preview| preview.italic()) |
| 966 | .text_color(preview_color) |
| 967 | .child(div().truncate().child(text)) |
| 968 | }), |
| 969 | }; |
| 970 | let lines = v_flex() |
| 971 | .flex_1() |
| 972 | .min_w_0() |
| 973 | .gap(px(2.)) |
| 974 | .child( |
| 975 | h_flex() |
| 976 | .gap(px(8.)) |
| 977 | .items_baseline() |
| 978 | .child(name) |
| 979 | .child(time), |
| 980 | ) |
| 981 | .child( |
| 982 | h_flex() |
| 983 | .h(px(18.)) |
| 984 | .gap(px(8.)) |
| 985 | .items_center() |
| 986 | .child(preview.unwrap_or_else(|| div().flex_1())) |
| 987 | .children(match proposal_agent { |
| 988 | Some(_) => Some( |
| 989 | div() |
| 990 | .size(px(PROPOSAL_DOT_SIZE)) |
| 991 | .flex_none() |
| 992 | .rounded_full() |
| 993 | .bg(theme::accent()) |
| 994 | .into_any_element(), |
| 995 | ), |
| 996 | None => unread_marker(item.unread, item.muted) |
| 997 | .map(IntoElement::into_any_element), |
| 998 | }), |
| 999 | ); |
| 1000 | let state = self.state.clone(); |
| 1001 | let chat_id = item.id.clone(); |
| 1002 | let dragged = DraggedChat { |
| 1003 | id: item.id.clone(), |
| 1004 | title: item.title.clone(), |
| 1005 | }; |
| 1006 | let menu_context = menu.clone(); |
| 1007 | let target = ChatMenuTarget { |
| 1008 | id: item.id.clone(), |
| 1009 | title: item.title.clone(), |
| 1010 | folder_id: item.folder_id.clone(), |
| 1011 | unread, |
| 1012 | muted: item.muted, |
| 1013 | is_group: item.is_group, |
| 1014 | member_count: item.member_count, |
| 1015 | }; |
| 1016 | h_flex() |
| 1017 | .id(SharedString::from(format!("chat-{}", item.id))) |
| 1018 | .mx(px(6.)) |
| 1019 | .h(px(ROW_HEIGHT)) |
| 1020 | .flex_none() |
| 1021 | .px(px(10.)) |
| 1022 | .gap(px(12.)) |
| 1023 | .items_center() |
| 1024 | .rounded(px(8.)) |
| 1025 | .cursor_pointer() |
| 1026 | .when(selected, |row| row.bg(theme::surface_raised())) |
| 1027 | .when(!selected, |row| row.hover(|row| row.bg(theme::row_hover()))) |
| 1028 | .on_drag(dragged, |dragged, _, _, cx| { |
| 1029 | let title = dragged.title.clone(); |
| 1030 | cx.new(|_| DragGhost { title }) |
| 1031 | }) |
| 1032 | .child(avatar) |
| 1033 | .child(lines) |
| 1034 | .on_click(move |_, _, cx| { |
| 1035 | let selection = Selection::Chat(chat_id.clone()); |
| 1036 | state.update(cx, |state, cx| state.select(selection, cx)); |
| 1037 | }) |
| 1038 | .context_menu(move |popup, window, cx| { |
| 1039 | chat_menu(popup, window, cx, &menu_context, &target) |
| 1040 | }) |
| 1041 | .into_any_element() |
| 1042 | } |
| 1043 | |
| 1044 | fn chats_body(&self, window_height: f32, cx: &mut Context<Self>) -> AnyElement { |
| 1045 | let proposal_agents = crate::agent_api::pending_agents(cx); |
| 1046 | let (sections, section_menus, menu, local_previews) = { |
| 1047 | let state = self.state.read(cx); |
| 1048 | let now = Utc::now(); |
| 1049 | let offset = Local::now().offset().fix(); |
| 1050 | let input = SectionInput { |
| 1051 | chats: &state.sidebar.chats, |
| 1052 | directory: &state.directory, |
| 1053 | collapsed: &state.collapsed, |
| 1054 | expanded_lists: &state.expanded_lists, |
| 1055 | typing: &state.typing, |
| 1056 | now, |
| 1057 | offset, |
| 1058 | }; |
| 1059 | let sections = build_sections(&input); |
| 1060 | let menu = MenuContext { |
| 1061 | state: self.state.clone(), |
| 1062 | folders: state.directory.move_targets(), |
| 1063 | }; |
| 1064 | let section_menus = |
| 1065 | section_menu_targets(§ions, &state.directory, state.mode.read_only); |
| 1066 | (sections, section_menus, menu, state.local_previews.clone()) |
| 1067 | }; |
| 1068 | |
| 1069 | let top = -f32::from(self.chats_scroll.offset().y); |
| 1070 | let measured: f32 = f32::from(self.chats_scroll.bounds().size.height); |
| 1071 | let height = if measured > 1. { |
| 1072 | measured |
| 1073 | } else { |
| 1074 | window_height.max(FALLBACK_VIEWPORT) |
| 1075 | }; |
| 1076 | let (visible_from, visible_to) = (top - VIEWPORT_MARGIN, top + height + VIEWPORT_MARGIN); |
| 1077 | |
| 1078 | let (selected, drafting) = { |
| 1079 | let state = self.state.read(cx); |
| 1080 | ( |
| 1081 | state.selection.clone().filter(|_| !state.new_chat), |
| 1082 | state.new_chat, |
| 1083 | ) |
| 1084 | }; |
| 1085 | let mut faces: Vec<String> = Vec::new(); |
| 1086 | let mut presence_users: Vec<String> = Vec::new(); |
| 1087 | let mut y = 0.; |
| 1088 | let mut list = v_flex().w_full().pb(px(12.)); |
| 1089 | let mut skipped_height = 0.; |
| 1090 | let mut others_end = None; |
| 1091 | let directory_state = self.state.clone(); |
| 1092 | if drafting { |
| 1093 | list = list.child(self.draft_row()); |
| 1094 | y += ROW_HEIGHT; |
| 1095 | } |
| 1096 | for (section, section_menu) in sections.iter().zip(section_menus) { |
| 1097 | list = with_spacer(list, &mut skipped_height); |
| 1098 | list = list.child(self.section_header(section, section_menu, cx)); |
| 1099 | y += SECTION_GAP + SECTION_HEIGHT; |
| 1100 | if section.collapsed { |
| 1101 | continue; |
| 1102 | } |
| 1103 | for item in §ion.items { |
| 1104 | let on_screen = y + ROW_HEIGHT >= visible_from && y <= visible_to; |
| 1105 | if on_screen { |
| 1106 | collect_faces(&item.avatar, &mut faces); |
| 1107 | presence_users.extend(item.presence_user.clone()); |
| 1108 | } |
| 1109 | if on_screen { |
| 1110 | list = with_spacer(list, &mut skipped_height); |
| 1111 | let is_selected = selected == Some(Selection::Chat(item.id.clone())); |
| 1112 | let directory = &directory_state.read(cx).directory; |
| 1113 | let local_preview = local_previews.get(&item.id).filter(|_| !is_selected); |
| 1114 | list = list.child(self.chat_row( |
| 1115 | item, |
| 1116 | is_selected, |
| 1117 | local_preview, |
| 1118 | proposal_agents.get(&item.id).copied(), |
| 1119 | directory, |
| 1120 | &menu, |
| 1121 | )); |
| 1122 | } else { |
| 1123 | skipped_height += ROW_HEIGHT; |
| 1124 | } |
| 1125 | y += ROW_HEIGHT; |
| 1126 | } |
| 1127 | if section.kind == SectionKind::Others { |
| 1128 | others_end = Some(y); |
| 1129 | } |
| 1130 | if section.hidden_count > 0 || section.list_expanded { |
| 1131 | list = with_spacer(list, &mut skipped_height); |
| 1132 | list = list.child(self.see_more_row(section, cx)); |
| 1133 | y += SEE_MORE_HEIGHT; |
| 1134 | } |
| 1135 | if section.show_empty_hint { |
| 1136 | list = with_spacer(list, &mut skipped_height); |
| 1137 | list = list.child( |
| 1138 | div() |
| 1139 | .ml(px(16.)) |
| 1140 | .mr(px(16.)) |
| 1141 | .h(px(HINT_HEIGHT)) |
| 1142 | .flex() |
| 1143 | .items_center() |
| 1144 | .text_size(px(12.)) |
| 1145 | .italic() |
| 1146 | .text_color(theme::text_faint()) |
| 1147 | .child(EMPTY_FOLDER_HINT), |
| 1148 | ); |
| 1149 | y += HINT_HEIGHT; |
| 1150 | } |
| 1151 | } |
| 1152 | let (has_older, loading_older) = self.state.read(cx).older_chats(); |
| 1153 | let previous = self.older_chats_seen.replace((has_older, loading_older)); |
| 1154 | if older_chats_trigger_resets(previous, (has_older, loading_older)) { |
| 1155 | self.older_chats_trigger_top.set(None); |
| 1156 | } |
| 1157 | if has_older |
| 1158 | && wants_older_chats( |
| 1159 | others_end, |
| 1160 | top + height, |
| 1161 | top, |
| 1162 | self.older_chats_trigger_top.get(), |
| 1163 | loading_older, |
| 1164 | ) |
| 1165 | { |
| 1166 | self.older_chats_trigger_top.set(Some(top)); |
| 1167 | let state = self.state.clone(); |
| 1168 | cx.defer(move |cx| state.read(cx).request_older_chats()); |
| 1169 | } |
| 1170 | if loading_older { |
| 1171 | list = with_spacer(list, &mut skipped_height); |
| 1172 | list = list.child( |
| 1173 | div() |
| 1174 | .pl(px(16. + AVATAR_SIZE + 12.)) |
| 1175 | .h(px(SEE_MORE_HEIGHT)) |
| 1176 | .flex() |
| 1177 | .items_center() |
| 1178 | .text_size(px(12.5)) |
| 1179 | .italic() |
| 1180 | .text_color(theme::text_faint()) |
| 1181 | .child(LOADING_OLDER_LABEL), |
| 1182 | ); |
| 1183 | } |
| 1184 | list = with_spacer(list, &mut skipped_height); |
| 1185 | self.request_lazy_data(faces, presence_users, cx); |
| 1186 | let scroll_area = div() |
| 1187 | .id("chats-scroll") |
| 1188 | .flex_1() |
| 1189 | .min_h_0() |
| 1190 | .overflow_y_scroll() |
| 1191 | .track_scroll(&self.chats_scroll) |
| 1192 | .child(list); |
| 1193 | with_top_edge(scroll_area, &self.chats_scroll, SIDEBAR_SCROLL_DIVIDER).into_any_element() |
| 1194 | } |
| 1195 | |
| 1196 | fn request_lazy_data( |
| 1197 | &self, |
| 1198 | faces: Vec<String>, |
| 1199 | presence_users: Vec<String>, |
| 1200 | cx: &mut Context<Self>, |
| 1201 | ) { |
| 1202 | if faces.is_empty() && presence_users.is_empty() { |
| 1203 | return; |
| 1204 | } |
| 1205 | let state = self.state.clone(); |
| 1206 | cx.defer(move |cx| { |
| 1207 | state.update(cx, |state, cx| { |
| 1208 | state.request_avatars(faces, cx); |
| 1209 | state.request_presence(presence_users, cx); |
| 1210 | }); |
| 1211 | }); |
| 1212 | } |
| 1213 | |
| 1214 | fn channel_row( |
| 1215 | &self, |
| 1216 | channel_id: &str, |
| 1217 | team: &TeamRecord, |
| 1218 | channel: &ChannelRecord, |
| 1219 | selected: bool, |
| 1220 | notifications: ChannelNotifications, |
| 1221 | ) -> AnyElement { |
| 1222 | let unread = channel.unread; |
| 1223 | let name_style = channel_name_style(unread, true, selected); |
| 1224 | let state = self.state.clone(); |
| 1225 | let menu_state = self.state.clone(); |
| 1226 | let menu_channel_id = channel_id.to_owned(); |
| 1227 | let selection = Selection::Channel(channel_id.to_owned()); |
| 1228 | let now = Utc::now(); |
| 1229 | let offset = Local::now().offset().fix(); |
| 1230 | let time = channel |
| 1231 | .last_message_at |
| 1232 | .map(|time| { |
| 1233 | format::list_time_label(time, now.with_timezone(&offset).date_naive(), offset) |
| 1234 | }) |
| 1235 | .unwrap_or_default(); |
| 1236 | h_flex() |
| 1237 | .id(SharedString::from(format!("pinned-{channel_id}"))) |
| 1238 | .mx(px(6.)) |
| 1239 | .h(px(PINNED_CHANNEL_HEIGHT)) |
| 1240 | .px(px(10.)) |
| 1241 | .gap(px(12.)) |
| 1242 | .items_center() |
| 1243 | .rounded(px(8.)) |
| 1244 | .cursor_pointer() |
| 1245 | .when(selected, |row| row.bg(theme::surface_raised())) |
| 1246 | .when(!selected, |row| row.hover(|row| row.bg(theme::row_hover()))) |
| 1247 | .child(square_avatar(&team.name, &team.id, 28., 7.)) |
| 1248 | .child( |
| 1249 | v_flex() |
| 1250 | .flex_1() |
| 1251 | .min_w_0() |
| 1252 | .gap(px(1.)) |
| 1253 | .child( |
| 1254 | h_flex() |
| 1255 | .gap(px(8.)) |
| 1256 | .items_baseline() |
| 1257 | .child( |
| 1258 | div() |
| 1259 | .flex_1() |
| 1260 | .min_w_0() |
| 1261 | .truncate() |
| 1262 | .text_size(px(13.5)) |
| 1263 | .font_weight(name_weight(name_style)) |
| 1264 | .text_color(name_color(name_style)) |
| 1265 | .child(format!("{} / {}", team.name, channel.name)), |
| 1266 | ) |
| 1267 | .children(notification_badge(notifications.level)) |
| 1268 | .child( |
| 1269 | div() |
| 1270 | .flex_none() |
| 1271 | .text_size(px(11.)) |
| 1272 | .text_color(if unread { |
| 1273 | theme::accent_text() |
| 1274 | } else { |
| 1275 | theme::text_muted() |
| 1276 | }) |
| 1277 | .child(time), |
| 1278 | ), |
| 1279 | ) |
| 1280 | .child( |
| 1281 | h_flex().gap(px(8.)).items_center().child( |
| 1282 | div() |
| 1283 | .flex_1() |
| 1284 | .min_w_0() |
| 1285 | .truncate() |
| 1286 | .text_size(px(12.)) |
| 1287 | .text_color(theme::text_muted()) |
| 1288 | .child(team.name.clone()), |
| 1289 | ), |
| 1290 | ), |
| 1291 | ) |
| 1292 | .on_click(move |_, _, cx| { |
| 1293 | let selection = selection.clone(); |
| 1294 | state.update(cx, |state, cx| state.select(selection, cx)); |
| 1295 | }) |
| 1296 | .context_menu(move |popup, window, cx| { |
| 1297 | channel_menu( |
| 1298 | popup, |
| 1299 | window, |
| 1300 | cx, |
| 1301 | menu_state.clone(), |
| 1302 | &menu_channel_id, |
| 1303 | notifications, |
| 1304 | ) |
| 1305 | }) |
| 1306 | .into_any_element() |
| 1307 | } |
| 1308 | |
| 1309 | fn plain_header(&self, label: &str) -> impl IntoElement { |
| 1310 | div() |
| 1311 | .h(px(SECTION_HEIGHT)) |
| 1312 | .mt(px(SECTION_GAP)) |
| 1313 | .px(px(16.)) |
| 1314 | .flex() |
| 1315 | .items_center() |
| 1316 | .text_size(px(12.)) |
| 1317 | .font_weight(FontWeight::SEMIBOLD) |
| 1318 | .text_color(theme::text_soft()) |
| 1319 | .child(label.to_owned()) |
| 1320 | } |
| 1321 | |
| 1322 | fn team_block( |
| 1323 | &self, |
| 1324 | mut list: Div, |
| 1325 | entry: &SidebarTeam, |
| 1326 | selected: Option<&Selection>, |
| 1327 | cx: &mut Context<Self>, |
| 1328 | ) -> Div { |
| 1329 | let team_id = entry.team.id.clone(); |
| 1330 | let expanded = team_expanded(&team_id, &self.collapsed_teams); |
| 1331 | let name_style = team_name_style(entry, expanded); |
| 1332 | let toggle_id = team_id.clone(); |
| 1333 | list = list.child( |
| 1334 | h_flex() |
| 1335 | .id(SharedString::from(format!("team-{team_id}"))) |
| 1336 | .mx(px(6.)) |
| 1337 | .h(px(TEAM_ROW_HEIGHT)) |
| 1338 | .flex_none() |
| 1339 | .px(px(10.)) |
| 1340 | .gap(px(8.)) |
| 1341 | .items_center() |
| 1342 | .rounded(px(8.)) |
| 1343 | .cursor_pointer() |
| 1344 | .hover(|row| row.bg(theme::row_hover())) |
| 1345 | .child(icon( |
| 1346 | if expanded { |
| 1347 | IconName::ChevronDown |
| 1348 | } else { |
| 1349 | IconName::ChevronRight |
| 1350 | }, |
| 1351 | 14., |
| 1352 | theme::text_muted(), |
| 1353 | )) |
| 1354 | .child(square_avatar(&entry.team.name, &entry.team.id, 28., 7.)) |
| 1355 | .child( |
| 1356 | div() |
| 1357 | .flex_1() |
| 1358 | .min_w_0() |
| 1359 | .truncate() |
| 1360 | .text_size(px(13.5)) |
| 1361 | .font_weight(name_weight(name_style)) |
| 1362 | .text_color(name_color(name_style)) |
| 1363 | .child(entry.team.name.clone()), |
| 1364 | ) |
| 1365 | .on_click(cx.listener(move |this, _, _, cx| { |
| 1366 | if !this.collapsed_teams.remove(&toggle_id) { |
| 1367 | this.collapsed_teams.insert(toggle_id.clone()); |
| 1368 | } |
| 1369 | save_collapsed_teams(&this.state.read(cx).store, &this.collapsed_teams); |
| 1370 | cx.notify(); |
| 1371 | })), |
| 1372 | ); |
| 1373 | if !expanded { |
| 1374 | return list; |
| 1375 | } |
| 1376 | let is_hidden = |channel: &ChannelRecord| entry.hidden_channel_ids.contains(&channel.id); |
| 1377 | let is_selected = |
| 1378 | |channel: &ChannelRecord| selected == Some(&Selection::Channel(channel.id.clone())); |
| 1379 | for channel in entry |
| 1380 | .channels |
| 1381 | .iter() |
| 1382 | .filter(|channel| !is_hidden(channel) || is_selected(channel)) |
| 1383 | { |
| 1384 | list = list.child(self.team_channel_row( |
| 1385 | channel, |
| 1386 | is_selected(channel), |
| 1387 | channel_name_style(channel.unread, !is_hidden(channel), is_selected(channel)), |
| 1388 | entry.channel_notifications(&channel.id), |
| 1389 | )); |
| 1390 | } |
| 1391 | let hidden: Vec<&ChannelRecord> = entry |
| 1392 | .channels |
| 1393 | .iter() |
| 1394 | .filter(|channel| is_hidden(channel) && !is_selected(channel)) |
| 1395 | .collect(); |
| 1396 | if hidden.is_empty() { |
| 1397 | return list; |
| 1398 | } |
| 1399 | let revealed = self.revealed_channel_teams.contains(&team_id); |
| 1400 | let toggle_id = team_id.clone(); |
| 1401 | list = list.child(self.reveal_row( |
| 1402 | SharedString::from(format!("hidden-channels-{team_id}")), |
| 1403 | hidden_channels_label(hidden.len()), |
| 1404 | revealed, |
| 1405 | 40., |
| 1406 | cx.listener(move |this, _, _, cx| { |
| 1407 | if !this.revealed_channel_teams.remove(&toggle_id) { |
| 1408 | this.revealed_channel_teams.insert(toggle_id.clone()); |
| 1409 | } |
| 1410 | cx.notify(); |
| 1411 | }), |
| 1412 | )); |
| 1413 | if revealed { |
| 1414 | for channel in hidden { |
| 1415 | list = list.child(self.team_channel_row( |
| 1416 | channel, |
| 1417 | false, |
| 1418 | channel_name_style(channel.unread, false, false), |
| 1419 | entry.channel_notifications(&channel.id), |
| 1420 | )); |
| 1421 | } |
| 1422 | } |
| 1423 | list |
| 1424 | } |
| 1425 | |
| 1426 | fn reveal_row( |
| 1427 | &self, |
| 1428 | id: SharedString, |
| 1429 | label: String, |
| 1430 | open: bool, |
| 1431 | indent: f32, |
| 1432 | on_click: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static, |
| 1433 | ) -> impl IntoElement { |
| 1434 | h_flex() |
| 1435 | .id(id) |
| 1436 | .mx(px(6.)) |
| 1437 | .h(px(REVEAL_ROW_HEIGHT)) |
| 1438 | .flex_none() |
| 1439 | .pl(px(indent)) |
| 1440 | .pr(px(10.)) |
| 1441 | .gap(px(8.)) |
| 1442 | .items_center() |
| 1443 | .rounded(px(8.)) |
| 1444 | .cursor_pointer() |
| 1445 | .hover(|row| row.bg(theme::row_hover())) |
| 1446 | .child(icon( |
| 1447 | if open { |
| 1448 | IconName::ChevronDown |
| 1449 | } else { |
| 1450 | IconName::ChevronRight |
| 1451 | }, |
| 1452 | 12., |
| 1453 | theme::text_faint(), |
| 1454 | )) |
| 1455 | .child( |
| 1456 | div() |
| 1457 | .text_size(px(12.5)) |
| 1458 | .text_color(theme::text_muted()) |
| 1459 | .child(label), |
| 1460 | ) |
| 1461 | .on_click(on_click) |
| 1462 | } |
| 1463 | |
| 1464 | fn team_channel_row( |
| 1465 | &self, |
| 1466 | channel: &ChannelRecord, |
| 1467 | is_selected: bool, |
| 1468 | name_style: NameStyle, |
| 1469 | notifications: ChannelNotifications, |
| 1470 | ) -> impl IntoElement { |
| 1471 | let state = self.state.clone(); |
| 1472 | let menu_state = self.state.clone(); |
| 1473 | let menu_channel_id = channel.id.clone(); |
| 1474 | let selection = Selection::Channel(channel.id.clone()); |
| 1475 | h_flex() |
| 1476 | .id(SharedString::from(format!("channel-{}", channel.id))) |
| 1477 | .mx(px(6.)) |
| 1478 | .h(px(TEAM_ROW_HEIGHT)) |
| 1479 | .flex_none() |
| 1480 | .pl(px(40.)) |
| 1481 | .pr(px(10.)) |
| 1482 | .gap(px(8.)) |
| 1483 | .items_center() |
| 1484 | .rounded(px(8.)) |
| 1485 | .cursor_pointer() |
| 1486 | .when(is_selected, |row| row.bg(theme::surface_raised())) |
| 1487 | .when(!is_selected, |row| { |
| 1488 | row.hover(|row| row.bg(theme::row_hover())) |
| 1489 | }) |
| 1490 | .child( |
| 1491 | div() |
| 1492 | .w(px(14.)) |
| 1493 | .flex() |
| 1494 | .justify_center() |
| 1495 | .text_size(px(15.)) |
| 1496 | .text_color(theme::text_faint()) |
| 1497 | .child("#"), |
| 1498 | ) |
| 1499 | .child( |
| 1500 | div() |
| 1501 | .flex_1() |
| 1502 | .min_w_0() |
| 1503 | .truncate() |
| 1504 | .text_size(px(13.5)) |
| 1505 | .font_weight(name_weight(name_style)) |
| 1506 | .text_color(name_color(name_style)) |
| 1507 | .child(channel.name.clone()), |
| 1508 | ) |
| 1509 | .children(notification_badge(notifications.level)) |
| 1510 | .on_click(move |_, _, cx| { |
| 1511 | let selection = selection.clone(); |
| 1512 | state.update(cx, |state, cx| state.select(selection, cx)); |
| 1513 | }) |
| 1514 | .context_menu(move |popup, window, cx| { |
| 1515 | channel_menu( |
| 1516 | popup, |
| 1517 | window, |
| 1518 | cx, |
| 1519 | menu_state.clone(), |
| 1520 | &menu_channel_id, |
| 1521 | notifications, |
| 1522 | ) |
| 1523 | }) |
| 1524 | } |
| 1525 | |
| 1526 | fn channels_body(&self, cx: &mut Context<Self>) -> AnyElement { |
| 1527 | let (sidebar, pinned_ids, selected) = { |
| 1528 | let state = self.state.read(cx); |
| 1529 | ( |
| 1530 | state.sidebar.clone(), |
| 1531 | state.directory.pinned_channels.clone(), |
| 1532 | state.selection.clone().filter(|_| !state.new_chat), |
| 1533 | ) |
| 1534 | }; |
| 1535 | let mut list = v_flex().w_full().pb(px(12.)); |
| 1536 | let pinned: Vec<(&SidebarTeam, &ChannelRecord)> = pinned_ids |
| 1537 | .iter() |
| 1538 | .filter_map(|id| { |
| 1539 | sidebar.teams.iter().find_map(|entry| { |
| 1540 | entry |
| 1541 | .channels |
| 1542 | .iter() |
| 1543 | .find(|channel| &channel.id == id) |
| 1544 | .map(|channel| (entry, channel)) |
| 1545 | }) |
| 1546 | }) |
| 1547 | .collect(); |
| 1548 | if !pinned.is_empty() { |
| 1549 | list = list.child(self.plain_header(PINNED_LABEL)); |
| 1550 | for (entry, channel) in pinned { |
| 1551 | let is_selected = selected == Some(Selection::Channel(channel.id.clone())); |
| 1552 | list = list.child(self.channel_row( |
| 1553 | &channel.id, |
| 1554 | &entry.team, |
| 1555 | channel, |
| 1556 | is_selected, |
| 1557 | entry.channel_notifications(&channel.id), |
| 1558 | )); |
| 1559 | } |
| 1560 | } |
| 1561 | list = list.child(self.plain_header(TEAMS_LABEL)); |
| 1562 | let (shown_teams, hidden_teams): (Vec<&SidebarTeam>, Vec<&SidebarTeam>) = |
| 1563 | sidebar.teams.iter().partition(|entry| !entry.hidden); |
| 1564 | for entry in shown_teams { |
| 1565 | list = self.team_block(list, entry, selected.as_ref(), cx); |
| 1566 | } |
| 1567 | if !hidden_teams.is_empty() { |
| 1568 | let holds_selection = hidden_teams |
| 1569 | .iter() |
| 1570 | .any(|entry| team_holds(entry, selected.as_ref())); |
| 1571 | let open = self.hidden_teams_open || holds_selection; |
| 1572 | list = list.child(self.reveal_row( |
| 1573 | "hidden-teams".into(), |
| 1574 | format!("{HIDDEN_TEAMS_LABEL} ({})", hidden_teams.len()), |
| 1575 | open, |
| 1576 | 10., |
| 1577 | cx.listener(|this, _, _, cx| { |
| 1578 | this.hidden_teams_open = !this.hidden_teams_open; |
| 1579 | cx.notify(); |
| 1580 | }), |
| 1581 | )); |
| 1582 | if open { |
| 1583 | for entry in hidden_teams { |
| 1584 | list = self.team_block(list, entry, selected.as_ref(), cx); |
| 1585 | } |
| 1586 | } |
| 1587 | } |
| 1588 | let scroll_area = div() |
| 1589 | .id("channels-scroll") |
| 1590 | .flex_1() |
| 1591 | .min_h_0() |
| 1592 | .overflow_y_scroll() |
| 1593 | .track_scroll(&self.channels_scroll) |
| 1594 | .child(list); |
| 1595 | with_top_edge(scroll_area, &self.channels_scroll, SIDEBAR_SCROLL_DIVIDER).into_any_element() |
| 1596 | } |
| 1597 | } |
| 1598 | |
| 1599 | fn name_weight(style: NameStyle) -> FontWeight { |
| 1600 | match style.weight { |
| 1601 | NameWeight::Regular => FontWeight::NORMAL, |
| 1602 | NameWeight::Semibold => FontWeight::SEMIBOLD, |
| 1603 | NameWeight::Bold => FontWeight::BOLD, |
| 1604 | } |
| 1605 | } |
| 1606 | |
| 1607 | fn name_color(style: NameStyle) -> Hsla { |
| 1608 | if style.highlighted { |
| 1609 | theme::text() |
| 1610 | } else { |
| 1611 | theme::text_muted() |
| 1612 | } |
| 1613 | } |
| 1614 | |
| 1615 | fn notification_badge(level: ChannelNotificationLevel) -> Option<impl IntoElement> { |
| 1616 | let name = match level { |
| 1617 | ChannelNotificationLevel::BannerAndFeed => IconName::Bell, |
| 1618 | ChannelNotificationLevel::Off => IconName::BellOff, |
| 1619 | ChannelNotificationLevel::Feed => return None, |
| 1620 | }; |
| 1621 | Some(icon(name, NOTIFICATION_ICON_SIZE, theme::text_muted())) |
| 1622 | } |
| 1623 | |
| 1624 | fn channel_menu( |
| 1625 | popup: PopupMenu, |
| 1626 | window: &mut Window, |
| 1627 | cx: &mut Context<PopupMenu>, |
| 1628 | state: Entity<AppState>, |
| 1629 | channel_id: &str, |
| 1630 | notifications: ChannelNotifications, |
| 1631 | ) -> PopupMenu { |
| 1632 | let channel_id = channel_id.to_owned(); |
| 1633 | popup.submenu_with_icon( |
| 1634 | Some(menu_icon(IconName::Bell)), |
| 1635 | "Notifications", |
| 1636 | window, |
| 1637 | cx, |
| 1638 | move |submenu, _, _| { |
| 1639 | let level_item = |label: &'static str, level: ChannelNotificationLevel| { |
| 1640 | let (state, channel_id) = (state.clone(), channel_id.clone()); |
| 1641 | PopupMenuItem::new(label) |
| 1642 | .checked(notifications.level == level) |
| 1643 | .on_click(move |_, _, cx| { |
| 1644 | let changed = ChannelNotifications { |
| 1645 | level, |
| 1646 | include_replies: notifications.include_replies |
| 1647 | && level != ChannelNotificationLevel::Off, |
| 1648 | }; |
| 1649 | state.update(cx, |state, cx| { |
| 1650 | state.set_channel_notifications(&channel_id, changed, cx) |
| 1651 | }); |
| 1652 | }) |
| 1653 | }; |
| 1654 | let (replies_state, replies_id) = (state.clone(), channel_id.clone()); |
| 1655 | submenu |
| 1656 | .item(level_item( |
| 1657 | "Banner and activity", |
| 1658 | ChannelNotificationLevel::BannerAndFeed, |
| 1659 | )) |
| 1660 | .item(level_item("Activity only", ChannelNotificationLevel::Feed)) |
| 1661 | .item(level_item("Off", ChannelNotificationLevel::Off)) |
| 1662 | .separator() |
| 1663 | .item( |
| 1664 | PopupMenuItem::new("Include thread replies") |
| 1665 | .checked(notifications.include_replies) |
| 1666 | .disabled(notifications.level == ChannelNotificationLevel::Off) |
| 1667 | .on_click(move |_, _, cx| { |
| 1668 | let changed = ChannelNotifications { |
| 1669 | level: notifications.level, |
| 1670 | include_replies: !notifications.include_replies, |
| 1671 | }; |
| 1672 | replies_state.update(cx, |state, cx| { |
| 1673 | state.set_channel_notifications(&replies_id, changed, cx) |
| 1674 | }); |
| 1675 | }), |
| 1676 | ) |
| 1677 | }, |
| 1678 | ) |
| 1679 | } |
| 1680 | |
| 1681 | fn team_expanded(team_id: &str, collapsed_teams: &HashSet<String>) -> bool { |
| 1682 | !collapsed_teams.contains(team_id) |
| 1683 | } |
| 1684 | |
| 1685 | fn load_collapsed_teams(store: &Store) -> HashSet<String> { |
| 1686 | store |
| 1687 | .meta(COLLAPSED_TEAMS_META_KEY) |
| 1688 | .ok() |
| 1689 | .flatten() |
| 1690 | .map(|value| parse_collapsed_teams(&value)) |
| 1691 | .unwrap_or_default() |
| 1692 | } |
| 1693 | |
| 1694 | fn save_collapsed_teams(store: &Store, collapsed_teams: &HashSet<String>) { |
| 1695 | let _ = store.set_meta( |
| 1696 | COLLAPSED_TEAMS_META_KEY, |
| 1697 | &serialize_collapsed_teams(collapsed_teams), |
| 1698 | ); |
| 1699 | } |
| 1700 | |
| 1701 | fn serialize_collapsed_teams(collapsed_teams: &HashSet<String>) -> String { |
| 1702 | let mut team_ids: Vec<&String> = collapsed_teams.iter().collect(); |
| 1703 | team_ids.sort_unstable(); |
| 1704 | serde_json::to_string(&team_ids).unwrap_or_default() |
| 1705 | } |
| 1706 | |
| 1707 | fn parse_collapsed_teams(value: &str) -> HashSet<String> { |
| 1708 | serde_json::from_str(value).unwrap_or_default() |
| 1709 | } |
| 1710 | |
| 1711 | fn team_holds(entry: &SidebarTeam, selected: Option<&Selection>) -> bool { |
| 1712 | entry |
| 1713 | .channels |
| 1714 | .iter() |
| 1715 | .any(|channel| selected == Some(&Selection::Channel(channel.id.clone()))) |
| 1716 | } |
| 1717 | |
| 1718 | fn hidden_channels_label(count: usize) -> String { |
| 1719 | if count == 1 { |
| 1720 | "1 hidden channel".to_owned() |
| 1721 | } else { |
| 1722 | format!("{count} hidden channels") |
| 1723 | } |
| 1724 | } |
| 1725 | |
| 1726 | fn collect_faces(spec: &AvatarSpec, faces: &mut Vec<String>) { |
| 1727 | match spec { |
| 1728 | AvatarSpec::Single(face) | AvatarSpec::Bot(face) => faces.extend(face.user_id.clone()), |
| 1729 | AvatarSpec::Pair(first, second) => { |
| 1730 | faces.extend(first.user_id.clone()); |
| 1731 | faces.extend(second.user_id.clone()); |
| 1732 | } |
| 1733 | AvatarSpec::Meeting | AvatarSpec::Muted => {} |
| 1734 | } |
| 1735 | } |
| 1736 | |
| 1737 | fn with_spacer(list: Div, skipped_height: &mut f32) -> Div { |
| 1738 | let height = std::mem::take(skipped_height); |
| 1739 | if height > 0. { |
| 1740 | list.child(div().h(px(height)).flex_none()) |
| 1741 | } else { |
| 1742 | list |
| 1743 | } |
| 1744 | } |
| 1745 | |
| 1746 | impl Render for SidebarView { |
| 1747 | fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 1748 | let window_height = f32::from(window.viewport_size().height); |
| 1749 | let body = match self.tab { |
| 1750 | SidebarTab::Activity => self.activity_panel.clone().into_any_element(), |
| 1751 | SidebarTab::Chats => self.chats_body(window_height, cx), |
| 1752 | SidebarTab::Channels => self.channels_body(cx), |
| 1753 | SidebarTab::Calls => calls_body(&self.state, &self.calls_scroll, window_height, cx), |
| 1754 | SidebarTab::Saved => self.saved_panel.clone().into_any_element(), |
| 1755 | SidebarTab::Agents => match &self.agents_panel { |
| 1756 | Some(panel) => panel.clone().into_any_element(), |
| 1757 | None => div().into_any_element(), |
| 1758 | }, |
| 1759 | }; |
| 1760 | let header = match self.tab { |
| 1761 | SidebarTab::Chats => Some( |
| 1762 | page_header(self.tab.title()) |
| 1763 | .child(self.chat_list_menu_button(cx)) |
| 1764 | .child(self.new_chat_button(cx)), |
| 1765 | ), |
| 1766 | SidebarTab::Channels | SidebarTab::Calls | SidebarTab::Agents => { |
| 1767 | Some(page_header(self.tab.title())) |
| 1768 | } |
| 1769 | SidebarTab::Activity | SidebarTab::Saved => None, |
| 1770 | }; |
| 1771 | v_flex() |
| 1772 | .debug_selector(|| "shell/islands/sidebar".into()) |
| 1773 | .w(px(SIDEBAR_WIDTH)) |
| 1774 | .h_full() |
| 1775 | .flex_none() |
| 1776 | .bg(theme::surface()) |
| 1777 | .rounded(px(10.)) |
| 1778 | .overflow_hidden() |
| 1779 | .children(header) |
| 1780 | .child(body) |
| 1781 | } |
| 1782 | } |
| 1783 | |
| 1784 | #[cfg(test)] |
| 1785 | mod tests { |
| 1786 | use std::collections::HashSet; |
| 1787 | |
| 1788 | use super::{ |
| 1789 | drop_target, leave_description, parse_collapsed_teams, serialize_collapsed_teams, |
| 1790 | team_expanded, |
| 1791 | }; |
| 1792 | use crate::data::PENDING_FOLDER_PREFIX; |
| 1793 | use crate::sidebar_model::{Section, SectionKind}; |
| 1794 | |
| 1795 | fn section(id: &str, kind: SectionKind) -> Section { |
| 1796 | Section { |
| 1797 | id: id.to_owned(), |
| 1798 | name: id.to_owned(), |
| 1799 | kind, |
| 1800 | collapsed: false, |
| 1801 | count: 0, |
| 1802 | unread_chats: 0, |
| 1803 | items: Vec::new(), |
| 1804 | hidden_count: 0, |
| 1805 | list_expanded: false, |
| 1806 | show_empty_hint: false, |
| 1807 | } |
| 1808 | } |
| 1809 | |
| 1810 | #[test] |
| 1811 | fn pending_sections_take_no_dropped_chats() { |
| 1812 | let pending = format!("{PENDING_FOLDER_PREFIX}1"); |
| 1813 | assert_eq!(drop_target(§ion(&pending, SectionKind::Folder)), None); |
| 1814 | assert_eq!( |
| 1815 | drop_target(§ion("work", SectionKind::Folder)), |
| 1816 | Some(Some("work".to_owned())) |
| 1817 | ); |
| 1818 | assert_eq!( |
| 1819 | drop_target(§ion("recent", SectionKind::Others)), |
| 1820 | Some(None) |
| 1821 | ); |
| 1822 | } |
| 1823 | |
| 1824 | #[test] |
| 1825 | fn teams_start_expanded() { |
| 1826 | assert!(team_expanded("t1", &HashSet::new())); |
| 1827 | } |
| 1828 | |
| 1829 | #[test] |
| 1830 | fn collapsed_team_stays_closed() { |
| 1831 | let collapsed = HashSet::from(["t1".to_owned()]); |
| 1832 | assert!(!team_expanded("t1", &collapsed)); |
| 1833 | assert!(team_expanded("t2", &collapsed)); |
| 1834 | } |
| 1835 | |
| 1836 | #[test] |
| 1837 | fn collapsed_teams_round_trip_as_json() { |
| 1838 | let collapsed = HashSet::from(["b".to_owned(), "a".to_owned()]); |
| 1839 | let json = serialize_collapsed_teams(&collapsed); |
| 1840 | assert_eq!(json, r#"["a","b"]"#); |
| 1841 | assert_eq!(parse_collapsed_teams(&json), collapsed); |
| 1842 | assert!(parse_collapsed_teams("garbage").is_empty()); |
| 1843 | } |
| 1844 | |
| 1845 | #[test] |
| 1846 | fn leave_text_counts_the_other_members() { |
| 1847 | assert_eq!( |
| 1848 | leave_description(4), |
| 1849 | "The 3 other members see that you left. You can only come back if someone adds you." |
| 1850 | ); |
| 1851 | assert!(leave_description(2).starts_with("The other member sees")); |
| 1852 | assert!(leave_description(0).starts_with("The other members see")); |
| 1853 | } |
| 1854 | } |