| 1 | use app_base::orgs::{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 | /// Orgs to offer in the account menu; a single org needs no switcher, like Teams. |
| 16 | pub fn switchable_orgs(&self) -> &[Org] { |
| 17 | if self.orgs.list.len() > 1 { |
| 18 | &self.orgs.list |
| 19 | } else { |
| 20 | &[] |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | /// Restarts the app on `org`: its own cache opens at once and the backend signs in to its tenant. |
| 25 | pub fn switch_org(&mut self, org: &Org, cx: &mut Context<Self>) { |
| 26 | if self.orgs.is_active(org) { |
| 27 | return; |
| 28 | } |
| 29 | let mut next = self.orgs.clone(); |
| 30 | next.activate(org); |
| 31 | if !self.save_orgs(&next) { |
| 32 | return; |
| 33 | } |
| 34 | self.orgs = next; |
| 35 | match std::env::current_exe().and_then(|exe| { |
| 36 | std::process::Command::new(exe) |
| 37 | .args(std::env::args_os().skip(1)) |
| 38 | .env(app_base::updater::RELAUNCH_ENV, std::process::id().to_string()) |
| 39 | .spawn() |
| 40 | }) { |
| 41 | Ok(_) => cx.quit(), |
| 42 | Err(error) => { |
| 43 | crate::diagnostics::record(format!("orgs: restart for the switch failed: {error}")) |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | fn save_orgs(&self, orgs: &Orgs) -> bool { |
| 49 | if self.mode.demo { |
| 50 | return false; |
| 51 | } |
| 52 | let saved = Orgs::path().map(|path| orgs.save(&path)); |
| 53 | if let Some(Err(error)) = &saved { |
| 54 | crate::diagnostics::record(format!("orgs: {} not saved: {error}", app_base::orgs::ORGS_FILE)); |
| 55 | } |
| 56 | matches!(saved, Some(Ok(()))) |
| 57 | } |
| 58 | } |