BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-state / src / org_actions.rs

org_actions.rs

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