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