| 1 | use gpui_kit::component::v_flex; |
| 2 | use gpui_kit::*; |
| 3 | use store::{CalendarProvider, CalendarRef}; |
| 4 | |
| 5 | use super::rows::{caption, control_row, text_button, toggle_row}; |
| 6 | use crate::app_state::AppState; |
| 7 | use crate::theme; |
| 8 | |
| 9 | pub fn render(app: &Entity<AppState>, cx: &App) -> Div { |
| 10 | let accounts = app.read(cx).calendar_accounts(); |
| 11 | let mut body = v_flex() |
| 12 | .w_full() |
| 13 | .child(caption("GOOGLE")) |
| 14 | .child(connect_row(app, accounts.has_google())); |
| 15 | for account in &accounts.google_accounts { |
| 16 | body = body.child(account_block(app, account, &accounts)); |
| 17 | } |
| 18 | body = body |
| 19 | .child(caption("HOME CALENDAR")) |
| 20 | .child(home_choices(app, &accounts)) |
| 21 | .child(caption("ALSO WRITE TO")); |
| 22 | if accounts.home.provider != CalendarProvider::Outlook { |
| 23 | let write_app = app.clone(); |
| 24 | let target = CalendarRef::outlook(); |
| 25 | let selected = accounts |
| 26 | .write_targets |
| 27 | .iter() |
| 28 | .any(|existing| existing.key() == target.key()); |
| 29 | body = body.child( |
| 30 | toggle_row( |
| 31 | "settings-write-outlook", |
| 32 | "Outlook (Microsoft mailbox)", |
| 33 | "Dual-write when home is Google", |
| 34 | theme::text_muted(), |
| 35 | selected, |
| 36 | ) |
| 37 | .on_click(move |_, _, cx| { |
| 38 | write_app.update(cx, |state, cx| { |
| 39 | state.toggle_write_target(target.clone(), cx) |
| 40 | }) |
| 41 | }), |
| 42 | ); |
| 43 | } else if accounts.google_accounts.is_empty() { |
| 44 | body = body.child(control_row( |
| 45 | "No extra write targets", |
| 46 | "Connect Google, then enable dual-write on a calendar above.", |
| 47 | div(), |
| 48 | )); |
| 49 | } |
| 50 | body |
| 51 | } |
| 52 | |
| 53 | fn connect_row(app: &Entity<AppState>, connected: bool) -> Div { |
| 54 | let app = app.clone(); |
| 55 | if connected { |
| 56 | control_row( |
| 57 | "Google Calendar", |
| 58 | "Connected accounts appear below. Disconnect removes tokens from this device.", |
| 59 | div(), |
| 60 | ) |
| 61 | } else { |
| 62 | control_row( |
| 63 | "Google Calendar", |
| 64 | "Requires TEAMS_GOOGLE_CLIENT_ID. Opens a browser for sign-in.", |
| 65 | text_button("settings-google-connect", "Connect Google").on_click(move |_, _, cx| { |
| 66 | app.update(cx, |state, cx| state.connect_google_calendar(cx)) |
| 67 | }), |
| 68 | ) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | fn account_block( |
| 73 | app: &Entity<AppState>, |
| 74 | account: &store::GoogleAccount, |
| 75 | accounts: &store::CalendarAccounts, |
| 76 | ) -> Div { |
| 77 | let disconnect_app = app.clone(); |
| 78 | let account_id = account.id.clone(); |
| 79 | let mut block = v_flex().w_full().child(control_row( |
| 80 | account.email.clone(), |
| 81 | "Google account", |
| 82 | text_button( |
| 83 | format!("settings-google-disconnect-{}", account.id), |
| 84 | "Disconnect", |
| 85 | ) |
| 86 | .on_click(move |_, _, cx| { |
| 87 | disconnect_app.update(cx, |state, cx| { |
| 88 | state.disconnect_google_account(&account_id, cx) |
| 89 | }) |
| 90 | }), |
| 91 | )); |
| 92 | for calendar in &account.calendars { |
| 93 | let toggle_app = app.clone(); |
| 94 | let account_id = account.id.clone(); |
| 95 | let calendar_id = calendar.id.clone(); |
| 96 | let write_ref = CalendarRef::google(&account.id, &calendar.id); |
| 97 | let is_home = accounts.home.key() == write_ref.key(); |
| 98 | let is_write = accounts |
| 99 | .write_targets |
| 100 | .iter() |
| 101 | .any(|target| target.key() == write_ref.key()); |
| 102 | block = block.child( |
| 103 | toggle_row( |
| 104 | format!("settings-google-cal-{}", calendar.id), |
| 105 | calendar.summary.clone(), |
| 106 | if calendar.primary { |
| 107 | "Primary calendar" |
| 108 | } else { |
| 109 | "Show in calendar grid" |
| 110 | }, |
| 111 | theme::text_muted(), |
| 112 | calendar.selected, |
| 113 | ) |
| 114 | .on_click(move |_, _, cx| { |
| 115 | toggle_app.update(cx, |state, cx| { |
| 116 | state.toggle_google_calendar_selected(&account_id, &calendar_id, cx) |
| 117 | }) |
| 118 | }), |
| 119 | ); |
| 120 | if !is_home { |
| 121 | let write_app = app.clone(); |
| 122 | let target = write_ref; |
| 123 | block = block.child( |
| 124 | toggle_row( |
| 125 | format!("settings-google-write-{}", calendar.id), |
| 126 | format!("Also write new meetings to {}", calendar.summary), |
| 127 | "", |
| 128 | theme::text_muted(), |
| 129 | is_write, |
| 130 | ) |
| 131 | .on_click(move |_, _, cx| { |
| 132 | write_app.update(cx, |state, cx| { |
| 133 | state.toggle_write_target(target.clone(), cx) |
| 134 | }) |
| 135 | }), |
| 136 | ); |
| 137 | } |
| 138 | } |
| 139 | block |
| 140 | } |
| 141 | |
| 142 | fn home_choices(app: &Entity<AppState>, accounts: &store::CalendarAccounts) -> Div { |
| 143 | let mut choices = v_flex().w_full(); |
| 144 | let outlook_app = app.clone(); |
| 145 | let outlook_home = accounts.home.provider == CalendarProvider::Outlook; |
| 146 | choices = choices.child( |
| 147 | toggle_row( |
| 148 | "settings-home-outlook", |
| 149 | "Outlook (Microsoft mailbox)", |
| 150 | "Default when no Google home is chosen", |
| 151 | theme::text_muted(), |
| 152 | outlook_home, |
| 153 | ) |
| 154 | .on_click(move |_, _, cx| { |
| 155 | outlook_app.update(cx, |state, cx| { |
| 156 | state.set_home_calendar(CalendarRef::outlook(), cx) |
| 157 | }) |
| 158 | }), |
| 159 | ); |
| 160 | for account in &accounts.google_accounts { |
| 161 | for calendar in &account.calendars { |
| 162 | let home_app = app.clone(); |
| 163 | let target = CalendarRef::google(&account.id, &calendar.id); |
| 164 | let selected = accounts.home.key() == target.key(); |
| 165 | choices = choices.child( |
| 166 | toggle_row( |
| 167 | format!("settings-home-google-{}", calendar.id), |
| 168 | format!("Google · {}", calendar.summary), |
| 169 | account.email.clone(), |
| 170 | theme::text_muted(), |
| 171 | selected, |
| 172 | ) |
| 173 | .on_click(move |_, _, cx| { |
| 174 | home_app.update(cx, |state, cx| state.set_home_calendar(target.clone(), cx)) |
| 175 | }), |
| 176 | ); |
| 177 | } |
| 178 | } |
| 179 | choices |
| 180 | } |