BlueGit

ict rusty-teams

Files

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

week.rs

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