BlueGit

ict rusty-teams

Files

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

sidebar.rs

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