| 1 | use std::sync::OnceLock; |
| 2 | |
| 3 | use chatsvc::CalendarEvent; |
| 4 | use chrono::{DateTime, Local, Offset, Utc}; |
| 5 | use gpui_kit::Context; |
| 6 | |
| 7 | use super::agenda::{AgendaRow, demo_events}; |
| 8 | use crate::app_state::{AppEvent, AppState, Selection}; |
| 9 | use crate::notify::selection_for; |
| 10 | |
| 11 | const NO_MEETING_CHAT_NOTICE: &str = "This meeting chat is not available"; |
| 12 | static DEMO_ANCHOR: OnceLock<DateTime<Utc>> = OnceLock::new(); |
| 13 | |
| 14 | impl AppState { |
| 15 | pub fn apply_calendar(&mut self, events: Vec<CalendarEvent>, cx: &mut Context<Self>) { |
| 16 | self.calendar_events = events; |
| 17 | self.calls_list.invalidate(); |
| 18 | cx.emit(AppEvent::CallHistory); |
| 19 | cx.notify(); |
| 20 | } |
| 21 | |
| 22 | pub fn refresh_calendar(&mut self, cx: &mut Context<Self>) { |
| 23 | if self.mode.demo { |
| 24 | let mut events = demo_events(*DEMO_ANCHOR.get_or_init(Utc::now)); |
| 25 | events.extend(self.demo_meetings.iter().cloned()); |
| 26 | events.sort_by_key(|event| (event.start, event.end)); |
| 27 | self.apply_calendar(events, cx); |
| 28 | return; |
| 29 | } |
| 30 | if let Some(nudge) = &self.calendar_nudge { |
| 31 | let _ = nudge.send(()); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | pub fn rebuild_calls_list(&mut self, cx: &mut Context<Self>) { |
| 36 | let now = Utc::now(); |
| 37 | let offset = Local::now().offset().fix(); |
| 38 | self.calls_list.rebuild( |
| 39 | &self.call_history, |
| 40 | &self.sidebar.chats, |
| 41 | &self.calendar_events, |
| 42 | now, |
| 43 | offset, |
| 44 | ); |
| 45 | cx.emit(AppEvent::CallHistory); |
| 46 | cx.notify(); |
| 47 | } |
| 48 | |
| 49 | pub fn join_agenda_row(&mut self, row: &AgendaRow, cx: &mut Context<Self>) { |
| 50 | if let Some(url) = &row.join_url { |
| 51 | self.join_meeting_link(url, cx); |
| 52 | if let Some(pending) = self.prejoin.as_mut() { |
| 53 | pending.join.title = row.subject.clone(); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | pub fn open_agenda_chat(&mut self, row: &AgendaRow, cx: &mut Context<Self>) { |
| 59 | let Some(thread_id) = &row.thread_id else { |
| 60 | return; |
| 61 | }; |
| 62 | match selection_for(&self.sidebar, thread_id) { |
| 63 | Some(selection @ Selection::Chat(_)) => self.select(selection, cx), |
| 64 | _ => self.raise_notice(NO_MEETING_CHAT_NOTICE.to_owned(), None, cx), |
| 65 | } |
| 66 | } |
| 67 | } |