| 1 | use std::future::Future; |
| 2 | |
| 3 | use gpui_kit::*; |
| 4 | |
| 5 | use crate::app_state::AppState; |
| 6 | use crate::sidebar_model::channel_shown; |
| 7 | |
| 8 | pub const COPIED_TO_CLIPBOARD: &str = "Copied to clipboard"; |
| 9 | pub const PINNED_CHANNEL_CANNOT_HIDE: &str = "Pinned channels can't be hidden."; |
| 10 | |
| 11 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 12 | pub enum ChannelMenuItem { |
| 13 | Notifications, |
| 14 | Pin, |
| 15 | Unpin, |
| 16 | Show, |
| 17 | Hide, |
| 18 | ManageChannel, |
| 19 | CopyLink, |
| 20 | } |
| 21 | |
| 22 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 23 | pub enum TeamMenuItem { |
| 24 | AddChannel, |
| 25 | Show, |
| 26 | Hide, |
| 27 | ManageTeam, |
| 28 | CopyLink, |
| 29 | } |
| 30 | |
| 31 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 32 | pub enum MenuEntry<Item> { |
| 33 | Item(Item), |
| 34 | Separator, |
| 35 | } |
| 36 | |
| 37 | impl ChannelMenuItem { |
| 38 | pub fn label(self) -> &'static str { |
| 39 | match self { |
| 40 | ChannelMenuItem::Notifications => "Notifications", |
| 41 | ChannelMenuItem::Pin => "Pin", |
| 42 | ChannelMenuItem::Unpin => "Unpin", |
| 43 | ChannelMenuItem::Show => "Show", |
| 44 | ChannelMenuItem::Hide => "Hide", |
| 45 | ChannelMenuItem::ManageChannel => "Manage channel", |
| 46 | ChannelMenuItem::CopyLink => "Copy link", |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | impl TeamMenuItem { |
| 52 | pub fn label(self) -> &'static str { |
| 53 | match self { |
| 54 | TeamMenuItem::AddChannel => "Add channel", |
| 55 | TeamMenuItem::Show => "Show", |
| 56 | TeamMenuItem::Hide => "Hide", |
| 57 | TeamMenuItem::ManageTeam => "Manage team", |
| 58 | TeamMenuItem::CopyLink => "Copy link", |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 64 | enum LinkUse { |
| 65 | Copy, |
| 66 | OpenInBrowser, |
| 67 | } |
| 68 | |
| 69 | impl LinkUse { |
| 70 | fn failure(self, reason: &str) -> String { |
| 71 | match self { |
| 72 | LinkUse::Copy => format!("Copy link failed: {reason}"), |
| 73 | LinkUse::OpenInBrowser => format!("Couldn't open Teams on the web: {reason}"), |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 79 | pub struct ChannelMenuState { |
| 80 | pub pinned: bool, |
| 81 | pub shown: bool, |
| 82 | } |
| 83 | |
| 84 | impl ChannelMenuState { |
| 85 | pub fn disables(self, item: ChannelMenuItem) -> bool { |
| 86 | item == ChannelMenuItem::Hide && self.pinned |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | pub fn channel_menu_entries(state: ChannelMenuState) -> Vec<MenuEntry<ChannelMenuItem>> { |
| 91 | vec![ |
| 92 | MenuEntry::Item(ChannelMenuItem::Notifications), |
| 93 | MenuEntry::Item(if state.pinned { |
| 94 | ChannelMenuItem::Unpin |
| 95 | } else { |
| 96 | ChannelMenuItem::Pin |
| 97 | }), |
| 98 | MenuEntry::Item(if state.shown { |
| 99 | ChannelMenuItem::Hide |
| 100 | } else { |
| 101 | ChannelMenuItem::Show |
| 102 | }), |
| 103 | MenuEntry::Separator, |
| 104 | MenuEntry::Item(ChannelMenuItem::ManageChannel), |
| 105 | MenuEntry::Item(ChannelMenuItem::CopyLink), |
| 106 | ] |
| 107 | } |
| 108 | |
| 109 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 110 | pub struct TeamMenuState { |
| 111 | pub hidden: bool, |
| 112 | pub can_add_channel: bool, |
| 113 | } |
| 114 | |
| 115 | pub fn team_menu_entries(state: TeamMenuState) -> Vec<MenuEntry<TeamMenuItem>> { |
| 116 | let mut entries = Vec::new(); |
| 117 | if state.can_add_channel { |
| 118 | entries.push(MenuEntry::Item(TeamMenuItem::AddChannel)); |
| 119 | } |
| 120 | entries.extend([ |
| 121 | MenuEntry::Item(if state.hidden { |
| 122 | TeamMenuItem::Show |
| 123 | } else { |
| 124 | TeamMenuItem::Hide |
| 125 | }), |
| 126 | MenuEntry::Separator, |
| 127 | MenuEntry::Item(TeamMenuItem::ManageTeam), |
| 128 | MenuEntry::Item(TeamMenuItem::CopyLink), |
| 129 | ]); |
| 130 | entries |
| 131 | } |
| 132 | |
| 133 | pub fn pin_failure(pinned: bool) -> &'static str { |
| 134 | if pinned { |
| 135 | "We couldn't pin the channel." |
| 136 | } else { |
| 137 | "We couldn't unpin the channel." |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | pub fn channel_visibility_failure(shown: bool) -> &'static str { |
| 142 | if shown { |
| 143 | "We couldn't show the channel." |
| 144 | } else { |
| 145 | "We couldn't hide the channel." |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | pub fn team_visibility_failure(shown: bool) -> &'static str { |
| 150 | if shown { |
| 151 | "We couldn't show the team." |
| 152 | } else { |
| 153 | "We couldn't hide the team." |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 158 | pub struct PostPinDialog { |
| 159 | pub title: &'static str, |
| 160 | pub body: &'static str, |
| 161 | pub confirm: &'static str, |
| 162 | } |
| 163 | |
| 164 | pub fn post_pin_dialog(pin: bool) -> PostPinDialog { |
| 165 | if pin { |
| 166 | PostPinDialog { |
| 167 | title: "Pin in channel", |
| 168 | body: "The message will be pinned for everyone who views the channel.", |
| 169 | confirm: "Pin", |
| 170 | } |
| 171 | } else { |
| 172 | PostPinDialog { |
| 173 | title: "Unpin this message?", |
| 174 | body: "The message will be unpinned for everyone who views the channel.", |
| 175 | confirm: "Unpin", |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | pub fn post_pin_notice(pin: bool) -> &'static str { |
| 181 | if pin { "Post pinned" } else { "Post unpinned" } |
| 182 | } |
| 183 | |
| 184 | pub fn post_pin_failure(pin: bool) -> &'static str { |
| 185 | if pin { |
| 186 | "We couldn't pin the post." |
| 187 | } else { |
| 188 | "We couldn't unpin the post." |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | impl AppState { |
| 193 | pub fn channel_menu_state(&self, channel_id: &str) -> ChannelMenuState { |
| 194 | ChannelMenuState { |
| 195 | pinned: self.channel_pinned(channel_id), |
| 196 | shown: self |
| 197 | .sidebar |
| 198 | .teams |
| 199 | .iter() |
| 200 | .find(|team| team.channels.iter().any(|channel| channel.id == channel_id)) |
| 201 | .is_none_or(|team| channel_shown(team, channel_id)), |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | pub fn channel_pinned(&self, channel_id: &str) -> bool { |
| 206 | self.directory |
| 207 | .pinned_channels |
| 208 | .iter() |
| 209 | .any(|id| id == channel_id) |
| 210 | } |
| 211 | |
| 212 | pub fn set_channel_pinned(&mut self, channel_id: &str, pinned: bool, cx: &mut Context<Self>) { |
| 213 | if self.channel_pinned(channel_id) == pinned { |
| 214 | return; |
| 215 | } |
| 216 | let Some(engine_or_demo) = self.chat_action_engine(cx) else { |
| 217 | return; |
| 218 | }; |
| 219 | let previous = self.directory.pinned_channels.clone(); |
| 220 | let mut changed = previous.clone(); |
| 221 | changed.retain(|id| id != channel_id); |
| 222 | if pinned { |
| 223 | changed.push(channel_id.to_owned()); |
| 224 | } |
| 225 | self.apply_pinned_channels(changed, cx); |
| 226 | let Some(engine) = engine_or_demo else { |
| 227 | return; |
| 228 | }; |
| 229 | let owned_id = channel_id.to_owned(); |
| 230 | self.run_channel_action( |
| 231 | pin_failure(pinned), |
| 232 | async move { engine.set_channel_pinned(&owned_id, pinned).await }, |
| 233 | move |state, cx| state.apply_pinned_channels(previous, cx), |
| 234 | None, |
| 235 | cx, |
| 236 | ); |
| 237 | } |
| 238 | |
| 239 | fn apply_pinned_channels(&mut self, channel_ids: Vec<String>, cx: &mut Context<Self>) { |
| 240 | let _ = self.store.replace_pinned_channels(&channel_ids); |
| 241 | self.directory.pinned_channels = channel_ids; |
| 242 | cx.emit(crate::app_state::AppEvent::Sidebar); |
| 243 | cx.notify(); |
| 244 | } |
| 245 | |
| 246 | pub fn set_channel_shown(&mut self, channel_id: &str, shown: bool, cx: &mut Context<Self>) { |
| 247 | if !shown && self.channel_pinned(channel_id) { |
| 248 | self.raise_notice(PINNED_CHANNEL_CANNOT_HIDE.to_owned(), None, cx); |
| 249 | return; |
| 250 | } |
| 251 | let Some(engine_or_demo) = self.chat_action_engine(cx) else { |
| 252 | return; |
| 253 | }; |
| 254 | let _ = self.store.set_channel_hidden(channel_id, !shown); |
| 255 | self.reload_sidebar(cx); |
| 256 | let Some(engine) = engine_or_demo else { |
| 257 | return; |
| 258 | }; |
| 259 | let owned_id = channel_id.to_owned(); |
| 260 | let revert_id = owned_id.clone(); |
| 261 | self.run_channel_action( |
| 262 | channel_visibility_failure(shown), |
| 263 | async move { engine.set_channel_shown(&owned_id, shown).await }, |
| 264 | move |state, cx| { |
| 265 | let _ = state.store.set_channel_hidden(&revert_id, shown); |
| 266 | state.reload_sidebar(cx); |
| 267 | }, |
| 268 | None, |
| 269 | cx, |
| 270 | ); |
| 271 | } |
| 272 | |
| 273 | pub fn set_team_shown(&mut self, team_id: &str, shown: bool, cx: &mut Context<Self>) { |
| 274 | let Some(engine_or_demo) = self.chat_action_engine(cx) else { |
| 275 | return; |
| 276 | }; |
| 277 | let _ = self.store.set_team_hidden(team_id, !shown); |
| 278 | self.reload_sidebar(cx); |
| 279 | let Some(engine) = engine_or_demo else { |
| 280 | return; |
| 281 | }; |
| 282 | let owned_id = team_id.to_owned(); |
| 283 | let revert_id = owned_id.clone(); |
| 284 | self.run_channel_action( |
| 285 | team_visibility_failure(shown), |
| 286 | async move { engine.set_team_shown(&owned_id, shown).await }, |
| 287 | move |state, cx| { |
| 288 | let _ = state.store.set_team_hidden(&revert_id, shown); |
| 289 | state.reload_sidebar(cx); |
| 290 | }, |
| 291 | None, |
| 292 | cx, |
| 293 | ); |
| 294 | } |
| 295 | |
| 296 | pub fn copy_channel_link(&mut self, channel_id: &str, cx: &mut Context<Self>) { |
| 297 | self.with_channel_link(channel_id, LinkUse::Copy, cx); |
| 298 | } |
| 299 | |
| 300 | pub fn manage_channel_in_browser(&mut self, channel_id: &str, cx: &mut Context<Self>) { |
| 301 | self.with_channel_link(channel_id, LinkUse::OpenInBrowser, cx); |
| 302 | } |
| 303 | |
| 304 | fn with_channel_link(&mut self, channel_id: &str, link_use: LinkUse, cx: &mut Context<Self>) { |
| 305 | let Some(engine) = self.engine.clone() else { |
| 306 | if self.mode.demo { |
| 307 | let link = teams_core::channel_link(&teams_core::ChannelAddress { |
| 308 | tenant_id: crate::demo::DEMO_TENANT_ID.to_owned(), |
| 309 | group_id: self.channel_team_id(channel_id).unwrap_or_default(), |
| 310 | channel_id: channel_id.to_owned(), |
| 311 | channel_name: self.channel_name(channel_id).unwrap_or_default(), |
| 312 | membership_type: None, |
| 313 | }); |
| 314 | self.use_link(link, link_use, cx); |
| 315 | } |
| 316 | return; |
| 317 | }; |
| 318 | let owned_id = channel_id.to_owned(); |
| 319 | self.link_from(async move { engine.channel_link(&owned_id).await }, link_use, cx); |
| 320 | } |
| 321 | |
| 322 | pub fn copy_team_link(&mut self, team_id: &str, cx: &mut Context<Self>) { |
| 323 | self.with_team_link(team_id, LinkUse::Copy, cx); |
| 324 | } |
| 325 | |
| 326 | pub fn manage_team_in_browser(&mut self, team_id: &str, cx: &mut Context<Self>) { |
| 327 | self.with_team_link(team_id, LinkUse::OpenInBrowser, cx); |
| 328 | } |
| 329 | |
| 330 | fn with_team_link(&mut self, team_id: &str, link_use: LinkUse, cx: &mut Context<Self>) { |
| 331 | let Some(engine) = self.engine.clone() else { |
| 332 | if self.mode.demo { |
| 333 | let team_thread_id = self.store.team_thread_id(team_id).ok().flatten(); |
| 334 | let link = teams_core::team_link( |
| 335 | team_thread_id.as_deref().unwrap_or(team_id), |
| 336 | team_id, |
| 337 | crate::demo::DEMO_TENANT_ID, |
| 338 | ); |
| 339 | self.use_link(link, link_use, cx); |
| 340 | } |
| 341 | return; |
| 342 | }; |
| 343 | let owned_id = team_id.to_owned(); |
| 344 | self.link_from(async move { engine.team_link(&owned_id).await }, link_use, cx); |
| 345 | } |
| 346 | |
| 347 | fn channel_team_id(&self, channel_id: &str) -> Option<String> { |
| 348 | self.sidebar.teams.iter().find_map(|team| { |
| 349 | team.channels |
| 350 | .iter() |
| 351 | .any(|channel| channel.id == channel_id) |
| 352 | .then(|| team.team.id.clone()) |
| 353 | }) |
| 354 | } |
| 355 | |
| 356 | pub fn channel_name(&self, channel_id: &str) -> Option<String> { |
| 357 | self.sidebar |
| 358 | .teams |
| 359 | .iter() |
| 360 | .flat_map(|team| &team.channels) |
| 361 | .find(|channel| channel.id == channel_id) |
| 362 | .map(|channel| channel.name.clone()) |
| 363 | } |
| 364 | |
| 365 | fn use_link(&mut self, link: String, link_use: LinkUse, cx: &mut Context<Self>) { |
| 366 | match link_use { |
| 367 | LinkUse::Copy => { |
| 368 | cx.write_to_clipboard(ClipboardItem::new_string(link)); |
| 369 | self.raise_notice(COPIED_TO_CLIPBOARD.to_owned(), None, cx); |
| 370 | } |
| 371 | LinkUse::OpenInBrowser => cx.open_url(&link), |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | fn link_from( |
| 376 | &mut self, |
| 377 | call: impl Future<Output = teams_core::Result<String>> + Send + 'static, |
| 378 | link_use: LinkUse, |
| 379 | cx: &mut Context<Self>, |
| 380 | ) { |
| 381 | let receiver = crate::runtime::spawn(call); |
| 382 | cx.spawn(async move |this, cx| { |
| 383 | let outcome = match receiver.await { |
| 384 | Ok(Ok(link)) => Ok(link), |
| 385 | Ok(Err(error)) => Err(crate::notice::short_error(&error)), |
| 386 | Err(_) => Err("cancelled".to_owned()), |
| 387 | }; |
| 388 | this.update(cx, |state, cx| match outcome { |
| 389 | Ok(link) => state.use_link(link, link_use, cx), |
| 390 | Err(reason) => state.raise_notice(link_use.failure(&reason), None, cx), |
| 391 | }) |
| 392 | .ok(); |
| 393 | }) |
| 394 | .detach(); |
| 395 | } |
| 396 | |
| 397 | pub(crate) fn run_channel_action( |
| 398 | &mut self, |
| 399 | failure: &'static str, |
| 400 | call: impl Future<Output = teams_core::Result<()>> + Send + 'static, |
| 401 | revert: impl FnOnce(&mut Self, &mut Context<Self>) + 'static, |
| 402 | success: Option<&'static str>, |
| 403 | cx: &mut Context<Self>, |
| 404 | ) { |
| 405 | let receiver = crate::runtime::spawn(call); |
| 406 | cx.spawn(async move |this, cx| { |
| 407 | let failed = !matches!(receiver.await, Ok(Ok(()))); |
| 408 | this.update(cx, |state, cx| { |
| 409 | if failed { |
| 410 | revert(state, cx); |
| 411 | state.raise_notice(failure.to_owned(), None, cx); |
| 412 | } else if let Some(text) = success { |
| 413 | state.raise_notice(text.to_owned(), None, cx); |
| 414 | } |
| 415 | }) |
| 416 | .ok(); |
| 417 | }) |
| 418 | .detach(); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | #[cfg(test)] |
| 423 | mod tests { |
| 424 | use super::{ |
| 425 | ChannelMenuItem, ChannelMenuState, MenuEntry, TeamMenuItem, TeamMenuState, |
| 426 | channel_menu_entries, channel_visibility_failure, pin_failure, post_pin_dialog, |
| 427 | post_pin_failure, post_pin_notice, team_menu_entries, team_visibility_failure, |
| 428 | }; |
| 429 | |
| 430 | fn labels<Item: Copy>( |
| 431 | entries: &[MenuEntry<Item>], |
| 432 | label: impl Fn(Item) -> &'static str, |
| 433 | ) -> Vec<&'static str> { |
| 434 | entries |
| 435 | .iter() |
| 436 | .map(|entry| match entry { |
| 437 | MenuEntry::Item(item) => label(*item), |
| 438 | MenuEntry::Separator => "-", |
| 439 | }) |
| 440 | .collect() |
| 441 | } |
| 442 | |
| 443 | #[test] |
| 444 | fn channel_menu_follows_teams_order() { |
| 445 | let shown = channel_menu_entries(ChannelMenuState { |
| 446 | pinned: false, |
| 447 | shown: true, |
| 448 | }); |
| 449 | assert_eq!( |
| 450 | labels(&shown, ChannelMenuItem::label), |
| 451 | ["Notifications", "Pin", "Hide", "-", "Manage channel", "Copy link"] |
| 452 | ); |
| 453 | let hidden_pinned = channel_menu_entries(ChannelMenuState { |
| 454 | pinned: true, |
| 455 | shown: false, |
| 456 | }); |
| 457 | assert_eq!( |
| 458 | labels(&hidden_pinned, ChannelMenuItem::label), |
| 459 | ["Notifications", "Unpin", "Show", "-", "Manage channel", "Copy link"] |
| 460 | ); |
| 461 | } |
| 462 | |
| 463 | #[test] |
| 464 | fn pinned_channels_cannot_be_hidden() { |
| 465 | let pinned = ChannelMenuState { |
| 466 | pinned: true, |
| 467 | shown: true, |
| 468 | }; |
| 469 | assert!(pinned.disables(ChannelMenuItem::Hide)); |
| 470 | assert!(!pinned.disables(ChannelMenuItem::Unpin)); |
| 471 | let unpinned = ChannelMenuState { |
| 472 | pinned: false, |
| 473 | shown: true, |
| 474 | }; |
| 475 | assert!(!unpinned.disables(ChannelMenuItem::Hide)); |
| 476 | } |
| 477 | |
| 478 | #[test] |
| 479 | fn team_menu_switches_between_show_and_hide() { |
| 480 | let shown = TeamMenuState::default(); |
| 481 | assert_eq!( |
| 482 | labels(&team_menu_entries(shown), TeamMenuItem::label), |
| 483 | ["Hide", "-", "Manage team", "Copy link"] |
| 484 | ); |
| 485 | let hidden = TeamMenuState { |
| 486 | hidden: true, |
| 487 | ..shown |
| 488 | }; |
| 489 | assert_eq!( |
| 490 | labels(&team_menu_entries(hidden), TeamMenuItem::label), |
| 491 | ["Show", "-", "Manage team", "Copy link"] |
| 492 | ); |
| 493 | } |
| 494 | |
| 495 | #[test] |
| 496 | fn add_channel_leads_the_team_menu_when_the_team_allows_it() { |
| 497 | let state = TeamMenuState { |
| 498 | hidden: false, |
| 499 | can_add_channel: true, |
| 500 | }; |
| 501 | assert_eq!( |
| 502 | labels(&team_menu_entries(state), TeamMenuItem::label), |
| 503 | ["Add channel", "Hide", "-", "Manage team", "Copy link"] |
| 504 | ); |
| 505 | } |
| 506 | |
| 507 | #[test] |
| 508 | fn post_pin_dialogs_and_notices_use_teams_strings() { |
| 509 | let unpin = post_pin_dialog(false); |
| 510 | assert_eq!(unpin.title, "Unpin this message?"); |
| 511 | assert_eq!( |
| 512 | unpin.body, |
| 513 | "The message will be unpinned for everyone who views the channel." |
| 514 | ); |
| 515 | assert_eq!(unpin.confirm, "Unpin"); |
| 516 | let pin = post_pin_dialog(true); |
| 517 | assert_eq!(pin.title, "Pin in channel"); |
| 518 | assert_eq!(pin.confirm, "Pin"); |
| 519 | assert_eq!(post_pin_notice(true), "Post pinned"); |
| 520 | assert_eq!(post_pin_notice(false), "Post unpinned"); |
| 521 | assert_eq!(post_pin_failure(true), "We couldn't pin the post."); |
| 522 | assert_eq!(post_pin_failure(false), "We couldn't unpin the post."); |
| 523 | } |
| 524 | |
| 525 | #[test] |
| 526 | fn failures_use_teams_strings() { |
| 527 | assert_eq!(pin_failure(true), "We couldn't pin the channel."); |
| 528 | assert_eq!(pin_failure(false), "We couldn't unpin the channel."); |
| 529 | assert_eq!( |
| 530 | channel_visibility_failure(false), |
| 531 | "We couldn't hide the channel." |
| 532 | ); |
| 533 | assert_eq!(team_visibility_failure(true), "We couldn't show the team."); |
| 534 | } |
| 535 | } |