BlueGit

ict rusty-teams

Files

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

sidebar.rs

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