BlueGit

ict rusty-teams

Files

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

sidebar.rs

sidebar.rs 79.8 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 dragged = DraggedChat {
1080 id: item.id.clone(),
1081 title: item.title.clone(),
1082 };
1083 let menu_context = menu.clone();
1084 let reorder_folder = item
1085 .folder_id
1086 .clone()
1087 .filter(|folder_id| !is_pending_folder(folder_id));
1088 let target = ChatMenuTarget {
1089 id: item.id.clone(),
1090 title: item.title.clone(),
1091 folder_id: item.folder_id.clone(),
1092 unread,
1093 muted: item.muted,
1094 is_group: item.is_group,
1095 member_count: item.member_count,
1096 };
1097 let more_context = menu.clone();
1098 let more_target = target.clone();
1099 let more_button = div()
1100 .absolute()
1101 .top(px(ROW_MORE_TOP))
1102 .right(px(ROW_MORE_RIGHT))
1103 .invisible()
1104 .group_hover(row_group.clone(), |button| button.visible())
1105 .on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation())
1106 .child(
1107 Button::new(SharedString::from(format!("chat-more-{}", item.id)))
1108 .ghost()
1109 .xsmall()
1110 .icon(IconName::Ellipsis)
1111 .tooltip("More options")
1112 .dropdown_menu(move |popup, window, cx| {
1113 chat_menu(popup, window, cx, &more_context, &more_target)
1114 }),
1115 );
1116 h_flex()
1117 .id(SharedString::from(format!("chat-{}", item.id)))
1118 .debug_selector(|| chat_row_selector(&item.id))
1119 .group(row_group)
1120 .relative()
1121 .mx(px(6.))
1122 .h(px(ROW_HEIGHT))
1123 .flex_none()
1124 .px(px(10.))
1125 .gap(px(12.))
1126 .items_center()
1127 .rounded(px(8.))
1128 .cursor_pointer()
1129 .when(selected, |row| row.bg(theme::surface_raised()))
1130 .when(!selected, |row| row.hover(|row| row.bg(theme::row_hover())))
1131 .on_drag(dragged, |dragged, _, _, cx| {
1132 let title = dragged.title.clone();
1133 cx.new(|_| DragGhost { title })
1134 })
1135 .when_some(reorder_folder, |row, folder_id| {
1136 let (drop_state, target_id) = (self.state.clone(), item.id.clone());
1137 row.drag_over::<DraggedChat>(|style, _, _, _| style.bg(theme::drop_background()))
1138 .on_drop(move |dragged: &DraggedChat, _, cx| {
1139 drop_state.update(cx, |state, cx| {
1140 state.drop_chat_on(&dragged.id, &target_id, &folder_id, cx)
1141 });
1142 })
1143 })
1144 .child(avatar)
1145 .child(lines)
1146 .child(more_button)
1147 .on_click(move |_, _, cx| {
1148 let selection = Selection::Chat(chat_id.clone());
1149 state.update(cx, |state, cx| state.select(selection, cx));
1150 })
1151 .context_menu(move |popup, window, cx| {
1152 chat_menu(popup, window, cx, &menu_context, &target)
1153 })
1154 .into_any_element()
1155 }
1156
1157 fn chats_body(&self, window_height: f32, cx: &mut Context<Self>) -> AnyElement {
1158 let proposal_agents = crate::agent_api::pending_agents(cx);
1159 let (sections, section_menus, menu, local_previews) = {
1160 let state = self.state.read(cx);
1161 let now = Utc::now();
1162 let offset = Local::now().offset().fix();
1163 let input = SectionInput {
1164 chats: &state.sidebar.chats,
1165 directory: &state.directory,
1166 collapsed: &state.collapsed,
1167 expanded_lists: &state.expanded_lists,
1168 typing: &state.typing,
1169 now,
1170 offset,
1171 };
1172 let sections = if self.chat_filter.is_active() {
1173 let mentioned = self.mentioned_chats(cx);
1174 let selected_id = self.selected_chat_id(cx);
1175 filtered_sections(
1176 &input,
1177 &self.chat_filter,
1178 &mentioned,
1179 selected_id.as_deref(),
1180 )
1181 } else {
1182 build_sections(&input)
1183 };
1184 let menu = MenuContext::new(&self.state, cx);
1185 let section_menus =
1186 section_menu_targets(&sections, &state.directory, state.mode.read_only);
1187 *self.section_ids.borrow_mut() =
1188 sections.iter().map(|section| section.id.clone()).collect();
1189 (sections, section_menus, menu, state.local_previews.clone())
1190 };
1191
1192 let top = -f32::from(scroll_offset(&self.chats_scroll).y);
1193 let measured: f32 = f32::from(self.chats_scroll.bounds().size.height);
1194 let height = if measured > 1. {
1195 measured
1196 } else {
1197 window_height.max(FALLBACK_VIEWPORT)
1198 };
1199 let (visible_from, visible_to) = (top - VIEWPORT_MARGIN, top + height + VIEWPORT_MARGIN);
1200
1201 let (selected, drafting) = {
1202 let state = self.state.read(cx);
1203 (
1204 state
1205 .selection
1206 .clone()
1207 .filter(|_| !state.new_chat && state.quick_view.is_none()),
1208 state.new_chat,
1209 )
1210 };
1211 let mut faces: Vec<String> = Vec::new();
1212 let mut presence_users: Vec<String> = Vec::new();
1213 let mut y = 0.;
1214 let mut list = v_flex().w_full().pb(px(12.));
1215 let mut skipped_height = 0.;
1216 let mut others_end = None;
1217 let directory_state = self.state.clone();
1218 if drafting {
1219 list = list.child(self.draft_row());
1220 y += ROW_HEIGHT;
1221 }
1222 if !self.chat_filter.is_active() {
1223 let (quick_views, quick_height) = self.quick_views_section(cx);
1224 list = list.child(quick_views);
1225 y += quick_height;
1226 }
1227 for (section, section_menu) in sections.iter().zip(section_menus) {
1228 list = with_spacer(list, &mut skipped_height);
1229 list = list.child(self.section_header(section, section_menu, cx));
1230 y += SECTION_GAP + SECTION_HEIGHT;
1231 if section.collapsed {
1232 continue;
1233 }
1234 for item in &section.items {
1235 let on_screen = y + ROW_HEIGHT >= visible_from && y <= visible_to;
1236 if on_screen {
1237 collect_faces(&item.avatar, &mut faces);
1238 presence_users.extend(item.presence_user.clone());
1239 }
1240 if on_screen {
1241 list = with_spacer(list, &mut skipped_height);
1242 let is_selected = selected == Some(Selection::Chat(item.id.clone()));
1243 let directory = &directory_state.read(cx).directory;
1244 let local_preview = local_previews.get(&item.id).filter(|_| !is_selected);
1245 list = list.child(self.chat_row(
1246 item,
1247 is_selected,
1248 local_preview,
1249 proposal_agents.get(&item.id).copied(),
1250 directory,
1251 &menu,
1252 ));
1253 } else {
1254 skipped_height += ROW_HEIGHT;
1255 }
1256 y += ROW_HEIGHT;
1257 }
1258 if section.kind == SectionKind::Others {
1259 others_end = Some(y);
1260 }
1261 if section.hidden_count > 0 || section.list_expanded {
1262 list = with_spacer(list, &mut skipped_height);
1263 list = list.child(self.see_more_row(section, cx));
1264 y += SEE_MORE_HEIGHT;
1265 }
1266 if section.show_empty_hint {
1267 list = with_spacer(list, &mut skipped_height);
1268 list = list.child(
1269 div()
1270 .ml(px(16.))
1271 .mr(px(16.))
1272 .h(px(HINT_HEIGHT))
1273 .flex()
1274 .items_center()
1275 .text_size(px(12.))
1276 .italic()
1277 .text_color(theme::text_faint())
1278 .child(EMPTY_FOLDER_HINT),
1279 );
1280 y += HINT_HEIGHT;
1281 }
1282 }
1283 let (has_older, loading_older) = self.state.read(cx).older_chats();
1284 let previous = self.older_chats_seen.replace((has_older, loading_older));
1285 if older_chats_trigger_resets(previous, (has_older, loading_older)) {
1286 self.older_chats_trigger_top.set(None);
1287 }
1288 if has_older
1289 && wants_older_chats(
1290 others_end,
1291 top + height,
1292 top,
1293 self.older_chats_trigger_top.get(),
1294 loading_older,
1295 )
1296 {
1297 self.older_chats_trigger_top.set(Some(top));
1298 let state = self.state.clone();
1299 cx.defer(move |cx| state.read(cx).request_older_chats());
1300 }
1301 if loading_older {
1302 list = with_spacer(list, &mut skipped_height);
1303 list = list.child(
1304 div()
1305 .pl(px(16. + AVATAR_SIZE + 12.))
1306 .h(px(SEE_MORE_HEIGHT))
1307 .flex()
1308 .items_center()
1309 .text_size(px(12.5))
1310 .italic()
1311 .text_color(theme::text_faint())
1312 .child(LOADING_OLDER_LABEL),
1313 );
1314 }
1315 let selected_chat_id = self.selected_chat_id(cx);
1316 if self.chat_filter.is_active() && has_no_matches(&sections, selected_chat_id.as_deref()) {
1317 list = list.child(self.filter_empty_state(cx));
1318 }
1319 list = with_spacer(list, &mut skipped_height);
1320 self.request_lazy_data(faces, presence_users, cx);
1321 let scroll_area = div()
1322 .id("chats-scroll")
1323 .flex_1()
1324 .min_h_0()
1325 .overflow_y_scrollbar()
1326 .track_scroll(&self.chats_scroll)
1327 .child(list);
1328 with_top_edge(scroll_area, &self.chats_scroll, SIDEBAR_SCROLL_DIVIDER).into_any_element()
1329 }
1330
1331 fn request_lazy_data(
1332 &self,
1333 faces: Vec<String>,
1334 presence_users: Vec<String>,
1335 cx: &mut Context<Self>,
1336 ) {
1337 if faces.is_empty() && presence_users.is_empty() {
1338 return;
1339 }
1340 let state = self.state.clone();
1341 cx.defer(move |cx| {
1342 state.update(cx, |state, cx| {
1343 state.request_avatars(faces, cx);
1344 state.request_presence(presence_users, cx);
1345 });
1346 });
1347 }
1348
1349 fn channel_row(
1350 &self,
1351 channel_id: &str,
1352 entry: &SidebarTeam,
1353 channel: &ChannelRecord,
1354 selected: bool,
1355 team_photo: Option<&AvatarState>,
1356 ) -> AnyElement {
1357 let team = &entry.team;
1358 let unread = channel_shows_unread(entry, channel);
1359 let notifications = entry.channel_notifications(channel_id);
1360 let badge = channel_badge(entry, channel_id);
1361 let name_style = channel_name_style(unread, true, selected);
1362 let state = self.state.clone();
1363 let menu_state = self.state.clone();
1364 let menu_channel_id = channel_id.to_owned();
1365 let selection = Selection::Channel(channel_id.to_owned());
1366 let time = channel_activity(channel);
1367 h_flex()
1368 .id(SharedString::from(format!("pinned-{channel_id}")))
1369 .mx(px(6.))
1370 .h(px(PINNED_CHANNEL_HEIGHT))
1371 .px(px(10.))
1372 .gap(px(12.))
1373 .items_center()
1374 .rounded(px(8.))
1375 .cursor_pointer()
1376 .when(selected, |row| row.bg(theme::surface_raised()))
1377 .when(!selected, |row| row.hover(|row| row.bg(theme::row_hover())))
1378 .child(team_avatar(team_photo, &team.name, &team.id, 28., 7.))
1379 .child(
1380 v_flex()
1381 .flex_1()
1382 .min_w_0()
1383 .gap(px(1.))
1384 .child(
1385 h_flex()
1386 .gap(px(8.))
1387 .items_baseline()
1388 .child(
1389 div()
1390 .flex_1()
1391 .min_w_0()
1392 .truncate()
1393 .text_size(px(13.5))
1394 .font_weight(name_weight(name_style))
1395 .text_color(name_color(name_style))
1396 .child(format!("{} / {}", team.name, channel.name)),
1397 )
1398 .children(notification_badge(notifications.level))
1399 .child(
1400 div()
1401 .flex_none()
1402 .text_size(px(11.))
1403 .text_color(if unread {
1404 theme::accent_text()
1405 } else {
1406 theme::text_muted()
1407 })
1408 .child(time),
1409 ),
1410 )
1411 .child(
1412 h_flex()
1413 .gap(px(8.))
1414 .items_center()
1415 .child(
1416 div()
1417 .flex_1()
1418 .min_w_0()
1419 .truncate()
1420 .text_size(px(12.))
1421 .text_color(theme::text_muted())
1422 .child(team.name.clone()),
1423 )
1424 .children(badge.map(mention_badge)),
1425 ),
1426 )
1427 .on_click(move |_, _, cx| {
1428 let selection = selection.clone();
1429 state.update(cx, |state, cx| state.select(selection, cx));
1430 })
1431 .context_menu(move |popup, window, cx| {
1432 channel_menu(
1433 popup,
1434 window,
1435 cx,
1436 menu_state.clone(),
1437 &menu_channel_id,
1438 notifications,
1439 )
1440 })
1441 .into_any_element()
1442 }
1443
1444 fn plain_header(&self, label: &str) -> impl IntoElement {
1445 div()
1446 .h(px(SECTION_HEIGHT))
1447 .mt(px(SECTION_GAP))
1448 .px(px(16.))
1449 .flex()
1450 .items_center()
1451 .text_size(px(12.))
1452 .font_weight(FontWeight::SEMIBOLD)
1453 .text_color(theme::text_soft())
1454 .child(label.to_owned())
1455 }
1456
1457 fn team_block(
1458 &self,
1459 mut list: Div,
1460 entry: &SidebarTeam,
1461 hoisted_only: bool,
1462 selected: Option<&Selection>,
1463 team_photo: Option<&AvatarState>,
1464 cx: &mut Context<Self>,
1465 ) -> Div {
1466 let team_id = entry.team.id.clone();
1467 let expanded =
1468 self.channel_filter.is_active() || team_expanded(&team_id, &self.collapsed_teams);
1469 let name_style = team_name_style(entry, expanded);
1470 let badge = team_badge(entry, expanded);
1471 let toggle_id = team_id.clone();
1472 let (team_menu_state, team_menu_id) = (self.state.clone(), team_id.clone());
1473 let team_menu_flags = TeamMenuState {
1474 hidden: entry.hidden,
1475 can_add_channel: entry.rights.add_channel,
1476 };
1477 list = list.child(
1478 h_flex()
1479 .id(SharedString::from(format!("team-{team_id}")))
1480 .mx(px(6.))
1481 .h(px(TEAM_ROW_HEIGHT))
1482 .flex_none()
1483 .px(px(10.))
1484 .gap(px(8.))
1485 .items_center()
1486 .rounded(px(8.))
1487 .cursor_pointer()
1488 .hover(|row| row.bg(theme::row_hover()))
1489 .child(icon(
1490 if expanded {
1491 IconName::ChevronDown
1492 } else {
1493 IconName::ChevronRight
1494 },
1495 14.,
1496 theme::text_muted(),
1497 ))
1498 .child(team_avatar(
1499 team_photo,
1500 &entry.team.name,
1501 &entry.team.id,
1502 TEAM_ICON_SIZE,
1503 TEAM_ICON_RADIUS,
1504 ))
1505 .child(
1506 div()
1507 .flex_1()
1508 .min_w_0()
1509 .truncate()
1510 .text_size(px(13.5))
1511 .font_weight(name_weight(name_style))
1512 .text_color(name_color(name_style))
1513 .child(entry.team.name.clone()),
1514 )
1515 .children(badge.map(mention_badge))
1516 .on_click(cx.listener(move |this, _, _, cx| {
1517 if !this.collapsed_teams.remove(&toggle_id) {
1518 this.collapsed_teams.insert(toggle_id.clone());
1519 }
1520 save_collapsed_teams(&this.state.read(cx).store, &this.collapsed_teams);
1521 cx.notify();
1522 }))
1523 .context_menu(move |popup, _, _| {
1524 team_menu(popup, team_menu_state.clone(), &team_menu_id, team_menu_flags)
1525 }),
1526 );
1527 if !expanded {
1528 return list;
1529 }
1530 let is_selected =
1531 |channel: &ChannelRecord| selected == Some(&Selection::Channel(channel.id.clone()));
1532 let revealed = self.revealed_channel_teams.contains(&team_id);
1533 let rows = team_channel_rows(entry, hoisted_only, revealed, is_selected);
1534 for channel in &rows.shown {
1535 list = list.child(self.team_channel_row(
1536 channel,
1537 is_selected(channel),
1538 channel_name_style(
1539 channel_shows_unread(entry, channel),
1540 channel_shown(entry, &channel.id),
1541 is_selected(channel),
1542 ),
1543 entry.channel_notifications(&channel.id),
1544 channel_badge(entry, &channel.id),
1545 ));
1546 }
1547 if rows.hidden_count == 0 {
1548 return list;
1549 }
1550 let toggle_id = team_id.clone();
1551 list = list.child(self.see_all_channels_row(
1552 SharedString::from(format!("hidden-channels-{team_id}")),
1553 revealed,
1554 cx.listener(move |this, _, _, cx| {
1555 if !this.revealed_channel_teams.remove(&toggle_id) {
1556 this.revealed_channel_teams.insert(toggle_id.clone());
1557 }
1558 cx.notify();
1559 }),
1560 ));
1561 for channel in rows.revealed {
1562 list = list.child(self.team_channel_row(
1563 channel,
1564 false,
1565 channel_name_style(channel_shows_unread(entry, channel), false, false),
1566 entry.channel_notifications(&channel.id),
1567 channel_badge(entry, &channel.id),
1568 ));
1569 }
1570 list
1571 }
1572
1573 fn see_all_channels_row(
1574 &self,
1575 id: SharedString,
1576 open: bool,
1577 on_click: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
1578 ) -> impl IntoElement {
1579 h_flex()
1580 .id(id)
1581 .debug_selector(|| SEE_ALL_CHANNELS_TAG.into())
1582 .mx(px(6.))
1583 .h(px(CHANNEL_ROW_HEIGHT))
1584 .flex_none()
1585 .pl(px(CHANNEL_INDENT))
1586 .pr(px(10.))
1587 .items_center()
1588 .rounded(px(8.))
1589 .cursor_pointer()
1590 .hover(|row| row.bg(theme::row_hover()))
1591 .text_size(px(13.5))
1592 .text_color(theme::accent_text())
1593 .child(if open { SEE_FEWER_CHANNELS } else { SEE_ALL_CHANNELS })
1594 .on_click(on_click)
1595 }
1596
1597 fn reveal_row(
1598 &self,
1599 id: SharedString,
1600 label: String,
1601 open: bool,
1602 indent: f32,
1603 on_click: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
1604 ) -> impl IntoElement {
1605 h_flex()
1606 .id(id)
1607 .mx(px(6.))
1608 .h(px(REVEAL_ROW_HEIGHT))
1609 .flex_none()
1610 .pl(px(indent))
1611 .pr(px(10.))
1612 .gap(px(8.))
1613 .items_center()
1614 .rounded(px(8.))
1615 .cursor_pointer()
1616 .hover(|row| row.bg(theme::row_hover()))
1617 .child(icon(
1618 if open {
1619 IconName::ChevronDown
1620 } else {
1621 IconName::ChevronRight
1622 },
1623 12.,
1624 theme::text_faint(),
1625 ))
1626 .child(
1627 div()
1628 .text_size(px(12.5))
1629 .text_color(theme::text_muted())
1630 .child(label),
1631 )
1632 .on_click(on_click)
1633 }
1634
1635 fn team_channel_row(
1636 &self,
1637 channel: &ChannelRecord,
1638 is_selected: bool,
1639 name_style: NameStyle,
1640 notifications: ChannelNotifications,
1641 badge: Option<MentionBadge>,
1642 ) -> impl IntoElement {
1643 let state = self.state.clone();
1644 let menu_state = self.state.clone();
1645 let menu_channel_id = channel.id.clone();
1646 let selection = Selection::Channel(channel.id.clone());
1647 h_flex()
1648 .id(SharedString::from(format!("channel-{}", channel.id)))
1649 .debug_selector(|| CHANNEL_ROW_TAG.into())
1650 .mx(px(6.))
1651 .h(px(CHANNEL_ROW_HEIGHT))
1652 .flex_none()
1653 .pl(px(CHANNEL_INDENT))
1654 .pr(px(10.))
1655 .gap(px(8.))
1656 .items_center()
1657 .rounded(px(8.))
1658 .cursor_pointer()
1659 .when(is_selected, |row| row.bg(theme::surface_raised()))
1660 .when(!is_selected, |row| {
1661 row.hover(|row| row.bg(theme::row_hover()))
1662 })
1663 .child(
1664 h_flex()
1665 .flex_1()
1666 .min_w_0()
1667 .gap(px(4.))
1668 .items_center()
1669 .child(
1670 div()
1671 .min_w_0()
1672 .truncate()
1673 .text_size(px(13.5))
1674 .font_weight(name_weight(name_style))
1675 .text_color(name_color(name_style))
1676 .child(channel.name.clone()),
1677 )
1678 .when(is_private_channel(channel), |name| {
1679 name.child(
1680 div()
1681 .flex_none()
1682 .child(symbol("lock", 14., theme::text_muted())),
1683 )
1684 }),
1685 )
1686 .children(notification_badge(notifications.level))
1687 .child(
1688 div()
1689 .flex_none()
1690 .text_size(px(11.))
1691 .text_color(theme::text_muted())
1692 .child(channel_activity(channel)),
1693 )
1694 .children(badge.map(mention_badge))
1695 .on_click(move |_, _, cx| {
1696 let selection = selection.clone();
1697 state.update(cx, |state, cx| state.select(selection, cx));
1698 })
1699 .context_menu(move |popup, window, cx| {
1700 channel_menu(
1701 popup,
1702 window,
1703 cx,
1704 menu_state.clone(),
1705 &menu_channel_id,
1706 notifications,
1707 )
1708 })
1709 }
1710
1711 fn collapse_all_teams(&mut self, team_ids: Vec<String>, cx: &mut Context<Self>) {
1712 self.collapsed_teams.extend(team_ids);
1713 save_collapsed_teams(&self.state.read(cx).store, &self.collapsed_teams);
1714 cx.notify();
1715 }
1716
1717 fn channels_body(&self, cx: &mut Context<Self>) -> AnyElement {
1718 let (sidebar, pinned_ids, selected, team_photos) = {
1719 let state = self.state.read(cx);
1720 let team_photos: HashMap<String, AvatarState> = state
1721 .sidebar
1722 .teams
1723 .iter()
1724 .filter_map(|entry| {
1725 let photo = state.directory.avatar(&team_avatar_key(&entry.team.id))?;
1726 Some((entry.team.id.clone(), photo.clone()))
1727 })
1728 .collect();
1729 (
1730 state.sidebar.clone(),
1731 state.directory.pinned_channels.clone(),
1732 state.selection.clone().filter(|_| !state.new_chat),
1733 team_photos,
1734 )
1735 };
1736 let filtering = self.channel_filter.is_active();
1737 let mut sidebar = sidebar;
1738 let pinned_ids = if filtering {
1739 sidebar.teams = self.channel_filter.apply(&sidebar.teams);
1740 Vec::new()
1741 } else {
1742 pinned_ids
1743 };
1744 let team_keys = sidebar
1745 .teams
1746 .iter()
1747 .map(|entry| team_avatar_key(&entry.team.id))
1748 .collect();
1749 let mut list = v_flex().w_full().pb(px(12.));
1750 let pinned: Vec<(&SidebarTeam, &ChannelRecord)> = pinned_ids
1751 .iter()
1752 .filter_map(|id| {
1753 sidebar.teams.iter().find_map(|entry| {
1754 entry
1755 .channels
1756 .iter()
1757 .find(|channel| &channel.id == id)
1758 .map(|channel| (entry, channel))
1759 })
1760 })
1761 .collect();
1762 if !pinned.is_empty() {
1763 list = list.child(self.plain_header(PINNED_LABEL));
1764 for (entry, channel) in pinned {
1765 let is_selected = selected == Some(Selection::Channel(channel.id.clone()));
1766 list = list.child(self.channel_row(
1767 &channel.id,
1768 entry,
1769 channel,
1770 is_selected,
1771 team_photos.get(&entry.team.id),
1772 ));
1773 }
1774 }
1775 list = list.child(self.plain_header(TEAMS_LABEL));
1776 let hidden_teams: Vec<&SidebarTeam> =
1777 sidebar.teams.iter().filter(|entry| entry.hidden).collect();
1778 for entry in sidebar
1779 .teams
1780 .iter()
1781 .filter(|entry| !entry.hidden || team_hoisted(entry))
1782 {
1783 let team_photo = team_photos.get(&entry.team.id);
1784 list = self.team_block(list, entry, entry.hidden, selected.as_ref(), team_photo, cx);
1785 }
1786 if !hidden_teams.is_empty() {
1787 let holds_selection = hidden_teams
1788 .iter()
1789 .any(|entry| team_holds(entry, selected.as_ref()));
1790 let open = self.hidden_teams_open || holds_selection;
1791 list = list.child(self.reveal_row(
1792 "hidden-teams".into(),
1793 format!("{HIDDEN_TEAMS_LABEL} ({})", hidden_teams.len()),
1794 open,
1795 10.,
1796 cx.listener(|this, _, _, cx| {
1797 this.hidden_teams_open = !this.hidden_teams_open;
1798 cx.notify();
1799 }),
1800 ));
1801 if open {
1802 for entry in hidden_teams {
1803 let team_photo = team_photos.get(&entry.team.id);
1804 list = self.team_block(list, entry, false, selected.as_ref(), team_photo, cx);
1805 }
1806 }
1807 }
1808 self.request_lazy_data(team_keys, Vec::new(), cx);
1809 let scroll_area = div()
1810 .id("channels-scroll")
1811 .flex_1()
1812 .min_h_0()
1813 .overflow_y_scrollbar()
1814 .track_scroll(&self.channels_scroll)
1815 .child(list);
1816 with_top_edge(scroll_area, &self.channels_scroll, SIDEBAR_SCROLL_DIVIDER).into_any_element()
1817 }
1818}
1819
1820fn name_weight(style: NameStyle) -> FontWeight {
1821 match style.weight {
1822 NameWeight::Regular => FontWeight::NORMAL,
1823 NameWeight::Semibold => FontWeight::SEMIBOLD,
1824 NameWeight::Bold => FontWeight::BOLD,
1825 }
1826}
1827
1828fn name_color(style: NameStyle) -> Hsla {
1829 if style.highlighted {
1830 theme::text()
1831 } else {
1832 theme::text_muted()
1833 }
1834}
1835
1836/// Teams chat list: red Important / AlertUrgent glyph while the last unread message is marked.
1837fn importance_marker(importance: MessageImportance) -> Option<Icon> {
1838 let name = match importance {
1839 MessageImportance::Normal => return None,
1840 MessageImportance::High => "priority_high",
1841 MessageImportance::Urgent => "notification_important_fill",
1842 };
1843 Some(symbol(name, IMPORTANCE_MARKER_SIZE, theme::red()))
1844}
1845
1846fn mention_badge(badge: MentionBadge) -> AnyElement {
1847 match badge {
1848 MentionBadge::Count(count) => count_badge(count, false).into_any_element(),
1849 MentionBadge::Channel => {
1850 icon(IconName::Hash, MENTION_ICON_SIZE, theme::accent_text()).into_any_element()
1851 }
1852 MentionBadge::Team => {
1853 icon(IconName::Users, MENTION_ICON_SIZE, theme::accent_text()).into_any_element()
1854 }
1855 }
1856}
1857
1858fn notification_badge(level: ChannelNotificationLevel) -> Option<impl IntoElement> {
1859 let name = match level {
1860 ChannelNotificationLevel::BannerAndFeed => IconName::Bell,
1861 ChannelNotificationLevel::Off => IconName::BellOff,
1862 ChannelNotificationLevel::Feed => return None,
1863 };
1864 Some(icon(name, NOTIFICATION_ICON_SIZE, theme::text_muted()))
1865}
1866
1867pub(crate) fn channel_menu(
1868 popup: PopupMenu,
1869 window: &mut Window,
1870 cx: &mut Context<PopupMenu>,
1871 state: Entity<AppState>,
1872 channel_id: &str,
1873 notifications: ChannelNotifications,
1874) -> PopupMenu {
1875 let menu_state = state.read(cx).channel_menu_state(channel_id);
1876 let mut popup = popup;
1877 for entry in channel_menu_entries(menu_state) {
1878 let item = match entry {
1879 MenuEntry::Separator => {
1880 popup = popup.separator();
1881 continue;
1882 }
1883 MenuEntry::Item(ChannelMenuItem::Notifications) => {
1884 popup = notifications_submenu(popup, window, cx, &state, channel_id, notifications);
1885 continue;
1886 }
1887 MenuEntry::Item(item) => item,
1888 };
1889 let (action_state, action_id) = (state.clone(), channel_id.to_owned());
1890 popup = popup.item(
1891 PopupMenuItem::new(item.label())
1892 .icon(menu_icon(channel_menu_icon(item)))
1893 .disabled(menu_state.disables(item))
1894 .on_click(move |_, window, cx| {
1895 match item {
1896 ChannelMenuItem::EditChannel => {
1897 open_edit_channel_dialog(action_state.clone(), &action_id, window, cx);
1898 return;
1899 }
1900 ChannelMenuItem::DeleteChannel => {
1901 confirm_delete_channel(action_state.clone(), &action_id, window, cx);
1902 return;
1903 }
1904 _ => {}
1905 }
1906 action_state.update(cx, |state, cx| match item {
1907 ChannelMenuItem::Pin => state.set_channel_pinned(&action_id, true, cx),
1908 ChannelMenuItem::Unpin => state.set_channel_pinned(&action_id, false, cx),
1909 ChannelMenuItem::Show => state.set_channel_shown(&action_id, true, cx),
1910 ChannelMenuItem::Hide => state.set_channel_shown(&action_id, false, cx),
1911 ChannelMenuItem::ManageChannel => {
1912 state.manage_channel_in_browser(&action_id, cx)
1913 }
1914 ChannelMenuItem::CopyLink => state.copy_channel_link(&action_id, cx),
1915 ChannelMenuItem::Notifications
1916 | ChannelMenuItem::EditChannel
1917 | ChannelMenuItem::DeleteChannel => {}
1918 });
1919 }),
1920 );
1921 }
1922 popup
1923}
1924
1925fn channel_menu_icon(item: ChannelMenuItem) -> IconName {
1926 match item {
1927 ChannelMenuItem::Notifications => IconName::Bell,
1928 ChannelMenuItem::Pin => IconName::Pin,
1929 ChannelMenuItem::Unpin => IconName::PinOff,
1930 ChannelMenuItem::Show => IconName::Eye,
1931 ChannelMenuItem::Hide => IconName::EyeOff,
1932 ChannelMenuItem::EditChannel => IconName::Pencil,
1933 ChannelMenuItem::ManageChannel => IconName::Settings,
1934 ChannelMenuItem::CopyLink => IconName::Link,
1935 ChannelMenuItem::DeleteChannel => IconName::Trash,
1936 }
1937}
1938
1939fn team_menu(
1940 popup: PopupMenu,
1941 state: Entity<AppState>,
1942 team_id: &str,
1943 flags: TeamMenuState,
1944) -> PopupMenu {
1945 let mut popup = popup;
1946 for entry in team_menu_entries(flags) {
1947 let MenuEntry::Item(item) = entry else {
1948 popup = popup.separator();
1949 continue;
1950 };
1951 let (action_state, action_id) = (state.clone(), team_id.to_owned());
1952 popup = popup.item(
1953 PopupMenuItem::new(item.label())
1954 .icon(menu_icon(match item {
1955 TeamMenuItem::AddChannel => IconName::Plus,
1956 TeamMenuItem::Show => IconName::Eye,
1957 TeamMenuItem::Hide => IconName::EyeOff,
1958 TeamMenuItem::ManageTeam => IconName::Settings,
1959 TeamMenuItem::CopyLink => IconName::Link,
1960 }))
1961 .on_click(move |_, window, cx| {
1962 if item == TeamMenuItem::AddChannel {
1963 open_create_channel_dialog(action_state.clone(), &action_id, window, cx);
1964 return;
1965 }
1966 action_state.update(cx, |state, cx| match item {
1967 TeamMenuItem::AddChannel => {}
1968 TeamMenuItem::Show => state.set_team_shown(&action_id, true, cx),
1969 TeamMenuItem::Hide => state.set_team_shown(&action_id, false, cx),
1970 TeamMenuItem::ManageTeam => state.manage_team_in_browser(&action_id, cx),
1971 TeamMenuItem::CopyLink => state.copy_team_link(&action_id, cx),
1972 });
1973 }),
1974 );
1975 }
1976 popup
1977}
1978
1979fn notifications_submenu(
1980 popup: PopupMenu,
1981 window: &mut Window,
1982 cx: &mut Context<PopupMenu>,
1983 state: &Entity<AppState>,
1984 channel_id: &str,
1985 notifications: ChannelNotifications,
1986) -> PopupMenu {
1987 let (state, channel_id) = (state.clone(), channel_id.to_owned());
1988 popup.submenu_with_icon(
1989 Some(menu_icon(IconName::Bell)),
1990 "Notifications",
1991 window,
1992 cx,
1993 move |submenu, _, _| {
1994 let level_item = |label: &'static str, level: ChannelNotificationLevel| {
1995 let (state, channel_id) = (state.clone(), channel_id.clone());
1996 PopupMenuItem::new(label)
1997 .checked(notifications.level == level)
1998 .on_click(move |_, _, cx| {
1999 let changed = ChannelNotifications {
2000 level,
2001 include_replies: notifications.include_replies
2002 && level != ChannelNotificationLevel::Off,
2003 };
2004 state.update(cx, |state, cx| {
2005 state.set_channel_notifications(&channel_id, changed, cx)
2006 });
2007 })
2008 };
2009 let (replies_state, replies_id) = (state.clone(), channel_id.clone());
2010 submenu
2011 .item(level_item(
2012 "Banner and activity",
2013 ChannelNotificationLevel::BannerAndFeed,
2014 ))
2015 .item(level_item("Activity only", ChannelNotificationLevel::Feed))
2016 .item(level_item("Off", ChannelNotificationLevel::Off))
2017 .separator()
2018 .item(
2019 PopupMenuItem::new("Include thread replies")
2020 .checked(notifications.include_replies)
2021 .disabled(notifications.level == ChannelNotificationLevel::Off)
2022 .on_click(move |_, _, cx| {
2023 let changed = ChannelNotifications {
2024 level: notifications.level,
2025 include_replies: !notifications.include_replies,
2026 };
2027 replies_state.update(cx, |state, cx| {
2028 state.set_channel_notifications(&replies_id, changed, cx)
2029 });
2030 }),
2031 )
2032 },
2033 )
2034}
2035
2036fn team_expanded(team_id: &str, collapsed_teams: &HashSet<String>) -> bool {
2037 !collapsed_teams.contains(team_id)
2038}
2039
2040fn load_collapsed_teams(store: &Store) -> HashSet<String> {
2041 store
2042 .meta(COLLAPSED_TEAMS_META_KEY)
2043 .ok()
2044 .flatten()
2045 .map(|value| parse_collapsed_teams(&value))
2046 .unwrap_or_default()
2047}
2048
2049fn save_collapsed_teams(store: &Store, collapsed_teams: &HashSet<String>) {
2050 let _ = store.set_meta(
2051 COLLAPSED_TEAMS_META_KEY,
2052 &serialize_collapsed_teams(collapsed_teams),
2053 );
2054}
2055
2056fn serialize_collapsed_teams(collapsed_teams: &HashSet<String>) -> String {
2057 let mut team_ids: Vec<&String> = collapsed_teams.iter().collect();
2058 team_ids.sort_unstable();
2059 serde_json::to_string(&team_ids).unwrap_or_default()
2060}
2061
2062fn parse_collapsed_teams(value: &str) -> HashSet<String> {
2063 serde_json::from_str(value).unwrap_or_default()
2064}
2065
2066fn team_holds(entry: &SidebarTeam, selected: Option<&Selection>) -> bool {
2067 entry
2068 .channels
2069 .iter()
2070 .any(|channel| selected == Some(&Selection::Channel(channel.id.clone())))
2071}
2072
2073fn is_private_channel(channel: &ChannelRecord) -> bool {
2074 channel
2075 .membership_type
2076 .as_deref()
2077 .is_some_and(|membership| membership.eq_ignore_ascii_case(PRIVATE_MEMBERSHIP))
2078}
2079
2080fn channel_activity(channel: &ChannelRecord) -> String {
2081 let offset = Local::now().offset().fix();
2082 channel
2083 .last_message_at
2084 .map(|time| {
2085 format::channel_time_label(time, Utc::now().with_timezone(&offset).date_naive(), offset)
2086 })
2087 .unwrap_or_default()
2088}
2089
2090fn collect_faces(spec: &AvatarSpec, faces: &mut Vec<String>) {
2091 match spec {
2092 AvatarSpec::Single(face) | AvatarSpec::Bot(face) => faces.extend(face.user_id.clone()),
2093 AvatarSpec::Pair(first, second) => {
2094 faces.extend(first.user_id.clone());
2095 faces.extend(second.user_id.clone());
2096 }
2097 AvatarSpec::Meeting | AvatarSpec::Muted => {}
2098 }
2099}
2100
2101fn with_spacer(list: Div, skipped_height: &mut f32) -> Div {
2102 let height = std::mem::take(skipped_height);
2103 if height > 0. {
2104 list.child(div().h(px(height)).flex_none())
2105 } else {
2106 list
2107 }
2108}
2109
2110impl Render for SidebarView {
2111 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
2112 let window_height = f32::from(window.viewport_size().height);
2113 let body = match self.tab {
2114 SidebarTab::Activity => self.activity_panel.clone().into_any_element(),
2115 SidebarTab::Chats => self.chats_body(window_height, cx),
2116 SidebarTab::Channels => self.channels_body(cx),
2117 SidebarTab::Agents => match &self.agents_panel {
2118 Some(panel) => panel.clone().into_any_element(),
2119 None => div().into_any_element(),
2120 },
2121 SidebarTab::Calls | SidebarTab::Calendar => div().into_any_element(),
2122 };
2123 let header = match self.tab {
2124 SidebarTab::Chats => Some(
2125 v_flex()
2126 .flex_none()
2127 .child(
2128 page_header(self.tab.title())
2129 .child(self.chat_list_menu_button(cx))
2130 .child(self.filter_toggle_button(cx))
2131 .child(self.new_chat_button(cx)),
2132 )
2133 .child(self.filter_bar(cx)),
2134 ),
2135 SidebarTab::Channels => Some(
2136 v_flex()
2137 .flex_none()
2138 .child(
2139 page_header(self.tab.title())
2140 .child(self.channel_list_menu_button(cx))
2141 .child(self.channel_filter_button(cx))
2142 .child(self.new_channel_items_button(cx)),
2143 )
2144 .child(self.channel_filter_bar(cx)),
2145 ),
2146 SidebarTab::Agents => Some(page_header(self.tab.title())),
2147 SidebarTab::Activity | SidebarTab::Calls | SidebarTab::Calendar => None,
2148 };
2149 v_flex()
2150 .debug_selector(|| "shell/islands/sidebar".into())
2151 .w(px(SIDEBAR_WIDTH))
2152 .h_full()
2153 .flex_none()
2154 .bg(theme::surface())
2155 .rounded(px(10.))
2156 .overflow_hidden()
2157 .children(header)
2158 .child(body)
2159 }
2160}
2161
2162#[cfg(test)]
2163mod tests {
2164 use std::collections::HashSet;
2165
2166 use super::{
2167 drop_target, parse_collapsed_teams, serialize_collapsed_teams,
2168 team_expanded,
2169 };
2170 use crate::data::PENDING_FOLDER_PREFIX;
2171 use crate::sidebar_model::{Section, SectionKind};
2172
2173 fn section(id: &str, kind: SectionKind) -> Section {
2174 Section {
2175 id: id.to_owned(),
2176 name: id.to_owned(),
2177 kind,
2178 collapsed: false,
2179 count: 0,
2180 unread_chats: 0,
2181 items: Vec::new(),
2182 hidden_count: 0,
2183 list_expanded: false,
2184 show_empty_hint: false,
2185 }
2186 }
2187
2188 #[test]
2189 fn pending_sections_take_no_dropped_chats() {
2190 let pending = format!("{PENDING_FOLDER_PREFIX}1");
2191 assert_eq!(drop_target(&section(&pending, SectionKind::Folder)), None);
2192 assert_eq!(
2193 drop_target(&section("work", SectionKind::Folder)),
2194 Some(Some("work".to_owned()))
2195 );
2196 assert_eq!(
2197 drop_target(&section("recent", SectionKind::Others)),
2198 Some(None)
2199 );
2200 }
2201
2202 #[test]
2203 fn teams_start_expanded() {
2204 assert!(team_expanded("t1", &HashSet::new()));
2205 }
2206
2207 #[test]
2208 fn collapsed_team_stays_closed() {
2209 let collapsed = HashSet::from(["t1".to_owned()]);
2210 assert!(!team_expanded("t1", &collapsed));
2211 assert!(team_expanded("t2", &collapsed));
2212 }
2213
2214 #[test]
2215 fn collapsed_teams_round_trip_as_json() {
2216 let collapsed = HashSet::from(["b".to_owned(), "a".to_owned()]);
2217 let json = serialize_collapsed_teams(&collapsed);
2218 assert_eq!(json, r#"["a","b"]"#);
2219 assert_eq!(parse_collapsed_teams(&json), collapsed);
2220 assert!(parse_collapsed_teams("garbage").is_empty());
2221 }
2222}