| 1 | use gpui_kit::component::input::{Input, InputContentType, InputState}; |
| 2 | use gpui_kit::component::{h_flex, v_flex}; |
| 3 | use gpui_kit::*; |
| 4 | |
| 5 | use super::rows::{ROW_INSET, control_row, stacked_field, text_button}; |
| 6 | use crate::app_state::AppState; |
| 7 | use crate::i18n::tr; |
| 8 | use crate::theme; |
| 9 | |
| 10 | pub struct GoogleClientForm { |
| 11 | app: Entity<AppState>, |
| 12 | client_id: Entity<InputState>, |
| 13 | secret: Entity<InputState>, |
| 14 | saved_id: String, |
| 15 | secret_present: bool, |
| 16 | submits: u64, |
| 17 | saving: bool, |
| 18 | } |
| 19 | |
| 20 | impl GoogleClientForm { |
| 21 | pub fn new(app: Entity<AppState>, window: &mut Window, cx: &mut Context<Self>) -> Self { |
| 22 | let saved_id = crate::google_oauth_client::load_client_id(&app.read(cx).store); |
| 23 | let client_id = |
| 24 | cx.new(|cx| InputState::new(window, cx).placeholder("….apps.googleusercontent.com")); |
| 25 | if !saved_id.is_empty() { |
| 26 | let text = saved_id.clone(); |
| 27 | client_id.update(cx, |input, cx| input.set_value(text, window, cx)); |
| 28 | } |
| 29 | let secret = cx.new(|cx| InputState::new(window, cx)); |
| 30 | secret.update(cx, |input, cx| input.toggle_masked(window, cx)); |
| 31 | if !saved_id.is_empty() { |
| 32 | cx.spawn_in(window, async move |form, cx| { |
| 33 | let present = |
| 34 | crate::runtime::spawn(crate::google_oauth_client::secret_is_stored()).await; |
| 35 | if !matches!(present, Ok(true)) { |
| 36 | return; |
| 37 | } |
| 38 | form.update_in(cx, |form, window, cx| { |
| 39 | if form.submits > 0 { |
| 40 | return; |
| 41 | } |
| 42 | form.secret_present = true; |
| 43 | form.secret.update(cx, |input, cx| { |
| 44 | input.set_placeholder( |
| 45 | tr("settings.calendar.google_client_secret_kept"), |
| 46 | window, |
| 47 | cx, |
| 48 | ); |
| 49 | }); |
| 50 | cx.notify(); |
| 51 | }) |
| 52 | .ok(); |
| 53 | }) |
| 54 | .detach(); |
| 55 | } |
| 56 | GoogleClientForm { |
| 57 | app, |
| 58 | client_id, |
| 59 | secret, |
| 60 | saved_id, |
| 61 | secret_present: false, |
| 62 | submits: 0, |
| 63 | saving: false, |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | fn status_detail(&self) -> String { |
| 68 | if !self.saved_id.is_empty() && self.secret_present { |
| 69 | tr("settings.calendar.google_client_status_own") |
| 70 | } else if chatsvc::client_id_from_env().is_some() { |
| 71 | tr("settings.calendar.google_client_status_builtin") |
| 72 | } else { |
| 73 | tr("settings.calendar.google_client_status_missing") |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | fn submit(&mut self, clear: bool, window: &mut Window, cx: &mut Context<Self>) { |
| 78 | if self.saving { |
| 79 | return; |
| 80 | } |
| 81 | self.saving = true; |
| 82 | self.submits += 1; |
| 83 | let client_id = if clear { |
| 84 | String::new() |
| 85 | } else { |
| 86 | self.client_id.read(cx).value().to_string() |
| 87 | }; |
| 88 | let secret = if clear { |
| 89 | String::new() |
| 90 | } else { |
| 91 | self.secret.read(cx).value().to_string() |
| 92 | }; |
| 93 | let store = self.app.read(cx).store.clone(); |
| 94 | let connected = self.app.read(cx).calendar_accounts().has_google(); |
| 95 | let app = self.app.clone(); |
| 96 | let id_field = self.client_id.clone(); |
| 97 | let secret_field = self.secret.clone(); |
| 98 | let saved_label = client_id.trim().to_owned(); |
| 99 | cx.spawn_in(window, async move |form, cx| { |
| 100 | let result = crate::runtime::spawn(async move { |
| 101 | crate::google_oauth_client::save(&store, &client_id, &secret).await |
| 102 | }) |
| 103 | .await; |
| 104 | form.update_in(cx, |form, window, cx| { |
| 105 | form.saving = false; |
| 106 | let notice = match result { |
| 107 | Ok(Ok(crate::google_oauth_client::GoogleClientSave::Saved)) => { |
| 108 | form.saved_id = saved_label; |
| 109 | form.secret_present = true; |
| 110 | secret_field.update(cx, |input, cx| input.set_value("", window, cx)); |
| 111 | if connected { |
| 112 | tr("settings.calendar.google_client_saved_reconnect") |
| 113 | } else { |
| 114 | tr("settings.calendar.google_client_saved") |
| 115 | } |
| 116 | } |
| 117 | Ok(Ok(crate::google_oauth_client::GoogleClientSave::Cleared)) => { |
| 118 | form.saved_id.clear(); |
| 119 | form.secret_present = false; |
| 120 | id_field.update(cx, |input, cx| input.set_value("", window, cx)); |
| 121 | secret_field.update(cx, |input, cx| { |
| 122 | input.set_value("", window, cx); |
| 123 | input.set_placeholder("", window, cx); |
| 124 | }); |
| 125 | if connected { |
| 126 | tr("settings.calendar.google_client_cleared_reconnect") |
| 127 | } else { |
| 128 | tr("settings.calendar.google_client_cleared") |
| 129 | } |
| 130 | } |
| 131 | Ok(Err(crate::google_oauth_client::GoogleClientSaveError::SecretRequired)) => { |
| 132 | tr("settings.calendar.google_client_secret_required") |
| 133 | } |
| 134 | _ => tr("settings.calendar.google_client_save_failed"), |
| 135 | }; |
| 136 | if form.secret_present { |
| 137 | secret_field.update(cx, |input, cx| { |
| 138 | input.set_placeholder( |
| 139 | tr("settings.calendar.google_client_secret_kept"), |
| 140 | window, |
| 141 | cx, |
| 142 | ); |
| 143 | }); |
| 144 | } |
| 145 | app.update(cx, |state, cx| state.raise_notice(notice, None, cx)); |
| 146 | cx.notify(); |
| 147 | }) |
| 148 | .ok(); |
| 149 | }) |
| 150 | .detach(); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | impl Render for GoogleClientForm { |
| 155 | fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 156 | let save = cx.entity().clone(); |
| 157 | let reset = cx.entity().clone(); |
| 158 | v_flex() |
| 159 | .w_full() |
| 160 | .gap(px(8.)) |
| 161 | .child(control_row( |
| 162 | tr("settings.calendar.google_client_status_title"), |
| 163 | self.status_detail(), |
| 164 | div(), |
| 165 | )) |
| 166 | .child(stacked_field( |
| 167 | tr("settings.calendar.google_client_id"), |
| 168 | "", |
| 169 | Input::new(&self.client_id), |
| 170 | )) |
| 171 | .child(stacked_field( |
| 172 | tr("settings.calendar.google_client_secret"), |
| 173 | "", |
| 174 | Input::new(&self.secret) |
| 175 | .content_type(InputContentType::Password) |
| 176 | .mask_toggle(), |
| 177 | )) |
| 178 | .child(guide()) |
| 179 | .child(control_row( |
| 180 | tr("settings.calendar.google_client_save"), |
| 181 | "", |
| 182 | h_flex() |
| 183 | .gap_2() |
| 184 | .child( |
| 185 | text_button( |
| 186 | "settings-google-client-save", |
| 187 | tr("settings.calendar.google_client_save"), |
| 188 | ) |
| 189 | .on_click(move |_, window, cx| { |
| 190 | save.update(cx, |form, cx| form.submit(false, window, cx)) |
| 191 | }), |
| 192 | ) |
| 193 | .child( |
| 194 | text_button( |
| 195 | "settings-google-client-reset", |
| 196 | tr("settings.calendar.google_client_reset"), |
| 197 | ) |
| 198 | .on_click(move |_, window, cx| { |
| 199 | reset.update(cx, |form, cx| form.submit(true, window, cx)) |
| 200 | }), |
| 201 | ), |
| 202 | )) |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | fn guide() -> Div { |
| 207 | v_flex() |
| 208 | .w_full() |
| 209 | .px(px(ROW_INSET)) |
| 210 | .child(guide_line(tr("settings.calendar.google_client_guide_1"))) |
| 211 | .child(guide_line(tr("settings.calendar.google_client_guide_2"))) |
| 212 | .child(guide_line(tr("settings.calendar.google_client_guide_3"))) |
| 213 | .child(guide_line(tr("settings.calendar.google_client_guide_4"))) |
| 214 | .child(guide_line(tr("settings.calendar.google_client_guide_5"))) |
| 215 | } |
| 216 | |
| 217 | fn guide_line(text: String) -> Div { |
| 218 | div() |
| 219 | .w_full() |
| 220 | .text_size(px(12.)) |
| 221 | .line_height(px(16.)) |
| 222 | .text_color(theme::text_muted()) |
| 223 | .child(text) |
| 224 | } |