| 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.load_calendar_week(cx); |
| 31 | } |
| 32 | |
| 33 | pub fn shift_calendar_week(&mut self, steps: i32, cx: &mut Context<Self>) { |
| 34 | let span = self.calendar_week.span; |
| 35 | self.calendar_week.start = Some(span.shifted(self.calendar_start(), steps)); |
| 36 | self.load_calendar_week(cx); |
| 37 | } |
| 38 | |
| 39 | pub fn calendar_to_today(&mut self, cx: &mut Context<Self>) { |
| 40 | self.calendar_week.start = None; |
| 41 | self.open_calendar(cx); |
| 42 | } |
| 43 | |
| 44 | pub fn set_calendar_span(&mut self, span: CalendarSpan, cx: &mut Context<Self>) { |
| 45 | let today = Local::now().date_naive(); |
| 46 | let start = self.calendar_start(); |
| 47 | let shown = |
| 48 | start <= today && today < start + Duration::days(self.calendar_week.span.days()); |
| 49 | self.calendar_week.span = span; |
| 50 | self.calendar_week.start = Some(span.start_for(if shown { today } else { start })); |
| 51 | self.load_calendar_week(cx); |
| 52 | } |
| 53 | |
| 54 | pub fn show_calendar_date(&mut self, date: NaiveDate, cx: &mut Context<Self>) { |
| 55 | self.calendar_week.start = Some(self.calendar_week.span.start_for(date)); |
| 56 | self.load_calendar_week(cx); |
| 57 | } |
| 58 | |
| 59 | pub fn show_calendar_day(&mut self, date: NaiveDate, cx: &mut Context<Self>) { |
| 60 | self.calendar_week.span = CalendarSpan::Day; |
| 61 | self.show_calendar_date(date, cx); |
| 62 | } |
| 63 | |
| 64 | fn load_calendar_week(&mut self, cx: &mut Context<Self>) { |
| 65 | let start = self.calendar_start(); |
| 66 | let offset = Local::now().offset().fix(); |
| 67 | let (from, to) = week_range(start, self.calendar_week.span.days(), offset); |
| 68 | if self.mode.demo { |
| 69 | let mut events = demo_events(demo_anchor()); |
| 70 | events.extend(demo_week(week_start(start), offset)); |
| 71 | events.extend(self.demo_meetings.iter().cloned()); |
| 72 | events.retain(|event| event.end > from && event.start < to); |
| 73 | events.sort_by_key(|event| (event.start, event.end)); |
| 74 | self.calendar_week.events = events; |
| 75 | cx.notify(); |
| 76 | return; |
| 77 | } |
| 78 | let Some(launcher) = self.call_launcher.clone() else { |
| 79 | return; |
| 80 | }; |
| 81 | self.calendar_week.request += 1; |
| 82 | let request = self.calendar_week.request; |
| 83 | self.calendar_week.loading = true; |
| 84 | cx.notify(); |
| 85 | let receiver = crate::runtime::spawn(async move { launcher.calendar_view(from, to).await }); |
| 86 | cx.spawn(async move |this, cx| { |
| 87 | let loaded = receiver.await.ok().and_then(Result::ok); |
| 88 | this.update(cx, |state, cx| { |
| 89 | if state.calendar_week.request != request { |
| 90 | return; |
| 91 | } |
| 92 | state.calendar_week.loading = false; |
| 93 | match loaded { |
| 94 | Some(events) => state.calendar_week.events = events, |
| 95 | None => state.raise_notice(CALENDAR_LOAD_FAILURE.to_owned(), None, cx), |
| 96 | } |
| 97 | cx.notify(); |
| 98 | }) |
| 99 | .ok(); |
| 100 | }) |
| 101 | .detach(); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | fn demo_show_as(subject: &str, response: ResponseType) -> ShowAs { |
| 106 | match response { |
| 107 | _ if subject.starts_with("Following: ") => ShowAs::Free, |
| 108 | ResponseType::Tentative | ResponseType::NotResponded => ShowAs::Tentative, |
| 109 | _ => ShowAs::Busy, |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | fn demo_week(monday: NaiveDate, offset: FixedOffset) -> Vec<CalendarEvent> { |
| 114 | let slots: [(&str, &str, i64, i64, i64, ResponseType); 8] = [ |
| 115 | ( |
| 116 | "Team standup", |
| 117 | "Mara Lindqvist", |
| 118 | 0, |
| 119 | 9 * 60 + 30, |
| 120 | 15, |
| 121 | ResponseType::Accepted, |
| 122 | ), |
| 123 | ( |
| 124 | "Release review", |
| 125 | "Jonas Ortega", |
| 126 | 0, |
| 127 | 11 * 60, |
| 128 | 60, |
| 129 | ResponseType::Organizer, |
| 130 | ), |
| 131 | ( |
| 132 | "Customer sync", |
| 133 | "Priya Nair", |
| 134 | 0, |
| 135 | 11 * 60 + 30, |
| 136 | 60, |
| 137 | ResponseType::NotResponded, |
| 138 | ), |
| 139 | ( |
| 140 | "Team standup", |
| 141 | "Mara Lindqvist", |
| 142 | 1, |
| 143 | 9 * 60 + 30, |
| 144 | 15, |
| 145 | ResponseType::Accepted, |
| 146 | ), |
| 147 | ( |
| 148 | "Architecture deep dive", |
| 149 | "Lena Brandt", |
| 150 | 2, |
| 151 | 14 * 60, |
| 152 | 90, |
| 153 | ResponseType::Tentative, |
| 154 | ), |
| 155 | ( |
| 156 | "1:1 Jonas", |
| 157 | "Jonas Ortega", |
| 158 | 3, |
| 159 | 10 * 60, |
| 160 | 30, |
| 161 | ResponseType::Accepted, |
| 162 | ), |
| 163 | ( |
| 164 | "Following: Design office hours", |
| 165 | "Lena Brandt", |
| 166 | 3, |
| 167 | 13 * 60, |
| 168 | 60, |
| 169 | ResponseType::Tentative, |
| 170 | ), |
| 171 | ( |
| 172 | "Sprint retro", |
| 173 | "Mara Lindqvist", |
| 174 | 4, |
| 175 | 15 * 60, |
| 176 | 60, |
| 177 | ResponseType::Accepted, |
| 178 | ), |
| 179 | ]; |
| 180 | slots |
| 181 | .iter() |
| 182 | .enumerate() |
| 183 | .map(|(index, (subject, organizer, day, minute, length, response))| { |
| 184 | let start = |
| 185 | local_midnight(monday + Duration::days(*day), offset) + Duration::minutes(*minute); |
| 186 | CalendarEvent { |
| 187 | object_id: format!("demo-week-{index}"), |
| 188 | subject: (*subject).to_owned(), |
| 189 | start, |
| 190 | end: start + Duration::minutes(*length), |
| 191 | all_day: false, |
| 192 | cancelled: false, |
| 193 | online: true, |
| 194 | join_url: Some(format!( |
| 195 | "https://teams.microsoft.com/l/meetup-join/19%3ameeting_demoweek{index}%40thread.v2/0" |
| 196 | )), |
| 197 | thread_id: None, |
| 198 | organizer_name: Some((*organizer).to_owned()), |
| 199 | organizer_address: None, |
| 200 | ical_uid: None, |
| 201 | show_as: demo_show_as(subject, *response), |
| 202 | dial_in: None, |
| 203 | response: *response, |
| 204 | } |
| 205 | }) |
| 206 | .collect() |
| 207 | } |