BlueGit

ict rusty-teams

Files

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

sidebar.rs

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