From ad8162fc9cf0109b2ebca8420ffc4201e606195d Mon Sep 17 00:00:00 2001 From: Mikayla Maki Date: Thu, 20 Apr 2023 08:36:29 -0700 Subject: [PATCH] Make sign_in init conditional --- crates/copilot/src/sign_in.rs | 74 +++++++++++++++++------------------ 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/crates/copilot/src/sign_in.rs b/crates/copilot/src/sign_in.rs index dc09ddf3f2..3b949217bc 100644 --- a/crates/copilot/src/sign_in.rs +++ b/crates/copilot/src/sign_in.rs @@ -23,51 +23,51 @@ struct OpenGithub; const COPILOT_SIGN_UP_URL: &'static str = "https://github.com/features/copilot"; pub fn init(cx: &mut AppContext) { - let copilot = Copilot::global(cx).unwrap(); + if let Some(copilot) = Copilot::global(cx) { + let mut code_verification: Option> = None; + cx.observe(&copilot, move |copilot, cx| { + let status = copilot.read(cx).status(); - let mut code_verification: Option> = None; - cx.observe(&copilot, move |copilot, cx| { - let status = copilot.read(cx).status(); - - match &status { - crate::Status::SigningIn { prompt } => { - if let Some(code_verification_handle) = code_verification.as_mut() { - if cx.has_window(code_verification_handle.window_id()) { - code_verification_handle.update(cx, |code_verification_view, cx| { - code_verification_view.set_status(status, cx) - }); - cx.activate_window(code_verification_handle.window_id()); - } else { + match &status { + crate::Status::SigningIn { prompt } => { + if let Some(code_verification_handle) = code_verification.as_mut() { + if cx.has_window(code_verification_handle.window_id()) { + code_verification_handle.update(cx, |code_verification_view, cx| { + code_verification_view.set_status(status, cx) + }); + cx.activate_window(code_verification_handle.window_id()); + } else { + create_copilot_auth_window(cx, &status, &mut code_verification); + } + } else if let Some(_prompt) = prompt { create_copilot_auth_window(cx, &status, &mut code_verification); } - } else if let Some(_prompt) = prompt { - create_copilot_auth_window(cx, &status, &mut code_verification); } - } - Status::Authorized | Status::Unauthorized => { - if let Some(code_verification) = code_verification.as_ref() { - code_verification.update(cx, |code_verification, cx| { - code_verification.set_status(status, cx) - }); + Status::Authorized | Status::Unauthorized => { + if let Some(code_verification) = code_verification.as_ref() { + code_verification.update(cx, |code_verification, cx| { + code_verification.set_status(status, cx) + }); - cx.platform().activate(true); - cx.activate_window(code_verification.window_id()); + cx.platform().activate(true); + cx.activate_window(code_verification.window_id()); + } + } + _ => { + if let Some(code_verification) = code_verification.take() { + cx.remove_window(code_verification.window_id()); + } } } - _ => { - if let Some(code_verification) = code_verification.take() { - cx.remove_window(code_verification.window_id()); - } - } - } - }) - .detach(); + }) + .detach(); - cx.add_action( - |code_verification: &mut CopilotCodeVerification, _: &ClickedConnect, _| { - code_verification.connect_clicked = true; - }, - ); + cx.add_action( + |code_verification: &mut CopilotCodeVerification, _: &ClickedConnect, _| { + code_verification.connect_clicked = true; + }, + ); + } } fn create_copilot_auth_window(