BlueGit

ict rusty-teams

Files

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

sidebar.rs

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