BlueGit

ict rusty-teams

Files

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

sidebar.rs

sidebar.rs 75.8 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, 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 MENU_ICON_SIZE: f32 = 15.;
71const NOTIFICATION_ICON_SIZE: f32 = 13.;
72const MENTION_ICON_SIZE: f32 = 15.;
73
74#[derive(Debug, Clone, Copy, PartialEq, Eq)]
75pub enum SidebarTab {
76 Activity,
77 Chats,
78 Channels,
79 Calls,
80 Saved,
81 Agents,
82}
83
84impl SidebarTab {
85 fn title(self) -> &'static str {
86 match self {
87 SidebarTab::Activity => "Activity",
88 SidebarTab::Chats => "Chats",
89 SidebarTab::Channels => "Channels",
90 SidebarTab::Calls => "Calls",
91 SidebarTab::Saved => "Saved",
92 SidebarTab::Agents => "Agents",
93 }
94 }
95}
96
97#[derive(Clone)]
98struct DraggedChat {
99 id: String,
100 title: String,
101}
102
103struct DragGhost {
104 title: String,
105}
106
107impl Render for DragGhost {
108 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
109 div()
110 .px(px(12.))
111 .py(px(6.))
112 .rounded(px(8.))
113 .bg(theme::surface_raised())
114 .border_1()
115 .border_color(theme::accent())
116 .text_size(px(13.))
117 .text_color(theme::text())
118 .child(self.title.clone())
119 }
120}
121
122struct ChatMenuTarget {
123 id: String,
124 title: String,
125 folder_id: Option<String>,
126 unread: bool,
127 muted: bool,
128 is_group: bool,
129 member_count: usize,
130}
131
132#[derive(Clone)]
133struct MenuContext {
134 state: Entity<AppState>,
135 folders: Vec<(String, String)>,
136}
137
138fn menu_icon(name: IconName) -> gpui_kit::component::Icon {
139 icon(name, MENU_ICON_SIZE, theme::text_muted())
140}
141
142fn chat_menu(
143 popup: PopupMenu,
144 window: &mut Window,
145 cx: &mut Context<PopupMenu>,
146 context: &MenuContext,
147 target: &ChatMenuTarget,
148) -> PopupMenu {
149 let mut popup = popup;
150 let current = target.folder_id.clone();
151 let folders: Vec<(String, String)> = context
152 .folders
153 .iter()
154 .filter(|(folder_id, _)| current.as_ref() != Some(folder_id))
155 .cloned()
156 .collect();
157 if !folders.is_empty() {
158 let move_state = context.state.clone();
159 let move_id = target.id.clone();
160 popup = popup.submenu_with_icon(
161 Some(menu_icon(IconName::Folder)),
162 "Move to section",
163 window,
164 cx,
165 move |submenu, _, _| {
166 folders.iter().fold(submenu, |submenu, (folder_id, name)| {
167 let (state, chat_id, folder_id) =
168 (move_state.clone(), move_id.clone(), folder_id.clone());
169 submenu.item(PopupMenuItem::new(name.clone()).on_click(move |_, _, cx| {
170 state.update(cx, |state, cx| {
171 state.move_chat(&chat_id, Some(&folder_id), cx)
172 });
173 }))
174 })
175 },
176 );
177 }
178 if current.is_some() {
179 let remove_state = context.state.clone();
180 let remove_id = target.id.clone();
181 popup = popup.item(
182 PopupMenuItem::new("Remove from section")
183 .icon(menu_icon(IconName::FolderMinus))
184 .on_click(move |_, _, cx| {
185 remove_state.update(cx, |state, cx| state.move_chat(&remove_id, None, cx));
186 }),
187 );
188 }
189 let read_state = context.state.clone();
190 let read_id = target.id.clone();
191 let unread = target.unread;
192 let mute_state = context.state.clone();
193 let mute_id = target.id.clone();
194 let muted = target.muted;
195 let hide_state = context.state.clone();
196 let hide_id = target.id.clone();
197 let notes = is_notes_chat(&target.id);
198 popup = popup.separator().item(
199 PopupMenuItem::new(if unread {
200 "Mark as read"
201 } else {
202 "Mark as unread"
203 })
204 .icon(menu_icon(if unread {
205 IconName::MailOpen
206 } else {
207 IconName::Mail
208 }))
209 .on_click(move |_, _, cx| {
210 read_state.update(cx, |state, cx| {
211 if unread {
212 state.mark_chat_read(&read_id, cx);
213 } else {
214 state.mark_chat_unread(&read_id, cx);
215 }
216 });
217 }),
218 );
219 if notes {
220 return popup;
221 }
222 popup = popup.item(
223 PopupMenuItem::new(if muted { "Unmute" } else { "Mute" })
224 .icon(menu_icon(if muted {
225 IconName::Bell
226 } else {
227 IconName::BellOff
228 }))
229 .on_click(move |_, _, cx| {
230 mute_state.update(cx, |state, cx| state.set_chat_muted(&mute_id, !muted, cx));
231 }),
232 );
233 popup = chat_notifications_submenu(popup, window, cx, context, target)
234 .separator()
235 .item(
236 PopupMenuItem::new("Hide")
237 .icon(menu_icon(IconName::EyeOff))
238 .on_click(move |_, _, cx| {
239 hide_state.update(cx, |state, cx| state.hide_chat(&hide_id, cx));
240 }),
241 );
242 if !target.is_group {
243 let remove_state = context.state.clone();
244 let remove_id = target.id.clone();
245 popup = popup.item(
246 PopupMenuItem::new(REMOVE_HISTORY_LABEL)
247 .icon(menu_icon(IconName::Trash))
248 .on_click(move |_, window, cx| {
249 confirm_remove_history(&remove_state, &remove_id, window, cx);
250 }),
251 );
252 }
253 if target.is_group {
254 let leave_state = context.state.clone();
255 let leave_id = target.id.clone();
256 let title = target.title.clone();
257 let member_count = target.member_count;
258 popup = popup.item(
259 PopupMenuItem::element(|_, _| {
260 div().text_color(theme::red_soft()).child("Leave chat...")
261 })
262 .icon(icon(IconName::LogOut, MENU_ICON_SIZE, theme::red_soft()))
263 .on_click(move |_, window, cx| {
264 confirm_leave(&leave_state, &leave_id, &title, member_count, window, cx);
265 }),
266 );
267 }
268 popup
269}
270
271const CHAT_NOTIFICATION_MODES: [(&str, ChatNotificationMode); 2] = [
272 ("All new messages", ChatNotificationMode::AllNewMessages),
273 ("Mute", ChatNotificationMode::Mute),
274];
275
276const DESKTOP_NOTIFICATIONS: [(&str, DesktopNotifications); 2] = [
277 (
278 "Match settings used for all chats",
279 DesktopNotifications::MatchAllChats,
280 ),
281 ("Don't get desktop notifications", DesktopNotifications::Off),
282];
283
284fn chat_notifications_submenu(
285 popup: PopupMenu,
286 window: &mut Window,
287 cx: &mut Context<PopupMenu>,
288 context: &MenuContext,
289 target: &ChatMenuTarget,
290) -> PopupMenu {
291 let state = context.state.clone();
292 let chat_id = target.id.clone();
293 let mode = if target.muted {
294 ChatNotificationMode::Mute
295 } else {
296 ChatNotificationMode::AllNewMessages
297 };
298 let desktop = state.read(cx).chat_desktop_notifications(&chat_id);
299 popup.submenu_with_icon(
300 Some(menu_icon(IconName::Bell)),
301 "Manage notifications",
302 window,
303 cx,
304 move |submenu, _, _| {
305 let choose = |chosen_mode: ChatNotificationMode,
306 chosen_desktop: DesktopNotifications| {
307 let (state, chat_id) = (state.clone(), chat_id.clone());
308 move |_: &ClickEvent, _: &mut Window, cx: &mut App| {
309 state.update(cx, |state, cx| {
310 state.set_chat_notifications(&chat_id, chosen_mode, chosen_desktop, cx)
311 });
312 }
313 };
314 let submenu = submenu.label("Chat notification mode");
315 let submenu =
316 CHAT_NOTIFICATION_MODES
317 .iter()
318 .fold(submenu, |submenu, (label, value)| {
319 submenu.item(
320 PopupMenuItem::new(*label)
321 .checked(*value == mode)
322 .on_click(choose(*value, desktop)),
323 )
324 });
325 let submenu = submenu
326 .separator()
327 .label("Set a desktop notification preference");
328 DESKTOP_NOTIFICATIONS
329 .iter()
330 .fold(submenu, |submenu, (label, value)| {
331 submenu.item(
332 PopupMenuItem::new(*label)
333 .checked(*value == desktop)
334 .on_click(choose(mode, *value)),
335 )
336 })
337 },
338 )
339}
340
341#[derive(Clone)]
342struct SectionMenuTarget {
343 id: String,
344 name: String,
345 user_created: bool,
346 previous_id: Option<String>,
347 next_id: Option<String>,
348 movable: bool,
349 read_only: bool,
350}
351
352fn section_menu_targets(
353 sections: &[Section],
354 directory: &Directory,
355 read_only: bool,
356) -> Vec<SectionMenuTarget> {
357 let movable_ids: HashSet<&str> = directory
358 .folders
359 .iter()
360 .map(|folder| folder.id.as_str())
361 .filter(|folder_id| !is_pending_folder(folder_id))
362 .collect();
363 let movable = |section: &Section| movable_ids.contains(section.id.as_str());
364 sections
365 .iter()
366 .enumerate()
367 .map(|(index, section)| SectionMenuTarget {
368 id: section.id.clone(),
369 name: section.name.clone(),
370 user_created: section.kind == SectionKind::Folder && movable(section),
371 previous_id: sections[..index]
372 .iter()
373 .rev()
374 .find(|other| movable(other))
375 .map(|other| other.id.clone()),
376 next_id: sections[index + 1..]
377 .iter()
378 .find(|other| movable(other))
379 .map(|other| other.id.clone()),
380 movable: movable(section),
381 read_only,
382 })
383 .collect()
384}
385
386fn drop_target(section: &Section) -> Option<Option<String>> {
387 if is_pending_folder(&section.id) {
388 return None;
389 }
390 Some(
391 matches!(section.kind, SectionKind::Favorites | SectionKind::Folder)
392 .then(|| section.id.clone()),
393 )
394}
395
396fn new_section_item(state: &Entity<AppState>, read_only: bool) -> PopupMenuItem {
397 let state = state.clone();
398 PopupMenuItem::new("New section")
399 .icon(menu_icon(IconName::Plus))
400 .disabled(read_only)
401 .on_click(move |_, window, cx| {
402 open_section_name_dialog(state.clone(), SectionNameTarget::New, window, cx);
403 })
404}
405
406fn move_section_item(
407 state: &Entity<AppState>,
408 label: &'static str,
409 glyph: IconName,
410 folder_id: &str,
411 target_id: Option<&String>,
412 read_only: bool,
413) -> PopupMenuItem {
414 let state = state.clone();
415 let folder_id = folder_id.to_owned();
416 let target_id = target_id.cloned();
417 PopupMenuItem::new(label)
418 .icon(menu_icon(glyph))
419 .disabled(read_only || target_id.is_none())
420 .on_click(move |_, _, cx| {
421 if let Some(target_id) = &target_id {
422 state.update(cx, |state, cx| {
423 state.move_section(&folder_id, target_id, cx)
424 });
425 }
426 })
427}
428
429fn section_menu(
430 popup: PopupMenu,
431 state: &Entity<AppState>,
432 target: &SectionMenuTarget,
433) -> PopupMenu {
434 let mut popup = popup;
435 if target.user_created {
436 let rename_state = state.clone();
437 let rename_target = SectionNameTarget::Rename {
438 folder_id: target.id.clone(),
439 name: target.name.clone(),
440 };
441 popup = popup.item(
442 PopupMenuItem::new("Rename section")
443 .icon(menu_icon(IconName::Pencil))
444 .disabled(target.read_only)
445 .on_click(move |_, window, cx| {
446 open_section_name_dialog(
447 rename_state.clone(),
448 rename_target.clone(),
449 window,
450 cx,
451 );
452 }),
453 );
454 }
455 if target.movable {
456 popup = popup
457 .item(move_section_item(
458 state,
459 "Move up",
460 IconName::ArrowUp,
461 &target.id,
462 target.previous_id.as_ref(),
463 target.read_only,
464 ))
465 .item(move_section_item(
466 state,
467 "Move down",
468 IconName::ArrowDown,
469 &target.id,
470 target.next_id.as_ref(),
471 target.read_only,
472 ))
473 .separator();
474 }
475 popup = popup.item(new_section_item(state, target.read_only));
476 if target.user_created {
477 let delete_state = state.clone();
478 let (folder_id, name) = (target.id.clone(), target.name.clone());
479 popup = popup.separator().item(
480 PopupMenuItem::element(|_, _| {
481 div().text_color(theme::red_soft()).child("Delete section")
482 })
483 .icon(icon(IconName::Trash, MENU_ICON_SIZE, theme::red_soft()))
484 .disabled(target.read_only)
485 .on_click(move |_, window, cx| {
486 confirm_delete_section(&delete_state, &folder_id, &name, window, cx);
487 }),
488 );
489 }
490 popup
491}
492
493fn confirm_delete_section(
494 state: &Entity<AppState>,
495 folder_id: &str,
496 name: &str,
497 window: &mut Window,
498 cx: &mut App,
499) {
500 let state = state.clone();
501 let folder_id = folder_id.to_owned();
502 let heading = format!("Delete section \"{}\"?", truncated(name, TITLE_LIMIT));
503 window.open_alert_dialog(cx, move |alert, _, _| {
504 let state = state.clone();
505 let folder_id = folder_id.clone();
506 let cancel = Button::new("delete-section-cancel")
507 .label("Cancel")
508 .on_click(|_, window, cx| window.close_dialog(cx));
509 let delete = Button::new("delete-section-confirm")
510 .label("Delete")
511 .danger()
512 .on_click(move |_, window, cx| {
513 state.update(cx, |state, cx| state.delete_section(&folder_id, cx));
514 window.close_dialog(cx);
515 });
516 alert
517 .title(heading.clone())
518 .description("Chats in it move back to Chats.")
519 .footer(DialogFooter::new().child(cancel).child(delete))
520 .on_ok(|_, _, _| false)
521 });
522}
523
524fn leave_description(member_count: usize) -> String {
525 let who = match member_count {
526 0 | 1 => "The other members".to_owned(),
527 2 => "The other member".to_owned(),
528 count => format!("The {} other members", count - 1),
529 };
530 let verb = if member_count == 2 { "sees" } else { "see" };
531 format!("{who} {verb} that you left. You can only come back if someone adds you.")
532}
533
534const REMOVE_HISTORY_LABEL: &str = "Remove chat history";
535const REMOVE_HISTORY_TITLE: &str = "Remove chat history?";
536const REMOVE_HISTORY_TEXT: &str =
537 "This will remove the chat from your list and search. It affects you only.";
538
539fn confirm_remove_history(
540 state: &Entity<AppState>,
541 chat_id: &str,
542 window: &mut Window,
543 cx: &mut App,
544) {
545 let state = state.clone();
546 let chat_id = chat_id.to_owned();
547 window.open_alert_dialog(cx, move |alert, _, _| {
548 let state = state.clone();
549 let chat_id = chat_id.clone();
550 let cancel = Button::new("remove-history-cancel")
551 .label("Cancel")
552 .on_click(|_, window, cx| window.close_dialog(cx));
553 let remove = Button::new("remove-history-confirm")
554 .label("Remove")
555 .primary()
556 .on_click(move |_, window, cx| {
557 state.update(cx, |state, cx| state.remove_chat_history(&chat_id, cx));
558 window.close_dialog(cx);
559 });
560 alert
561 .title(REMOVE_HISTORY_TITLE)
562 .description(REMOVE_HISTORY_TEXT)
563 .footer(DialogFooter::new().child(cancel).child(remove))
564 .on_ok(|_, _, _| false)
565 });
566}
567
568pub(crate) fn confirm_leave(
569 state: &Entity<AppState>,
570 chat_id: &str,
571 title: &str,
572 member_count: usize,
573 window: &mut Window,
574 cx: &mut App,
575) {
576 let state = state.clone();
577 let chat_id = chat_id.to_owned();
578 let heading = format!("Leave \"{}\"?", truncated(title, TITLE_LIMIT));
579 let description = leave_description(member_count);
580 window.open_alert_dialog(cx, move |alert, _, _| {
581 let state = state.clone();
582 let chat_id = chat_id.clone();
583 let cancel = Button::new("leave-cancel")
584 .label("Cancel")
585 .on_click(|_, window, cx| window.close_dialog(cx));
586 let leave = Button::new("leave-confirm")
587 .label("Leave")
588 .danger()
589 .on_click(move |_, window, cx| {
590 state.update(cx, |state, cx| state.leave_chat(&chat_id, cx));
591 window.close_dialog(cx);
592 });
593 alert
594 .title(heading.clone())
595 .description(description.clone())
596 .footer(DialogFooter::new().child(cancel).child(leave))
597 .on_ok(|_, _, _| false)
598 });
599}
600
601pub enum SidebarEvent {
602 RestoreFocus,
603}
604
605impl EventEmitter<SidebarEvent> for SidebarView {}
606
607pub struct SidebarView {
608 state: Entity<AppState>,
609 tab: SidebarTab,
610 activity_panel: Entity<ActivityPanel>,
611 saved_panel: Entity<SavedPanel>,
612 agents_panel: Option<Entity<AgentsPanel>>,
613 collapsed_teams: HashSet<String>,
614 revealed_channel_teams: HashSet<String>,
615 hidden_teams_open: bool,
616 chats_scroll: ScrollHandle,
617 older_chats_trigger_top: Cell<Option<f32>>,
618 older_chats_seen: Cell<(bool, bool)>,
619 channels_scroll: ScrollHandle,
620 calls_scroll: ScrollHandle,
621 chat_filter: ChatFilter,
622 filter_box_open: bool,
623 more_filters_open: bool,
624 filter_input: Option<Entity<InputState>>,
625 filter_subscription: Option<Subscription>,
626 _activity_observation: Subscription,
627 _subscription: Subscription,
628 _state_observation: Subscription,
629 _agent_observation: Option<Subscription>,
630 _runs_observation: Option<Subscription>,
631}
632
633fn preview_text(preview: &Preview) -> Option<(String, bool)> {
634 match preview {
635 Preview::Empty => None,
636 Preview::Deleted => Some((DELETED_CHAT_PREVIEW.to_owned(), true)),
637 Preview::Event(text) => Some((text.clone(), true)),
638 Preview::Typing(text) => Some((text.clone(), true)),
639 Preview::Text {
640 prefix: Some(prefix),
641 text,
642 } => Some((format!("{prefix}: {text}"), false)),
643 Preview::Text { prefix: None, text } => Some((text.clone(), false)),
644 }
645}
646
647fn local_preview_row(preview: &LocalPreview, color: Hsla) -> Div {
648 let row = h_flex()
649 .flex_1()
650 .min_w_0()
651 .overflow_hidden()
652 .gap(px(4.))
653 .items_center()
654 .text_size(px(12.5))
655 .text_color(color);
656 match preview {
657 LocalPreview::Draft(text) => row
658 .child(
659 div()
660 .flex_none()
661 .text_color(theme::accent_text())
662 .child("Draft:"),
663 )
664 .child(div().truncate().child(text.clone())),
665 LocalPreview::NotSent => row.child(
666 div()
667 .flex_none()
668 .text_color(theme::red_soft())
669 .child("Not sent"),
670 ),
671 }
672}
673
674impl SidebarView {
675 pub fn new(
676 state: Entity<AppState>,
677 activity_panel: Entity<ActivityPanel>,
678 saved_panel: Entity<SavedPanel>,
679 agents_panel: Option<Entity<AgentsPanel>>,
680 cx: &mut Context<Self>,
681 ) -> Self {
682 let subscription = cx.subscribe(&state, |this, state, event: &AppEvent, cx| {
683 if matches!(event, AppEvent::Selection) {
684 if state.read(cx).new_chat {
685 this.tab = SidebarTab::Chats;
686 }
687 this.expand_team_of_selection(cx);
688 }
689 if matches!(
690 event,
691 AppEvent::Sidebar
692 | AppEvent::CallHistory
693 | AppEvent::Selection
694 | AppEvent::Directory
695 | AppEvent::Typing
696 | AppEvent::LocalPreviews
697 ) {
698 cx.notify();
699 }
700 });
701 let state_observation = cx.observe(&state, |_, _, cx| cx.notify());
702 let agent_observation =
703 crate::agent_api::handle(cx).map(|api| cx.observe(&api, |_, _, cx| cx.notify()));
704 let runs_observation = crate::agents::runs_handle(cx).map(|runs| {
705 cx.observe(&runs, |view, _, cx| {
706 if view.tab == SidebarTab::Agents && !crate::agents::agents_enabled(cx) {
707 view.open_tab(SidebarTab::Chats, cx);
708 }
709 })
710 });
711 let tab = if state.read(cx).start_on_channels {
712 SidebarTab::Channels
713 } else {
714 SidebarTab::Chats
715 };
716 let collapsed_teams = load_collapsed_teams(&state.read(cx).store);
717 let activity = activity_panel.read(cx).activity();
718 let activity_observation = cx.observe(&activity, |view, _, cx| {
719 if view
720 .chat_filter
721 .has(crate::chat_filter::ChatFilterPill::Mentions)
722 {
723 cx.notify();
724 }
725 });
726 cx.spawn(async move |this, cx| {
727 loop {
728 cx.background_executor().timer(CALLS_REFRESH).await;
729 let alive = this.update(cx, |view, cx| {
730 if view.tab == SidebarTab::Calls {
731 cx.notify();
732 }
733 });
734 if alive.is_err() {
735 break;
736 }
737 }
738 })
739 .detach();
740 SidebarView {
741 state,
742 tab,
743 activity_panel,
744 saved_panel,
745 agents_panel,
746 collapsed_teams,
747 revealed_channel_teams: HashSet::new(),
748 hidden_teams_open: false,
749 chats_scroll: ScrollHandle::new(),
750 older_chats_trigger_top: Cell::new(None),
751 older_chats_seen: Cell::new((false, false)),
752 channels_scroll: ScrollHandle::new(),
753 calls_scroll: ScrollHandle::new(),
754 chat_filter: ChatFilter::default(),
755 filter_box_open: false,
756 more_filters_open: false,
757 filter_input: None,
758 filter_subscription: None,
759 _activity_observation: activity_observation,
760 _subscription: subscription,
761 _state_observation: state_observation,
762 _agent_observation: agent_observation,
763 _runs_observation: runs_observation,
764 }
765 }
766
767 pub fn tab(&self) -> SidebarTab {
768 self.tab
769 }
770
771 pub fn show_activity(&mut self, cx: &mut Context<Self>) {
772 self.open_tab(SidebarTab::Activity, cx);
773 }
774
775 pub fn show_chats(&mut self, cx: &mut Context<Self>) {
776 self.open_tab(SidebarTab::Chats, cx);
777 }
778
779 pub fn show_channels(&mut self, cx: &mut Context<Self>) {
780 self.open_tab(SidebarTab::Channels, cx);
781 }
782
783 pub fn show_calls(&mut self, cx: &mut Context<Self>) {
784 self.open_tab(SidebarTab::Calls, cx);
785 }
786
787 pub fn show_saved(&mut self, cx: &mut Context<Self>) {
788 self.open_tab(SidebarTab::Saved, cx);
789 }
790
791 pub fn show_agents(&mut self, cx: &mut Context<Self>) {
792 self.open_tab(SidebarTab::Agents, cx);
793 }
794
795 pub fn open_tab(&mut self, tab: SidebarTab, cx: &mut Context<Self>) {
796 if tab == SidebarTab::Agents && !crate::agents::agents_enabled(cx) {
797 return;
798 }
799 self.tab = tab;
800 match tab {
801 SidebarTab::Calls => self.state.update(cx, |state, cx| {
802 state.refresh_call_history(cx);
803 state.refresh_calendar(cx);
804 }),
805 SidebarTab::Saved => self.state.update(cx, |state, cx| state.refresh_saved(cx)),
806 _ => {}
807 }
808 cx.notify();
809 }
810
811 pub fn select_adjacent(&mut self, step: Step, cx: &mut Context<Self>) {
812 let selection = match self.tab {
813 SidebarTab::Chats => self
814 .state
815 .read(cx)
816 .adjacent_chat_id(step)
817 .map(Selection::Chat),
818 SidebarTab::Channels => self.adjacent_channel_id(step, cx).map(Selection::Channel),
819 SidebarTab::Activity | SidebarTab::Calls | SidebarTab::Saved | SidebarTab::Agents => {
820 None
821 }
822 };
823 if let Some(selection) = selection {
824 self.state
825 .update(cx, |state, cx| state.select(selection, cx));
826 }
827 }
828
829 fn adjacent_channel_id(&self, step: Step, cx: &mut Context<Self>) -> Option<String> {
830 let state = self.state.read(cx);
831 let selected_channel = match &state.selection {
832 Some(Selection::Channel(channel_id)) => Some(channel_id.as_str()),
833 _ => None,
834 };
835 let input = ChannelOrderInput {
836 teams: &state.sidebar.teams,
837 pinned_ids: &state.directory.pinned_channels,
838 collapsed_teams: &self.collapsed_teams,
839 revealed_channel_teams: &self.revealed_channel_teams,
840 hidden_teams_open: self.hidden_teams_open,
841 selected_channel,
842 };
843 step_id(&visible_channel_ids(&input), selected_channel, step)
844 }
845
846 fn expand_team_of_selection(&mut self, cx: &mut Context<Self>) {
847 let state = self.state.read(cx);
848 let Some(Selection::Channel(channel_id)) = state.selection.clone() else {
849 return;
850 };
851 let team_id = state
852 .sidebar
853 .teams
854 .iter()
855 .find(|entry| {
856 entry
857 .channels
858 .iter()
859 .any(|channel| channel.id == channel_id)
860 })
861 .map(|entry| entry.team.id.clone());
862 if let Some(team_id) = team_id
863 && self.collapsed_teams.remove(&team_id)
864 {
865 save_collapsed_teams(&self.state.read(cx).store, &self.collapsed_teams);
866 }
867 }
868
869 fn new_chat_button(&self, cx: &mut Context<Self>) -> impl IntoElement {
870 let active = self.state.read(cx).new_chat;
871 div()
872 .id("new-chat")
873 .size(px(NEW_CHAT_BUTTON_SIZE))
874 .flex_none()
875 .flex()
876 .items_center()
877 .justify_center()
878 .rounded(px(8.))
879 .bg(theme::surface_raised())
880 .cursor_pointer()
881 .when(active, |button| button.bg(theme::popup_hover()))
882 .hover(|button| button.bg(theme::popup_hover()))
883 .tooltip(|window, cx| Tooltip::new("New chat (Ctrl+N)").build(window, cx))
884 .child(icon(IconName::Pencil, 16., theme::text_soft()))
885 .on_click(cx.listener(|this, _, _, cx| {
886 this.state.update(cx, |state, cx| state.start_new_chat(cx));
887 }))
888 }
889
890 fn chat_list_menu_button(&self, cx: &mut Context<Self>) -> impl IntoElement {
891 let state = self.state.clone();
892 let read_only = self.state.read(cx).mode.read_only;
893 Button::new("chat-list-more")
894 .icon(IconName::Ellipsis)
895 .ghost()
896 .size(px(NEW_CHAT_BUTTON_SIZE))
897 .rounded(px(8.))
898 .tooltip("More options")
899 .dropdown_menu(move |menu, _, _| menu.item(new_section_item(&state, read_only)))
900 }
901
902 fn channel_list_menu_button(&self, cx: &mut Context<Self>) -> impl IntoElement {
903 let state = self.state.clone();
904 let read_only = self.state.read(cx).mode.read_only;
905 Button::new("channel-list-more")
906 .icon(IconName::Ellipsis)
907 .ghost()
908 .size(px(NEW_CHAT_BUTTON_SIZE))
909 .rounded(px(8.))
910 .tooltip("More options")
911 .dropdown_menu(move |menu, _, _| {
912 let state = state.clone();
913 menu.item(
914 PopupMenuItem::new(MARK_ALL_READ_LABEL)
915 .icon(menu_icon(IconName::CheckCheck))
916 .disabled(read_only)
917 .on_click(move |_, _, cx| {
918 state.update(cx, |state, cx| state.mark_all_channels_read(cx));
919 }),
920 )
921 })
922 }
923
924 fn draft_row(&self) -> impl IntoElement {
925 h_flex()
926 .id("new-chat-draft")
927 .mx(px(6.))
928 .h(px(ROW_HEIGHT))
929 .flex_none()
930 .px(px(10.))
931 .gap(px(12.))
932 .items_center()
933 .rounded(px(8.))
934 .bg(theme::surface_raised())
935 .child(
936 div()
937 .size(px(AVATAR_SIZE))
938 .flex_none()
939 .flex()
940 .items_center()
941 .justify_center()
942 .rounded_full()
943 .bg(theme::border_strong())
944 .child(icon(IconName::Pencil, 16., theme::text())),
945 )
946 .child(
947 v_flex()
948 .flex_1()
949 .min_w_0()
950 .gap(px(2.))
951 .child(
952 div()
953 .truncate()
954 .text_size(px(14.))
955 .text_color(theme::text())
956 .child("New chat"),
957 )
958 .child(
959 div()
960 .truncate()
961 .text_size(px(12.5))
962 .text_color(theme::text_muted())
963 .child("Draft"),
964 ),
965 )
966 }
967
968 fn section_header(
969 &self,
970 section: &Section,
971 menu: SectionMenuTarget,
972 cx: &mut Context<Self>,
973 ) -> impl IntoElement {
974 let drop_target = drop_target(section);
975 let toggle_id = section.id.clone();
976 let menu_state = self.state.clone();
977 h_flex()
978 .id(SharedString::from(format!("section-{}", section.id)))
979 .h(px(SECTION_HEIGHT))
980 .mt(px(SECTION_GAP))
981 .mx(px(6.))
982 .px(px(10.))
983 .gap(px(6.))
984 .items_center()
985 .rounded(px(6.))
986 .cursor_pointer()
987 .hover(|header| header.bg(theme::row_hover()))
988 .when_some(drop_target, |header, drop_target| {
989 header
990 .drag_over::<DraggedChat>(|style, _, _, _| {
991 style
992 .bg(theme::drop_background())
993 .border_1()
994 .border_color(theme::accent())
995 })
996 .on_drop(cx.listener(move |this, dragged: &DraggedChat, _, cx| {
997 let target = drop_target.clone();
998 this.state.update(cx, |state, cx| {
999 state.move_chat(&dragged.id, target.as_deref(), cx)
1000 });
1001 }))
1002 })
1003 .child(icon(
1004 if section.collapsed {
1005 IconName::ChevronRight
1006 } else {
1007 IconName::ChevronDown
1008 },
1009 12.,
1010 theme::text_muted(),
1011 ))
1012 .child(
1013 div()
1014 .flex_1()
1015 .min_w_0()
1016 .truncate()
1017 .text_size(px(12.))
1018 .font_weight(FontWeight::SEMIBOLD)
1019 .text_color(theme::text_soft())
1020 .child(section.name.clone()),
1021 )
1022 .when(section.collapsed && section.unread_chats > 0, |header| {
1023 header.child(count_badge(section.unread_chats, false))
1024 })
1025 .child(
1026 div()
1027 .text_size(px(11.))
1028 .text_color(theme::text_faint())
1029 .child(section.count.to_string()),
1030 )
1031 .on_click(cx.listener(move |this, _, _, cx| {
1032 let id = toggle_id.clone();
1033 this.state
1034 .update(cx, |state, cx| state.toggle_collapsed(&id, cx));
1035 }))
1036 .context_menu(move |popup, _, _| section_menu(popup, &menu_state, &menu))
1037 }
1038
1039 fn see_more_row(&self, section: &Section, cx: &mut Context<Self>) -> impl IntoElement {
1040 let toggle_id = section.id.clone();
1041 let label = if section.hidden_count > 0 {
1042 format!("See more ({})", section.hidden_count)
1043 } else {
1044 "See less".to_owned()
1045 };
1046 div()
1047 .id(SharedString::from(format!("see-more-{}", section.id)))
1048 .mx(px(6.))
1049 .pl(px(10. + AVATAR_SIZE + 12.))
1050 .h(px(SEE_MORE_HEIGHT))
1051 .flex()
1052 .items_center()
1053 .rounded(px(6.))
1054 .cursor_pointer()
1055 .text_size(px(12.5))
1056 .text_color(theme::accent_text())
1057 .hover(|row| row.bg(theme::row_hover()))
1058 .child(label)
1059 .on_click(cx.listener(move |this, _, _, cx| {
1060 let id = toggle_id.clone();
1061 this.state
1062 .update(cx, |state, cx| state.toggle_list_expanded(&id, cx));
1063 }))
1064 }
1065
1066 fn chat_row(
1067 &self,
1068 item: &ChatItem,
1069 selected: bool,
1070 local_preview: Option<&LocalPreview>,
1071 proposal_agent: Option<&'static str>,
1072 directory: &Directory,
1073 menu: &MenuContext,
1074 ) -> AnyElement {
1075 let ring = if selected {
1076 theme::surface_raised()
1077 } else {
1078 theme::surface()
1079 };
1080 let unread = item.unread.is_unread();
1081 let presence = item
1082 .presence_user
1083 .as_deref()
1084 .map(|user_id| directory.presence_of(user_id));
1085 let avatar = spec_avatar(directory, &item.avatar, AVATAR_SIZE, ring);
1086 let avatar = match presence {
1087 Some(kind) => with_presence(avatar, kind, AVATAR_SIZE, ring).into_any_element(),
1088 None => avatar,
1089 };
1090 let name = div()
1091 .flex_1()
1092 .min_w_0()
1093 .truncate()
1094 .text_size(px(14.))
1095 .font_weight(if unread {
1096 FontWeight::BOLD
1097 } else {
1098 FontWeight::NORMAL
1099 })
1100 .text_color(theme::text())
1101 .child(item.title.clone());
1102 let time = h_flex()
1103 .flex_none()
1104 .gap(px(4.))
1105 .items_center()
1106 .text_size(px(11.))
1107 .text_color(if unread {
1108 theme::accent_text()
1109 } else {
1110 theme::text_muted()
1111 })
1112 .child(item.time_label.clone());
1113 let preview_color = if matches!(item.preview, Preview::Typing(_)) {
1114 theme::accent_text()
1115 } else if unread {
1116 theme::text_strong()
1117 } else {
1118 theme::text_muted()
1119 };
1120 let preview = match (proposal_agent, local_preview) {
1121 (Some(agent), _) => Some(
1122 h_flex()
1123 .flex_1()
1124 .min_w_0()
1125 .overflow_hidden()
1126 .text_size(px(12.5))
1127 .text_color(theme::accent_text())
1128 .child(div().truncate().child(format!("Proposal from {agent}"))),
1129 ),
1130 (None, Some(local)) => Some(local_preview_row(local, preview_color)),
1131 (None, None) => preview_text(&item.preview).map(|(text, italic)| {
1132 h_flex()
1133 .flex_1()
1134 .min_w_0()
1135 .overflow_hidden()
1136 .gap(px(4.))
1137 .items_center()
1138 .text_size(px(12.5))
1139 .when(italic, |preview| preview.italic())
1140 .text_color(preview_color)
1141 .child(div().truncate().child(text))
1142 }),
1143 };
1144 let lines = v_flex()
1145 .flex_1()
1146 .min_w_0()
1147 .gap(px(2.))
1148 .child(
1149 h_flex()
1150 .gap(px(8.))
1151 .items_baseline()
1152 .child(name)
1153 .child(time),
1154 )
1155 .child(
1156 h_flex()
1157 .h(px(18.))
1158 .gap(px(8.))
1159 .items_center()
1160 .child(preview.unwrap_or_else(|| div().flex_1()))
1161 .children(match proposal_agent {
1162 Some(_) => Some(
1163 div()
1164 .size(px(PROPOSAL_DOT_SIZE))
1165 .flex_none()
1166 .rounded_full()
1167 .bg(theme::accent())
1168 .into_any_element(),
1169 ),
1170 None => unread_marker(item.unread, item.muted)
1171 .map(IntoElement::into_any_element),
1172 }),
1173 );
1174 let state = self.state.clone();
1175 let chat_id = item.id.clone();
1176 let dragged = DraggedChat {
1177 id: item.id.clone(),
1178 title: item.title.clone(),
1179 };
1180 let menu_context = menu.clone();
1181 let reorder_folder = item
1182 .folder_id
1183 .clone()
1184 .filter(|folder_id| !is_pending_folder(folder_id));
1185 let target = ChatMenuTarget {
1186 id: item.id.clone(),
1187 title: item.title.clone(),
1188 folder_id: item.folder_id.clone(),
1189 unread,
1190 muted: item.muted,
1191 is_group: item.is_group,
1192 member_count: item.member_count,
1193 };
1194 h_flex()
1195 .id(SharedString::from(format!("chat-{}", 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}