BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-state / src / call / week.rs

week.rs

week.rs 8.4 KiB Download Raw
1use chatsvc::{CalendarEvent, ResponseType, ShowAs};
2use chrono::{Duration, FixedOffset, Local, NaiveDate, Offset};
3use gpui_kit::Context;
4
5use super::agenda::demo_events;
6use super::calendar::demo_anchor;
7use super::week_grid::{CalendarSpan, local_midnight, week_range, week_start};
8use crate::app_state::AppState;
9
10pub const CALENDAR_LOAD_FAILURE: &str = "We couldn't load your calendar.";
11
12#[derive(Debug, Clone, Default)]
13pub 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
21impl 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 match crate::google_calendar_sync::fetch_google_events(&mut accounts, from, to).await {
114 Ok(events) => {
115 let _ = store::save_calendar_accounts(&store, &accounts);
116 combined.extend(events);
117 }
118 Err(error) => crate::diagnostics::record(format!(
119 "calls: google calendar week not loaded: {error}"
120 )),
121 }
122 if combined.is_empty() && outlook_failed && !accounts.has_google() {
123 return Err(CALENDAR_LOAD_FAILURE.to_owned());
124 }
125 Ok(chatsvc::merge_calendar_events(combined))
126 });
127 cx.spawn(async move |this, cx| {
128 let loaded = receiver
129 .await
130 .unwrap_or_else(|_| Err(CALENDAR_LOAD_FAILURE.to_owned()));
131 this.update(cx, |state, cx| {
132 if state.calendar_week.request != request {
133 return;
134 }
135 state.calendar_week.loading = false;
136 match loaded {
137 Ok(events) => state.calendar_week.events = events,
138 Err(notice) => state.raise_notice(notice, None, cx),
139 }
140 cx.notify();
141 })
142 .ok();
143 })
144 .detach();
145 }
146}
147
148fn demo_show_as(subject: &str, response: ResponseType) -> ShowAs {
149 match response {
150 _ if subject.starts_with("Following: ") => ShowAs::Free,
151 ResponseType::Tentative | ResponseType::NotResponded => ShowAs::Tentative,
152 _ => ShowAs::Busy,
153 }
154}
155
156fn demo_week(monday: NaiveDate, offset: FixedOffset) -> Vec<CalendarEvent> {
157 let slots: [(&str, &str, i64, i64, i64, ResponseType); 8] = [
158 (
159 "Team standup",
160 "Mara Lindqvist",
161 0,
162 9 * 60 + 30,
163 15,
164 ResponseType::Accepted,
165 ),
166 (
167 "Release review",
168 "Jonas Ortega",
169 0,
170 11 * 60,
171 60,
172 ResponseType::Organizer,
173 ),
174 (
175 "Customer sync",
176 "Priya Nair",
177 0,
178 11 * 60 + 30,
179 60,
180 ResponseType::NotResponded,
181 ),
182 (
183 "Team standup",
184 "Mara Lindqvist",
185 1,
186 9 * 60 + 30,
187 15,
188 ResponseType::Accepted,
189 ),
190 (
191 "Architecture deep dive",
192 "Lena Brandt",
193 2,
194 14 * 60,
195 90,
196 ResponseType::Tentative,
197 ),
198 (
199 "1:1 Jonas",
200 "Jonas Ortega",
201 3,
202 10 * 60,
203 30,
204 ResponseType::Accepted,
205 ),
206 (
207 "Following: Design office hours",
208 "Lena Brandt",
209 3,
210 13 * 60,
211 60,
212 ResponseType::Tentative,
213 ),
214 (
215 "Sprint retro",
216 "Mara Lindqvist",
217 4,
218 15 * 60,
219 60,
220 ResponseType::Accepted,
221 ),
222 ];
223 slots
224 .iter()
225 .enumerate()
226 .map(|(index, (subject, organizer, day, minute, length, response))| {
227 let start =
228 local_midnight(monday + Duration::days(*day), offset) + Duration::minutes(*minute);
229 CalendarEvent {
230 object_id: format!("demo-week-{index}"),
231 subject: (*subject).to_owned(),
232 start,
233 end: start + Duration::minutes(*length),
234 all_day: false,
235 cancelled: false,
236 online: true,
237 join_url: Some(format!(
238 "https://teams.microsoft.com/l/meetup-join/19%3ameeting_demoweek{index}%40thread.v2/0"
239 )),
240 thread_id: None,
241 organizer_name: Some((*organizer).to_owned()),
242 organizer_address: None,
243 ical_uid: None,
244 show_as: demo_show_as(subject, *response),
245 dial_in: None,
246 response: *response,
247 ..Default::default()
248 }
249 })
250 .collect()
251}