| 1 | use std::sync::OnceLock; |
| 2 | |
| 3 | use chatsvc::CalendarEvent; |
| 4 | use chrono::{DateTime, 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 | // Agenda / reminders poll — must not replace calendar_week.events. |
| 21 | // That window is only ~today+7d; overwriting wiped earlier days in the |
| 22 | // visible week/month and made entries "vanish" after delete or poll. |
| 23 | self.calendar_events = events; |
| 24 | self.calls_list.invalidate(); |
| 25 | cx.emit(AppEvent::CallHistory); |
| 26 | cx.notify(); |
| 27 | } |
| 28 | |
| 29 | pub fn refresh_calendar(&mut self, cx: &mut Context<Self>) { |
| 30 | if self.mode.demo { |
| 31 | let mut events = demo_events(demo_anchor()); |
| 32 | events.extend(self.demo_meetings.iter().cloned()); |
| 33 | events.sort_by_key(|event| (event.start, event.end)); |
| 34 | self.apply_calendar(events, cx); |
| 35 | if self.calendar_week.start.is_some() { |
| 36 | self.load_calendar_week(cx); |
| 37 | } |
| 38 | return; |
| 39 | } |
| 40 | if let Some(nudge) = &self.calendar_nudge { |
| 41 | let _ = nudge.send(()); |
| 42 | } |
| 43 | // Visible grid SoT is load_calendar_week (full span + Google merge). |
| 44 | if self.calendar_week.start.is_some() { |
| 45 | self.load_calendar_week(cx); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | pub fn rebuild_calls_list(&mut self, cx: &mut Context<Self>) { |
| 50 | let now = Utc::now(); |
| 51 | self.calls_list.rebuild( |
| 52 | &self.call_history, |
| 53 | &self.sidebar.chats, |
| 54 | now, |
| 55 | app_base::format::Zone::Local, |
| 56 | ); |
| 57 | cx.emit(AppEvent::CallHistory); |
| 58 | cx.notify(); |
| 59 | } |
| 60 | |
| 61 | pub fn set_call_history_filter(&mut self, filter: HistoryFilter, cx: &mut Context<Self>) { |
| 62 | self.calls_list.set_filter(filter); |
| 63 | self.rebuild_calls_list(cx); |
| 64 | } |
| 65 | |
| 66 | pub fn join_agenda_row(&mut self, row: &AgendaRow, cx: &mut Context<Self>) { |
| 67 | self.join_calendar_meeting(row.join_url.as_deref(), &row.subject, cx); |
| 68 | } |
| 69 | |
| 70 | pub fn join_calendar_meeting( |
| 71 | &mut self, |
| 72 | join_url: Option<&str>, |
| 73 | subject: &str, |
| 74 | cx: &mut Context<Self>, |
| 75 | ) { |
| 76 | if let Some(url) = join_url { |
| 77 | self.join_meeting_link(url, cx); |
| 78 | if let Some(pending) = self.prejoin.as_mut() { |
| 79 | pending.join.title = subject.to_owned(); |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | pub fn open_agenda_chat(&mut self, row: &AgendaRow, cx: &mut Context<Self>) { |
| 85 | self.open_meeting_chat(row.thread_id.as_deref(), cx); |
| 86 | } |
| 87 | |
| 88 | pub fn open_meeting_chat(&mut self, thread_id: Option<&str>, cx: &mut Context<Self>) { |
| 89 | let Some(thread_id) = thread_id else { |
| 90 | return; |
| 91 | }; |
| 92 | match selection_for(&self.sidebar, thread_id) { |
| 93 | Some(selection @ Selection::Chat(_)) => self.select(selection, cx), |
| 94 | _ => self.raise_notice(NO_MEETING_CHAT_NOTICE.to_owned(), None, cx), |
| 95 | } |
| 96 | } |
| 97 | } |