BlueGit

ict rusty-teams

Files

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

sidebar.rs

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