BlueGit

ict rusty-teams

Files

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

sidebar.rs

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