BlueGit

ict rusty-teams

Files

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

sidebar.rs

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