BlueGit

ict rusty-teams

Files

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

sidebar.rs

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