| 1 | use chatsvc::{CalendarEvent, ResponseType, ShowAs}; |
| 2 | use chrono::{Duration, FixedOffset, Local, NaiveDate, Offset}; |
| 3 | use gpui_kit::Context; |
| 4 | |
| 5 | use super::agenda::demo_events; |
| 6 | use super::calendar::demo_anchor; |
| 7 | use super::week_grid::{CalendarSpan, local_midnight, week_range, week_start}; |
| 8 | use crate::app_state::AppState; |
| 9 | |
| 10 | pub const CALENDAR_LOAD_FAILURE: &str = "We couldn't load your calendar."; |
| 11 | |
| 12 | #[derive(Debug, Clone, Default)] |
| 13 | pub struct CalendarWeek { |
| 14 | pub start: Option<NaiveDate>, |
| 15 | pub span: CalendarSpan, |
| 16 | pub events: Vec<CalendarEvent>, |
| 17 | pub loading: bool, |
| 18 | request: u64, |
| 19 | } |
| 20 | |
| 21 | impl AppState { |
| 22 | pub fn calendar_start(&self) -> NaiveDate { |
| 23 | self.calendar_week |
| 24 | .start |
| 25 | .unwrap_or_else(|| self.calendar_week.span.start_for(Local::now().date_naive())) |
| 26 | } |
| 27 | |
| 28 | pub fn open_calendar(&mut self, cx: &mut Context<Self>) { |
| 29 | self.calendar_week.start = Some(self.calendar_start()); |
| 30 | self.request_calendar_scroll_to_now(cx); |
| 31 | self.load_calendar_week(cx); |
| 32 | } |
| 33 | |
| 34 | pub fn shift_calendar_week(&mut self, steps: i32, cx: &mut Context<Self>) { |
| 35 | let span = self.calendar_week.span; |
| 36 | self.calendar_week.start = Some(span.shifted(self.calendar_start(), steps)); |
| 37 | self.load_calendar_week(cx); |
| 38 | } |
| 39 | |
| 40 | pub fn calendar_to_today(&mut self, cx: &mut Context<Self>) { |
| 41 | self.calendar_week.start = None; |
| 42 | self.open_calendar(cx); |
| 43 | } |
| 44 | |
| 45 | pub fn request_calendar_scroll_to_now(&mut self, cx: &mut Context<Self>) { |
| 46 | if !crate::call::calendar_now::shows_hour_grid(self.calendar_week.span) { |
| 47 | return; |
| 48 | } |
| 49 | self.calendar_scroll_to_now = self.calendar_scroll_to_now.wrapping_add(1); |
| 50 | cx.notify(); |
| 51 | } |
| 52 | |
| 53 | pub fn set_calendar_span(&mut self, span: CalendarSpan, cx: &mut Context<Self>) { |
| 54 | let today = Local::now().date_naive(); |
| 55 | let start = self.calendar_start(); |
| 56 | let shown = |
| 57 | start <= today && today < start + Duration::days(self.calendar_week.span.days()); |
| 58 | self.calendar_week.span = span; |
| 59 | self.calendar_week.start = Some(span.start_for(if shown { today } else { start })); |
| 60 | if crate::call::calendar_now::shows_hour_grid(span) { |
| 61 | self.request_calendar_scroll_to_now(cx); |
| 62 | } |
| 63 | self.load_calendar_week(cx); |
| 64 | } |
| 65 | |
| 66 | pub fn show_calendar_date(&mut self, date: NaiveDate, cx: &mut Context<Self>) { |
| 67 | self.calendar_week.start = Some(self.calendar_week.span.start_for(date)); |
| 68 | self.load_calendar_week(cx); |
| 69 | } |
| 70 | |
| 71 | pub fn show_calendar_day(&mut self, date: NaiveDate, cx: &mut Context<Self>) { |
| 72 | self.calendar_week.span = CalendarSpan::Day; |
| 73 | self.show_calendar_date(date, cx); |
| 74 | } |
| 75 | |
| 76 | pub(crate) fn load_calendar_week(&mut self, cx: &mut Context<Self>) { |
| 77 | let start = self.calendar_start(); |
| 78 | let offset = Local::now().offset().fix(); |
| 79 | let (from, to) = week_range(start, self.calendar_week.span.days(), offset); |
| 80 | if self.mode.demo { |
| 81 | let mut events = demo_events(demo_anchor()); |
| 82 | events.extend(demo_week(week_start(start), offset)); |
| 83 | events.extend(self.demo_meetings.iter().cloned()); |
| 84 | events.retain(|event| event.end > from && event.start < to); |
| 85 | events.sort_by_key(|event| (event.start, event.end)); |
| 86 | self.calendar_week.events = events; |
| 87 | cx.notify(); |
| 88 | return; |
| 89 | } |
| 90 | let Some(launcher) = self.call_launcher.clone() else { |
| 91 | return; |
| 92 | }; |
| 93 | self.calendar_week.request += 1; |
| 94 | let request = self.calendar_week.request; |
| 95 | self.calendar_week.loading = true; |
| 96 | cx.notify(); |
| 97 | let store = self.store.clone(); |
| 98 | let receiver = crate::runtime::spawn(async move { |
| 99 | let mut accounts = store::load_calendar_accounts(&store); |
| 100 | let mut combined = Vec::new(); |
| 101 | let mut outlook_failed = false; |
| 102 | if accounts.outlook_selected { |
| 103 | match launcher.calendar_view(from, to).await { |
| 104 | Ok(events) => combined.extend(events), |
| 105 | Err(error) => { |
| 106 | outlook_failed = true; |
| 107 | crate::diagnostics::record(format!( |
| 108 | "calls: outlook calendar week not loaded: {error}" |
| 109 | )); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | let mut google_error = None; |
| 114 | match crate::google_calendar_sync::fetch_google_events(&mut accounts, from, to).await { |
| 115 | Ok(events) => { |
| 116 | let _ = store::save_calendar_accounts(&store, &accounts); |
| 117 | combined.extend(events); |
| 118 | } |
| 119 | Err(error) => { |
| 120 | crate::diagnostics::record(format!( |
| 121 | "calls: google calendar week not loaded: {error}" |
| 122 | )); |
| 123 | google_error = Some(error); |
| 124 | } |
| 125 | } |
| 126 | if combined.is_empty() && outlook_failed && !accounts.has_google() { |
| 127 | return Err(CALENDAR_LOAD_FAILURE.to_owned()); |
| 128 | } |
| 129 | Ok((chatsvc::merge_calendar_events(combined), google_error)) |
| 130 | }); |
| 131 | cx.spawn(async move |this, cx| { |
| 132 | let loaded = receiver |
| 133 | .await |
| 134 | .unwrap_or_else(|_| Err(CALENDAR_LOAD_FAILURE.to_owned())); |
| 135 | this.update(cx, |state, cx| { |
| 136 | if state.calendar_week.request != request { |
| 137 | return; |
| 138 | } |
| 139 | state.calendar_week.loading = false; |
| 140 | match loaded { |
| 141 | Ok((events, google_error)) => { |
| 142 | // Keep detail enrichment (agenda/location/RSVP counts) across week reloads. |
| 143 | let previous = std::mem::take(&mut state.calendar_week.events); |
| 144 | let mut events = events; |
| 145 | retain_peek_details(&previous, &mut events); |
| 146 | state.calendar_week.events = events; |
| 147 | if let Some(error) = google_error { |
| 148 | if !state.google_sync_notice_shown { |
| 149 | state.google_sync_notice_shown = true; |
| 150 | state.raise_notice(error, None, cx); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | Err(notice) => state.raise_notice(notice, None, cx), |
| 155 | } |
| 156 | cx.notify(); |
| 157 | }) |
| 158 | .ok(); |
| 159 | }) |
| 160 | .detach(); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | fn retain_peek_details(previous: &[CalendarEvent], events: &mut [CalendarEvent]) { |
| 165 | for event in events { |
| 166 | let Some(old) = previous |
| 167 | .iter() |
| 168 | .find(|candidate| candidate.object_id == event.object_id) |
| 169 | else { |
| 170 | continue; |
| 171 | }; |
| 172 | if event.body_preview.is_none() { |
| 173 | event.body_preview = old.body_preview.clone(); |
| 174 | } |
| 175 | if event.body_html.is_none() { |
| 176 | event.body_html = old.body_html.clone(); |
| 177 | } |
| 178 | if event.attendees.is_empty() { |
| 179 | event.attendees = old.attendees.clone(); |
| 180 | } |
| 181 | if event.location.is_none() { |
| 182 | event.location = old.location.clone(); |
| 183 | } |
| 184 | if event.response_counts.is_none() { |
| 185 | event.response_counts = old.response_counts; |
| 186 | } |
| 187 | if event.series_master_id.is_none() { |
| 188 | event.series_master_id = old.series_master_id.clone(); |
| 189 | } |
| 190 | if event.reminders.is_empty() { |
| 191 | event.reminders = old.reminders.clone(); |
| 192 | } |
| 193 | if event.source_link.is_none() { |
| 194 | event.source_link = old.source_link.clone(); |
| 195 | } |
| 196 | if event.web_link.is_none() { |
| 197 | event.web_link = old.web_link.clone(); |
| 198 | } |
| 199 | if event.calendar_name.is_none() { |
| 200 | event.calendar_name = old.calendar_name.clone(); |
| 201 | } |
| 202 | if old.series { |
| 203 | event.series = true; |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | fn demo_show_as(subject: &str, response: ResponseType) -> ShowAs { |
| 209 | match response { |
| 210 | _ if subject.starts_with("Following: ") => ShowAs::Free, |
| 211 | ResponseType::Tentative | ResponseType::NotResponded => ShowAs::Tentative, |
| 212 | _ => ShowAs::Busy, |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | fn demo_week(monday: NaiveDate, offset: FixedOffset) -> Vec<CalendarEvent> { |
| 217 | let slots: [(&str, &str, i64, i64, i64, ResponseType); 8] = [ |
| 218 | ( |
| 219 | "Team standup", |
| 220 | "Mara Lindqvist", |
| 221 | 0, |
| 222 | 9 * 60 + 30, |
| 223 | 15, |
| 224 | ResponseType::Accepted, |
| 225 | ), |
| 226 | ( |
| 227 | "Release review", |
| 228 | "Jonas Ortega", |
| 229 | 0, |
| 230 | 11 * 60, |
| 231 | 60, |
| 232 | ResponseType::Organizer, |
| 233 | ), |
| 234 | ( |
| 235 | "Customer sync", |
| 236 | "Priya Nair", |
| 237 | 0, |
| 238 | 11 * 60 + 30, |
| 239 | 60, |
| 240 | ResponseType::NotResponded, |
| 241 | ), |
| 242 | ( |
| 243 | "Team standup", |
| 244 | "Mara Lindqvist", |
| 245 | 1, |
| 246 | 9 * 60 + 30, |
| 247 | 15, |
| 248 | ResponseType::Accepted, |
| 249 | ), |
| 250 | ( |
| 251 | "Architecture deep dive", |
| 252 | "Lena Brandt", |
| 253 | 2, |
| 254 | 14 * 60, |
| 255 | 90, |
| 256 | ResponseType::Tentative, |
| 257 | ), |
| 258 | ( |
| 259 | "1:1 Jonas", |
| 260 | "Jonas Ortega", |
| 261 | 3, |
| 262 | 10 * 60, |
| 263 | 30, |
| 264 | ResponseType::Accepted, |
| 265 | ), |
| 266 | ( |
| 267 | "Following: Design office hours", |
| 268 | "Lena Brandt", |
| 269 | 3, |
| 270 | 13 * 60, |
| 271 | 60, |
| 272 | ResponseType::Tentative, |
| 273 | ), |
| 274 | ( |
| 275 | "Sprint retro", |
| 276 | "Mara Lindqvist", |
| 277 | 4, |
| 278 | 15 * 60, |
| 279 | 60, |
| 280 | ResponseType::Accepted, |
| 281 | ), |
| 282 | ]; |
| 283 | slots |
| 284 | .iter() |
| 285 | .enumerate() |
| 286 | .map(|(index, (subject, organizer, day, minute, length, response))| { |
| 287 | let start = |
| 288 | local_midnight(monday + Duration::days(*day), offset) + Duration::minutes(*minute); |
| 289 | CalendarEvent { |
| 290 | object_id: format!("demo-week-{index}"), |
| 291 | subject: (*subject).to_owned(), |
| 292 | start, |
| 293 | end: start + Duration::minutes(*length), |
| 294 | all_day: false, |
| 295 | cancelled: false, |
| 296 | online: true, |
| 297 | join_url: Some(format!( |
| 298 | "https://teams.microsoft.com/l/meetup-join/19%3ameeting_demoweek{index}%40thread.v2/0" |
| 299 | )), |
| 300 | thread_id: None, |
| 301 | organizer_name: Some((*organizer).to_owned()), |
| 302 | organizer_address: None, |
| 303 | ical_uid: None, |
| 304 | show_as: demo_show_as(subject, *response), |
| 305 | dial_in: None, |
| 306 | response: *response, |
| 307 | ..Default::default() |
| 308 | } |
| 309 | }) |
| 310 | .collect() |
| 311 | } |