BlueGit

ict rusty-teams

Files

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

sidebar.rs

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