Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix rrules, exclude events on holidays, mondays on tuesdays #138

Merged
merged 1 commit into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
"@react-oauth/google": "^0.2.8",
"ag-grid-react": "^33.0.4",
"html-entities": "^2.5.2",
"ical-generator": "^6.0.1",
"ical-generator": "^8.1.1",
"msgpack-lite": "^0.1.26",
"nanoid": "^3.3.8",
"next-themes": "^0.4.4",
"react": "19.0.0",
"react-dom": "19.0.0",
"react-icons": "^5.4.0",
"react-use": "^17.6.0",
"rrule": "^2.8.1",
"timezones-ical-library": "^1.9.1"
},
"scripts": {
Expand Down
9 changes: 8 additions & 1 deletion src/lib/dates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,14 @@ export class Term {
const res = this.holidays.filter((date) => date.getDay() === slot.weekday);
// ex dates can't be empty, so add an extra one:
res.push(new Date("2000-01-01"));
return res.map((date) => slot.onDate(date));
const resDates = res.map((date) => slot.onDate(date));

// remove the tuesday for monday schedule
if (slot.weekday === 2 && this.mondaySchedule) {
resDates.push(slot.onDate(this.mondaySchedule));
}

return resDates;
}

/** An extra date a given slot would fall on, if it exists. */
Expand Down
58 changes: 37 additions & 21 deletions src/lib/gapi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useGoogleLogin } from "@react-oauth/google";
import { ICalCalendar, ICalEventData } from "ical-generator";
import { RRule, RRuleSet } from "rrule";
import { tzlib_get_ical_block } from "timezones-ical-library";

import { Activity } from "./activity";
Expand Down Expand Up @@ -45,13 +46,6 @@ function download(filename: string, text: string) {
document.body.removeChild(element);
}

/** Returns a date as an RRULE string without a timezone. */
function toRRuleString(date: Date): string {
return Array.from(toISOString(date))
.filter((char) => char !== "-" && char !== ":")
.join("");
}

/** Return a list of events for an activity that happen on a given term. */
function toGoogleCalendarEvents(
activity: Activity,
Expand All @@ -73,18 +67,29 @@ function toGoogleCalendarEvents(
const exDates = term.exDatesFor(slot.startSlot);
const rDate = term.rDateFor(slot.startSlot);

const rrule = new RRule({
freq: RRule.WEEKLY,
until: endDate,
});

const rruleSet = new RRuleSet();
rruleSet.rrule(rrule);

for (const exdate of exDates) {
rruleSet.exdate(exdate);
}

if (rDate) {
rruleSet.rdate(rDate);
}

return {
summary: event.name,
location: event.room,
start: { dateTime: toISOString(startDate), timeZone: TIMEZONE },
end: { dateTime: toISOString(startDateEnd), timeZone: TIMEZONE },
recurrence: [
// for some reason, gcal wants UNTIL to be a date, not time
`RRULE:FREQ=WEEKLY;UNTIL=${toRRuleString(endDate).split("T")[0]}`,
`EXDATE;TZID=${TIMEZONE}:${exDates.map(toRRuleString).join(",")}`,
rDate && `RDATE;TZID=${TIMEZONE}:${toRRuleString(rDate)}`,
].filter((t): t is string => t !== undefined),
};
recurrence: rruleSet.valueOf(),
} satisfies gapi.client.calendar.Event;
}),
);
}
Expand All @@ -106,19 +111,30 @@ function toICalEvents(activity: Activity, term: Term): Array<ICalEventData> {
const exDates = term.exDatesFor(slot.startSlot);
const rDate = term.rDateFor(slot.startSlot);

const rrule = new RRule({
freq: RRule.WEEKLY,
until: endDate,
});

const rruleSet = new RRuleSet();
rruleSet.rrule(rrule);

for (const exdate of exDates) {
rruleSet.exdate(exdate);
}

if (rDate) {
rruleSet.rdate(rDate);
}

return {
summary: event.name,
location: event.room,
start: startDate,
end: startDateEnd,
timezone: TIMEZONE,
repeating: [
// for some reason, gcal wants UNTIL to be a date, not time
`FREQ=WEEKLY;UNTIL=${toRRuleString(endDate).split("T")[0]}`,
`EXDATE;TZID=${TIMEZONE}:${exDates.map(toRRuleString).join(",")}`,
rDate && `RDATE;TZID=${TIMEZONE}:${toRRuleString(rDate)}`,
].filter((t): t is string => t !== undefined)[0],
};
repeating: rruleSet,
} satisfies ICalEventData;
}),
);
}
Expand Down