BlueGit

ict rusty-teams

Files

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

sidebar.rs

sidebar.rs 80.5 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 self.restore_last_view(tab, cx);
534 cx.emit(SidebarEvent::TabChanged);
535 }
536 match tab {
537 SidebarTab::Calls => self.state.update(cx, |state, cx| {
538 state.refresh_call_history(cx);
539 state.refresh_calendar(cx);
540 }),
541 SidebarTab::Calendar => self.state.update(cx, |state, cx| state.open_calendar(cx)),
542 SidebarTab::Activity => {
543 let activity = self.activity_panel.read(cx).activity();
544 activity.update(cx, |activity, cx| activity.mark_seen(cx));
545 }
546 _ => {}
547 }
548 cx.notify();
549 }
550
551 fn restore_last_view(&mut self, tab: SidebarTab, cx: &mut Context<Self>) {
552 let first_channel_id = self.first_channel_id(cx);
553 let state = self.state.read(cx);
554 if let Some(current) = &state.selection {
555 self.last_views.remember(current);
556 }
557 let first_chat_id = state.sidebar.chats.first().map(|chat| chat.id.as_str());
558 if let Some(selection) = self.last_views.restore(
559 tab,
560 state.selection.as_ref(),
561 first_chat_id,
562 first_channel_id.as_deref(),
563 ) {
564 self.state
565 .update(cx, |state, cx| state.select(selection, cx));
566 }
567 }
568
569 pub fn select_adjacent(&mut self, step: Step, cx: &mut Context<Self>) {
570 let selection = match self.tab {
571 SidebarTab::Chats => self
572 .state
573 .read(cx)
574 .adjacent_chat_id(step)
575 .map(Selection::Chat),
576 SidebarTab::Channels => self.adjacent_channel_id(step, cx).map(Selection::Channel),
577 SidebarTab::Activity
578 | SidebarTab::Calendar
579 | SidebarTab::Calls
580 | SidebarTab::Agents => None,
581 };
582 if let Some(selection) = selection {
583 self.state
584 .update(cx, |state, cx| state.select(selection, cx));
585 }
586 }
587
588 fn adjacent_channel_id(&self, step: Step, cx: &mut Context<Self>) -> Option<String> {
589 let state = self.state.read(cx);
590 let selected_channel = match &state.selection {
591 Some(Selection::Channel(channel_id)) => Some(channel_id.as_str()),
592 _ => None,
593 };
594 let input = self.channel_order_input(state, selected_channel);
595 step_id(&visible_channel_ids(&input), selected_channel, step)
596 }
597
598 fn first_channel_id(&self, cx: &App) -> Option<String> {
599 let input = self.channel_order_input(self.state.read(cx), None);
600 visible_channel_ids(&input)
601 .first()
602 .map(|id| (*id).to_owned())
603 }
604
605 fn channel_order_input<'a>(
606 &'a self,
607 state: &'a AppState,
608 selected_channel: Option<&'a str>,
609 ) -> ChannelOrderInput<'a> {
610 ChannelOrderInput {
611 teams: &state.sidebar.teams,
612 pinned_ids: &state.directory.pinned_channels,
613 collapsed_teams: &self.collapsed_teams,
614 revealed_channel_teams: &self.revealed_channel_teams,
615 hidden_teams_open: self.hidden_teams_open,
616 selected_channel,
617 }
618 }
619
620 fn expand_team_of_selection(&mut self, cx: &mut Context<Self>) {
621 let state = self.state.read(cx);
622 let Some(Selection::Channel(channel_id)) = state.selection.clone() else {
623 return;
624 };
625 let team_id = state
626 .sidebar
627 .teams
628 .iter()
629 .find(|entry| {
630 entry
631 .channels
632 .iter()
633 .any(|channel| channel.id == channel_id)
634 })
635 .map(|entry| entry.team.id.clone());
636 if let Some(team_id) = team_id
637 && self.collapsed_teams.remove(&team_id)
638 {
639 save_collapsed_teams(&self.state.read(cx).store, &self.collapsed_teams);
640 }
641 }
642
643 fn new_chat_button(&self, cx: &mut Context<Self>) -> impl IntoElement {
644 let active = self.state.read(cx).new_chat;
645 div()
646 .id("new-chat")
647 .size(px(NEW_CHAT_BUTTON_SIZE))
648 .flex_none()
649 .flex()
650 .items_center()
651 .justify_center()
652 .rounded(px(8.))
653 .bg(theme::surface_raised())
654 .cursor_pointer()
655 .when(active, |button| button.bg(theme::popup_hover()))
656 .hover(|button| button.bg(theme::popup_hover()))
657 .tooltip(|window, cx| Tooltip::new(tr("sidebar.new_chat_shortcut")).build(window, cx))
658 .child(icon(IconName::Pencil, 16., theme::text_soft()))
659 .on_click(cx.listener(|this, _, _, cx| {
660 this.state.update(cx, |state, cx| state.start_new_chat(cx));
661 }))
662 }
663
664 fn chat_list_menu_button(&self, cx: &mut Context<Self>) -> impl IntoElement {
665 let state = self.state.clone();
666 let section_ids = self.section_ids.borrow().clone();
667 let read_only = self.state.read(cx).mode.read_only;
668 Button::new("chat-list-more")
669 .icon(IconName::Ellipsis)
670 .ghost()
671 .size(px(NEW_CHAT_BUTTON_SIZE))
672 .rounded(px(8.))
673 .tooltip(tr("sidebar.more_options"))
674 .dropdown_menu(move |menu, _, _| {
675 let (read_state, collapse_state) = (state.clone(), state.clone());
676 let section_ids = section_ids.clone();
677 menu.item(
678 PopupMenuItem::new(tr("sidebar.mark_all_read"))
679 .icon(menu_icon(IconName::CheckCheck))
680 .disabled(read_only)
681 .on_click(move |_, _, cx| {
682 read_state.update(cx, |state, cx| state.mark_all_chats_read(cx));
683 }),
684 )
685 .item(
686 PopupMenuItem::new(tr("sidebar.collapse_all"))
687 .icon(menu_icon(IconName::ChevronsDownUp))
688 .on_click(move |_, _, cx| {
689 let ids = section_ids.clone();
690 collapse_state
691 .update(cx, |state, cx| state.collapse_sections(&ids, cx));
692 }),
693 )
694 .separator()
695 .item(new_section_item(&state, read_only))
696 })
697 }
698
699 fn channel_list_menu_button(&self, cx: &mut Context<Self>) -> impl IntoElement {
700 let state = self.state.clone();
701 let read_only = self.state.read(cx).mode.read_only;
702 let view = cx.entity().downgrade();
703 let team_ids: Vec<String> = self
704 .state
705 .read(cx)
706 .sidebar
707 .teams
708 .iter()
709 .map(|entry| entry.team.id.clone())
710 .collect();
711 Button::new("channel-list-more")
712 .icon(IconName::Ellipsis)
713 .ghost()
714 .size(px(NEW_CHAT_BUTTON_SIZE))
715 .rounded(px(8.))
716 .tooltip(tr("sidebar.more_options"))
717 .dropdown_menu(move |menu, _, _| {
718 let (state, view, team_ids) = (state.clone(), view.clone(), team_ids.clone());
719 menu.item(
720 PopupMenuItem::new(tr("sidebar.mark_all_read"))
721 .icon(menu_icon(IconName::CheckCheck))
722 .disabled(read_only)
723 .on_click(move |_, _, cx| {
724 state.update(cx, |state, cx| state.mark_all_channels_read(cx));
725 }),
726 )
727 .item(
728 PopupMenuItem::new(tr("sidebar.collapse_all"))
729 .icon(menu_icon(IconName::ChevronsDownUp))
730 .on_click(move |_, _, cx| {
731 let team_ids = team_ids.clone();
732 view.update(cx, |this, cx| this.collapse_all_teams(team_ids, cx))
733 .ok();
734 }),
735 )
736 })
737 }
738
739 fn draft_row(&self) -> impl IntoElement {
740 h_flex()
741 .id("new-chat-draft")
742 .mx(px(6.))
743 .h(px(ROW_HEIGHT))
744 .flex_none()
745 .px(px(10.))
746 .gap(px(12.))
747 .items_center()
748 .rounded(px(8.))
749 .bg(theme::surface_raised())
750 .child(
751 div()
752 .size(px(AVATAR_SIZE))
753 .flex_none()
754 .flex()
755 .items_center()
756 .justify_center()
757 .rounded_full()
758 .bg(theme::border_strong())
759 .child(icon(IconName::Pencil, 16., theme::text())),
760 )
761 .child(
762 v_flex()
763 .flex_1()
764 .min_w_0()
765 .gap(px(2.))
766 .child(
767 div()
768 .truncate()
769 .text_size(px(14.))
770 .text_color(theme::text())
771 .child(tr("sidebar.new_chat")),
772 )
773 .child(
774 div()
775 .truncate()
776 .text_size(px(12.5))
777 .text_color(theme::text_muted())
778 .child(tr("sidebar.draft")),
779 ),
780 )
781 }
782
783 fn section_header(
784 &self,
785 section: &Section,
786 menu: SectionMenuTarget,
787 cx: &mut Context<Self>,
788 ) -> impl IntoElement {
789 let drop_target = drop_target(section);
790 let toggle_id = section.id.clone();
791 let menu_state = self.state.clone();
792 h_flex()
793 .id(SharedString::from(format!("section-{}", section.id)))
794 .h(px(SECTION_HEIGHT))
795 .mt(px(SECTION_GAP))
796 .mx(px(6.))
797 .px(px(10.))
798 .gap(px(6.))
799 .items_center()
800 .rounded(px(6.))
801 .cursor_pointer()
802 .hover(|header| header.bg(theme::row_hover()))
803 .when_some(drop_target, |header, drop_target| {
804 header
805 .drag_over::<DraggedChat>(|style, _, _, _| {
806 style
807 .bg(theme::drop_background())
808 .border_1()
809 .border_color(theme::accent())
810 })
811 .on_drop(cx.listener(move |this, dragged: &DraggedChat, _, cx| {
812 let target = drop_target.clone();
813 this.state.update(cx, |state, cx| {
814 state.move_chat(&dragged.id, target.as_deref(), cx)
815 });
816 }))
817 })
818 .child(icon(
819 if section.collapsed {
820 IconName::ChevronRight
821 } else {
822 IconName::ChevronDown
823 },
824 12.,
825 theme::text_muted(),
826 ))
827 .child(
828 div()
829 .flex_1()
830 .min_w_0()
831 .truncate()
832 .text_size(px(12.))
833 .font_weight(FontWeight::SEMIBOLD)
834 .text_color(theme::text_soft())
835 .child(section.name.clone()),
836 )
837 .when(section.collapsed && section.unread_chats > 0, |header| {
838 header.child(count_badge(section.unread_chats, false))
839 })
840 .on_click(cx.listener(move |this, _, _, cx| {
841 let id = toggle_id.clone();
842 this.state
843 .update(cx, |state, cx| state.toggle_collapsed(&id, cx));
844 }))
845 .context_menu(move |popup, _, _| section_menu(popup, &menu_state, &menu))
846 }
847
848 fn quick_views_section(&self, cx: &mut Context<Self>) -> (Div, f32) {
849 let (collapsed, open_view) = {
850 let state = self.state.read(cx);
851 (
852 state.collapsed.contains(QUICK_VIEWS_SECTION_ID),
853 state.quick_view,
854 )
855 };
856 let header = h_flex()
857 .id(QUICK_VIEWS_SECTION_ID)
858 .h(px(SECTION_HEIGHT))
859 .flex_none()
860 .mt(px(SECTION_GAP))
861 .mx(px(6.))
862 .px(px(10.))
863 .gap(px(6.))
864 .items_center()
865 .rounded(px(6.))
866 .cursor_pointer()
867 .hover(|header| header.bg(theme::row_hover()))
868 .child(icon(
869 if collapsed {
870 IconName::ChevronRight
871 } else {
872 IconName::ChevronDown
873 },
874 12.,
875 theme::text_muted(),
876 ))
877 .child(
878 div()
879 .flex_1()
880 .text_size(px(12.))
881 .font_weight(FontWeight::SEMIBOLD)
882 .text_color(theme::text_soft())
883 .child(tr("sidebar.quick_views")),
884 )
885 .on_click(cx.listener(|this, _, _, cx| {
886 this.state.update(cx, |state, cx| {
887 state.toggle_collapsed(QUICK_VIEWS_SECTION_ID, cx)
888 });
889 }));
890 let mut section = v_flex().w_full().child(header);
891 let mut height = SECTION_GAP + SECTION_HEIGHT;
892 if !collapsed {
893 for (view, glyph) in QUICK_VIEWS {
894 section =
895 section.child(self.quick_view_row(view, glyph, open_view == Some(view), cx));
896 height += QUICK_ROW_HEIGHT;
897 }
898 }
899 (section, height)
900 }
901
902 fn quick_view_row(
903 &self,
904 view: QuickView,
905 glyph: &'static str,
906 selected: bool,
907 cx: &mut Context<Self>,
908 ) -> Stateful<Div> {
909 h_flex()
910 .id(SharedString::from(format!("quick-view-{}", view.title())))
911 .debug_selector(move || format!("quick-view/{}", view.title()))
912 .h(px(QUICK_ROW_HEIGHT))
913 .flex_none()
914 .mx(px(6.))
915 .px(px(10.))
916 .gap(px(10.))
917 .items_center()
918 .rounded(px(8.))
919 .cursor_pointer()
920 .when(selected, |row| row.bg(theme::surface_raised()))
921 .when(!selected, |row| row.hover(|row| row.bg(theme::row_hover())))
922 .child(symbol(glyph, QUICK_ICON_SIZE, theme::text_soft()))
923 .child(
924 div()
925 .text_size(px(13.))
926 .text_color(theme::text())
927 .child(view.title()),
928 )
929 .on_click(cx.listener(move |this, _, _, cx| {
930 this.state
931 .update(cx, |state, cx| state.open_quick_view(view, cx));
932 }))
933 }
934
935 fn see_more_row(&self, section: &Section, cx: &mut Context<Self>) -> impl IntoElement {
936 let toggle_id = section.id.clone();
937 let label = if section.hidden_count > 0 {
938 let mut args = HashMap::new();
939 args.insert("count".to_owned(), section.hidden_count.to_string());
940 tr_args("sidebar.see_more", &args)
941 } else {
942 tr("sidebar.see_less")
943 };
944 div()
945 .id(SharedString::from(format!("see-more-{}", section.id)))
946 .mx(px(6.))
947 .pl(px(10. + AVATAR_SIZE + 12.))
948 .h(px(SEE_MORE_HEIGHT))
949 .flex()
950 .items_center()
951 .rounded(px(6.))
952 .cursor_pointer()
953 .text_size(px(12.5))
954 .text_color(theme::accent_text())
955 .hover(|row| row.bg(theme::row_hover()))
956 .child(label)
957 .on_click(cx.listener(move |this, _, _, cx| {
958 let id = toggle_id.clone();
959 this.state
960 .update(cx, |state, cx| state.toggle_list_expanded(&id, cx));
961 }))
962 }
963
964 fn chat_row(
965 &self,
966 item: &ChatItem,
967 selected: bool,
968 local_preview: Option<&LocalPreview>,
969 proposal_agent: Option<&'static str>,
970 directory: &Directory,
971 menu: &MenuContext,
972 ) -> AnyElement {
973 let ring = if selected {
974 theme::surface_raised()
975 } else {
976 theme::surface()
977 };
978 let unread = item.unread.is_unread();
979 let presence = item
980 .presence_user
981 .as_deref()
982 .map(|user_id| directory.presence_of(user_id));
983 let avatar = spec_avatar(directory, &item.avatar, AVATAR_SIZE, ring);
984 let avatar = match presence {
985 Some(kind) => with_presence(avatar, kind, AVATAR_SIZE, ring).into_any_element(),
986 None => avatar,
987 };
988 let name = div()
989 .flex_1()
990 .min_w_0()
991 .truncate()
992 .text_size(px(14.))
993 .font_weight(if unread {
994 FontWeight::BOLD
995 } else {
996 FontWeight::NORMAL
997 })
998 .text_color(theme::text())
999 .child(item.title.clone());
1000 let row_group = SharedString::from(format!("chat-row-{}", item.id));
1001 let time = h_flex()
1002 .flex_none()
1003 .gap(px(4.))
1004 .items_center()
1005 .text_size(px(11.))
1006 .text_color(if unread {
1007 theme::accent_text()
1008 } else {
1009 theme::text_muted()
1010 })
1011 .group_hover(row_group.clone(), |time| time.invisible())
1012 .child(item.time_label.clone());
1013 let preview_color = if matches!(item.preview, Preview::Typing(_)) {
1014 theme::accent_text()
1015 } else if unread {
1016 theme::text_strong()
1017 } else {
1018 theme::text_muted()
1019 };
1020 let preview = match (proposal_agent, local_preview) {
1021 (Some(agent), _) => Some(
1022 h_flex()
1023 .flex_1()
1024 .min_w_0()
1025 .overflow_hidden()
1026 .text_size(px(12.5))
1027 .text_color(theme::accent_text())
1028 .child(div().truncate().child(format!("Proposal from {agent}"))),
1029 ),
1030 (None, Some(local)) => Some(local_preview_row(local, preview_color)),
1031 (None, None) => preview_text(&item.preview).map(|(text, italic)| {
1032 h_flex()
1033 .flex_1()
1034 .min_w_0()
1035 .overflow_hidden()
1036 .gap(px(4.))
1037 .items_center()
1038 .text_size(px(12.5))
1039 .when(italic, |preview| preview.italic())
1040 .text_color(preview_color)
1041 .child(div().truncate().child(text))
1042 }),
1043 };
1044 let lines = v_flex()
1045 .flex_1()
1046 .min_w_0()
1047 .gap(px(2.))
1048 .child(
1049 h_flex()
1050 .gap(px(8.))
1051 .items_baseline()
1052 .child(name)
1053 .child(time),
1054 )
1055 .child(
1056 h_flex()
1057 .h(px(18.))
1058 .gap(px(8.))
1059 .items_center()
1060 .child(preview.unwrap_or_else(|| div().flex_1()))
1061 .children(importance_marker(item.importance))
1062 .children(match proposal_agent {
1063 Some(_) => Some(
1064 div()
1065 .size(px(PROPOSAL_DOT_SIZE))
1066 .flex_none()
1067 .rounded_full()
1068 .bg(theme::accent())
1069 .into_any_element(),
1070 ),
1071 None => unread_marker(item.unread, item.muted)
1072 .map(IntoElement::into_any_element),
1073 }),
1074 );
1075 let state = self.state.clone();
1076 let chat_id = item.id.clone();
1077 let (warm_state, warm_chat_id) = (self.state.clone(), item.id.clone());
1078 let dragged = DraggedChat {
1079 id: item.id.clone(),
1080 title: item.title.clone(),
1081 };
1082 let menu_context = menu.clone();
1083 let reorder_folder = item
1084 .folder_id
1085 .clone()
1086 .filter(|folder_id| !is_pending_folder(folder_id));
1087 let target = ChatMenuTarget {
1088 id: item.id.clone(),
1089 title: item.title.clone(),
1090 folder_id: item.folder_id.clone(),
1091 unread,
1092 muted: item.muted,
1093 is_group: item.is_group,
1094 member_count: item.member_count,
1095 };
1096 let more_context = menu.clone();
1097 let more_target = target.clone();
1098 let more_button = div()
1099 .absolute()
1100 .top(px(ROW_MORE_TOP))
1101 .right(px(ROW_MORE_RIGHT))
1102 .invisible()
1103 .group_hover(row_group.clone(), |button| button.visible())
1104 .on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation())
1105 .child(
1106 Button::new(SharedString::from(format!("chat-more-{}", item.id)))
1107 .ghost()
1108 .xsmall()
1109 .icon(IconName::Ellipsis)
1110 .tooltip(tr("sidebar.more_options"))
1111 .dropdown_menu(move |popup, window, cx| {
1112 chat_menu(popup, window, cx, &more_context, &more_target)
1113 }),
1114 );
1115 h_flex()
1116 .id(SharedString::from(format!("chat-{}", item.id)))
1117 .debug_selector(|| chat_row_selector(&item.id))
1118 .group(row_group)
1119 .relative()
1120 .mx(px(6.))
1121 .h(px(ROW_HEIGHT))
1122 .flex_none()
1123 .px(px(10.))
1124 .gap(px(12.))
1125 .items_center()
1126 .rounded(px(8.))
1127 .cursor_pointer()
1128 .when(selected, |row| row.bg(theme::surface_raised()))
1129 .when(!selected, |row| row.hover(|row| row.bg(theme::row_hover())))
1130 .on_drag(dragged, |dragged, _, _, cx| {
1131 let title = dragged.title.clone();
1132 cx.new(|_| DragGhost { title })
1133 })
1134 .when_some(reorder_folder, |row, folder_id| {
1135 let (drop_state, target_id) = (self.state.clone(), item.id.clone());
1136 row.drag_over::<DraggedChat>(|style, _, _, _| style.bg(theme::drop_background()))
1137 .on_drop(move |dragged: &DraggedChat, _, cx| {
1138 drop_state.update(cx, |state, cx| {
1139 state.drop_chat_on(&dragged.id, &target_id, &folder_id, cx)
1140 });
1141 })
1142 })
1143 .child(avatar)
1144 .child(lines)
1145 .child(more_button)
1146 .on_hover(move |hovered, _, cx| {
1147 if *hovered {
1148 warm_state.update(cx, |state, _| state.warm_chat(&warm_chat_id));
1149 }
1150 })
1151 .on_click(move |_, _, cx| {
1152 let selection = Selection::Chat(chat_id.clone());
1153 state.update(cx, |state, cx| state.select(selection, cx));
1154 })
1155 .context_menu(move |popup, window, cx| {
1156 chat_menu(popup, window, cx, &menu_context, &target)
1157 })
1158 .into_any_element()
1159 }
1160
1161 fn chats_body(&self, window_height: f32, cx: &mut Context<Self>) -> AnyElement {
1162 let proposal_agents = crate::agent_api::pending_agents(cx);
1163 let (sections, section_menus, menu, local_previews) = {
1164 let state = self.state.read(cx);
1165 let now = Utc::now();
1166 let offset = app_base::format::Zone::Local;
1167 let input = SectionInput {
1168 chats: &state.sidebar.chats,
1169 directory: &state.directory,
1170 collapsed: &state.collapsed,
1171 expanded_lists: &state.expanded_lists,
1172 typing: &state.typing,
1173 now,
1174 offset,
1175 };
1176 let sections = if self.chat_filter.is_active() {
1177 let mentioned = self.mentioned_chats(cx);
1178 let selected_id = self.selected_chat_id(cx);
1179 filtered_sections(
1180 &input,
1181 &self.chat_filter,
1182 &mentioned,
1183 selected_id.as_deref(),
1184 )
1185 } else {
1186 build_sections(&input)
1187 };
1188 let menu = MenuContext::new(&self.state, cx);
1189 let section_menus =
1190 section_menu_targets(&sections, &state.directory, state.mode.read_only);
1191 *self.section_ids.borrow_mut() =
1192 sections.iter().map(|section| section.id.clone()).collect();
1193 (sections, section_menus, menu, state.local_previews.clone())
1194 };
1195
1196 let top = -f32::from(scroll_offset(&self.chats_scroll).y);
1197 let measured: f32 = f32::from(self.chats_scroll.bounds().size.height);
1198 let height = if measured > 1. {
1199 measured
1200 } else {
1201 window_height.max(FALLBACK_VIEWPORT)
1202 };
1203 let (visible_from, visible_to) = (top - VIEWPORT_MARGIN, top + height + VIEWPORT_MARGIN);
1204
1205 let (selected, drafting) = {
1206 let state = self.state.read(cx);
1207 (
1208 state
1209 .selection
1210 .clone()
1211 .filter(|_| !state.new_chat && state.quick_view.is_none()),
1212 state.new_chat,
1213 )
1214 };
1215 let mut faces: Vec<String> = Vec::new();
1216 let mut presence_users: Vec<String> = Vec::new();
1217 let mut y = 0.;
1218 let mut list = v_flex().w_full().pb(px(12.));
1219 let mut skipped_height = 0.;
1220 let mut others_end = None;
1221 let directory_state = self.state.clone();
1222 if drafting {
1223 list = list.child(self.draft_row());
1224 y += ROW_HEIGHT;
1225 }
1226 if !self.chat_filter.is_active() {
1227 let (quick_views, quick_height) = self.quick_views_section(cx);
1228 list = list.child(quick_views);
1229 y += quick_height;
1230 }
1231 for (section, section_menu) in sections.iter().zip(section_menus) {
1232 list = with_spacer(list, &mut skipped_height);
1233 list = list.child(self.section_header(section, section_menu, cx));
1234 y += SECTION_GAP + SECTION_HEIGHT;
1235 if section.collapsed {
1236 continue;
1237 }
1238 for item in &section.items {
1239 let on_screen = y + ROW_HEIGHT >= visible_from && y <= visible_to;
1240 if on_screen {
1241 collect_faces(&item.avatar, &mut faces);
1242 presence_users.extend(item.presence_user.clone());
1243 }
1244 if on_screen {
1245 list = with_spacer(list, &mut skipped_height);
1246 let is_selected = selected == Some(Selection::Chat(item.id.clone()));
1247 let directory = &directory_state.read(cx).directory;
1248 let local_preview = local_previews.get(&item.id).filter(|_| !is_selected);
1249 list = list.child(self.chat_row(
1250 item,
1251 is_selected,
1252 local_preview,
1253 proposal_agents.get(&item.id).copied(),
1254 directory,
1255 &menu,
1256 ));
1257 } else {
1258 skipped_height += ROW_HEIGHT;
1259 }
1260 y += ROW_HEIGHT;
1261 }
1262 if section.kind == SectionKind::Others {
1263 others_end = Some(y);
1264 }
1265 if section.hidden_count > 0 || section.list_expanded {
1266 list = with_spacer(list, &mut skipped_height);
1267 list = list.child(self.see_more_row(section, cx));
1268 y += SEE_MORE_HEIGHT;
1269 }
1270 if section.show_empty_hint {
1271 list = with_spacer(list, &mut skipped_height);
1272 list = list.child(
1273 div()
1274 .ml(px(16.))
1275 .mr(px(16.))
1276 .h(px(HINT_HEIGHT))
1277 .flex()
1278 .items_center()
1279 .text_size(px(12.))
1280 .italic()
1281 .text_color(theme::text_faint())
1282 .child(EMPTY_FOLDER_HINT),
1283 );
1284 y += HINT_HEIGHT;
1285 }
1286 }
1287 let (has_older, loading_older) = self.state.read(cx).older_chats();
1288 let previous = self.older_chats_seen.replace((has_older, loading_older));
1289 if older_chats_trigger_resets(previous, (has_older, loading_older)) {
1290 self.older_chats_trigger_top.set(None);
1291 }
1292 if has_older
1293 && wants_older_chats(
1294 others_end,
1295 top + height,
1296 top,
1297 self.older_chats_trigger_top.get(),
1298 loading_older,
1299 )
1300 {
1301 self.older_chats_trigger_top.set(Some(top));
1302 let state = self.state.clone();
1303 cx.defer(move |cx| state.read(cx).request_older_chats());
1304 }
1305 if loading_older {
1306 list = with_spacer(list, &mut skipped_height);
1307 list = list.child(
1308 div()
1309 .pl(px(16. + AVATAR_SIZE + 12.))
1310 .h(px(SEE_MORE_HEIGHT))
1311 .flex()
1312 .items_center()
1313 .text_size(px(12.5))
1314 .italic()
1315 .text_color(theme::text_faint())
1316 .child(tr("sidebar.loading_older")),
1317 );
1318 }
1319 let selected_chat_id = self.selected_chat_id(cx);
1320 if self.chat_filter.is_active() && has_no_matches(&sections, selected_chat_id.as_deref()) {
1321 list = list.child(self.filter_empty_state(cx));
1322 }
1323 list = with_spacer(list, &mut skipped_height);
1324 self.request_lazy_data(faces, presence_users, cx);
1325 let scroll_area = div()
1326 .id("chats-scroll")
1327 .flex_1()
1328 .min_h_0()
1329 .overflow_y_scrollbar()
1330 .track_scroll(&self.chats_scroll)
1331 .child(list);
1332 with_top_edge(scroll_area, &self.chats_scroll, SIDEBAR_SCROLL_DIVIDER).into_any_element()
1333 }
1334
1335 fn request_lazy_data(
1336 &self,
1337 faces: Vec<String>,
1338 presence_users: Vec<String>,
1339 cx: &mut Context<Self>,
1340 ) {
1341 if faces.is_empty() && presence_users.is_empty() {
1342 return;
1343 }
1344 let state = self.state.clone();
1345 cx.defer(move |cx| {
1346 state.update(cx, |state, cx| {
1347 state.request_avatars(faces, cx);
1348 state.request_presence(presence_users, cx);
1349 });
1350 });
1351 }
1352
1353 fn channel_row(
1354 &self,
1355 channel_id: &str,
1356 entry: &SidebarTeam,
1357 channel: &ChannelRecord,
1358 selected: bool,
1359 team_photo: Option<&AvatarState>,
1360 ) -> AnyElement {
1361 let team = &entry.team;
1362 let unread = channel_shows_unread(entry, channel);
1363 let notifications = entry.channel_notifications(channel_id);
1364 let badge = channel_badge(entry, channel_id);
1365 let name_style = channel_name_style(unread, true, selected);
1366 let state = self.state.clone();
1367 let menu_state = self.state.clone();
1368 let menu_channel_id = channel_id.to_owned();
1369 let selection = Selection::Channel(channel_id.to_owned());
1370 let time = channel_activity(channel);
1371 h_flex()
1372 .id(SharedString::from(format!("pinned-{channel_id}")))
1373 .mx(px(6.))
1374 .h(px(PINNED_CHANNEL_HEIGHT))
1375 .px(px(10.))
1376 .gap(px(12.))
1377 .items_center()
1378 .rounded(px(8.))
1379 .cursor_pointer()
1380 .when(selected, |row| row.bg(theme::surface_raised()))
1381 .when(!selected, |row| row.hover(|row| row.bg(theme::row_hover())))
1382 .child(team_avatar(team_photo, &team.name, &team.id, 28., 7.))
1383 .child(
1384 v_flex()
1385 .flex_1()
1386 .min_w_0()
1387 .gap(px(1.))
1388 .child(
1389 h_flex()
1390 .gap(px(8.))
1391 .items_baseline()
1392 .child(
1393 div()
1394 .flex_1()
1395 .min_w_0()
1396 .truncate()
1397 .text_size(px(13.5))
1398 .font_weight(name_weight(name_style))
1399 .text_color(name_color(name_style))
1400 .child(format!("{} / {}", team.name, channel.name)),
1401 )
1402 .children(notification_badge(notifications.level))
1403 .child(
1404 div()
1405 .flex_none()
1406 .text_size(px(11.))
1407 .text_color(if unread {
1408 theme::accent_text()
1409 } else {
1410 theme::text_muted()
1411 })
1412 .child(time),
1413 ),
1414 )
1415 .child(
1416 h_flex()
1417 .gap(px(8.))
1418 .items_center()
1419 .child(
1420 div()
1421 .flex_1()
1422 .min_w_0()
1423 .truncate()
1424 .text_size(px(12.))
1425 .text_color(theme::text_muted())
1426 .child(team.name.clone()),
1427 )
1428 .children(badge.map(mention_badge)),
1429 ),
1430 )
1431 .on_click(move |_, _, cx| {
1432 let selection = selection.clone();
1433 state.update(cx, |state, cx| state.select(selection, cx));
1434 })
1435 .context_menu(move |popup, window, cx| {
1436 channel_menu(
1437 popup,
1438 window,
1439 cx,
1440 menu_state.clone(),
1441 &menu_channel_id,
1442 notifications,
1443 )
1444 })
1445 .into_any_element()
1446 }
1447
1448 fn plain_header(&self, label: impl Into<SharedString>) -> impl IntoElement {
1449 div()
1450 .h(px(SECTION_HEIGHT))
1451 .mt(px(SECTION_GAP))
1452 .px(px(16.))
1453 .flex()
1454 .items_center()
1455 .text_size(px(12.))
1456 .font_weight(FontWeight::SEMIBOLD)
1457 .text_color(theme::text_soft())
1458 .child(label.into())
1459 }
1460
1461 fn team_block(
1462 &self,
1463 mut list: Div,
1464 entry: &SidebarTeam,
1465 hoisted_only: bool,
1466 selected: Option<&Selection>,
1467 team_photo: Option<&AvatarState>,
1468 cx: &mut Context<Self>,
1469 ) -> Div {
1470 let team_id = entry.team.id.clone();
1471 let expanded =
1472 self.channel_filter.is_active() || team_expanded(&team_id, &self.collapsed_teams);
1473 let name_style = team_name_style(entry, expanded);
1474 let badge = team_badge(entry, expanded);
1475 let toggle_id = team_id.clone();
1476 let (team_menu_state, team_menu_id) = (self.state.clone(), team_id.clone());
1477 let team_menu_flags = TeamMenuState {
1478 hidden: entry.hidden,
1479 can_add_channel: entry.rights.add_channel,
1480 };
1481 list = list.child(
1482 h_flex()
1483 .id(SharedString::from(format!("team-{team_id}")))
1484 .mx(px(6.))
1485 .h(px(TEAM_ROW_HEIGHT))
1486 .flex_none()
1487 .px(px(10.))
1488 .gap(px(8.))
1489 .items_center()
1490 .rounded(px(8.))
1491 .cursor_pointer()
1492 .hover(|row| row.bg(theme::row_hover()))
1493 .child(icon(
1494 if expanded {
1495 IconName::ChevronDown
1496 } else {
1497 IconName::ChevronRight
1498 },
1499 14.,
1500 theme::text_muted(),
1501 ))
1502 .child(team_avatar(
1503 team_photo,
1504 &entry.team.name,
1505 &entry.team.id,
1506 TEAM_ICON_SIZE,
1507 TEAM_ICON_RADIUS,
1508 ))
1509 .child(
1510 div()
1511 .flex_1()
1512 .min_w_0()
1513 .truncate()
1514 .text_size(px(13.5))
1515 .font_weight(name_weight(name_style))
1516 .text_color(name_color(name_style))
1517 .child(entry.team.name.clone()),
1518 )
1519 .children(badge.map(mention_badge))
1520 .on_click(cx.listener(move |this, _, _, cx| {
1521 if !this.collapsed_teams.remove(&toggle_id) {
1522 this.collapsed_teams.insert(toggle_id.clone());
1523 }
1524 save_collapsed_teams(&this.state.read(cx).store, &this.collapsed_teams);
1525 cx.notify();
1526 }))
1527 .context_menu(move |popup, _, _| {
1528 team_menu(
1529 popup,
1530 team_menu_state.clone(),
1531 &team_menu_id,
1532 team_menu_flags,
1533 )
1534 }),
1535 );
1536 if !expanded {
1537 return list;
1538 }
1539 let is_selected =
1540 |channel: &ChannelRecord| selected == Some(&Selection::Channel(channel.id.clone()));
1541 let revealed = self.revealed_channel_teams.contains(&team_id);
1542 let rows = team_channel_rows(entry, hoisted_only, revealed, is_selected);
1543 for channel in &rows.shown {
1544 list = list.child(self.team_channel_row(
1545 channel,
1546 is_selected(channel),
1547 channel_name_style(
1548 channel_shows_unread(entry, channel),
1549 channel_shown(entry, &channel.id),
1550 is_selected(channel),
1551 ),
1552 entry.channel_notifications(&channel.id),
1553 channel_badge(entry, &channel.id),
1554 ));
1555 }
1556 if rows.hidden_count == 0 {
1557 return list;
1558 }
1559 let toggle_id = team_id.clone();
1560 list = list.child(self.see_all_channels_row(
1561 SharedString::from(format!("hidden-channels-{team_id}")),
1562 revealed,
1563 cx.listener(move |this, _, _, cx| {
1564 if !this.revealed_channel_teams.remove(&toggle_id) {
1565 this.revealed_channel_teams.insert(toggle_id.clone());
1566 }
1567 cx.notify();
1568 }),
1569 ));
1570 for channel in rows.revealed {
1571 list = list.child(self.team_channel_row(
1572 channel,
1573 false,
1574 channel_name_style(channel_shows_unread(entry, channel), false, false),
1575 entry.channel_notifications(&channel.id),
1576 channel_badge(entry, &channel.id),
1577 ));
1578 }
1579 list
1580 }
1581
1582 fn see_all_channels_row(
1583 &self,
1584 id: SharedString,
1585 open: bool,
1586 on_click: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
1587 ) -> impl IntoElement {
1588 h_flex()
1589 .id(id)
1590 .debug_selector(|| SEE_ALL_CHANNELS_TAG.into())
1591 .mx(px(6.))
1592 .h(px(CHANNEL_ROW_HEIGHT))
1593 .flex_none()
1594 .pl(px(CHANNEL_INDENT))
1595 .pr(px(10.))
1596 .items_center()
1597 .rounded(px(8.))
1598 .cursor_pointer()
1599 .hover(|row| row.bg(theme::row_hover()))
1600 .text_size(px(13.5))
1601 .text_color(theme::accent_text())
1602 .child(if open {
1603 tr("sidebar.see_fewer_channels")
1604 } else {
1605 tr("sidebar.see_all_channels")
1606 })
1607 .on_click(on_click)
1608 }
1609
1610 fn reveal_row(
1611 &self,
1612 id: SharedString,
1613 label: String,
1614 open: bool,
1615 indent: f32,
1616 on_click: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
1617 ) -> impl IntoElement {
1618 h_flex()
1619 .id(id)
1620 .mx(px(6.))
1621 .h(px(REVEAL_ROW_HEIGHT))
1622 .flex_none()
1623 .pl(px(indent))
1624 .pr(px(10.))
1625 .gap(px(8.))
1626 .items_center()
1627 .rounded(px(8.))
1628 .cursor_pointer()
1629 .hover(|row| row.bg(theme::row_hover()))
1630 .child(icon(
1631 if open {
1632 IconName::ChevronDown
1633 } else {
1634 IconName::ChevronRight
1635 },
1636 12.,
1637 theme::text_faint(),
1638 ))
1639 .child(
1640 div()
1641 .text_size(px(12.5))
1642 .text_color(theme::text_muted())
1643 .child(label),
1644 )
1645 .on_click(on_click)
1646 }
1647
1648 fn team_channel_row(
1649 &self,
1650 channel: &ChannelRecord,
1651 is_selected: bool,
1652 name_style: NameStyle,
1653 notifications: ChannelNotifications,
1654 badge: Option<MentionBadge>,
1655 ) -> impl IntoElement {
1656 let state = self.state.clone();
1657 let menu_state = self.state.clone();
1658 let menu_channel_id = channel.id.clone();
1659 let selection = Selection::Channel(channel.id.clone());
1660 h_flex()
1661 .id(SharedString::from(format!("channel-{}", channel.id)))
1662 .debug_selector(|| CHANNEL_ROW_TAG.into())
1663 .mx(px(6.))
1664 .h(px(CHANNEL_ROW_HEIGHT))
1665 .flex_none()
1666 .pl(px(CHANNEL_INDENT))
1667 .pr(px(10.))
1668 .gap(px(8.))
1669 .items_center()
1670 .rounded(px(8.))
1671 .cursor_pointer()
1672 .when(is_selected, |row| row.bg(theme::surface_raised()))
1673 .when(!is_selected, |row| {
1674 row.hover(|row| row.bg(theme::row_hover()))
1675 })
1676 .child(
1677 h_flex()
1678 .flex_1()
1679 .min_w_0()
1680 .gap(px(4.))
1681 .items_center()
1682 .child(
1683 div()
1684 .min_w_0()
1685 .truncate()
1686 .text_size(px(13.5))
1687 .font_weight(name_weight(name_style))
1688 .text_color(name_color(name_style))
1689 .child(channel.name.clone()),
1690 )
1691 .when(is_private_channel(channel), |name| {
1692 name.child(div().flex_none().child(symbol(
1693 "lock",
1694 14.,
1695 theme::text_muted(),
1696 )))
1697 }),
1698 )
1699 .children(notification_badge(notifications.level))
1700 .child(
1701 div()
1702 .flex_none()
1703 .text_size(px(11.))
1704 .text_color(theme::text_muted())
1705 .child(channel_activity(channel)),
1706 )
1707 .children(badge.map(mention_badge))
1708 .on_click(move |_, _, cx| {
1709 let selection = selection.clone();
1710 state.update(cx, |state, cx| state.select(selection, cx));
1711 })
1712 .context_menu(move |popup, window, cx| {
1713 channel_menu(
1714 popup,
1715 window,
1716 cx,
1717 menu_state.clone(),
1718 &menu_channel_id,
1719 notifications,
1720 )
1721 })
1722 }
1723
1724 fn collapse_all_teams(&mut self, team_ids: Vec<String>, cx: &mut Context<Self>) {
1725 self.collapsed_teams.extend(team_ids);
1726 save_collapsed_teams(&self.state.read(cx).store, &self.collapsed_teams);
1727 cx.notify();
1728 }
1729
1730 fn channels_body(&self, cx: &mut Context<Self>) -> AnyElement {
1731 let (sidebar, pinned_ids, selected, team_photos) = {
1732 let state = self.state.read(cx);
1733 let team_photos: HashMap<String, AvatarState> = state
1734 .sidebar
1735 .teams
1736 .iter()
1737 .filter_map(|entry| {
1738 let photo = state.directory.avatar(&team_avatar_key(&entry.team.id))?;
1739 Some((entry.team.id.clone(), photo.clone()))
1740 })
1741 .collect();
1742 (
1743 state.sidebar.clone(),
1744 state.directory.pinned_channels.clone(),
1745 state.selection.clone().filter(|_| !state.new_chat),
1746 team_photos,
1747 )
1748 };
1749 let filtering = self.channel_filter.is_active();
1750 let mut sidebar = sidebar;
1751 let pinned_ids = if filtering {
1752 sidebar.teams = self.channel_filter.apply(&sidebar.teams);
1753 Vec::new()
1754 } else {
1755 pinned_ids
1756 };
1757 let team_keys = sidebar
1758 .teams
1759 .iter()
1760 .map(|entry| team_avatar_key(&entry.team.id))
1761 .collect();
1762 let mut list = v_flex().w_full().pb(px(12.));
1763 let pinned: Vec<(&SidebarTeam, &ChannelRecord)> = pinned_ids
1764 .iter()
1765 .filter_map(|id| {
1766 sidebar.teams.iter().find_map(|entry| {
1767 entry
1768 .channels
1769 .iter()
1770 .find(|channel| &channel.id == id)
1771 .map(|channel| (entry, channel))
1772 })
1773 })
1774 .collect();
1775 if !pinned.is_empty() {
1776 list = list.child(self.plain_header(tr("sidebar.pinned")));
1777 for (entry, channel) in pinned {
1778 let is_selected = selected == Some(Selection::Channel(channel.id.clone()));
1779 list = list.child(self.channel_row(
1780 &channel.id,
1781 entry,
1782 channel,
1783 is_selected,
1784 team_photos.get(&entry.team.id),
1785 ));
1786 }
1787 }
1788 list = list.child(self.plain_header(tr("sidebar.teams")));
1789 let hidden_teams: Vec<&SidebarTeam> =
1790 sidebar.teams.iter().filter(|entry| entry.hidden).collect();
1791 for entry in sidebar
1792 .teams
1793 .iter()
1794 .filter(|entry| !entry.hidden || team_hoisted(entry))
1795 {
1796 let team_photo = team_photos.get(&entry.team.id);
1797 list = self.team_block(list, entry, entry.hidden, selected.as_ref(), team_photo, cx);
1798 }
1799 if !hidden_teams.is_empty() {
1800 let holds_selection = hidden_teams
1801 .iter()
1802 .any(|entry| team_holds(entry, selected.as_ref()));
1803 let open = self.hidden_teams_open || holds_selection;
1804 list = list.child(self.reveal_row(
1805 "hidden-teams".into(),
1806 format!("{} ({})", tr("sidebar.hidden_teams"), hidden_teams.len()),
1807 open,
1808 10.,
1809 cx.listener(|this, _, _, cx| {
1810 this.hidden_teams_open = !this.hidden_teams_open;
1811 cx.notify();
1812 }),
1813 ));
1814 if open {
1815 for entry in hidden_teams {
1816 let team_photo = team_photos.get(&entry.team.id);
1817 list = self.team_block(list, entry, false, selected.as_ref(), team_photo, cx);
1818 }
1819 }
1820 }
1821 self.request_lazy_data(team_keys, Vec::new(), cx);
1822 let scroll_area = div()
1823 .id("channels-scroll")
1824 .flex_1()
1825 .min_h_0()
1826 .overflow_y_scrollbar()
1827 .track_scroll(&self.channels_scroll)
1828 .child(list);
1829 with_top_edge(scroll_area, &self.channels_scroll, SIDEBAR_SCROLL_DIVIDER).into_any_element()
1830 }
1831}
1832
1833fn name_weight(style: NameStyle) -> FontWeight {
1834 match style.weight {
1835 NameWeight::Regular => FontWeight::NORMAL,
1836 NameWeight::Semibold => FontWeight::SEMIBOLD,
1837 NameWeight::Bold => FontWeight::BOLD,
1838 }
1839}
1840
1841fn name_color(style: NameStyle) -> Hsla {
1842 if style.highlighted {
1843 theme::text()
1844 } else {
1845 theme::text_muted()
1846 }
1847}
1848
1849/// Teams chat list: red Important / AlertUrgent glyph while the last unread message is marked.
1850fn importance_marker(importance: MessageImportance) -> Option<Icon> {
1851 let name = match importance {
1852 MessageImportance::Normal => return None,
1853 MessageImportance::High => "priority_high",
1854 MessageImportance::Urgent => "notification_important_fill",
1855 };
1856 Some(symbol(name, IMPORTANCE_MARKER_SIZE, theme::red()))
1857}
1858
1859fn mention_badge(badge: MentionBadge) -> AnyElement {
1860 match badge {
1861 MentionBadge::Count(count) => count_badge(count, false).into_any_element(),
1862 MentionBadge::Channel => {
1863 icon(IconName::Hash, MENTION_ICON_SIZE, theme::accent_text()).into_any_element()
1864 }
1865 MentionBadge::Team => {
1866 icon(IconName::Users, MENTION_ICON_SIZE, theme::accent_text()).into_any_element()
1867 }
1868 }
1869}
1870
1871fn notification_badge(level: ChannelNotificationLevel) -> Option<impl IntoElement> {
1872 let name = match level {
1873 ChannelNotificationLevel::BannerAndFeed => IconName::Bell,
1874 ChannelNotificationLevel::Off => IconName::BellOff,
1875 ChannelNotificationLevel::Feed => return None,
1876 };
1877 Some(icon(name, NOTIFICATION_ICON_SIZE, theme::text_muted()))
1878}
1879
1880pub(crate) fn channel_menu(
1881 popup: PopupMenu,
1882 window: &mut Window,
1883 cx: &mut Context<PopupMenu>,
1884 state: Entity<AppState>,
1885 channel_id: &str,
1886 notifications: ChannelNotifications,
1887) -> PopupMenu {
1888 let menu_state = state.read(cx).channel_menu_state(channel_id);
1889 let mut popup = popup;
1890 for entry in channel_menu_entries(menu_state) {
1891 let item = match entry {
1892 MenuEntry::Separator => {
1893 popup = popup.separator();
1894 continue;
1895 }
1896 MenuEntry::Item(ChannelMenuItem::Notifications) => {
1897 popup = notifications_submenu(popup, window, cx, &state, channel_id, notifications);
1898 continue;
1899 }
1900 MenuEntry::Item(item) => item,
1901 };
1902 let (action_state, action_id) = (state.clone(), channel_id.to_owned());
1903 popup = popup.item(
1904 PopupMenuItem::new(item.label())
1905 .icon(menu_icon(channel_menu_icon(item)))
1906 .disabled(menu_state.disables(item))
1907 .on_click(move |_, window, cx| {
1908 match item {
1909 ChannelMenuItem::EditChannel => {
1910 open_edit_channel_dialog(action_state.clone(), &action_id, window, cx);
1911 return;
1912 }
1913 ChannelMenuItem::DeleteChannel => {
1914 confirm_delete_channel(action_state.clone(), &action_id, window, cx);
1915 return;
1916 }
1917 _ => {}
1918 }
1919 action_state.update(cx, |state, cx| match item {
1920 ChannelMenuItem::Pin => state.set_channel_pinned(&action_id, true, cx),
1921 ChannelMenuItem::Unpin => state.set_channel_pinned(&action_id, false, cx),
1922 ChannelMenuItem::Show => state.set_channel_shown(&action_id, true, cx),
1923 ChannelMenuItem::Hide => state.set_channel_shown(&action_id, false, cx),
1924 ChannelMenuItem::ManageChannel => {
1925 state.manage_channel_in_browser(&action_id, cx)
1926 }
1927 ChannelMenuItem::CopyLink => state.copy_channel_link(&action_id, cx),
1928 ChannelMenuItem::Notifications
1929 | ChannelMenuItem::EditChannel
1930 | ChannelMenuItem::DeleteChannel => {}
1931 });
1932 }),
1933 );
1934 }
1935 popup
1936}
1937
1938fn channel_menu_icon(item: ChannelMenuItem) -> IconName {
1939 match item {
1940 ChannelMenuItem::Notifications => IconName::Bell,
1941 ChannelMenuItem::Pin => IconName::Pin,
1942 ChannelMenuItem::Unpin => IconName::PinOff,
1943 ChannelMenuItem::Show => IconName::Eye,
1944 ChannelMenuItem::Hide => IconName::EyeOff,
1945 ChannelMenuItem::EditChannel => IconName::Pencil,
1946 ChannelMenuItem::ManageChannel => IconName::Settings,
1947 ChannelMenuItem::CopyLink => IconName::Link,
1948 ChannelMenuItem::DeleteChannel => IconName::Trash,
1949 }
1950}
1951
1952fn team_menu(
1953 popup: PopupMenu,
1954 state: Entity<AppState>,
1955 team_id: &str,
1956 flags: TeamMenuState,
1957) -> PopupMenu {
1958 let mut popup = popup;
1959 for entry in team_menu_entries(flags) {
1960 let MenuEntry::Item(item) = entry else {
1961 popup = popup.separator();
1962 continue;
1963 };
1964 let (action_state, action_id) = (state.clone(), team_id.to_owned());
1965 popup = popup.item(
1966 PopupMenuItem::new(item.label())
1967 .icon(menu_icon(match item {
1968 TeamMenuItem::AddChannel => IconName::Plus,
1969 TeamMenuItem::Show => IconName::Eye,
1970 TeamMenuItem::Hide => IconName::EyeOff,
1971 TeamMenuItem::ManageTeam => IconName::Settings,
1972 TeamMenuItem::CopyLink => IconName::Link,
1973 }))
1974 .on_click(move |_, window, cx| {
1975 if item == TeamMenuItem::AddChannel {
1976 open_create_channel_dialog(action_state.clone(), &action_id, window, cx);
1977 return;
1978 }
1979 action_state.update(cx, |state, cx| match item {
1980 TeamMenuItem::AddChannel => {}
1981 TeamMenuItem::Show => state.set_team_shown(&action_id, true, cx),
1982 TeamMenuItem::Hide => state.set_team_shown(&action_id, false, cx),
1983 TeamMenuItem::ManageTeam => state.open_manage_team(&action_id, cx),
1984 TeamMenuItem::CopyLink => state.copy_team_link(&action_id, cx),
1985 });
1986 }),
1987 );
1988 }
1989 popup
1990}
1991
1992fn notifications_submenu(
1993 popup: PopupMenu,
1994 window: &mut Window,
1995 cx: &mut Context<PopupMenu>,
1996 state: &Entity<AppState>,
1997 channel_id: &str,
1998 notifications: ChannelNotifications,
1999) -> PopupMenu {
2000 let (state, channel_id) = (state.clone(), channel_id.to_owned());
2001 popup.submenu_with_icon(
2002 Some(menu_icon(IconName::Bell)),
2003 tr("sidebar.notifications"),
2004 window,
2005 cx,
2006 move |submenu, _, _| {
2007 let level_item = |label: String, level: ChannelNotificationLevel| {
2008 let (state, channel_id) = (state.clone(), channel_id.clone());
2009 PopupMenuItem::new(label)
2010 .checked(notifications.level == level)
2011 .on_click(move |_, _, cx| {
2012 let changed = ChannelNotifications {
2013 level,
2014 include_replies: notifications.include_replies
2015 && level != ChannelNotificationLevel::Off,
2016 };
2017 state.update(cx, |state, cx| {
2018 state.set_channel_notifications(&channel_id, changed, cx)
2019 });
2020 })
2021 };
2022 let (replies_state, replies_id) = (state.clone(), channel_id.clone());
2023 submenu
2024 .item(level_item(
2025 tr("sidebar.notify_banner_activity"),
2026 ChannelNotificationLevel::BannerAndFeed,
2027 ))
2028 .item(level_item(
2029 tr("sidebar.notify_activity_only"),
2030 ChannelNotificationLevel::Feed,
2031 ))
2032 .item(level_item(
2033 tr("sidebar.notify_off"),
2034 ChannelNotificationLevel::Off,
2035 ))
2036 .separator()
2037 .item(
2038 PopupMenuItem::new(tr("sidebar.include_thread_replies"))
2039 .checked(notifications.include_replies)
2040 .disabled(notifications.level == ChannelNotificationLevel::Off)
2041 .on_click(move |_, _, cx| {
2042 let changed = ChannelNotifications {
2043 level: notifications.level,
2044 include_replies: !notifications.include_replies,
2045 };
2046 replies_state.update(cx, |state, cx| {
2047 state.set_channel_notifications(&replies_id, changed, cx)
2048 });
2049 }),
2050 )
2051 },
2052 )
2053}
2054
2055fn team_expanded(team_id: &str, collapsed_teams: &HashSet<String>) -> bool {
2056 !collapsed_teams.contains(team_id)
2057}
2058
2059fn load_collapsed_teams(store: &Store) -> HashSet<String> {
2060 store
2061 .meta(COLLAPSED_TEAMS_META_KEY)
2062 .ok()
2063 .flatten()
2064 .map(|value| parse_collapsed_teams(&value))
2065 .unwrap_or_default()
2066}
2067
2068fn save_collapsed_teams(store: &Store, collapsed_teams: &HashSet<String>) {
2069 let _ = store.set_meta(
2070 COLLAPSED_TEAMS_META_KEY,
2071 &serialize_collapsed_teams(collapsed_teams),
2072 );
2073}
2074
2075fn serialize_collapsed_teams(collapsed_teams: &HashSet<String>) -> String {
2076 let mut team_ids: Vec<&String> = collapsed_teams.iter().collect();
2077 team_ids.sort_unstable();
2078 serde_json::to_string(&team_ids).unwrap_or_default()
2079}
2080
2081fn parse_collapsed_teams(value: &str) -> HashSet<String> {
2082 serde_json::from_str(value).unwrap_or_default()
2083}
2084
2085fn team_holds(entry: &SidebarTeam, selected: Option<&Selection>) -> bool {
2086 entry
2087 .channels
2088 .iter()
2089 .any(|channel| selected == Some(&Selection::Channel(channel.id.clone())))
2090}
2091
2092fn is_private_channel(channel: &ChannelRecord) -> bool {
2093 channel
2094 .membership_type
2095 .as_deref()
2096 .is_some_and(|membership| membership.eq_ignore_ascii_case(PRIVATE_MEMBERSHIP))
2097}
2098
2099fn channel_activity(channel: &ChannelRecord) -> String {
2100 channel
2101 .last_message_at
2102 .map(|time| {
2103 format::channel_time_label(
2104 time,
2105 Local::now().date_naive(),
2106 app_base::format::Zone::Local,
2107 )
2108 })
2109 .unwrap_or_default()
2110}
2111
2112fn collect_faces(spec: &AvatarSpec, faces: &mut Vec<String>) {
2113 match spec {
2114 AvatarSpec::Single(face) | AvatarSpec::Bot(face) => faces.extend(face.user_id.clone()),
2115 AvatarSpec::Pair(first, second) => {
2116 faces.extend(first.user_id.clone());
2117 faces.extend(second.user_id.clone());
2118 }
2119 AvatarSpec::Meeting | AvatarSpec::Muted => {}
2120 }
2121}
2122
2123fn with_spacer(list: Div, skipped_height: &mut f32) -> Div {
2124 let height = std::mem::take(skipped_height);
2125 if height > 0. {
2126 list.child(div().h(px(height)).flex_none())
2127 } else {
2128 list
2129 }
2130}
2131
2132impl Render for SidebarView {
2133 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
2134 let window_height = f32::from(window.viewport_size().height);
2135 let body = match self.tab {
2136 SidebarTab::Activity => self.activity_panel.clone().into_any_element(),
2137 SidebarTab::Chats => self.chats_body(window_height, cx),
2138 SidebarTab::Channels => self.channels_body(cx),
2139 SidebarTab::Agents => match &self.agents_panel {
2140 Some(panel) => panel.clone().into_any_element(),
2141 None => div().into_any_element(),
2142 },
2143 SidebarTab::Calls | SidebarTab::Calendar => div().into_any_element(),
2144 };
2145 let header = match self.tab {
2146 SidebarTab::Chats => Some(
2147 v_flex()
2148 .flex_none()
2149 .child(
2150 page_header(&self.tab.title())
2151 .child(self.chat_list_menu_button(cx))
2152 .child(self.filter_toggle_button(cx))
2153 .child(self.new_chat_button(cx)),
2154 )
2155 .child(self.filter_bar(cx)),
2156 ),
2157 SidebarTab::Channels => Some(
2158 v_flex()
2159 .flex_none()
2160 .child(
2161 page_header(&self.tab.title())
2162 .child(self.channel_list_menu_button(cx))
2163 .child(self.channel_filter_button(cx))
2164 .child(self.new_channel_items_button(cx)),
2165 )
2166 .child(self.channel_filter_bar(cx)),
2167 ),
2168 SidebarTab::Agents => Some(page_header(&self.tab.title())),
2169 SidebarTab::Activity | SidebarTab::Calls | SidebarTab::Calendar => None,
2170 };
2171 v_flex()
2172 .debug_selector(|| "shell/islands/sidebar".into())
2173 .w(px(SIDEBAR_WIDTH))
2174 .h_full()
2175 .flex_none()
2176 .bg(theme::surface())
2177 .rounded(px(10.))
2178 .overflow_hidden()
2179 .children(header)
2180 .child(body)
2181 }
2182}
2183
2184#[cfg(test)]
2185mod tests {
2186 use std::collections::HashSet;
2187
2188 use super::{drop_target, parse_collapsed_teams, serialize_collapsed_teams, team_expanded};
2189 use crate::data::PENDING_FOLDER_PREFIX;
2190 use crate::sidebar_model::{Section, SectionKind};
2191
2192 fn section(id: &str, kind: SectionKind) -> Section {
2193 Section {
2194 id: id.to_owned(),
2195 name: id.to_owned(),
2196 kind,
2197 collapsed: false,
2198 count: 0,
2199 unread_chats: 0,
2200 items: Vec::new(),
2201 hidden_count: 0,
2202 list_expanded: false,
2203 show_empty_hint: false,
2204 }
2205 }
2206
2207 #[test]
2208 fn pending_sections_take_no_dropped_chats() {
2209 let pending = format!("{PENDING_FOLDER_PREFIX}1");
2210 assert_eq!(drop_target(&section(&pending, SectionKind::Folder)), None);
2211 assert_eq!(
2212 drop_target(&section("work", SectionKind::Folder)),
2213 Some(Some("work".to_owned()))
2214 );
2215 assert_eq!(
2216 drop_target(&section("recent", SectionKind::Others)),
2217 Some(None)
2218 );
2219 }
2220
2221 #[test]
2222 fn teams_start_expanded() {
2223 assert!(team_expanded("t1", &HashSet::new()));
2224 }
2225
2226 #[test]
2227 fn collapsed_team_stays_closed() {
2228 let collapsed = HashSet::from(["t1".to_owned()]);
2229 assert!(!team_expanded("t1", &collapsed));
2230 assert!(team_expanded("t2", &collapsed));
2231 }
2232
2233 #[test]
2234 fn collapsed_teams_round_trip_as_json() {
2235 let collapsed = HashSet::from(["b".to_owned(), "a".to_owned()]);
2236 let json = serialize_collapsed_teams(&collapsed);
2237 assert_eq!(json, r#"["a","b"]"#);
2238 assert_eq!(parse_collapsed_teams(&json), collapsed);
2239 assert!(parse_collapsed_teams("garbage").is_empty());
2240 }
2241}