BlueGit

ict rusty-teams

Files

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

sidebar.rs

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