BlueGit

ict rusty-teams

Files

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

sidebar.rs

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