BlueGit

ict rusty-teams

Files

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

sidebar.rs

7fee1d54 History
sidebar.rs 76.4 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::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, channel_shows_unread,
41 older_chats_trigger_resets, step_id, team_badge, team_channel_rows, team_hoisted,
42 team_name_style, visible_channel_ids, 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 entry: &SidebarTeam,
1423 channel: &ChannelRecord,
1424 selected: bool,
1425 ) -> AnyElement {
1426 let team = &entry.team;
1427 let unread = channel_shows_unread(entry, channel);
1428 let notifications = entry.channel_notifications(channel_id);
1429 let badge = channel_badge(entry, channel_id);
1430 let name_style = channel_name_style(unread, true, selected);
1431 let state = self.state.clone();
1432 let menu_state = self.state.clone();
1433 let menu_channel_id = channel_id.to_owned();
1434 let selection = Selection::Channel(channel_id.to_owned());
1435 let now = Utc::now();
1436 let offset = Local::now().offset().fix();
1437 let time = channel
1438 .last_message_at
1439 .map(|time| {
1440 format::list_time_label(time, now.with_timezone(&offset).date_naive(), offset)
1441 })
1442 .unwrap_or_default();
1443 h_flex()
1444 .id(SharedString::from(format!("pinned-{channel_id}")))
1445 .mx(px(6.))
1446 .h(px(PINNED_CHANNEL_HEIGHT))
1447 .px(px(10.))
1448 .gap(px(12.))
1449 .items_center()
1450 .rounded(px(8.))
1451 .cursor_pointer()
1452 .when(selected, |row| row.bg(theme::surface_raised()))
1453 .when(!selected, |row| row.hover(|row| row.bg(theme::row_hover())))
1454 .child(square_avatar(&team.name, &team.id, 28., 7.))
1455 .child(
1456 v_flex()
1457 .flex_1()
1458 .min_w_0()
1459 .gap(px(1.))
1460 .child(
1461 h_flex()
1462 .gap(px(8.))
1463 .items_baseline()
1464 .child(
1465 div()
1466 .flex_1()
1467 .min_w_0()
1468 .truncate()
1469 .text_size(px(13.5))
1470 .font_weight(name_weight(name_style))
1471 .text_color(name_color(name_style))
1472 .child(format!("{} / {}", team.name, channel.name)),
1473 )
1474 .children(notification_badge(notifications.level))
1475 .child(
1476 div()
1477 .flex_none()
1478 .text_size(px(11.))
1479 .text_color(if unread {
1480 theme::accent_text()
1481 } else {
1482 theme::text_muted()
1483 })
1484 .child(time),
1485 ),
1486 )
1487 .child(
1488 h_flex()
1489 .gap(px(8.))
1490 .items_center()
1491 .child(
1492 div()
1493 .flex_1()
1494 .min_w_0()
1495 .truncate()
1496 .text_size(px(12.))
1497 .text_color(theme::text_muted())
1498 .child(team.name.clone()),
1499 )
1500 .children(badge.map(mention_badge)),
1501 ),
1502 )
1503 .on_click(move |_, _, cx| {
1504 let selection = selection.clone();
1505 state.update(cx, |state, cx| state.select(selection, cx));
1506 })
1507 .context_menu(move |popup, window, cx| {
1508 channel_menu(
1509 popup,
1510 window,
1511 cx,
1512 menu_state.clone(),
1513 &menu_channel_id,
1514 notifications,
1515 )
1516 })
1517 .into_any_element()
1518 }
1519
1520 fn plain_header(&self, label: &str) -> impl IntoElement {
1521 div()
1522 .h(px(SECTION_HEIGHT))
1523 .mt(px(SECTION_GAP))
1524 .px(px(16.))
1525 .flex()
1526 .items_center()
1527 .text_size(px(12.))
1528 .font_weight(FontWeight::SEMIBOLD)
1529 .text_color(theme::text_soft())
1530 .child(label.to_owned())
1531 }
1532
1533 fn team_block(
1534 &self,
1535 mut list: Div,
1536 entry: &SidebarTeam,
1537 hoisted_only: bool,
1538 selected: Option<&Selection>,
1539 cx: &mut Context<Self>,
1540 ) -> Div {
1541 let team_id = entry.team.id.clone();
1542 let expanded = team_expanded(&team_id, &self.collapsed_teams);
1543 let name_style = team_name_style(entry, expanded);
1544 let badge = team_badge(entry, expanded);
1545 let toggle_id = team_id.clone();
1546 let (team_menu_state, team_menu_id, team_hidden) =
1547 (self.state.clone(), team_id.clone(), entry.hidden);
1548 list = list.child(
1549 h_flex()
1550 .id(SharedString::from(format!("team-{team_id}")))
1551 .mx(px(6.))
1552 .h(px(TEAM_ROW_HEIGHT))
1553 .flex_none()
1554 .px(px(10.))
1555 .gap(px(8.))
1556 .items_center()
1557 .rounded(px(8.))
1558 .cursor_pointer()
1559 .hover(|row| row.bg(theme::row_hover()))
1560 .child(icon(
1561 if expanded {
1562 IconName::ChevronDown
1563 } else {
1564 IconName::ChevronRight
1565 },
1566 14.,
1567 theme::text_muted(),
1568 ))
1569 .child(square_avatar(&entry.team.name, &entry.team.id, 28., 7.))
1570 .child(
1571 div()
1572 .flex_1()
1573 .min_w_0()
1574 .truncate()
1575 .text_size(px(13.5))
1576 .font_weight(name_weight(name_style))
1577 .text_color(name_color(name_style))
1578 .child(entry.team.name.clone()),
1579 )
1580 .children(badge.map(mention_badge))
1581 .on_click(cx.listener(move |this, _, _, cx| {
1582 if !this.collapsed_teams.remove(&toggle_id) {
1583 this.collapsed_teams.insert(toggle_id.clone());
1584 }
1585 save_collapsed_teams(&this.state.read(cx).store, &this.collapsed_teams);
1586 cx.notify();
1587 }))
1588 .context_menu(move |popup, _, _| {
1589 team_menu(popup, team_menu_state.clone(), &team_menu_id, team_hidden)
1590 }),
1591 );
1592 if !expanded {
1593 return list;
1594 }
1595 let is_selected =
1596 |channel: &ChannelRecord| selected == Some(&Selection::Channel(channel.id.clone()));
1597 let revealed = self.revealed_channel_teams.contains(&team_id);
1598 let rows = team_channel_rows(entry, hoisted_only, revealed, is_selected);
1599 for channel in &rows.shown {
1600 list = list.child(self.team_channel_row(
1601 channel,
1602 is_selected(channel),
1603 channel_name_style(
1604 channel_shows_unread(entry, channel),
1605 channel_shown(entry, &channel.id),
1606 is_selected(channel),
1607 ),
1608 entry.channel_notifications(&channel.id),
1609 channel_badge(entry, &channel.id),
1610 ));
1611 }
1612 if rows.hidden_count == 0 {
1613 return list;
1614 }
1615 let toggle_id = team_id.clone();
1616 list = list.child(self.reveal_row(
1617 SharedString::from(format!("hidden-channels-{team_id}")),
1618 hidden_channels_label(rows.hidden_count),
1619 revealed,
1620 40.,
1621 cx.listener(move |this, _, _, cx| {
1622 if !this.revealed_channel_teams.remove(&toggle_id) {
1623 this.revealed_channel_teams.insert(toggle_id.clone());
1624 }
1625 cx.notify();
1626 }),
1627 ));
1628 for channel in rows.revealed {
1629 list = list.child(self.team_channel_row(
1630 channel,
1631 false,
1632 channel_name_style(channel_shows_unread(entry, channel), false, false),
1633 entry.channel_notifications(&channel.id),
1634 channel_badge(entry, &channel.id),
1635 ));
1636 }
1637 list
1638 }
1639
1640 fn reveal_row(
1641 &self,
1642 id: SharedString,
1643 label: String,
1644 open: bool,
1645 indent: f32,
1646 on_click: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
1647 ) -> impl IntoElement {
1648 h_flex()
1649 .id(id)
1650 .mx(px(6.))
1651 .h(px(REVEAL_ROW_HEIGHT))
1652 .flex_none()
1653 .pl(px(indent))
1654 .pr(px(10.))
1655 .gap(px(8.))
1656 .items_center()
1657 .rounded(px(8.))
1658 .cursor_pointer()
1659 .hover(|row| row.bg(theme::row_hover()))
1660 .child(icon(
1661 if open {
1662 IconName::ChevronDown
1663 } else {
1664 IconName::ChevronRight
1665 },
1666 12.,
1667 theme::text_faint(),
1668 ))
1669 .child(
1670 div()
1671 .text_size(px(12.5))
1672 .text_color(theme::text_muted())
1673 .child(label),
1674 )
1675 .on_click(on_click)
1676 }
1677
1678 fn team_channel_row(
1679 &self,
1680 channel: &ChannelRecord,
1681 is_selected: bool,
1682 name_style: NameStyle,
1683 notifications: ChannelNotifications,
1684 badge: Option<MentionBadge>,
1685 ) -> impl IntoElement {
1686 let state = self.state.clone();
1687 let menu_state = self.state.clone();
1688 let menu_channel_id = channel.id.clone();
1689 let selection = Selection::Channel(channel.id.clone());
1690 h_flex()
1691 .id(SharedString::from(format!("channel-{}", channel.id)))
1692 .mx(px(6.))
1693 .h(px(TEAM_ROW_HEIGHT))
1694 .flex_none()
1695 .pl(px(40.))
1696 .pr(px(10.))
1697 .gap(px(8.))
1698 .items_center()
1699 .rounded(px(8.))
1700 .cursor_pointer()
1701 .when(is_selected, |row| row.bg(theme::surface_raised()))
1702 .when(!is_selected, |row| {
1703 row.hover(|row| row.bg(theme::row_hover()))
1704 })
1705 .child(
1706 div()
1707 .w(px(14.))
1708 .flex()
1709 .justify_center()
1710 .text_size(px(15.))
1711 .text_color(theme::text_faint())
1712 .child("#"),
1713 )
1714 .child(
1715 div()
1716 .flex_1()
1717 .min_w_0()
1718 .truncate()
1719 .text_size(px(13.5))
1720 .font_weight(name_weight(name_style))
1721 .text_color(name_color(name_style))
1722 .child(channel.name.clone()),
1723 )
1724 .children(notification_badge(notifications.level))
1725 .children(badge.map(mention_badge))
1726 .on_click(move |_, _, cx| {
1727 let selection = selection.clone();
1728 state.update(cx, |state, cx| state.select(selection, cx));
1729 })
1730 .context_menu(move |popup, window, cx| {
1731 channel_menu(
1732 popup,
1733 window,
1734 cx,
1735 menu_state.clone(),
1736 &menu_channel_id,
1737 notifications,
1738 )
1739 })
1740 }
1741
1742 fn channels_body(&self, cx: &mut Context<Self>) -> AnyElement {
1743 let (sidebar, pinned_ids, selected) = {
1744 let state = self.state.read(cx);
1745 (
1746 state.sidebar.clone(),
1747 state.directory.pinned_channels.clone(),
1748 state.selection.clone().filter(|_| !state.new_chat),
1749 )
1750 };
1751 let mut list = v_flex().w_full().pb(px(12.));
1752 let pinned: Vec<(&SidebarTeam, &ChannelRecord)> = pinned_ids
1753 .iter()
1754 .filter_map(|id| {
1755 sidebar.teams.iter().find_map(|entry| {
1756 entry
1757 .channels
1758 .iter()
1759 .find(|channel| &channel.id == id)
1760 .map(|channel| (entry, channel))
1761 })
1762 })
1763 .collect();
1764 if !pinned.is_empty() {
1765 list = list.child(self.plain_header(PINNED_LABEL));
1766 for (entry, channel) in pinned {
1767 let is_selected = selected == Some(Selection::Channel(channel.id.clone()));
1768 list = list.child(self.channel_row(&channel.id, entry, channel, is_selected));
1769 }
1770 }
1771 list = list.child(self.plain_header(TEAMS_LABEL));
1772 let hidden_teams: Vec<&SidebarTeam> =
1773 sidebar.teams.iter().filter(|entry| entry.hidden).collect();
1774 for entry in sidebar
1775 .teams
1776 .iter()
1777 .filter(|entry| !entry.hidden || team_hoisted(entry))
1778 {
1779 list = self.team_block(list, entry, entry.hidden, selected.as_ref(), cx);
1780 }
1781 if !hidden_teams.is_empty() {
1782 let holds_selection = hidden_teams
1783 .iter()
1784 .any(|entry| team_holds(entry, selected.as_ref()));
1785 let open = self.hidden_teams_open || holds_selection;
1786 list = list.child(self.reveal_row(
1787 "hidden-teams".into(),
1788 format!("{HIDDEN_TEAMS_LABEL} ({})", hidden_teams.len()),
1789 open,
1790 10.,
1791 cx.listener(|this, _, _, cx| {
1792 this.hidden_teams_open = !this.hidden_teams_open;
1793 cx.notify();
1794 }),
1795 ));
1796 if open {
1797 for entry in hidden_teams {
1798 list = self.team_block(list, entry, false, selected.as_ref(), cx);
1799 }
1800 }
1801 }
1802 let scroll_area = div()
1803 .id("channels-scroll")
1804 .flex_1()
1805 .min_h_0()
1806 .overflow_y_scroll()
1807 .track_scroll(&self.channels_scroll)
1808 .child(list);
1809 with_top_edge(scroll_area, &self.channels_scroll, SIDEBAR_SCROLL_DIVIDER).into_any_element()
1810 }
1811}
1812
1813fn name_weight(style: NameStyle) -> FontWeight {
1814 match style.weight {
1815 NameWeight::Regular => FontWeight::NORMAL,
1816 NameWeight::Semibold => FontWeight::SEMIBOLD,
1817 NameWeight::Bold => FontWeight::BOLD,
1818 }
1819}
1820
1821fn name_color(style: NameStyle) -> Hsla {
1822 if style.highlighted {
1823 theme::text()
1824 } else {
1825 theme::text_muted()
1826 }
1827}
1828
1829/// Teams chat list: red Important / AlertUrgent glyph while the last unread message is marked.
1830fn importance_marker(importance: MessageImportance) -> Option<Icon> {
1831 let name = match importance {
1832 MessageImportance::Normal => return None,
1833 MessageImportance::High => "fluent_important_filled",
1834 MessageImportance::Urgent => "fluent_alert_urgent_filled",
1835 };
1836 Some(symbol(name, IMPORTANCE_MARKER_SIZE, theme::red()))
1837}
1838
1839fn mention_badge(badge: MentionBadge) -> AnyElement {
1840 match badge {
1841 MentionBadge::Count(count) => count_badge(count, false).into_any_element(),
1842 MentionBadge::Channel => {
1843 icon(IconName::Hash, MENTION_ICON_SIZE, theme::accent_text()).into_any_element()
1844 }
1845 MentionBadge::Team => {
1846 icon(IconName::Users, MENTION_ICON_SIZE, theme::accent_text()).into_any_element()
1847 }
1848 }
1849}
1850
1851fn notification_badge(level: ChannelNotificationLevel) -> Option<impl IntoElement> {
1852 let name = match level {
1853 ChannelNotificationLevel::BannerAndFeed => IconName::Bell,
1854 ChannelNotificationLevel::Off => IconName::BellOff,
1855 ChannelNotificationLevel::Feed => return None,
1856 };
1857 Some(icon(name, NOTIFICATION_ICON_SIZE, theme::text_muted()))
1858}
1859
1860fn channel_menu(
1861 popup: PopupMenu,
1862 window: &mut Window,
1863 cx: &mut Context<PopupMenu>,
1864 state: Entity<AppState>,
1865 channel_id: &str,
1866 notifications: ChannelNotifications,
1867) -> PopupMenu {
1868 let menu_state = state.read(cx).channel_menu_state(channel_id);
1869 let mut popup = popup;
1870 for entry in channel_menu_entries(menu_state) {
1871 let item = match entry {
1872 MenuEntry::Separator => {
1873 popup = popup.separator();
1874 continue;
1875 }
1876 MenuEntry::Item(ChannelMenuItem::Notifications) => {
1877 popup = notifications_submenu(popup, window, cx, &state, channel_id, notifications);
1878 continue;
1879 }
1880 MenuEntry::Item(item) => item,
1881 };
1882 let (action_state, action_id) = (state.clone(), channel_id.to_owned());
1883 popup = popup.item(
1884 PopupMenuItem::new(item.label())
1885 .icon(menu_icon(channel_menu_icon(item)))
1886 .disabled(menu_state.disables(item))
1887 .on_click(move |_, _, cx| {
1888 action_state.update(cx, |state, cx| match item {
1889 ChannelMenuItem::Pin => state.set_channel_pinned(&action_id, true, cx),
1890 ChannelMenuItem::Unpin => state.set_channel_pinned(&action_id, false, cx),
1891 ChannelMenuItem::Show => state.set_channel_shown(&action_id, true, cx),
1892 ChannelMenuItem::Hide => state.set_channel_shown(&action_id, false, cx),
1893 ChannelMenuItem::CopyLink => state.copy_channel_link(&action_id, cx),
1894 ChannelMenuItem::Notifications => {}
1895 });
1896 }),
1897 );
1898 }
1899 popup
1900}
1901
1902fn channel_menu_icon(item: ChannelMenuItem) -> IconName {
1903 match item {
1904 ChannelMenuItem::Notifications => IconName::Bell,
1905 ChannelMenuItem::Pin => IconName::Pin,
1906 ChannelMenuItem::Unpin => IconName::PinOff,
1907 ChannelMenuItem::Show => IconName::Eye,
1908 ChannelMenuItem::Hide => IconName::EyeOff,
1909 ChannelMenuItem::CopyLink => IconName::Link,
1910 }
1911}
1912
1913fn team_menu(popup: PopupMenu, state: Entity<AppState>, team_id: &str, hidden: bool) -> PopupMenu {
1914 let mut popup = popup;
1915 for entry in team_menu_entries(hidden) {
1916 let MenuEntry::Item(item) = entry else {
1917 popup = popup.separator();
1918 continue;
1919 };
1920 let (action_state, action_id) = (state.clone(), team_id.to_owned());
1921 popup = popup.item(
1922 PopupMenuItem::new(item.label())
1923 .icon(menu_icon(match item {
1924 TeamMenuItem::Show => IconName::Eye,
1925 TeamMenuItem::Hide => IconName::EyeOff,
1926 TeamMenuItem::CopyLink => IconName::Link,
1927 }))
1928 .on_click(move |_, _, cx| {
1929 action_state.update(cx, |state, cx| match item {
1930 TeamMenuItem::Show => state.set_team_shown(&action_id, true, cx),
1931 TeamMenuItem::Hide => state.set_team_shown(&action_id, false, cx),
1932 TeamMenuItem::CopyLink => state.copy_team_link(&action_id, cx),
1933 });
1934 }),
1935 );
1936 }
1937 popup
1938}
1939
1940fn notifications_submenu(
1941 popup: PopupMenu,
1942 window: &mut Window,
1943 cx: &mut Context<PopupMenu>,
1944 state: &Entity<AppState>,
1945 channel_id: &str,
1946 notifications: ChannelNotifications,
1947) -> PopupMenu {
1948 let (state, channel_id) = (state.clone(), channel_id.to_owned());
1949 popup.submenu_with_icon(
1950 Some(menu_icon(IconName::Bell)),
1951 "Notifications",
1952 window,
1953 cx,
1954 move |submenu, _, _| {
1955 let level_item = |label: &'static str, level: ChannelNotificationLevel| {
1956 let (state, channel_id) = (state.clone(), channel_id.clone());
1957 PopupMenuItem::new(label)
1958 .checked(notifications.level == level)
1959 .on_click(move |_, _, cx| {
1960 let changed = ChannelNotifications {
1961 level,
1962 include_replies: notifications.include_replies
1963 && level != ChannelNotificationLevel::Off,
1964 };
1965 state.update(cx, |state, cx| {
1966 state.set_channel_notifications(&channel_id, changed, cx)
1967 });
1968 })
1969 };
1970 let (replies_state, replies_id) = (state.clone(), channel_id.clone());
1971 submenu
1972 .item(level_item(
1973 "Banner and activity",
1974 ChannelNotificationLevel::BannerAndFeed,
1975 ))
1976 .item(level_item("Activity only", ChannelNotificationLevel::Feed))
1977 .item(level_item("Off", ChannelNotificationLevel::Off))
1978 .separator()
1979 .item(
1980 PopupMenuItem::new("Include thread replies")
1981 .checked(notifications.include_replies)
1982 .disabled(notifications.level == ChannelNotificationLevel::Off)
1983 .on_click(move |_, _, cx| {
1984 let changed = ChannelNotifications {
1985 level: notifications.level,
1986 include_replies: !notifications.include_replies,
1987 };
1988 replies_state.update(cx, |state, cx| {
1989 state.set_channel_notifications(&replies_id, changed, cx)
1990 });
1991 }),
1992 )
1993 },
1994 )
1995}
1996
1997fn team_expanded(team_id: &str, collapsed_teams: &HashSet<String>) -> bool {
1998 !collapsed_teams.contains(team_id)
1999}
2000
2001fn load_collapsed_teams(store: &Store) -> HashSet<String> {
2002 store
2003 .meta(COLLAPSED_TEAMS_META_KEY)
2004 .ok()
2005 .flatten()
2006 .map(|value| parse_collapsed_teams(&value))
2007 .unwrap_or_default()
2008}
2009
2010fn save_collapsed_teams(store: &Store, collapsed_teams: &HashSet<String>) {
2011 let _ = store.set_meta(
2012 COLLAPSED_TEAMS_META_KEY,
2013 &serialize_collapsed_teams(collapsed_teams),
2014 );
2015}
2016
2017fn serialize_collapsed_teams(collapsed_teams: &HashSet<String>) -> String {
2018 let mut team_ids: Vec<&String> = collapsed_teams.iter().collect();
2019 team_ids.sort_unstable();
2020 serde_json::to_string(&team_ids).unwrap_or_default()
2021}
2022
2023fn parse_collapsed_teams(value: &str) -> HashSet<String> {
2024 serde_json::from_str(value).unwrap_or_default()
2025}
2026
2027fn team_holds(entry: &SidebarTeam, selected: Option<&Selection>) -> bool {
2028 entry
2029 .channels
2030 .iter()
2031 .any(|channel| selected == Some(&Selection::Channel(channel.id.clone())))
2032}
2033
2034fn hidden_channels_label(count: usize) -> String {
2035 if count == 1 {
2036 "1 hidden channel".to_owned()
2037 } else {
2038 format!("{count} hidden channels")
2039 }
2040}
2041
2042fn collect_faces(spec: &AvatarSpec, faces: &mut Vec<String>) {
2043 match spec {
2044 AvatarSpec::Single(face) | AvatarSpec::Bot(face) => faces.extend(face.user_id.clone()),
2045 AvatarSpec::Pair(first, second) => {
2046 faces.extend(first.user_id.clone());
2047 faces.extend(second.user_id.clone());
2048 }
2049 AvatarSpec::Meeting | AvatarSpec::Muted => {}
2050 }
2051}
2052
2053fn with_spacer(list: Div, skipped_height: &mut f32) -> Div {
2054 let height = std::mem::take(skipped_height);
2055 if height > 0. {
2056 list.child(div().h(px(height)).flex_none())
2057 } else {
2058 list
2059 }
2060}
2061
2062impl Render for SidebarView {
2063 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
2064 let window_height = f32::from(window.viewport_size().height);
2065 let body = match self.tab {
2066 SidebarTab::Activity => self.activity_panel.clone().into_any_element(),
2067 SidebarTab::Chats => self.chats_body(window_height, cx),
2068 SidebarTab::Channels => self.channels_body(cx),
2069 SidebarTab::Calls => calls_body(&self.state, &self.calls_scroll, window_height, cx),
2070 SidebarTab::Saved => self.saved_panel.clone().into_any_element(),
2071 SidebarTab::Agents => match &self.agents_panel {
2072 Some(panel) => panel.clone().into_any_element(),
2073 None => div().into_any_element(),
2074 },
2075 };
2076 let header = match self.tab {
2077 SidebarTab::Chats => Some(
2078 v_flex()
2079 .flex_none()
2080 .child(
2081 page_header(self.tab.title())
2082 .child(self.chat_list_menu_button(cx))
2083 .child(self.filter_toggle_button(cx))
2084 .child(self.new_chat_button(cx)),
2085 )
2086 .child(self.filter_bar(cx)),
2087 ),
2088 SidebarTab::Channels => {
2089 Some(page_header(self.tab.title()).child(self.channel_list_menu_button(cx)))
2090 }
2091 SidebarTab::Calls | SidebarTab::Agents => Some(page_header(self.tab.title())),
2092 SidebarTab::Activity | SidebarTab::Saved => None,
2093 };
2094 v_flex()
2095 .debug_selector(|| "shell/islands/sidebar".into())
2096 .w(px(SIDEBAR_WIDTH))
2097 .h_full()
2098 .flex_none()
2099 .bg(theme::surface())
2100 .rounded(px(10.))
2101 .overflow_hidden()
2102 .children(header)
2103 .child(body)
2104 }
2105}
2106
2107#[cfg(test)]
2108mod tests {
2109 use std::collections::HashSet;
2110
2111 use super::{
2112 drop_target, leave_description, parse_collapsed_teams, serialize_collapsed_teams,
2113 team_expanded,
2114 };
2115 use crate::data::PENDING_FOLDER_PREFIX;
2116 use crate::sidebar_model::{Section, SectionKind};
2117
2118 fn section(id: &str, kind: SectionKind) -> Section {
2119 Section {
2120 id: id.to_owned(),
2121 name: id.to_owned(),
2122 kind,
2123 collapsed: false,
2124 count: 0,
2125 unread_chats: 0,
2126 items: Vec::new(),
2127 hidden_count: 0,
2128 list_expanded: false,
2129 show_empty_hint: false,
2130 }
2131 }
2132
2133 #[test]
2134 fn pending_sections_take_no_dropped_chats() {
2135 let pending = format!("{PENDING_FOLDER_PREFIX}1");
2136 assert_eq!(drop_target(&section(&pending, SectionKind::Folder)), None);
2137 assert_eq!(
2138 drop_target(&section("work", SectionKind::Folder)),
2139 Some(Some("work".to_owned()))
2140 );
2141 assert_eq!(
2142 drop_target(&section("recent", SectionKind::Others)),
2143 Some(None)
2144 );
2145 }
2146
2147 #[test]
2148 fn teams_start_expanded() {
2149 assert!(team_expanded("t1", &HashSet::new()));
2150 }
2151
2152 #[test]
2153 fn collapsed_team_stays_closed() {
2154 let collapsed = HashSet::from(["t1".to_owned()]);
2155 assert!(!team_expanded("t1", &collapsed));
2156 assert!(team_expanded("t2", &collapsed));
2157 }
2158
2159 #[test]
2160 fn collapsed_teams_round_trip_as_json() {
2161 let collapsed = HashSet::from(["b".to_owned(), "a".to_owned()]);
2162 let json = serialize_collapsed_teams(&collapsed);
2163 assert_eq!(json, r#"["a","b"]"#);
2164 assert_eq!(parse_collapsed_teams(&json), collapsed);
2165 assert!(parse_collapsed_teams("garbage").is_empty());
2166 }
2167
2168 #[test]
2169 fn leave_text_counts_the_other_members() {
2170 assert_eq!(
2171 leave_description(4),
2172 "The 3 other members see that you left. You can only come back if someone adds you."
2173 );
2174 assert!(leave_description(2).starts_with("The other member sees"));
2175 assert!(leave_description(0).starts_with("The other members see"));
2176 }
2177}