2023-11-25 16:17:25 +00:00
|
|
|
import { DateTime } from "luxon";
|
|
|
|
import { parseString } from "cal-parser";
|
|
|
|
import { useEffect } from "react";
|
|
|
|
import { useTranslation } from "next-i18next";
|
2023-12-10 15:19:43 +00:00
|
|
|
import { RRule } from "rrule";
|
2023-11-25 16:17:25 +00:00
|
|
|
|
|
|
|
import useWidgetAPI from "../../../utils/proxy/use-widget-api";
|
|
|
|
import Error from "../../../components/services/widget/error";
|
|
|
|
|
|
|
|
export default function Integration({ config, params, setEvents, hideErrors }) {
|
|
|
|
const { t } = useTranslation();
|
|
|
|
const { data: icalData, error: icalError } = useWidgetAPI(config, config.name, {
|
|
|
|
refreshInterval: 300000, // 5 minutes
|
|
|
|
});
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
let parsedIcal;
|
|
|
|
|
|
|
|
if (!icalError && icalData && !icalData.error) {
|
|
|
|
parsedIcal = parseString(icalData.data);
|
|
|
|
if (parsedIcal.events.length === 0) {
|
|
|
|
icalData.error = { message: `'${config.name}': ${t("calendar.noEventsFound")}` };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 02:01:10 +00:00
|
|
|
const zone = config?.timezone || null;
|
|
|
|
const startDate = DateTime.fromISO(params.start, { zone });
|
|
|
|
const endDate = DateTime.fromISO(params.end, { zone });
|
2023-12-10 15:19:43 +00:00
|
|
|
|
|
|
|
if (icalError || !parsedIcal || !startDate.isValid || !endDate.isValid) {
|
2023-11-25 16:17:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const eventsToAdd = {};
|
2023-12-10 15:19:43 +00:00
|
|
|
const events = parsedIcal?.getEventsBetweenDates(startDate.toJSDate(), endDate.toJSDate());
|
2023-11-25 16:17:25 +00:00
|
|
|
|
|
|
|
events?.forEach((event) => {
|
|
|
|
let title = `${event?.summary?.value}`;
|
|
|
|
if (config?.params?.showName) {
|
|
|
|
title = `${config.name}: ${title}`;
|
|
|
|
}
|
|
|
|
|
2023-12-10 15:19:43 +00:00
|
|
|
const eventToAdd = (date, i, type) => {
|
|
|
|
const duration = event.dtend.value - event.dtstart.value;
|
|
|
|
const days = duration / (1000 * 60 * 60 * 24);
|
|
|
|
|
2024-01-15 02:01:10 +00:00
|
|
|
const now = DateTime.now().setZone(zone);
|
|
|
|
const eventDate = DateTime.fromJSDate(date, { zone });
|
|
|
|
|
2023-12-10 15:19:43 +00:00
|
|
|
for (let j = 0; j < days; j += 1) {
|
|
|
|
eventsToAdd[`${event?.uid?.value}${i}${j}${type}`] = {
|
|
|
|
title,
|
2024-01-15 02:01:10 +00:00
|
|
|
date: eventDate.plus({ days: j }),
|
2023-12-10 15:19:43 +00:00
|
|
|
color: config?.color ?? "zinc",
|
2024-01-15 02:01:10 +00:00
|
|
|
isCompleted: eventDate < now,
|
2023-12-10 15:19:43 +00:00
|
|
|
additional: event.location?.value,
|
|
|
|
type: "ical",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-01-14 21:49:28 +00:00
|
|
|
const recurrenceOptions = event?.recurrenceRule?.origOptions;
|
|
|
|
if (recurrenceOptions && Object.keys(recurrenceOptions).length !== 0) {
|
|
|
|
const rule = new RRule(recurrenceOptions);
|
2023-12-10 15:19:43 +00:00
|
|
|
const recurringEvents = rule.between(startDate.toJSDate(), endDate.toJSDate());
|
|
|
|
|
|
|
|
recurringEvents.forEach((date, i) => eventToAdd(date, i, "recurring"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
event.matchingDates.forEach((date, i) => eventToAdd(date, i, "single"));
|
2023-11-25 16:17:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
setEvents((prevEvents) => ({ ...prevEvents, ...eventsToAdd }));
|
|
|
|
}, [icalData, icalError, config, params, setEvents, t]);
|
|
|
|
|
|
|
|
const error = icalError ?? icalData?.error;
|
|
|
|
return error && !hideErrors && <Error error={{ message: `${config.type}: ${error.message ?? error}` }} />;
|
|
|
|
}
|