| 1 | use app_base::orgs::{OPEN_CONVERSATION_FLAG, Org, Orgs}; |
| 2 | use gpui_kit::Context; |
| 3 | |
| 4 | use crate::app_state::AppState; |
| 5 | |
| 6 | impl AppState { |
| 7 | pub(crate) fn take_org_list(&mut self, list: Vec<Org>) { |
| 8 | if self.orgs.list == list { |
| 9 | return; |
| 10 | } |
| 11 | self.orgs.replace_list(list); |
| 12 | self.save_orgs(&self.orgs); |
| 13 | } |
| 14 | |
| 15 | /// A guest org has no calendar for the account, like in Teams. |
| 16 | pub fn calendar_available(&self) -> bool { |
| 17 | self.orgs.active_guest().is_none() |
| 18 | } |
| 19 | |
| 20 | /// Orgs to offer in the account menu; a single org needs no switcher, like Teams. |
| 21 | pub fn switchable_orgs(&self) -> &[Org] { |
| 22 | if self.orgs.list.len() > 1 { |
| 23 | &self.orgs.list |
| 24 | } else { |
| 25 | &[] |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | /// Chats with new messages in `org` since this window opened; zero for the open org. |
| 30 | pub fn org_unread_count(&self, org: &Org) -> usize { |
| 31 | self.org_unread.get(&org.id).map_or(0, |chats| chats.len()) |
| 32 | } |
| 33 | |
| 34 | /// Restarts the app on `org`: its own cache opens at once and the backend signs in to its tenant. |
| 35 | /// `open_conversation` opens that chat once the org has loaded. |
| 36 | pub fn switch_org( |
| 37 | &mut self, |
| 38 | org: &Org, |
| 39 | open_conversation: Option<&str>, |
| 40 | cx: &mut Context<Self>, |
| 41 | ) { |
| 42 | if self.orgs.is_active(org) { |
| 43 | return; |
| 44 | } |
| 45 | let mut next = self.orgs.clone(); |
| 46 | next.activate(org); |
| 47 | if !self.save_orgs(&next) { |
| 48 | return; |
| 49 | } |
| 50 | self.orgs = next; |
| 51 | match std::env::current_exe().and_then(|exe| { |
| 52 | std::process::Command::new(exe) |
| 53 | .args(restart_arguments( |
| 54 | std::env::args_os().skip(1), |
| 55 | open_conversation, |
| 56 | )) |
| 57 | .env( |
| 58 | app_base::updater::RELAUNCH_ENV, |
| 59 | std::process::id().to_string(), |
| 60 | ) |
| 61 | .spawn() |
| 62 | }) { |
| 63 | Ok(_) => cx.quit(), |
| 64 | Err(error) => { |
| 65 | crate::diagnostics::record(format!("orgs: restart for the switch failed: {error}")) |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | fn save_orgs(&self, orgs: &Orgs) -> bool { |
| 71 | if self.mode.demo { |
| 72 | return false; |
| 73 | } |
| 74 | let saved = Orgs::path().map(|path| orgs.save(&path)); |
| 75 | if let Some(Err(error)) = &saved { |
| 76 | crate::diagnostics::record(format!( |
| 77 | "orgs: {} not saved: {error}", |
| 78 | app_base::orgs::ORGS_FILE |
| 79 | )); |
| 80 | } |
| 81 | matches!(saved, Some(Ok(()))) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /// The process's own flags without an earlier open target, plus the chat to open now. |
| 86 | fn restart_arguments( |
| 87 | current: impl Iterator<Item = std::ffi::OsString>, |
| 88 | open_conversation: Option<&str>, |
| 89 | ) -> Vec<std::ffi::OsString> { |
| 90 | let mut arguments = Vec::new(); |
| 91 | let mut current = current; |
| 92 | while let Some(argument) = current.next() { |
| 93 | if argument == OPEN_CONVERSATION_FLAG { |
| 94 | current.next(); |
| 95 | continue; |
| 96 | } |
| 97 | arguments.push(argument); |
| 98 | } |
| 99 | if let Some(conversation_id) = open_conversation { |
| 100 | arguments.push(OPEN_CONVERSATION_FLAG.into()); |
| 101 | arguments.push(conversation_id.into()); |
| 102 | } |
| 103 | arguments |
| 104 | } |
| 105 | |
| 106 | #[cfg(test)] |
| 107 | mod tests { |
| 108 | use super::*; |
| 109 | |
| 110 | #[test] |
| 111 | fn a_restart_keeps_the_flags_and_replaces_the_open_target() { |
| 112 | let current = ["--endpoint", "x", OPEN_CONVERSATION_FLAG, "19:old"].map(Into::into); |
| 113 | assert_eq!( |
| 114 | restart_arguments(current.into_iter(), Some("19:new")), |
| 115 | ["--endpoint", "x", OPEN_CONVERSATION_FLAG, "19:new"].map(std::ffi::OsString::from) |
| 116 | ); |
| 117 | } |
| 118 | } |