BlueGit

ict rusty-teams

Files

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

week.rs

week.rs 7.1 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 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 receiver = crate::runtime::spawn(async move { launcher.calendar_view(from, to).await });
98 cx.spawn(async move |this, cx| {
99 let loaded = receiver.await.ok().and_then(Result::ok);
100 this.update(cx, |state, cx| {
101 if state.calendar_week.request != request {
102 return;
103 }
104 state.calendar_week.loading = false;
105 match loaded {
106 Some(events) => state.calendar_week.events = events,
107 None => state.raise_notice(CALENDAR_LOAD_FAILURE.to_owned(), None, cx),
108 }
109 cx.notify();
110 })
111 .ok();
112 })
113 .detach();
114 }
115}
116
117fn demo_show_as(subject: &str, response: ResponseType) -> ShowAs {
118 match response {
119 _ if subject.starts_with("Following: ") => ShowAs::Free,
120 ResponseType::Tentative | ResponseType::NotResponded => ShowAs::Tentative,
121 _ => ShowAs::Busy,
122 }
123}
124
125fn demo_week(monday: NaiveDate, offset: FixedOffset) -> Vec<CalendarEvent> {
126 let slots: [(&str, &str, i64, i64, i64, ResponseType); 8] = [
127 (
128 "Team standup",
129 "Mara Lindqvist",
130 0,
131 9 * 60 + 30,
132 15,
133 ResponseType::Accepted,
134 ),
135 (
136 "Release review",
137 "Jonas Ortega",
138 0,
139 11 * 60,
140 60,
141 ResponseType::Organizer,
142 ),
143 (
144 "Customer sync",
145 "Priya Nair",
146 0,
147 11 * 60 + 30,
148 60,
149 ResponseType::NotResponded,
150 ),
151 (
152 "Team standup",
153 "Mara Lindqvist",
154 1,
155 9 * 60 + 30,
156 15,
157 ResponseType::Accepted,
158 ),
159 (
160 "Architecture deep dive",
161 "Lena Brandt",
162 2,
163 14 * 60,
164 90,
165 ResponseType::Tentative,
166 ),
167 (
168 "1:1 Jonas",
169 "Jonas Ortega",
170 3,
171 10 * 60,
172 30,
173 ResponseType::Accepted,
174 ),
175 (
176 "Following: Design office hours",
177 "Lena Brandt",
178 3,
179 13 * 60,
180 60,
181 ResponseType::Tentative,
182 ),
183 (
184 "Sprint retro",
185 "Mara Lindqvist",
186 4,
187 15 * 60,
188 60,
189 ResponseType::Accepted,
190 ),
191 ];
192 slots
193 .iter()
194 .enumerate()
195 .map(|(index, (subject, organizer, day, minute, length, response))| {
196 let start =
197 local_midnight(monday + Duration::days(*day), offset) + Duration::minutes(*minute);
198 CalendarEvent {
199 object_id: format!("demo-week-{index}"),
200 subject: (*subject).to_owned(),
201 start,
202 end: start + Duration::minutes(*length),
203 all_day: false,
204 cancelled: false,
205 online: true,
206 join_url: Some(format!(
207 "https://teams.microsoft.com/l/meetup-join/19%3ameeting_demoweek{index}%40thread.v2/0"
208 )),
209 thread_id: None,
210 organizer_name: Some((*organizer).to_owned()),
211 organizer_address: None,
212 ical_uid: None,
213 show_as: demo_show_as(subject, *response),
214 dial_in: None,
215 response: *response,
216 }
217 })
218 .collect()
219}