import * as React from "react" import { dowToString } from "../lib/utils"; import type { Event, SecretsCalendar } from "../lib/interfaces"; import * as styles from "../styles/containers/Calendar.module.css" const CALENDAR_REFRESH_INTERVAL = 15 * 60 * 1000; const CALENDAR_TOKEN_REFRESH_INTERVAL = 45 * 60 * 1000; function daysDifference(date1: Date, date2: Date) { if (date1.getTime() > date2.getTime()) { let tmp = date2; date2 = date1; date1 = tmp; } return Math.ceil((date2.getTime() - date1.getTime()) / 1000 / 60 / 60 / 24); } const Calendar = ({ secrets }: { secrets: SecretsCalendar }) => { const [token, setToken] = React.useState("") const [events, setEvents] = React.useState([]) React.useEffect(() => { requestToken().then(pullCalendar) const calendarInterval = setInterval(pullCalendar, CALENDAR_REFRESH_INTERVAL); const calendarTokenInterval = setInterval(requestToken, CALENDAR_TOKEN_REFRESH_INTERVAL); return () => { clearInterval(calendarInterval); clearInterval(calendarTokenInterval); } }, []) const processEventData = (events: Event[]) => { const eventTable = []; let lastDate = ""; let i = 0; for (const event of events) { const startDate = new Date(event.start.date ? event.start.date : event.start.dateTime) const startDateString = `${startDate.getFullYear()}${startDate.getMonth()}${startDate.getDate()}` if (startDateString !== lastDate) { lastDate = startDateString; const dayDiff = daysDifference(new Date(), startDate); let dayDiffString: string; switch (dayDiff) { case 0: dayDiffString = "Heute" break; case 1: dayDiffString = "Morgen" break; default: dayDiffString = `${dayDiff} Tage`; break; } eventTable.push(