phase 3 - ical import/export

This commit is contained in:
old4ever committed 2025-08-16 01:54:30 -04:00
1 parent b1dad2f6ba
commit 9d53e4da21
4 files changed
+101 -4

No files matched your search

+43
View File
@@ -0,0 +1,43 @@
import ICAL from "ical.js";
import type { CalendarEvent } from "@/lib/types";
export function parseICS(icsString: string): CalendarEvent[] {
const jcalData = ICAL.parse(icsString);
const comp = new ICAL.Component(jcalData);
const vevents = comp.getAllSubcomponents("vevent");
return vevents.map((v) => {
const ev = new ICAL.Event(v);
return {
id: ev.uid || crypto.randomUUID(),
title: ev.summary || "Untitled Event",
start: ev.startDate.toJSDate().toISOString().split("T")[0],
end: ev.endDate
? ev.endDate.toJSDate().toISOString().split("T")[0]
: undefined,
description: ev.description || "",
};
});
}
export function generateICS(events: CalendarEvent[]): string {
const comp = new ICAL.Component(["vcalendar", [], []]);
comp.addPropertyWithValue("version", "2.0");
comp.addPropertyWithValue("prodid", "-//YourAppName//EN");
events.forEach((ev) => {
const vevent = new ICAL.Component("vevent");
vevent.addPropertyWithValue("uid", ev.id);
vevent.addPropertyWithValue("summary", ev.title);
vevent.addPropertyWithValue("dtstart", ICAL.Time.fromDateString(ev.start));
if (ev.end) {
vevent.addPropertyWithValue("dtend", ICAL.Time.fromDateString(ev.end));
}
if (ev.description) {
vevent.addPropertyWithValue("description", ev.description);
}
comp.addSubcomponent(vevent);
});
return comp.toString();
}