fix ts errors

This commit is contained in:
2025-08-15 23:53:40 -04:00
parent 39c870998c
commit 3108053017
2 changed files with 56 additions and 20 deletions

29
src/lib/ical-helpers.ts Normal file
View File

@@ -0,0 +1,29 @@
import ICAL from "ical.js";
export function isRecur(val: unknown): val is ICAL.Recur {
return typeof val === "object" && val instanceof ICAL.Recur;
}
export function isTime(val: unknown): val is ICAL.Time {
return typeof val === "object" && val instanceof ICAL.Time;
}
// export function isGeo(val: unknown): val is ICAL.Geo {
// return typeof val === "object" && val instanceof ICAL.Geo;
// }
export function isUtcOffset(val: unknown): val is ICAL.UtcOffset {
return typeof val === "object" && val instanceof ICAL.UtcOffset;
}
export function isBinary(val: unknown): val is ICAL.Binary {
return typeof val === "object" && val instanceof ICAL.Binary;
}
export function isDuration(val: unknown): val is ICAL.Duration {
return typeof val === "object" && val instanceof ICAL.Duration;
}
export function isPeriod(val: unknown): val is ICAL.Period {
return typeof val === "object" && val instanceof ICAL.Period;
}

View File

@@ -1,5 +1,25 @@
import ICAL from "ical.js"; import ICAL from "ical.js";
import type { CalendarEvent } from "@/lib/types"; import type { CalendarEvent } from "@/lib/types";
import {
isRecur,
isTime,
isUtcOffset,
isBinary,
isDuration,
isPeriod,
} from "./ical-helpers";
function safeValueToString(
val: ReturnType<ICAL.Component["getFirstPropertyValue"]>,
): string | undefined {
if (val === undefined) return undefined;
if (typeof val === "string") return val;
if (isTime(val)) return val.toJSDate().toISOString();
if (isRecur(val)) return val.toString();
if (isUtcOffset(val)) return val.toString(); // already "±HHMM"
if (isBinary(val) || isDuration(val) || isPeriod(val)) return val.toString();
return undefined;
}
export function parseICS(icsString: string): CalendarEvent[] { export function parseICS(icsString: string): CalendarEvent[] {
const jcalData = ICAL.parse(icsString); const jcalData = ICAL.parse(icsString);
@@ -8,30 +28,19 @@ export function parseICS(icsString: string): CalendarEvent[] {
return vevents.map((v) => { return vevents.map((v) => {
const ev = new ICAL.Event(v); const ev = new ICAL.Event(v);
const isAllDay = ev.startDate.isDate;
return { return {
id: ev.uid || crypto.randomUUID(), id: ev.uid || crypto.randomUUID(),
title: ev.summary || "Untitled Event", title: ev.summary || "Untitled Event",
description: ev.description || "", description: ev.description || "",
location: ev.location || "", location: ev.location || "",
url: v.getFirstPropertyValue("url") || undefined, url: safeValueToString(v.getFirstPropertyValue("url")),
start: ev.startDate.toJSDate().toISOString(), start: ev.startDate.toJSDate().toISOString(),
end: ev.endDate ? ev.endDate.toJSDate().toISOString() : undefined, end: ev.endDate ? ev.endDate.toJSDate().toISOString() : undefined,
allDay: isAllDay, allDay: ev.startDate.isDate,
createdAt: v.getFirstPropertyValue("dtstamp") createdAt: safeValueToString(v.getFirstPropertyValue("dtstamp")),
? (v.getFirstPropertyValue("dtstamp") as ICAL.Time) lastModified: safeValueToString(v.getFirstPropertyValue("last-modified")),
.toJSDate() recurrenceRule: safeValueToString(v.getFirstPropertyValue("rrule")),
.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,
}; };
}); });
} }
@@ -50,7 +59,6 @@ export function generateICS(events: CalendarEvent[]): string {
if (ev.location) vevent.addPropertyWithValue("location", ev.location); if (ev.location) vevent.addPropertyWithValue("location", ev.location);
if (ev.url) vevent.addPropertyWithValue("url", ev.url); if (ev.url) vevent.addPropertyWithValue("url", ev.url);
// Start/End
if (ev.allDay) { if (ev.allDay) {
vevent.addPropertyWithValue( vevent.addPropertyWithValue(
"dtstart", "dtstart",
@@ -66,14 +74,14 @@ export function generateICS(events: CalendarEvent[]): string {
"dtstart", "dtstart",
ICAL.Time.fromJSDate(new Date(ev.start)), ICAL.Time.fromJSDate(new Date(ev.start)),
); );
if (ev.end) if (ev.end) {
vevent.addPropertyWithValue( vevent.addPropertyWithValue(
"dtend", "dtend",
ICAL.Time.fromJSDate(new Date(ev.end)), ICAL.Time.fromJSDate(new Date(ev.end)),
); );
}
} }
// Timestamps
vevent.addPropertyWithValue( vevent.addPropertyWithValue(
"dtstamp", "dtstamp",
ICAL.Time.fromJSDate(ev.createdAt ? new Date(ev.createdAt) : new Date()), ICAL.Time.fromJSDate(ev.createdAt ? new Date(ev.createdAt) : new Date()),
@@ -85,7 +93,6 @@ export function generateICS(events: CalendarEvent[]): string {
); );
} }
// Recurrence
if (ev.recurrenceRule) { if (ev.recurrenceRule) {
vevent.addPropertyWithValue( vevent.addPropertyWithValue(
"rrule", "rrule",