added raw recurrence support
This commit is contained in:
@@ -1,25 +1,6 @@
|
||||
import ICAL from "ical.js";
|
||||
import type { CalendarEvent } from "@/lib/types";
|
||||
|
||||
function valToString(val: unknown): string | undefined {
|
||||
if (!val) return undefined;
|
||||
if (typeof val === "string") return val;
|
||||
if ((val as any).toString) return (val as any).toString();
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function valToISOString(val: unknown): string | undefined {
|
||||
if (!val) return undefined;
|
||||
if (typeof val === "string") {
|
||||
const d = new Date(val);
|
||||
return isNaN(d.getTime()) ? undefined : d.toISOString();
|
||||
}
|
||||
if ((val as any).toJSDate) {
|
||||
return (val as any).toJSDate().toISOString();
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function parseICS(icsString: string): CalendarEvent[] {
|
||||
const jcalData = ICAL.parse(icsString);
|
||||
const comp = new ICAL.Component(jcalData);
|
||||
@@ -34,12 +15,23 @@ export function parseICS(icsString: string): CalendarEvent[] {
|
||||
title: ev.summary || "Untitled Event",
|
||||
description: ev.description || "",
|
||||
location: ev.location || "",
|
||||
url: valToString(v.getFirstPropertyValue("url")),
|
||||
url: v.getFirstPropertyValue("url") || undefined,
|
||||
start: ev.startDate.toJSDate().toISOString(),
|
||||
end: ev.endDate ? ev.endDate.toJSDate().toISOString() : undefined,
|
||||
allDay: isAllDay,
|
||||
createdAt: valToISOString(v.getFirstPropertyValue("dtstamp")),
|
||||
lastModified: valToISOString(v.getFirstPropertyValue("last-modified")),
|
||||
createdAt: v.getFirstPropertyValue("dtstamp")
|
||||
? (v.getFirstPropertyValue("dtstamp") as ICAL.Time)
|
||||
.toJSDate()
|
||||
.toISOString()
|
||||
: undefined,
|
||||
lastModified: v.getFirstPropertyValue("last-modified")
|
||||
? (v.getFirstPropertyValue("last-modified") as ICAL.Time)
|
||||
.toJSDate()
|
||||
.toISOString()
|
||||
: undefined,
|
||||
recurrenceRule: v.getFirstPropertyValue("rrule")
|
||||
? (v.getFirstPropertyValue("rrule") as ICAL.Recur).toString()
|
||||
: undefined,
|
||||
};
|
||||
});
|
||||
}
|
||||
@@ -58,30 +50,30 @@ export function generateICS(events: CalendarEvent[]): string {
|
||||
if (ev.location) vevent.addPropertyWithValue("location", ev.location);
|
||||
if (ev.url) vevent.addPropertyWithValue("url", ev.url);
|
||||
|
||||
// Start/End
|
||||
if (ev.allDay) {
|
||||
vevent.addPropertyWithValue(
|
||||
"dtstart",
|
||||
ICAL.Time.fromDateString(ev.start.split("T")[0]),
|
||||
);
|
||||
if (ev.end) {
|
||||
if (ev.end)
|
||||
vevent.addPropertyWithValue(
|
||||
"dtend",
|
||||
ICAL.Time.fromDateString(ev.end.split("T")[0]),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
vevent.addPropertyWithValue(
|
||||
"dtstart",
|
||||
ICAL.Time.fromJSDate(new Date(ev.start)),
|
||||
);
|
||||
if (ev.end) {
|
||||
if (ev.end)
|
||||
vevent.addPropertyWithValue(
|
||||
"dtend",
|
||||
ICAL.Time.fromJSDate(new Date(ev.end)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Timestamps
|
||||
vevent.addPropertyWithValue(
|
||||
"dtstamp",
|
||||
ICAL.Time.fromJSDate(ev.createdAt ? new Date(ev.createdAt) : new Date()),
|
||||
@@ -93,6 +85,14 @@ export function generateICS(events: CalendarEvent[]): string {
|
||||
);
|
||||
}
|
||||
|
||||
// Recurrence
|
||||
if (ev.recurrenceRule) {
|
||||
vevent.addPropertyWithValue(
|
||||
"rrule",
|
||||
ICAL.Recur.fromString(ev.recurrenceRule),
|
||||
);
|
||||
}
|
||||
|
||||
comp.addSubcomponent(vevent);
|
||||
});
|
||||
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
export type CalendarEvent = {
|
||||
id: string; // UID
|
||||
id: string;
|
||||
title: string;
|
||||
description?: string;
|
||||
location?: string;
|
||||
url?: string;
|
||||
start: string; // ISO datetime
|
||||
start: string;
|
||||
end?: string;
|
||||
allDay?: boolean;
|
||||
createdAt?: string;
|
||||
lastModified?: string;
|
||||
|
||||
recurrenceRule?: string;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user