BlueGit

ict rusty-teams

Files

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

sidebar.rs

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