BlueGit

ict rusty-teams

Files

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

sidebar.rs

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