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