added raw recurrence support

This commit is contained in:
2025-08-15 22:31:11 -04:00
parent 23b382c398
commit af94be7fff
3 changed files with 44 additions and 29 deletions

View File

@@ -25,6 +25,7 @@ export default function HomePage() {
const [start, setStart] = useState('') const [start, setStart] = useState('')
const [end, setEnd] = useState('') const [end, setEnd] = useState('')
const [allDay, setAllDay] = useState(false) const [allDay, setAllDay] = useState(false)
const [recurrenceRule, setRecurrenceRule] = useState('')
// AI // AI
const [aiPrompt, setAiPrompt] = useState('') const [aiPrompt, setAiPrompt] = useState('')
@@ -57,6 +58,7 @@ export default function HomePage() {
description, description,
location, location,
url, url,
recurrenceRule: recurrenceRule || undefined,
start, start,
end: end || undefined, end: end || undefined,
allDay, allDay,
@@ -105,7 +107,7 @@ export default function HomePage() {
const url = URL.createObjectURL(blob) const url = URL.createObjectURL(blob)
const a = document.createElement('a') const a = document.createElement('a')
a.href = url a.href = url
a.download = 'events.ics' a.download = `icallocal-export-${new Date().toLocaleTimeString()}.ics`
document.body.appendChild(a) document.body.appendChild(a)
a.click() a.click()
document.body.removeChild(a) document.body.removeChild(a)
@@ -153,6 +155,7 @@ export default function HomePage() {
setAllDay(ev.allDay || false) setAllDay(ev.allDay || false)
setEditingId(null) setEditingId(null)
setDialogOpen(true) setDialogOpen(true)
setRecurrenceRule(ev.recurrenceRule || '')
} else { } else {
// Save them all directly to DB // Save them all directly to DB
for (const ev of data) { for (const ev of data) {
@@ -265,6 +268,11 @@ export default function HomePage() {
<li key={ev.id} className="p-3 border rounded flex justify-between items-start"> <li key={ev.id} className="p-3 border rounded flex justify-between items-start">
<div> <div>
<div className="font-semibold">{ev.title}</div> <div className="font-semibold">{ev.title}</div>
{ev.recurrenceRule && (
<div className="text-xs text-blue-600 mt-1">
Repeats: {ev.recurrenceRule}
</div>
)}
<div className="text-sm text-gray-500"> <div className="text-sm text-gray-500">
{ev.allDay ? ev.start.split('T')[0] : new Date(ev.start).toLocaleString()} {ev.allDay ? ev.start.split('T')[0] : new Date(ev.start).toLocaleString()}
{ev.location && <span> @ {ev.location}</span>} {ev.location && <span> @ {ev.location}</span>}
@@ -300,6 +308,11 @@ export default function HomePage() {
value={description} onChange={e => setDescription(e.target.value)} /> value={description} onChange={e => setDescription(e.target.value)} />
<Input placeholder="Location" value={location} onChange={e => setLocation(e.target.value)} /> <Input placeholder="Location" value={location} onChange={e => setLocation(e.target.value)} />
<Input placeholder="URL" value={url} onChange={e => setUrl(e.target.value)} /> <Input placeholder="URL" value={url} onChange={e => setUrl(e.target.value)} />
<Input
placeholder="Recurrence rule (e.g. FREQ=WEEKLY;BYDAY=MO)"
value={recurrenceRule}
onChange={e => setRecurrenceRule(e.target.value)}
/>
<label className="flex items-center gap-2 mt-2"> <label className="flex items-center gap-2 mt-2">
<input type="checkbox" checked={allDay} onChange={e => setAllDay(e.target.checked)} /> <input type="checkbox" checked={allDay} onChange={e => setAllDay(e.target.checked)} />
All day event All day event

View File

@@ -1,25 +1,6 @@
import ICAL from "ical.js"; import ICAL from "ical.js";
import type { CalendarEvent } from "@/lib/types"; 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[] { export function parseICS(icsString: string): CalendarEvent[] {
const jcalData = ICAL.parse(icsString); const jcalData = ICAL.parse(icsString);
const comp = new ICAL.Component(jcalData); const comp = new ICAL.Component(jcalData);
@@ -34,12 +15,23 @@ export function parseICS(icsString: string): CalendarEvent[] {
title: ev.summary || "Untitled Event", title: ev.summary || "Untitled Event",
description: ev.description || "", description: ev.description || "",
location: ev.location || "", location: ev.location || "",
url: valToString(v.getFirstPropertyValue("url")), url: v.getFirstPropertyValue("url") || undefined,
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: isAllDay,
createdAt: valToISOString(v.getFirstPropertyValue("dtstamp")), createdAt: v.getFirstPropertyValue("dtstamp")
lastModified: valToISOString(v.getFirstPropertyValue("last-modified")), ? (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.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",
ICAL.Time.fromDateString(ev.start.split("T")[0]), ICAL.Time.fromDateString(ev.start.split("T")[0]),
); );
if (ev.end) { if (ev.end)
vevent.addPropertyWithValue( vevent.addPropertyWithValue(
"dtend", "dtend",
ICAL.Time.fromDateString(ev.end.split("T")[0]), ICAL.Time.fromDateString(ev.end.split("T")[0]),
); );
}
} else { } else {
vevent.addPropertyWithValue( vevent.addPropertyWithValue(
"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()),
@@ -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); comp.addSubcomponent(vevent);
}); });

View File

@@ -1,12 +1,14 @@
export type CalendarEvent = { export type CalendarEvent = {
id: string; // UID id: string;
title: string; title: string;
description?: string; description?: string;
location?: string; location?: string;
url?: string; url?: string;
start: string; // ISO datetime start: string;
end?: string; end?: string;
allDay?: boolean; allDay?: boolean;
createdAt?: string; createdAt?: string;
lastModified?: string; lastModified?: string;
recurrenceRule?: string;
}; };