| 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 super::list::HistoryFilter; |
| 9 | use crate::app_state::{AppEvent, AppState, Selection, 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 | pub(crate) fn demo_anchor() -> DateTime<Utc> { |
| 15 | *DEMO_ANCHOR.get_or_init(Utc::now) |
| 16 | } |
| 17 | |
| 18 | impl AppState { |
| 19 | pub fn apply_calendar(&mut self, events: Vec<CalendarEvent>, cx: &mut Context<Self>) { |
| 20 | self.calendar_events = events; |
| 21 | self.calls_list.invalidate(); |
| 22 | cx.emit(AppEvent::CallHistory); |
| 23 | cx.notify(); |
| 24 | } |
| 25 | |
| 26 | pub fn refresh_calendar(&mut self, cx: &mut Context<Self>) { |
| 27 | if self.mode.demo { |
| 28 | let mut events = demo_events(demo_anchor()); |
| 29 | events.extend(self.demo_meetings.iter().cloned()); |
| 30 | events.sort_by_key(|event| (event.start, event.end)); |
| 31 | self.apply_calendar(events, cx); |
| 32 | return; |
| 33 | } |
| 34 | if let Some(nudge) = &self.calendar_nudge { |
| 35 | let _ = nudge.send(()); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | pub fn rebuild_calls_list(&mut self, cx: &mut Context<Self>) { |
| 40 | let now = Utc::now(); |
| 41 | let offset = Local::now().offset().fix(); |
| 42 | self.calls_list.rebuild( |
| 43 | &self.call_history, |
| 44 | &self.sidebar.chats, |
| 45 | now, |
| 46 | offset, |
| 47 | ); |
| 48 | cx.emit(AppEvent::CallHistory); |
| 49 | cx.notify(); |
| 50 | } |
| 51 | |
| 52 | pub fn set_call_history_filter(&mut self, filter: HistoryFilter, cx: &mut Context<Self>) { |
| 53 | self.calls_list.set_filter(filter); |
| 54 | self.rebuild_calls_list(cx); |
| 55 | } |
| 56 | |
| 57 | pub fn join_agenda_row(&mut self, row: &AgendaRow, cx: &mut Context<Self>) { |
| 58 | self.join_calendar_meeting(row.join_url.as_deref(), &row.subject, cx); |
| 59 | } |
| 60 | |
| 61 | pub fn join_calendar_meeting( |
| 62 | &mut self, |
| 63 | join_url: Option<&str>, |
| 64 | subject: &str, |
| 65 | cx: &mut Context<Self>, |
| 66 | ) { |
| 67 | if let Some(url) = join_url { |
| 68 | self.join_meeting_link(url, cx); |
| 69 | if let Some(pending) = self.prejoin.as_mut() { |
| 70 | pending.join.title = subject.to_owned(); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | pub fn open_agenda_chat(&mut self, row: &AgendaRow, cx: &mut Context<Self>) { |
| 76 | self.open_meeting_chat(row.thread_id.as_deref(), cx); |
| 77 | } |
| 78 | |
| 79 | pub fn open_meeting_chat(&mut self, thread_id: Option<&str>, cx: &mut Context<Self>) { |
| 80 | let Some(thread_id) = thread_id else { |
| 81 | return; |
| 82 | }; |
| 83 | match selection_for(&self.sidebar, thread_id) { |
| 84 | Some(selection @ Selection::Chat(_)) => self.select(selection, cx), |
| 85 | _ => self.raise_notice(NO_MEETING_CHAT_NOTICE.to_owned(), None, cx), |
| 86 | } |
| 87 | } |
| 88 | } |