BlueGit

ict rusty-teams

Files

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

sidebar.rs

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