BlueGit

ict rusty-teams

Files

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

sidebar.rs

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