Skip to content
Open
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
30 changes: 28 additions & 2 deletions apps/next/src/components/ui/event-dialog-content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
eventDateRangeToString,
eventTimeRangeToString,
generateGCalLink,
generateICSDownload,
timeToString,
isUpcomingEvent,
isValidDate,
toTitleCase,
Expand Down Expand Up @@ -88,9 +90,11 @@ export default function EventDialogContent({
>
{props.description}
</Typography>

{isValidDate(props.start) && isUpcomingEvent(props) && (
<div className="w-full flex items-center justify-center pt-4">
<div className="w-full flex flex-col items-center justify-center gap-2 pt-4">
<a
className="block w-full max-w-xs"
href={generateGCalLink(
props.title,
props.description,
Expand All @@ -104,12 +108,34 @@ export default function EventDialogContent({
>
<Button
variant="contained"
className="[&_svg]:size-5 whitespace-nowrap"
className="w-full [&_svg]:size-5 whitespace-nowrap"
startIcon={<CalendarTodayOutlinedIcon />}
>
Add to Google Calendar
</Button>
</a>

<a
className="block w-full max-w-xs"
download={`${props.restaurantId}-${props.title.toLowerCase()}.ics`}
href={generateICSDownload(
props.title,
props.description,
props.restaurantId === "brandywine"
? HallEnum.BRANDYWINE
: HallEnum.ANTEATERY,
props.start,
props.end,
)}
>
<Button
variant="contained"
className="w-full [&_svg]:size-5 whitespace-nowrap"
startIcon={<CalendarTodayOutlinedIcon />}
>
Add to Apple Calendar
</Button>
</a>
</div>
)}
</DialogContent>
Expand Down
44 changes: 44 additions & 0 deletions apps/next/src/utils/funcs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,49 @@ function generateGCalLink(
return link;
}

/**
* Generates an object URL for an ICS calendar event.
* @param title The title of the event (usually the dish name).
* @param desc The description for the event.
* @param location The dining hall location (Anteatery or Brandywine).
* @param startTime The starting date and time of the event.
* @param endTime The ending date and time of the event.
* @returns An object URL for downloading the ICS file.
*/
function generateICSDownload(
title: string,
desc: string,
location: HallEnum,
startTime: Date,
endTime: Date,
) {
const formatDate = (date: Date) =>
date.toISOString().replace(/[-:]/g, "").split(".")[0] + "Z";

const escapeText = (str = "") =>
str
.replace(/\\/g, "\\\\")
.replace(/;/g, "\\;")
.replace(/,/g, "\\,")
.replace(/\n/g, "\\n");

const icsContent = [
"BEGIN:VCALENDAR",
"VERSION:2.0",
"BEGIN:VEVENT",
`DTSTART:${formatDate(startTime)}`,
`DTEND:${formatDate(endTime)}`,
`SUMMARY:${escapeText(title)}`,
`DESCRIPTION:${escapeText(desc)}`,
`LOCATION:${escapeText(decodeURIComponent((location === HallEnum.ANTEATERY ? ANTEAT_ADDY : BWINE_ADDY).replaceAll("+", " ")))}`,
"END:VEVENT",
"END:VCALENDAR",
].join("\r\n");

const blob = new Blob([icsContent], { type: "text/calendar;charset=utf-8" });
return URL.createObjectURL(blob);
}

/**
* Converts a UTC time string (HH:MM:SS) to a Date object in Pacific Time.
* The date part of the returned Date object will be the current date.
Expand Down Expand Up @@ -557,6 +600,7 @@ export {
toTitleCase,
dateRangeToString,
generateGCalLink,
generateICSDownload,
timeToString,
enhanceDescription,
utcToPacificTime,
Expand Down
Loading