BlueGit

ict rusty-teams

Files

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

sidebar.rs

sidebar.rs 52.2 KiB Download Raw
1use std::collections::HashSet;
2use std::time::Duration;
3
4use chrono::{Local, Offset, Utc};
5use gpui_kit::assets::IconName;
6use gpui_kit::component::button::{Button, ButtonVariants as _};
7use gpui_kit::component::dialog::DialogFooter;
8use gpui_kit::component::menu::{ContextMenuExt as _, PopupMenu, PopupMenuItem};
9use gpui_kit::component::{WindowExt as _, h_flex, tooltip::Tooltip, v_flex};
10use gpui_kit::prelude::FluentBuilder as _;
11use gpui_kit::*;
12use store::{
13 ChannelNotificationLevel, ChannelNotifications, ChannelRecord, SidebarTeam, Store, TeamRecord,
14};
15
16use super::avatar::{spec_avatar, square_avatar, with_presence};
17use super::activity_panel::ActivityPanel;
18use super::calls_tab::calls_body;
19use super::saved_panel::SavedPanel;
20use super::widgets::{count_badge, dot, icon, page_header, unread_marker};
21use crate::app_state::{AppEvent, AppState, Selection};
22use crate::chat_actions::TITLE_LIMIT;
23use crate::data::{Directory, FolderKind};
24use crate::format;
25use crate::local_previews::LocalPreview;
26use crate::notice::truncated;
27use crate::sidebar_model::{
28 AvatarSpec, ChannelOrderInput, ChatItem, DELETED_PREVIEW, EMPTY_FOLDER_HINT, Preview, Section,
29 SectionInput, SectionKind, Step, build_sections, step_id,
30 visible_channel_ids,
31};
32use crate::theme;
33
34const COLLAPSED_TEAMS_META_KEY: &str = "sidebar_collapsed_teams";
35pub const SIDEBAR_WIDTH: f32 = 300.;
36const ROW_HEIGHT: f32 = 54.;
37const PINNED_CHANNEL_HEIGHT: f32 = 48.;
38const SECTION_HEIGHT: f32 = 30.;
39const SECTION_GAP: f32 = 4.;
40const HINT_HEIGHT: f32 = 26.;
41const SEE_MORE_HEIGHT: f32 = 28.;
42const FALLBACK_VIEWPORT: f32 = 900.;
43const CALLS_REFRESH: Duration = Duration::from_secs(30);
44const VIEWPORT_MARGIN: f32 = 300.;
45const AVATAR_SIZE: f32 = 36.;
46const PINNED_LABEL: &str = "Pinned";
47const TEAMS_LABEL: &str = "Teams";
48const HIDDEN_TEAMS_LABEL: &str = "Hidden teams";
49const TEAM_ROW_HEIGHT: f32 = 40.;
50const REVEAL_ROW_HEIGHT: f32 = 32.;
51const NEW_CHAT_BUTTON_SIZE: f32 = 34.;
52const MENU_ICON_SIZE: f32 = 15.;
53const MUTED_ICON_SIZE: f32 = 13.;
54const NOTIFICATION_ICON_SIZE: f32 = 13.;
55
56#[derive(Debug, Clone, Copy, PartialEq, Eq)]
57pub enum SidebarTab {
58 Activity,
59 Chats,
60 Channels,
61 Calls,
62 Saved,
63}
64
65impl SidebarTab {
66 fn title(self) -> &'static str {
67 match self {
68 SidebarTab::Activity => "Activity",
69 SidebarTab::Chats => "Chats",
70 SidebarTab::Channels => "Channels",
71 SidebarTab::Calls => "Calls",
72 SidebarTab::Saved => "Saved",
73 }
74 }
75}
76
77#[derive(Clone)]
78struct DraggedChat {
79 id: String,
80 title: String,
81}
82
83struct DragGhost {
84 title: String,
85}
86
87impl Render for DragGhost {
88 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
89 div()
90 .px(px(12.))
91 .py(px(6.))
92 .rounded(px(8.))
93 .bg(theme::surface_raised())
94 .border_1()
95 .border_color(theme::accent())
96 .text_size(px(13.))
97 .text_color(theme::text())
98 .child(self.title.clone())
99 }
100}
101
102struct ChatMenuTarget {
103 id: String,
104 title: String,
105 pinned: bool,
106 unread: bool,
107 muted: bool,
108 is_group: bool,
109 member_count: usize,
110}
111
112#[derive(Clone)]
113struct MenuContext {
114 state: Entity<AppState>,
115 folders: Vec<(String, String)>,
116 favorites_id: Option<String>,
117}
118
119fn menu_icon(name: IconName) -> gpui_kit::component::Icon {
120 icon(name, MENU_ICON_SIZE, theme::text_muted())
121}
122
123fn chat_menu(
124 popup: PopupMenu,
125 window: &mut Window,
126 cx: &mut Context<PopupMenu>,
127 context: &MenuContext,
128 target: &ChatMenuTarget,
129) -> PopupMenu {
130 let pinned = target.pinned;
131 let pin_state = context.state.clone();
132 let pin_id = target.id.clone();
133 let mut popup = popup.item(
134 PopupMenuItem::new(if pinned { "Unpin" } else { "Pin" })
135 .icon(menu_icon(if pinned {
136 IconName::PinOff
137 } else {
138 IconName::Pin
139 }))
140 .on_click(move |_, _, cx| {
141 pin_state.update(cx, |state, cx| {
142 if pinned {
143 state.move_chat(&pin_id, None, cx);
144 } else {
145 state.pin_chat(&pin_id, cx);
146 }
147 });
148 }),
149 );
150 if !context.folders.is_empty() {
151 let folders = context.folders.clone();
152 let move_state = context.state.clone();
153 let move_id = target.id.clone();
154 popup = popup.submenu_with_icon(
155 Some(menu_icon(IconName::Folder)),
156 "Move to folder",
157 window,
158 cx,
159 move |submenu, _, _| {
160 folders.iter().fold(submenu, |submenu, (folder_id, name)| {
161 let (state, chat_id, folder_id) =
162 (move_state.clone(), move_id.clone(), folder_id.clone());
163 submenu.item(PopupMenuItem::new(name.clone()).on_click(move |_, _, cx| {
164 state.update(cx, |state, cx| {
165 state.move_chat(&chat_id, Some(&folder_id), cx)
166 });
167 }))
168 })
169 },
170 );
171 }
172 let read_state = context.state.clone();
173 let read_id = target.id.clone();
174 let unread = target.unread;
175 let mute_state = context.state.clone();
176 let mute_id = target.id.clone();
177 let muted = target.muted;
178 let hide_state = context.state.clone();
179 let hide_id = target.id.clone();
180 popup = popup
181 .separator()
182 .item(
183 PopupMenuItem::new(if unread {
184 "Mark as read"
185 } else {
186 "Mark as unread"
187 })
188 .icon(menu_icon(if unread {
189 IconName::MailOpen
190 } else {
191 IconName::Mail
192 }))
193 .on_click(move |_, _, cx| {
194 read_state.update(cx, |state, cx| {
195 if unread {
196 state.mark_chat_read(&read_id, cx);
197 } else {
198 state.mark_chat_unread(&read_id, cx);
199 }
200 });
201 }),
202 )
203 .item(
204 PopupMenuItem::new(if muted { "Unmute" } else { "Mute" })
205 .icon(menu_icon(if muted {
206 IconName::Bell
207 } else {
208 IconName::BellOff
209 }))
210 .on_click(move |_, _, cx| {
211 mute_state.update(cx, |state, cx| state.set_chat_muted(&mute_id, !muted, cx));
212 }),
213 )
214 .separator()
215 .item(
216 PopupMenuItem::new("Hide")
217 .icon(menu_icon(IconName::EyeOff))
218 .on_click(move |_, _, cx| {
219 hide_state.update(cx, |state, cx| state.hide_chat(&hide_id, cx));
220 }),
221 );
222 if target.is_group {
223 let leave_state = context.state.clone();
224 let leave_id = target.id.clone();
225 let title = target.title.clone();
226 let member_count = target.member_count;
227 popup = popup.item(
228 PopupMenuItem::element(|_, _| {
229 div().text_color(theme::red_soft()).child("Leave chat...")
230 })
231 .icon(icon(IconName::LogOut, MENU_ICON_SIZE, theme::red_soft()))
232 .on_click(move |_, window, cx| {
233 confirm_leave(&leave_state, &leave_id, &title, member_count, window, cx);
234 }),
235 );
236 }
237 popup
238}
239
240fn leave_description(member_count: usize) -> String {
241 let who = match member_count {
242 0 | 1 => "The other members".to_owned(),
243 2 => "The other member".to_owned(),
244 count => format!("The {} other members", count - 1),
245 };
246 let verb = if member_count == 2 { "sees" } else { "see" };
247 format!("{who} {verb} that you left. You can only come back if someone adds you.")
248}
249
250fn confirm_leave(
251 state: &Entity<AppState>,
252 chat_id: &str,
253 title: &str,
254 member_count: usize,
255 window: &mut Window,
256 cx: &mut App,
257) {
258 let state = state.clone();
259 let chat_id = chat_id.to_owned();
260 let heading = format!("Leave \"{}\"?", truncated(title, TITLE_LIMIT));
261 let description = leave_description(member_count);
262 window.open_alert_dialog(cx, move |alert, _, _| {
263 let state = state.clone();
264 let chat_id = chat_id.clone();
265 let cancel = Button::new("leave-cancel")
266 .label("Cancel")
267 .on_click(|_, window, cx| window.close_dialog(cx));
268 let leave = Button::new("leave-confirm")
269 .label("Leave")
270 .danger()
271 .on_click(move |_, window, cx| {
272 state.update(cx, |state, cx| state.leave_chat(&chat_id, cx));
273 window.close_dialog(cx);
274 });
275 alert
276 .title(heading.clone())
277 .description(description.clone())
278 .footer(DialogFooter::new().child(cancel).child(leave))
279 .on_ok(|_, _, _| false)
280 });
281}
282
283pub struct SidebarView {
284 state: Entity<AppState>,
285 tab: SidebarTab,
286 activity_panel: Entity<ActivityPanel>,
287 saved_panel: Entity<SavedPanel>,
288 collapsed_teams: HashSet<String>,
289 revealed_channel_teams: HashSet<String>,
290 hidden_teams_open: bool,
291 chats_scroll: ScrollHandle,
292 channels_scroll: ScrollHandle,
293 calls_scroll: ScrollHandle,
294 _subscription: Subscription,
295 _state_observation: Subscription,
296}
297
298fn preview_text(preview: &Preview) -> Option<(String, bool)> {
299 match preview {
300 Preview::Empty => None,
301 Preview::Deleted => Some((DELETED_PREVIEW.to_owned(), true)),
302 Preview::Typing(text) => Some((text.clone(), true)),
303 Preview::Text {
304 prefix: Some(prefix),
305 text,
306 } => Some((format!("{prefix}: {text}"), false)),
307 Preview::Text { prefix: None, text } => Some((text.clone(), false)),
308 }
309}
310
311fn preview_icon(text: &str) -> Option<IconName> {
312 match text.rsplit(": ").next()? {
313 "Image" => Some(IconName::Image),
314 "File" => Some(IconName::File),
315 _ => None,
316 }
317}
318
319fn local_preview_row(preview: &LocalPreview, color: Hsla) -> Div {
320 let row = h_flex()
321 .flex_1()
322 .min_w_0()
323 .overflow_hidden()
324 .gap(px(4.))
325 .items_center()
326 .text_size(px(12.5))
327 .text_color(color);
328 match preview {
329 LocalPreview::Draft(text) => row
330 .child(
331 div()
332 .flex_none()
333 .text_color(theme::accent_text())
334 .child("Draft:"),
335 )
336 .child(div().truncate().child(text.clone())),
337 LocalPreview::NotSent => row.child(
338 div()
339 .flex_none()
340 .text_color(theme::red_soft())
341 .child("Not sent"),
342 ),
343 }
344}
345
346impl SidebarView {
347 pub fn new(
348 state: Entity<AppState>,
349 activity_panel: Entity<ActivityPanel>,
350 saved_panel: Entity<SavedPanel>,
351 cx: &mut Context<Self>,
352 ) -> Self {
353 let subscription = cx.subscribe(&state, |this, state, event: &AppEvent, cx| {
354 if matches!(event, AppEvent::Selection) {
355 if state.read(cx).new_chat {
356 this.tab = SidebarTab::Chats;
357 }
358 this.expand_team_of_selection(cx);
359 }
360 if matches!(
361 event,
362 AppEvent::Sidebar
363 | AppEvent::CallHistory
364 | AppEvent::Selection
365 | AppEvent::Directory
366 | AppEvent::Typing
367 | AppEvent::LocalPreviews
368 ) {
369 cx.notify();
370 }
371 });
372 let state_observation = cx.observe(&state, |_, _, cx| cx.notify());
373 let tab = if state.read(cx).start_on_channels {
374 SidebarTab::Channels
375 } else {
376 SidebarTab::Chats
377 };
378 let collapsed_teams = load_collapsed_teams(&state.read(cx).store);
379 cx.spawn(async move |this, cx| {
380 loop {
381 cx.background_executor().timer(CALLS_REFRESH).await;
382 let alive = this.update(cx, |view, cx| {
383 if view.tab == SidebarTab::Calls {
384 cx.notify();
385 }
386 });
387 if alive.is_err() {
388 break;
389 }
390 }
391 })
392 .detach();
393 SidebarView {
394 state,
395 tab,
396 activity_panel,
397 saved_panel,
398 collapsed_teams,
399 revealed_channel_teams: HashSet::new(),
400 hidden_teams_open: false,
401 chats_scroll: ScrollHandle::new(),
402 channels_scroll: ScrollHandle::new(),
403 calls_scroll: ScrollHandle::new(),
404 _subscription: subscription,
405 _state_observation: state_observation,
406 }
407 }
408
409 pub fn tab(&self) -> SidebarTab {
410 self.tab
411 }
412
413 pub fn show_activity(&mut self, cx: &mut Context<Self>) {
414 self.open_tab(SidebarTab::Activity, cx);
415 }
416
417 pub fn show_chats(&mut self, cx: &mut Context<Self>) {
418 self.open_tab(SidebarTab::Chats, cx);
419 }
420
421 pub fn show_channels(&mut self, cx: &mut Context<Self>) {
422 self.open_tab(SidebarTab::Channels, cx);
423 }
424
425 pub fn show_calls(&mut self, cx: &mut Context<Self>) {
426 self.open_tab(SidebarTab::Calls, cx);
427 }
428
429 pub fn show_saved(&mut self, cx: &mut Context<Self>) {
430 self.open_tab(SidebarTab::Saved, cx);
431 }
432
433 pub fn open_tab(&mut self, tab: SidebarTab, cx: &mut Context<Self>) {
434 self.tab = tab;
435 match tab {
436 SidebarTab::Calls => self.state.update(cx, |state, cx| {
437 state.refresh_call_history(cx);
438 state.refresh_calendar(cx);
439 }),
440 SidebarTab::Saved => self.state.update(cx, |state, cx| state.refresh_saved(cx)),
441 _ => {}
442 }
443 cx.notify();
444 }
445
446 pub fn select_adjacent(&mut self, step: Step, cx: &mut Context<Self>) {
447 let selection = match self.tab {
448 SidebarTab::Chats => self
449 .state
450 .read(cx)
451 .adjacent_chat_id(step)
452 .map(Selection::Chat),
453 SidebarTab::Channels => self.adjacent_channel_id(step, cx).map(Selection::Channel),
454 SidebarTab::Activity | SidebarTab::Calls | SidebarTab::Saved => None,
455 };
456 if let Some(selection) = selection {
457 self.state.update(cx, |state, cx| state.select(selection, cx));
458 }
459 }
460
461 fn adjacent_channel_id(&self, step: Step, cx: &mut Context<Self>) -> Option<String> {
462 let state = self.state.read(cx);
463 let selected_channel = match &state.selection {
464 Some(Selection::Channel(channel_id)) => Some(channel_id.as_str()),
465 _ => None,
466 };
467 let input = ChannelOrderInput {
468 teams: &state.sidebar.teams,
469 pinned_ids: &state.directory.pinned_channels,
470 collapsed_teams: &self.collapsed_teams,
471 revealed_channel_teams: &self.revealed_channel_teams,
472 hidden_teams_open: self.hidden_teams_open,
473 selected_channel,
474 };
475 step_id(&visible_channel_ids(&input), selected_channel, step)
476 }
477
478 fn expand_team_of_selection(&mut self, cx: &mut Context<Self>) {
479 let state = self.state.read(cx);
480 let Some(Selection::Channel(channel_id)) = state.selection.clone() else {
481 return;
482 };
483 let team_id = state
484 .sidebar
485 .teams
486 .iter()
487 .find(|entry| {
488 entry
489 .channels
490 .iter()
491 .any(|channel| channel.id == channel_id)
492 })
493 .map(|entry| entry.team.id.clone());
494 if let Some(team_id) = team_id
495 && self.collapsed_teams.remove(&team_id)
496 {
497 save_collapsed_teams(&self.state.read(cx).store, &self.collapsed_teams);
498 }
499 }
500
501 fn new_chat_button(&self, cx: &mut Context<Self>) -> impl IntoElement {
502 let active = self.state.read(cx).new_chat;
503 div()
504 .id("new-chat")
505 .size(px(NEW_CHAT_BUTTON_SIZE))
506 .flex_none()
507 .flex()
508 .items_center()
509 .justify_center()
510 .rounded(px(8.))
511 .bg(theme::surface_raised())
512 .cursor_pointer()
513 .when(active, |button| button.bg(theme::popup_hover()))
514 .hover(|button| button.bg(theme::popup_hover()))
515 .tooltip(|window, cx| Tooltip::new("New chat (Ctrl+N)").build(window, cx))
516 .child(icon(IconName::Pencil, 16., theme::text_soft()))
517 .on_click(cx.listener(|this, _, _, cx| {
518 this.state.update(cx, |state, cx| state.start_new_chat(cx));
519 }))
520 }
521
522 fn draft_row(&self) -> impl IntoElement {
523 h_flex()
524 .id("new-chat-draft")
525 .mx(px(6.))
526 .h(px(ROW_HEIGHT))
527 .px(px(10.))
528 .gap(px(10.))
529 .items_center()
530 .rounded(px(8.))
531 .bg(theme::surface_raised())
532 .child(
533 div()
534 .size(px(AVATAR_SIZE))
535 .flex_none()
536 .flex()
537 .items_center()
538 .justify_center()
539 .rounded_full()
540 .bg(theme::border_strong())
541 .child(icon(IconName::Pencil, 16., theme::text())),
542 )
543 .child(
544 v_flex()
545 .flex_1()
546 .min_w_0()
547 .gap(px(2.))
548 .child(
549 div()
550 .truncate()
551 .text_size(px(14.))
552 .text_color(theme::text())
553 .child("New chat"),
554 )
555 .child(
556 div()
557 .truncate()
558 .text_size(px(12.5))
559 .text_color(theme::text_muted())
560 .child("Draft"),
561 ),
562 )
563 }
564
565 fn section_header(&self, section: &Section, cx: &mut Context<Self>) -> impl IntoElement {
566 let drop_target = matches!(section.kind, SectionKind::Favorites | SectionKind::Folder)
567 .then(|| section.id.clone());
568 let toggle_id = section.id.clone();
569 h_flex()
570 .id(SharedString::from(format!("section-{}", section.id)))
571 .h(px(SECTION_HEIGHT))
572 .mt(px(SECTION_GAP))
573 .mx(px(4.))
574 .pl(px(8.))
575 .pr(px(10.))
576 .gap(px(6.))
577 .items_center()
578 .rounded(px(6.))
579 .cursor_pointer()
580 .hover(|header| header.bg(theme::row_hover()))
581 .drag_over::<DraggedChat>(|style, _, _, _| {
582 style
583 .bg(theme::drop_background())
584 .border_1()
585 .border_dashed()
586 .border_color(theme::accent())
587 })
588 .on_drop(cx.listener(move |this, dragged: &DraggedChat, _, cx| {
589 let target = drop_target.clone();
590 this.state.update(cx, |state, cx| {
591 state.move_chat(&dragged.id, target.as_deref(), cx)
592 });
593 }))
594 .child(icon(
595 if section.collapsed {
596 IconName::ChevronRight
597 } else {
598 IconName::ChevronDown
599 },
600 12.,
601 theme::text_muted(),
602 ))
603 .child(
604 div()
605 .flex_1()
606 .min_w_0()
607 .truncate()
608 .text_size(px(12.))
609 .font_weight(FontWeight::SEMIBOLD)
610 .text_color(theme::text_soft())
611 .child(section.name.clone()),
612 )
613 .when(section.collapsed && section.unread_chats > 0, |header| {
614 header.child(count_badge(section.unread_chats, false))
615 })
616 .child(
617 div()
618 .text_size(px(11.))
619 .text_color(theme::text_faint())
620 .child(section.count.to_string()),
621 )
622 .on_click(cx.listener(move |this, _, _, cx| {
623 let id = toggle_id.clone();
624 this.state
625 .update(cx, |state, cx| state.toggle_collapsed(&id, cx));
626 }))
627 }
628
629 fn see_more_row(&self, section: &Section, cx: &mut Context<Self>) -> impl IntoElement {
630 let toggle_id = section.id.clone();
631 let label = if section.hidden_count > 0 {
632 format!("See more ({})", section.hidden_count)
633 } else {
634 "See less".to_owned()
635 };
636 div()
637 .id(SharedString::from(format!("see-more-{}", section.id)))
638 .mx(px(6.))
639 .pl(px(56.))
640 .h(px(SEE_MORE_HEIGHT))
641 .flex()
642 .items_center()
643 .rounded(px(6.))
644 .cursor_pointer()
645 .text_size(px(12.5))
646 .text_color(theme::accent_text())
647 .hover(|row| row.bg(theme::row_hover()))
648 .child(label)
649 .on_click(cx.listener(move |this, _, _, cx| {
650 let id = toggle_id.clone();
651 this.state
652 .update(cx, |state, cx| state.toggle_list_expanded(&id, cx));
653 }))
654 }
655
656 fn chat_row(
657 &self,
658 item: &ChatItem,
659 selected: bool,
660 local_preview: Option<&LocalPreview>,
661 directory: &Directory,
662 menu: &MenuContext,
663 ) -> AnyElement {
664 let ring = if selected {
665 theme::surface_raised()
666 } else {
667 theme::surface()
668 };
669 let unread = item.unread.is_unread();
670 let presence = item
671 .presence_user
672 .as_deref()
673 .map(|user_id| directory.presence_of(user_id));
674 let avatar = spec_avatar(directory, &item.avatar, AVATAR_SIZE, ring);
675 let avatar = match presence {
676 Some(kind) => with_presence(avatar, kind, AVATAR_SIZE, ring).into_any_element(),
677 None => avatar,
678 };
679 let name = div()
680 .flex_1()
681 .min_w_0()
682 .truncate()
683 .text_size(px(14.))
684 .font_weight(if unread {
685 FontWeight::BOLD
686 } else {
687 FontWeight::NORMAL
688 })
689 .text_color(theme::text())
690 .child(item.title.clone());
691 let time = h_flex()
692 .flex_none()
693 .gap(px(4.))
694 .items_center()
695 .text_size(px(11.))
696 .text_color(if unread {
697 theme::accent_text()
698 } else {
699 theme::text_muted()
700 })
701 .when(item.muted, |time| {
702 time.child(icon(
703 IconName::BellOff,
704 MUTED_ICON_SIZE,
705 theme::text_faint(),
706 ))
707 })
708 .child(item.time_label.clone());
709 let preview_color = if matches!(item.preview, Preview::Typing(_)) {
710 theme::accent_text()
711 } else if item.muted {
712 theme::text_faint()
713 } else if unread {
714 theme::text_strong()
715 } else {
716 theme::text_muted()
717 };
718 let preview = match local_preview {
719 Some(local) => Some(local_preview_row(local, preview_color)),
720 None => preview_text(&item.preview).map(|(text, italic)| {
721 let glyph = preview_icon(&text);
722 h_flex()
723 .flex_1()
724 .min_w_0()
725 .overflow_hidden()
726 .gap(px(4.))
727 .items_center()
728 .text_size(px(12.5))
729 .when(italic, |preview| preview.italic())
730 .text_color(preview_color)
731 .children(glyph.map(|name| icon(name, 12., preview_color)))
732 .child(div().truncate().child(text))
733 }),
734 };
735 let lines = v_flex()
736 .flex_1()
737 .min_w_0()
738 .gap(px(2.))
739 .child(
740 h_flex()
741 .gap(px(8.))
742 .items_baseline()
743 .child(name)
744 .child(time),
745 )
746 .child(
747 h_flex()
748 .h(px(18.))
749 .gap(px(8.))
750 .items_center()
751 .child(preview.unwrap_or_else(|| div().flex_1()))
752 .children(unread_marker(item.unread, item.muted)),
753 );
754 let state = self.state.clone();
755 let chat_id = item.id.clone();
756 let dragged = DraggedChat {
757 id: item.id.clone(),
758 title: item.title.clone(),
759 };
760 let pinned = menu.favorites_id.is_some() && item.folder_id == menu.favorites_id;
761 let menu_context = menu.clone();
762 let target = ChatMenuTarget {
763 id: item.id.clone(),
764 title: item.title.clone(),
765 pinned,
766 unread,
767 muted: item.muted,
768 is_group: item.is_group,
769 member_count: item.member_count,
770 };
771 h_flex()
772 .id(SharedString::from(format!("chat-{}", item.id)))
773 .mx(px(6.))
774 .h(px(ROW_HEIGHT))
775 .px(px(10.))
776 .gap(px(10.))
777 .items_center()
778 .rounded(px(8.))
779 .cursor_pointer()
780 .when(selected, |row| row.bg(theme::surface_raised()))
781 .when(!selected, |row| row.hover(|row| row.bg(theme::row_hover())))
782 .on_drag(dragged, |dragged, _, _, cx| {
783 let title = dragged.title.clone();
784 cx.new(|_| DragGhost { title })
785 })
786 .child(avatar)
787 .child(lines)
788 .on_click(move |_, _, cx| {
789 let selection = Selection::Chat(chat_id.clone());
790 state.update(cx, |state, cx| state.select(selection, cx));
791 })
792 .context_menu(move |popup, window, cx| {
793 chat_menu(popup, window, cx, &menu_context, &target)
794 })
795 .into_any_element()
796 }
797
798 fn chats_body(&self, window_height: f32, cx: &mut Context<Self>) -> AnyElement {
799 let (sections, menu, local_previews) = {
800 let state = self.state.read(cx);
801 let now = Utc::now();
802 let offset = Local::now().offset().fix();
803 let input = SectionInput {
804 chats: &state.sidebar.chats,
805 directory: &state.directory,
806 collapsed: &state.collapsed,
807 expanded_lists: &state.expanded_lists,
808 typing: &state.typing,
809 now,
810 offset,
811 };
812 let sections = build_sections(&input);
813 let menu = MenuContext {
814 state: self.state.clone(),
815 folders: state
816 .directory
817 .folders
818 .iter()
819 .filter(|folder| folder.kind == FolderKind::UserCreated)
820 .map(|folder| (folder.id.clone(), folder.name.clone()))
821 .collect(),
822 favorites_id: state.directory.favorites().map(|folder| folder.id.clone()),
823 };
824 (sections, menu, state.local_previews.clone())
825 };
826
827 let top = -f32::from(self.chats_scroll.offset().y);
828 let measured: f32 = f32::from(self.chats_scroll.bounds().size.height);
829 let height = if measured > 1. {
830 measured
831 } else {
832 window_height.max(FALLBACK_VIEWPORT)
833 };
834 let (visible_from, visible_to) = (top - VIEWPORT_MARGIN, top + height + VIEWPORT_MARGIN);
835
836 let (selected, drafting) = {
837 let state = self.state.read(cx);
838 (
839 state.selection.clone().filter(|_| !state.new_chat),
840 state.new_chat,
841 )
842 };
843 let mut faces: Vec<String> = Vec::new();
844 let mut presence_users: Vec<String> = Vec::new();
845 let mut y = 0.;
846 let mut list = v_flex().w_full().pb(px(12.));
847 let mut skipped_height = 0.;
848 let directory_state = self.state.clone();
849 if drafting {
850 list = list.child(self.draft_row());
851 y += ROW_HEIGHT;
852 }
853 for section in &sections {
854 list = with_spacer(list, &mut skipped_height);
855 list = list.child(self.section_header(section, cx));
856 y += SECTION_GAP + SECTION_HEIGHT;
857 if section.collapsed {
858 continue;
859 }
860 for item in &section.items {
861 let on_screen = y + ROW_HEIGHT >= visible_from && y <= visible_to;
862 if on_screen {
863 collect_faces(&item.avatar, &mut faces);
864 presence_users.extend(item.presence_user.clone());
865 }
866 if on_screen {
867 list = with_spacer(list, &mut skipped_height);
868 let is_selected = selected == Some(Selection::Chat(item.id.clone()));
869 let directory = &directory_state.read(cx).directory;
870 let local_preview = local_previews.get(&item.id).filter(|_| !is_selected);
871 list = list.child(self.chat_row(
872 item,
873 is_selected,
874 local_preview,
875 directory,
876 &menu,
877 ));
878 } else {
879 skipped_height += ROW_HEIGHT;
880 }
881 y += ROW_HEIGHT;
882 }
883 if section.hidden_count > 0 || section.list_expanded {
884 list = with_spacer(list, &mut skipped_height);
885 list = list.child(self.see_more_row(section, cx));
886 y += SEE_MORE_HEIGHT;
887 }
888 if section.show_empty_hint {
889 list = with_spacer(list, &mut skipped_height);
890 list = list.child(
891 div()
892 .ml(px(44.))
893 .mr(px(20.))
894 .h(px(HINT_HEIGHT))
895 .flex()
896 .items_center()
897 .text_size(px(12.))
898 .italic()
899 .text_color(theme::text_faint())
900 .child(EMPTY_FOLDER_HINT),
901 );
902 y += HINT_HEIGHT;
903 }
904 }
905 list = with_spacer(list, &mut skipped_height);
906 self.request_lazy_data(faces, presence_users, cx);
907 div()
908 .id("chats-scroll")
909 .flex_1()
910 .min_h_0()
911 .overflow_y_scroll()
912 .track_scroll(&self.chats_scroll)
913 .child(list)
914 .into_any_element()
915 }
916
917 fn request_lazy_data(
918 &self,
919 faces: Vec<String>,
920 presence_users: Vec<String>,
921 cx: &mut Context<Self>,
922 ) {
923 if faces.is_empty() && presence_users.is_empty() {
924 return;
925 }
926 let state = self.state.clone();
927 cx.defer(move |cx| {
928 state.update(cx, |state, cx| {
929 state.request_avatars(faces, cx);
930 state.request_presence(presence_users, cx);
931 });
932 });
933 }
934
935 fn channel_row(
936 &self,
937 channel_id: &str,
938 team: &TeamRecord,
939 channel: &ChannelRecord,
940 selected: bool,
941 notifications: ChannelNotifications,
942 ) -> AnyElement {
943 let unread = channel.unread;
944 let state = self.state.clone();
945 let menu_state = self.state.clone();
946 let menu_channel_id = channel_id.to_owned();
947 let selection = Selection::Channel(channel_id.to_owned());
948 let now = Utc::now();
949 let offset = Local::now().offset().fix();
950 let time = channel
951 .last_message_at
952 .map(|time| {
953 format::list_time_label(time, now.with_timezone(&offset).date_naive(), offset)
954 })
955 .unwrap_or_default();
956 h_flex()
957 .id(SharedString::from(format!("pinned-{channel_id}")))
958 .mx(px(6.))
959 .h(px(PINNED_CHANNEL_HEIGHT))
960 .px(px(10.))
961 .gap(px(10.))
962 .items_center()
963 .rounded(px(8.))
964 .cursor_pointer()
965 .when(selected, |row| row.bg(theme::surface_raised()))
966 .when(!selected, |row| row.hover(|row| row.bg(theme::row_hover())))
967 .child(square_avatar(&team.name, &team.id, 28., 7.))
968 .child(
969 v_flex()
970 .flex_1()
971 .min_w_0()
972 .gap(px(1.))
973 .child(
974 h_flex()
975 .gap(px(8.))
976 .items_baseline()
977 .child(
978 div()
979 .flex_1()
980 .min_w_0()
981 .truncate()
982 .text_size(px(13.5))
983 .font_weight(if unread {
984 FontWeight::BOLD
985 } else {
986 FontWeight::NORMAL
987 })
988 .text_color(theme::text())
989 .child(format!("{} / {}", team.name, channel.name)),
990 )
991 .children(notification_badge(notifications.level))
992 .child(
993 div()
994 .flex_none()
995 .text_size(px(11.))
996 .text_color(if unread {
997 theme::accent_text()
998 } else {
999 theme::text_muted()
1000 })
1001 .child(time),
1002 ),
1003 )
1004 .child(
1005 h_flex()
1006 .gap(px(8.))
1007 .items_center()
1008 .child(
1009 div()
1010 .flex_1()
1011 .min_w_0()
1012 .truncate()
1013 .text_size(px(12.))
1014 .text_color(theme::text_muted())
1015 .child(team.name.clone()),
1016 )
1017 .when(unread, |line| line.child(dot(7.))),
1018 ),
1019 )
1020 .on_click(move |_, _, cx| {
1021 let selection = selection.clone();
1022 state.update(cx, |state, cx| state.select(selection, cx));
1023 })
1024 .context_menu(move |popup, window, cx| {
1025 channel_menu(
1026 popup,
1027 window,
1028 cx,
1029 menu_state.clone(),
1030 &menu_channel_id,
1031 notifications,
1032 )
1033 })
1034 .into_any_element()
1035 }
1036
1037 fn plain_header(&self, label: &str) -> impl IntoElement {
1038 div()
1039 .h(px(SECTION_HEIGHT))
1040 .mt(px(SECTION_GAP))
1041 .px(px(12.))
1042 .flex()
1043 .items_center()
1044 .text_size(px(12.))
1045 .font_weight(FontWeight::SEMIBOLD)
1046 .text_color(theme::text_soft())
1047 .child(label.to_owned())
1048 }
1049
1050 fn team_block(
1051 &self,
1052 mut list: Div,
1053 entry: &SidebarTeam,
1054 selected: Option<&Selection>,
1055 cx: &mut Context<Self>,
1056 ) -> Div {
1057 let team_id = entry.team.id.clone();
1058 let expanded = team_expanded(&team_id, &self.collapsed_teams);
1059 let unread = entry.channels.iter().any(|channel| channel.unread);
1060 let toggle_id = team_id.clone();
1061 list = list.child(
1062 h_flex()
1063 .id(SharedString::from(format!("team-{team_id}")))
1064 .mx(px(6.))
1065 .h(px(TEAM_ROW_HEIGHT))
1066 .px(px(10.))
1067 .gap(px(8.))
1068 .items_center()
1069 .rounded(px(8.))
1070 .cursor_pointer()
1071 .hover(|row| row.bg(theme::row_hover()))
1072 .child(icon(
1073 if expanded {
1074 IconName::ChevronDown
1075 } else {
1076 IconName::ChevronRight
1077 },
1078 14.,
1079 theme::text_muted(),
1080 ))
1081 .child(square_avatar(&entry.team.name, &entry.team.id, 28., 7.))
1082 .child(
1083 div()
1084 .flex_1()
1085 .min_w_0()
1086 .truncate()
1087 .text_size(px(13.5))
1088 .font_weight(if unread || expanded {
1089 FontWeight::SEMIBOLD
1090 } else {
1091 FontWeight::NORMAL
1092 })
1093 .text_color(theme::text())
1094 .child(entry.team.name.clone()),
1095 )
1096 .when(unread && !expanded, |row| row.child(dot(7.)))
1097 .on_click(cx.listener(move |this, _, _, cx| {
1098 if !this.collapsed_teams.remove(&toggle_id) {
1099 this.collapsed_teams.insert(toggle_id.clone());
1100 }
1101 save_collapsed_teams(&this.state.read(cx).store, &this.collapsed_teams);
1102 cx.notify();
1103 })),
1104 );
1105 if !expanded {
1106 return list;
1107 }
1108 let is_hidden = |channel: &ChannelRecord| entry.hidden_channel_ids.contains(&channel.id);
1109 let is_selected =
1110 |channel: &ChannelRecord| selected == Some(&Selection::Channel(channel.id.clone()));
1111 for channel in entry
1112 .channels
1113 .iter()
1114 .filter(|channel| !is_hidden(channel) || is_selected(channel))
1115 {
1116 list = list.child(self.team_channel_row(
1117 channel,
1118 is_selected(channel),
1119 entry.channel_notifications(&channel.id),
1120 ));
1121 }
1122 let hidden: Vec<&ChannelRecord> = entry
1123 .channels
1124 .iter()
1125 .filter(|channel| is_hidden(channel) && !is_selected(channel))
1126 .collect();
1127 if hidden.is_empty() {
1128 return list;
1129 }
1130 let revealed = self.revealed_channel_teams.contains(&team_id);
1131 let toggle_id = team_id.clone();
1132 list = list.child(self.reveal_row(
1133 SharedString::from(format!("hidden-channels-{team_id}")),
1134 hidden_channels_label(hidden.len()),
1135 revealed,
1136 40.,
1137 cx.listener(move |this, _, _, cx| {
1138 if !this.revealed_channel_teams.remove(&toggle_id) {
1139 this.revealed_channel_teams.insert(toggle_id.clone());
1140 }
1141 cx.notify();
1142 }),
1143 ));
1144 if revealed {
1145 for channel in hidden {
1146 list = list.child(self.team_channel_row(
1147 channel,
1148 false,
1149 entry.channel_notifications(&channel.id),
1150 ));
1151 }
1152 }
1153 list
1154 }
1155
1156 fn reveal_row(
1157 &self,
1158 id: SharedString,
1159 label: String,
1160 open: bool,
1161 indent: f32,
1162 on_click: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
1163 ) -> impl IntoElement {
1164 h_flex()
1165 .id(id)
1166 .mx(px(6.))
1167 .h(px(REVEAL_ROW_HEIGHT))
1168 .pl(px(indent))
1169 .pr(px(10.))
1170 .gap(px(8.))
1171 .items_center()
1172 .rounded(px(8.))
1173 .cursor_pointer()
1174 .hover(|row| row.bg(theme::row_hover()))
1175 .child(icon(
1176 if open {
1177 IconName::ChevronDown
1178 } else {
1179 IconName::ChevronRight
1180 },
1181 12.,
1182 theme::text_faint(),
1183 ))
1184 .child(
1185 div()
1186 .text_size(px(12.5))
1187 .text_color(theme::text_muted())
1188 .child(label),
1189 )
1190 .on_click(on_click)
1191 }
1192
1193 fn team_channel_row(
1194 &self,
1195 channel: &ChannelRecord,
1196 is_selected: bool,
1197 notifications: ChannelNotifications,
1198 ) -> impl IntoElement {
1199 let state = self.state.clone();
1200 let menu_state = self.state.clone();
1201 let menu_channel_id = channel.id.clone();
1202 let selection = Selection::Channel(channel.id.clone());
1203 h_flex()
1204 .id(SharedString::from(format!("channel-{}", channel.id)))
1205 .mx(px(6.))
1206 .h(px(TEAM_ROW_HEIGHT))
1207 .pl(px(40.))
1208 .pr(px(10.))
1209 .gap(px(8.))
1210 .items_center()
1211 .rounded(px(8.))
1212 .cursor_pointer()
1213 .when(is_selected, |row| row.bg(theme::surface_raised()))
1214 .when(!is_selected, |row| {
1215 row.hover(|row| row.bg(theme::row_hover()))
1216 })
1217 .child(
1218 div()
1219 .w(px(14.))
1220 .flex()
1221 .justify_center()
1222 .text_size(px(15.))
1223 .text_color(theme::text_faint())
1224 .child("#"),
1225 )
1226 .child(
1227 div()
1228 .flex_1()
1229 .min_w_0()
1230 .truncate()
1231 .text_size(px(13.5))
1232 .font_weight(if channel.unread {
1233 FontWeight::BOLD
1234 } else {
1235 FontWeight::NORMAL
1236 })
1237 .text_color(theme::text())
1238 .child(channel.name.clone()),
1239 )
1240 .children(notification_badge(notifications.level))
1241 .when(channel.unread, |row| row.child(dot(7.)))
1242 .on_click(move |_, _, cx| {
1243 let selection = selection.clone();
1244 state.update(cx, |state, cx| state.select(selection, cx));
1245 })
1246 .context_menu(move |popup, window, cx| {
1247 channel_menu(
1248 popup,
1249 window,
1250 cx,
1251 menu_state.clone(),
1252 &menu_channel_id,
1253 notifications,
1254 )
1255 })
1256 }
1257
1258 fn channels_body(&self, cx: &mut Context<Self>) -> AnyElement {
1259 let (sidebar, pinned_ids, selected) = {
1260 let state = self.state.read(cx);
1261 (
1262 state.sidebar.clone(),
1263 state.directory.pinned_channels.clone(),
1264 state.selection.clone().filter(|_| !state.new_chat),
1265 )
1266 };
1267 let mut list = v_flex().w_full().pb(px(12.));
1268 let pinned: Vec<(&SidebarTeam, &ChannelRecord)> = pinned_ids
1269 .iter()
1270 .filter_map(|id| {
1271 sidebar.teams.iter().find_map(|entry| {
1272 entry
1273 .channels
1274 .iter()
1275 .find(|channel| &channel.id == id)
1276 .map(|channel| (entry, channel))
1277 })
1278 })
1279 .collect();
1280 if !pinned.is_empty() {
1281 list = list.child(self.plain_header(PINNED_LABEL));
1282 for (entry, channel) in pinned {
1283 let is_selected = selected == Some(Selection::Channel(channel.id.clone()));
1284 list = list.child(self.channel_row(
1285 &channel.id,
1286 &entry.team,
1287 channel,
1288 is_selected,
1289 entry.channel_notifications(&channel.id),
1290 ));
1291 }
1292 }
1293 list = list.child(self.plain_header(TEAMS_LABEL));
1294 let (shown_teams, hidden_teams): (Vec<&SidebarTeam>, Vec<&SidebarTeam>) =
1295 sidebar.teams.iter().partition(|entry| !entry.hidden);
1296 for entry in shown_teams {
1297 list = self.team_block(list, entry, selected.as_ref(), cx);
1298 }
1299 if !hidden_teams.is_empty() {
1300 let holds_selection = hidden_teams
1301 .iter()
1302 .any(|entry| team_holds(entry, selected.as_ref()));
1303 let open = self.hidden_teams_open || holds_selection;
1304 list = list.child(self.reveal_row(
1305 "hidden-teams".into(),
1306 format!("{HIDDEN_TEAMS_LABEL} ({})", hidden_teams.len()),
1307 open,
1308 10.,
1309 cx.listener(|this, _, _, cx| {
1310 this.hidden_teams_open = !this.hidden_teams_open;
1311 cx.notify();
1312 }),
1313 ));
1314 if open {
1315 for entry in hidden_teams {
1316 list = self.team_block(list, entry, selected.as_ref(), cx);
1317 }
1318 }
1319 }
1320 div()
1321 .id("channels-scroll")
1322 .flex_1()
1323 .min_h_0()
1324 .overflow_y_scroll()
1325 .track_scroll(&self.channels_scroll)
1326 .child(list)
1327 .into_any_element()
1328 }
1329}
1330
1331fn notification_badge(level: ChannelNotificationLevel) -> Option<impl IntoElement> {
1332 let name = match level {
1333 ChannelNotificationLevel::BannerAndFeed => IconName::Bell,
1334 ChannelNotificationLevel::Off => IconName::BellOff,
1335 ChannelNotificationLevel::Feed => return None,
1336 };
1337 Some(icon(name, NOTIFICATION_ICON_SIZE, theme::text_muted()))
1338}
1339
1340fn channel_menu(
1341 popup: PopupMenu,
1342 window: &mut Window,
1343 cx: &mut Context<PopupMenu>,
1344 state: Entity<AppState>,
1345 channel_id: &str,
1346 notifications: ChannelNotifications,
1347) -> PopupMenu {
1348 let channel_id = channel_id.to_owned();
1349 popup.submenu_with_icon(
1350 Some(menu_icon(IconName::Bell)),
1351 "Notifications",
1352 window,
1353 cx,
1354 move |submenu, _, _| {
1355 let level_item = |label: &'static str, level: ChannelNotificationLevel| {
1356 let (state, channel_id) = (state.clone(), channel_id.clone());
1357 PopupMenuItem::new(label)
1358 .checked(notifications.level == level)
1359 .on_click(move |_, _, cx| {
1360 let changed = ChannelNotifications {
1361 level,
1362 include_replies: notifications.include_replies
1363 && level != ChannelNotificationLevel::Off,
1364 };
1365 state.update(cx, |state, cx| {
1366 state.set_channel_notifications(&channel_id, changed, cx)
1367 });
1368 })
1369 };
1370 let (replies_state, replies_id) = (state.clone(), channel_id.clone());
1371 submenu
1372 .item(level_item(
1373 "Banner and activity",
1374 ChannelNotificationLevel::BannerAndFeed,
1375 ))
1376 .item(level_item("Activity only", ChannelNotificationLevel::Feed))
1377 .item(level_item("Off", ChannelNotificationLevel::Off))
1378 .separator()
1379 .item(
1380 PopupMenuItem::new("Include thread replies")
1381 .checked(notifications.include_replies)
1382 .disabled(notifications.level == ChannelNotificationLevel::Off)
1383 .on_click(move |_, _, cx| {
1384 let changed = ChannelNotifications {
1385 level: notifications.level,
1386 include_replies: !notifications.include_replies,
1387 };
1388 replies_state.update(cx, |state, cx| {
1389 state.set_channel_notifications(&replies_id, changed, cx)
1390 });
1391 }),
1392 )
1393 },
1394 )
1395}
1396
1397fn team_expanded(team_id: &str, collapsed_teams: &HashSet<String>) -> bool {
1398 !collapsed_teams.contains(team_id)
1399}
1400
1401fn load_collapsed_teams(store: &Store) -> HashSet<String> {
1402 store
1403 .meta(COLLAPSED_TEAMS_META_KEY)
1404 .ok()
1405 .flatten()
1406 .map(|value| parse_collapsed_teams(&value))
1407 .unwrap_or_default()
1408}
1409
1410fn save_collapsed_teams(store: &Store, collapsed_teams: &HashSet<String>) {
1411 let _ = store.set_meta(
1412 COLLAPSED_TEAMS_META_KEY,
1413 &serialize_collapsed_teams(collapsed_teams),
1414 );
1415}
1416
1417fn serialize_collapsed_teams(collapsed_teams: &HashSet<String>) -> String {
1418 let mut team_ids: Vec<&String> = collapsed_teams.iter().collect();
1419 team_ids.sort_unstable();
1420 serde_json::to_string(&team_ids).unwrap_or_default()
1421}
1422
1423fn parse_collapsed_teams(value: &str) -> HashSet<String> {
1424 serde_json::from_str(value).unwrap_or_default()
1425}
1426
1427fn team_holds(entry: &SidebarTeam, selected: Option<&Selection>) -> bool {
1428 entry
1429 .channels
1430 .iter()
1431 .any(|channel| selected == Some(&Selection::Channel(channel.id.clone())))
1432}
1433
1434fn hidden_channels_label(count: usize) -> String {
1435 if count == 1 {
1436 "1 hidden channel".to_owned()
1437 } else {
1438 format!("{count} hidden channels")
1439 }
1440}
1441
1442fn collect_faces(spec: &AvatarSpec, faces: &mut Vec<String>) {
1443 match spec {
1444 AvatarSpec::Single(face) => faces.extend(face.user_id.clone()),
1445 AvatarSpec::Pair(first, second) => {
1446 faces.extend(first.user_id.clone());
1447 faces.extend(second.user_id.clone());
1448 }
1449 }
1450}
1451
1452fn with_spacer(list: Div, skipped_height: &mut f32) -> Div {
1453 let height = std::mem::take(skipped_height);
1454 if height > 0. {
1455 list.child(div().h(px(height)).flex_none())
1456 } else {
1457 list
1458 }
1459}
1460
1461impl Render for SidebarView {
1462 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
1463 let window_height = f32::from(window.viewport_size().height);
1464 let body = match self.tab {
1465 SidebarTab::Activity => self.activity_panel.clone().into_any_element(),
1466 SidebarTab::Chats => self.chats_body(window_height, cx),
1467 SidebarTab::Channels => self.channels_body(cx),
1468 SidebarTab::Calls => calls_body(&self.state, &self.calls_scroll, window_height, cx),
1469 SidebarTab::Saved => self.saved_panel.clone().into_any_element(),
1470 };
1471 let header = match self.tab {
1472 SidebarTab::Chats => Some(page_header(self.tab.title()).child(self.new_chat_button(cx))),
1473 SidebarTab::Channels | SidebarTab::Calls => Some(page_header(self.tab.title())),
1474 SidebarTab::Activity | SidebarTab::Saved => None,
1475 };
1476 v_flex()
1477 .w(px(SIDEBAR_WIDTH))
1478 .h_full()
1479 .flex_none()
1480 .bg(theme::surface())
1481 .rounded(px(10.))
1482 .overflow_hidden()
1483 .children(header)
1484 .child(body)
1485 }
1486}
1487
1488#[cfg(test)]
1489mod tests {
1490 use std::collections::HashSet;
1491
1492 use super::{
1493 leave_description, parse_collapsed_teams, serialize_collapsed_teams, team_expanded,
1494 };
1495
1496 #[test]
1497 fn teams_start_expanded() {
1498 assert!(team_expanded("t1", &HashSet::new()));
1499 }
1500
1501 #[test]
1502 fn collapsed_team_stays_closed() {
1503 let collapsed = HashSet::from(["t1".to_owned()]);
1504 assert!(!team_expanded("t1", &collapsed));
1505 assert!(team_expanded("t2", &collapsed));
1506 }
1507
1508 #[test]
1509 fn collapsed_teams_round_trip_as_json() {
1510 let collapsed = HashSet::from(["b".to_owned(), "a".to_owned()]);
1511 let json = serialize_collapsed_teams(&collapsed);
1512 assert_eq!(json, r#"["a","b"]"#);
1513 assert_eq!(parse_collapsed_teams(&json), collapsed);
1514 assert!(parse_collapsed_teams("garbage").is_empty());
1515 }
1516
1517 #[test]
1518 fn leave_text_counts_the_other_members() {
1519 assert_eq!(
1520 leave_description(4),
1521 "The 3 other members see that you left. You can only come back if someone adds you."
1522 );
1523 assert!(leave_description(2).starts_with("The other member sees"));
1524 assert!(leave_description(0).starts_with("The other members see"));
1525 }
1526}