added raw recurrence support
This commit is contained in:
@@ -25,6 +25,7 @@ export default function HomePage() {
|
||||
const [start, setStart] = useState('')
|
||||
const [end, setEnd] = useState('')
|
||||
const [allDay, setAllDay] = useState(false)
|
||||
const [recurrenceRule, setRecurrenceRule] = useState('')
|
||||
|
||||
// AI
|
||||
const [aiPrompt, setAiPrompt] = useState('')
|
||||
@@ -57,6 +58,7 @@ export default function HomePage() {
|
||||
description,
|
||||
location,
|
||||
url,
|
||||
recurrenceRule: recurrenceRule || undefined,
|
||||
start,
|
||||
end: end || undefined,
|
||||
allDay,
|
||||
@@ -105,7 +107,7 @@ export default function HomePage() {
|
||||
const url = URL.createObjectURL(blob)
|
||||
const a = document.createElement('a')
|
||||
a.href = url
|
||||
a.download = 'events.ics'
|
||||
a.download = `icallocal-export-${new Date().toLocaleTimeString()}.ics`
|
||||
document.body.appendChild(a)
|
||||
a.click()
|
||||
document.body.removeChild(a)
|
||||
@@ -153,6 +155,7 @@ export default function HomePage() {
|
||||
setAllDay(ev.allDay || false)
|
||||
setEditingId(null)
|
||||
setDialogOpen(true)
|
||||
setRecurrenceRule(ev.recurrenceRule || '')
|
||||
} else {
|
||||
// Save them all directly to DB
|
||||
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">
|
||||
<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">
|
||||
{ev.allDay ? ev.start.split('T')[0] : new Date(ev.start).toLocaleString()}
|
||||
{ev.location && <span> @ {ev.location}</span>}
|
||||
@@ -300,6 +308,11 @@ export default function HomePage() {
|
||||
value={description} onChange={e => setDescription(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="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">
|
||||
<input type="checkbox" checked={allDay} onChange={e => setAllDay(e.target.checked)} />
|
||||
All day event
|
||||
|
||||
@@ -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