BlueGit

ict rusty-teams

Files

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

sidebar.rs

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