BlueGit

ict rusty-teams

Files

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

week.rs

d69b1473 History
week.rs 5.7 KiB Download Raw
1use chatsvc::{CalendarEvent, ResponseType};
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_week(monday: NaiveDate, offset: FixedOffset) -> Vec<CalendarEvent> {
96 let slots: [(&str, &str, i64, i64, i64, ResponseType); 7] = [
97 (
98 "Team standup",
99 "Mara Lindqvist",
100 0,
101 9 * 60 + 30,
102 15,
103 ResponseType::Accepted,
104 ),
105 (
106 "Release review",
107 "Jonas Ortega",
108 0,
109 11 * 60,
110 60,
111 ResponseType::Organizer,
112 ),
113 (
114 "Customer sync",
115 "Priya Nair",
116 0,
117 11 * 60 + 30,
118 60,
119 ResponseType::NotResponded,
120 ),
121 (
122 "Team standup",
123 "Mara Lindqvist",
124 1,
125 9 * 60 + 30,
126 15,
127 ResponseType::Accepted,
128 ),
129 (
130 "Architecture deep dive",
131 "Lena Brandt",
132 2,
133 14 * 60,
134 90,
135 ResponseType::Tentative,
136 ),
137 (
138 "1:1 Jonas",
139 "Jonas Ortega",
140 3,
141 10 * 60,
142 30,
143 ResponseType::Accepted,
144 ),
145 (
146 "Sprint retro",
147 "Mara Lindqvist",
148 4,
149 15 * 60,
150 60,
151 ResponseType::Accepted,
152 ),
153 ];
154 slots
155 .iter()
156 .enumerate()
157 .map(|(index, (subject, organizer, day, minute, length, response))| {
158 let start =
159 local_midnight(monday + Duration::days(*day), offset) + Duration::minutes(*minute);
160 CalendarEvent {
161 object_id: format!("demo-week-{index}"),
162 subject: (*subject).to_owned(),
163 start,
164 end: start + Duration::minutes(*length),
165 all_day: false,
166 cancelled: false,
167 online: true,
168 join_url: Some(format!(
169 "https://teams.microsoft.com/l/meetup-join/19%3ameeting_demoweek{index}%40thread.v2/0"
170 )),
171 thread_id: None,
172 organizer_name: Some((*organizer).to_owned()),
173 organizer_address: None,
174 ical_uid: None,
175 response: *response,
176 }
177 })
178 .collect()
179}