@moltenship/calendar 1.0.0-rc.2 → 2.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -4
- package/dist/index.d.mts +2 -2
- package/docs/accessibility.md +304 -0
- package/docs/agents/changes.md +449 -0
- package/docs/agents/coding-agents.md +117 -0
- package/docs/agents/connect.md +435 -0
- package/docs/agents/deleting.md +296 -0
- package/docs/agents/reference.md +512 -0
- package/docs/agents/server.md +317 -0
- package/docs/agents/webmcp.md +153 -0
- package/docs/agents.md +353 -0
- package/docs/annotations.md +196 -0
- package/docs/api/changes.md +248 -0
- package/docs/api/dnd-kit.md +267 -0
- package/docs/api/events.md +152 -0
- package/docs/api/ics.md +524 -0
- package/docs/api/primitives.md +168 -0
- package/docs/api/react.md +236 -0
- package/docs/api/recurrence.md +586 -0
- package/docs/api/solid.md +460 -0
- package/docs/api/store.md +92 -0
- package/docs/api/temporal.md +172 -0
- package/docs/api.md +57 -0
- package/docs/availability.md +573 -0
- package/docs/comparison.md +279 -0
- package/docs/concepts/anatomy.md +73 -0
- package/docs/concepts/events.md +164 -0
- package/docs/concepts/how-it-works.md +95 -0
- package/docs/concepts/recurrence.md +127 -0
- package/docs/concepts/state.md +244 -0
- package/docs/concepts/time.md +170 -0
- package/docs/guides/dnd-kit.md +333 -0
- package/docs/guides/google-calendar-sync.md +1246 -0
- package/docs/guides/google-calendar.md +367 -0
- package/docs/guides/ics.md +486 -0
- package/docs/guides/motion.md +414 -0
- package/docs/guides/nextjs-app-router.md +630 -0
- package/docs/guides/nextjs.md +149 -0
- package/docs/guides/performance.md +103 -0
- package/docs/guides/recurring-events.md +583 -0
- package/docs/guides/tanstack-query.md +485 -0
- package/docs/index.md +118 -0
- package/docs/installation.md +194 -0
- package/docs/interactions.md +408 -0
- package/docs/llms.txt +86 -0
- package/docs/migrating-to-2.md +322 -0
- package/docs/quick-start.md +159 -0
- package/docs/stylex.md +265 -0
- package/docs/testing.md +581 -0
- package/docs/theme-builder.md +81 -0
- package/docs/theming.md +169 -0
- package/docs/undo-redo.md +339 -0
- package/docs/views/agenda.md +221 -0
- package/docs/views/custom-views.md +192 -0
- package/docs/views/month.md +109 -0
- package/docs/views/resource-columns.md +261 -0
- package/docs/views/resource-timeline.md +225 -0
- package/docs/views/scrolling.md +192 -0
- package/docs/views/week-and-day.md +257 -0
- package/docs/views/year.md +138 -0
- package/docs/views.md +69 -0
- package/package.json +41 -14
- package/skills/moltenship-time/SKILL.md +75 -0
- package/skills/moltenship-time/references/agent-ready.md +272 -0
- package/skills/moltenship-time/references/changes.md +188 -0
- package/skills/moltenship-time/references/compose.md +168 -0
- package/skills/moltenship-time/references/mistakes.md +98 -0
- package/skills/moltenship-time/references/solid.md +202 -0
- package/skills/moltenship-time/references/state.md +129 -0
- package/skills/moltenship-time/references/testing.md +176 -0
|
@@ -0,0 +1,486 @@
|
|
|
1
|
+
# Show an ICS feed
|
|
2
|
+
|
|
3
|
+
Parse a Google, Outlook or iCloud feed on the server and show its events in the calendar.
|
|
4
|
+
|
|
5
|
+
Source: https://time.molten.sh/docs/guides/ics
|
|
6
|
+
|
|
7
|
+
> Live example: see it at https://time.molten.sh/docs/guides/ics
|
|
8
|
+
|
|
9
|
+
These events come from an ICS feed, the iCalendar text that Google Calendar, Outlook and iCloud
|
|
10
|
+
publish. The feed was written for this week, parsed in your browser with `@moltenship/ics`, and
|
|
11
|
+
shown in your time zone. Each title says where its time came from: a New York time zone, an Outlook
|
|
12
|
+
zone name, UTC, a date, or a floating time with no zone at all. Open the source to compare.
|
|
13
|
+
|
|
14
|
+
This guide does the same in a Next.js app: the server fetches and parses the feed, and the browser
|
|
15
|
+
shows it. The [Next.js guide](./nextjs.md) covers the rules for server rendering that apply
|
|
16
|
+
here too.
|
|
17
|
+
|
|
18
|
+
## Install
|
|
19
|
+
|
|
20
|
+
The parser is a separate package. It reads iCalendar text into Temporal values and doesn't depend
|
|
21
|
+
on the calendar, so it works on its own too.
|
|
22
|
+
|
|
23
|
+
Like the calendar, `@moltenship/ics` is on npm as a release candidate for 1.0.0.
|
|
24
|
+
|
|
25
|
+
**pnpm**
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pnpm add @moltenship/ics
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
**npm**
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
npm install @moltenship/ics
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
**yarn**
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
yarn add @moltenship/ics
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
**bun**
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
bun add @moltenship/ics
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
It needs `Temporal`, as the calendar does: Node.js 26 has it, and the browser may need the
|
|
50
|
+
[polyfill](../installation.md#temporal).
|
|
51
|
+
|
|
52
|
+
## Fetch and parse on the server
|
|
53
|
+
|
|
54
|
+
A Server Component fetches the feed, parses it with `parseIcs`, and maps the events to plain JSON
|
|
55
|
+
for the client:
|
|
56
|
+
|
|
57
|
+
```tsx title="page.tsx"
|
|
58
|
+
// app/team/page.tsx (a Server Component)
|
|
59
|
+
import { parseIcs } from "@moltenship/ics";
|
|
60
|
+
import { cacheLife } from "next/cache";
|
|
61
|
+
import { connection } from "next/server";
|
|
62
|
+
import { Suspense } from "react";
|
|
63
|
+
|
|
64
|
+
import { FeedCalendar } from "./feed-calendar";
|
|
65
|
+
import { toFeedEvents, type FeedEventJson } from "./feed-events";
|
|
66
|
+
|
|
67
|
+
const feedUrl = "https://calendar.example.com/team/basic.ics";
|
|
68
|
+
|
|
69
|
+
async function getFeedEvents(timeZone: string): Promise<readonly FeedEventJson[]> {
|
|
70
|
+
"use cache";
|
|
71
|
+
cacheLife("hours"); // fetch the feed again at most once an hour
|
|
72
|
+
|
|
73
|
+
const response = await fetch(feedUrl);
|
|
74
|
+
if (!response.ok) throw new Error(`The feed answered ${response.status}`);
|
|
75
|
+
|
|
76
|
+
const { calendars, issues } = parseIcs(await response.text());
|
|
77
|
+
for (const issue of issues) {
|
|
78
|
+
console.warn(`${feedUrl}, line ${issue.line ?? "?"}: ${issue.message}`);
|
|
79
|
+
}
|
|
80
|
+
return calendars.flatMap((calendar) => toFeedEvents(calendar, timeZone));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export default function TeamPage() {
|
|
84
|
+
return (
|
|
85
|
+
<Suspense fallback={<p>Loading the team calendar…</p>}>
|
|
86
|
+
<TeamFeed />
|
|
87
|
+
</Suspense>
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async function TeamFeed() {
|
|
92
|
+
await connection(); // render on each request, for a fresh `now`
|
|
93
|
+
|
|
94
|
+
// The viewer's time zone, for example from their profile or a cookie.
|
|
95
|
+
const timeZone = "Europe/Berlin";
|
|
96
|
+
const events = await getFeedEvents(timeZone);
|
|
97
|
+
|
|
98
|
+
return (
|
|
99
|
+
<FeedCalendar events={events} now={Temporal.Now.instant().toString()} timeZone={timeZone} />
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
- **Caching.** Feeds change slowly, and providers limit how often you fetch them. `"use cache"`
|
|
105
|
+
with `cacheLife("hours")` keeps the result for an hour. The time zone is an argument, so each
|
|
106
|
+
time zone gets its own entry. Without
|
|
107
|
+
[Cache Components](https://nextjs.org/docs/app/getting-started/caching), pass
|
|
108
|
+
`{ next: { revalidate: 3600 } }` to `fetch` instead.
|
|
109
|
+
- **Issues.** `parseIcs` doesn't throw on bad data. It keeps what it can and lists each problem in
|
|
110
|
+
`issues`, with its line. The page logs them; see [Issues](#issues) for more.
|
|
111
|
+
- **The current time.** `connection()` renders the calendar on each request, so `now` is today
|
|
112
|
+
rather than the day you built the app. That makes it request-time data, so it sits in a
|
|
113
|
+
`<Suspense>` boundary: the rest of the page still prerenders, and the build doesn't stop on it.
|
|
114
|
+
|
|
115
|
+
Temporal values can't cross from a Server Component to a Client Component, so `toFeedEvents` returns
|
|
116
|
+
ISO 8601 strings. The client turns them back into Temporal values:
|
|
117
|
+
|
|
118
|
+
```tsx title="feed-calendar.tsx"
|
|
119
|
+
// app/team/feed-calendar.tsx
|
|
120
|
+
"use client";
|
|
121
|
+
|
|
122
|
+
import { createCalendar, defineEventAdapter } from "@moltenship/calendar";
|
|
123
|
+
import { useMemo } from "react";
|
|
124
|
+
|
|
125
|
+
import { CalendarToolbar } from "@/components/calendar/calendar-toolbar";
|
|
126
|
+
import type { CalendarEventColor } from "@/components/calendar/event-colors";
|
|
127
|
+
import { createWeekView } from "@/components/calendar/time-grid-view/time-grid-view";
|
|
128
|
+
|
|
129
|
+
import type { FeedEventJson } from "./feed-events";
|
|
130
|
+
|
|
131
|
+
export type FeedEvent = {
|
|
132
|
+
readonly id: string;
|
|
133
|
+
readonly title: string;
|
|
134
|
+
readonly color: CalendarEventColor;
|
|
135
|
+
} & (
|
|
136
|
+
| { readonly kind: "timed"; readonly start: Temporal.Instant; readonly end: Temporal.Instant }
|
|
137
|
+
| {
|
|
138
|
+
readonly kind: "all-day";
|
|
139
|
+
readonly start: Temporal.PlainDate;
|
|
140
|
+
readonly end: Temporal.PlainDate;
|
|
141
|
+
}
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
/** Turns the strings from the server back into Temporal values. */
|
|
145
|
+
export function fromFeedJson({ kind, start, end, ...event }: FeedEventJson): FeedEvent {
|
|
146
|
+
return kind === "timed"
|
|
147
|
+
? { ...event, kind, start: Temporal.Instant.from(start), end: Temporal.Instant.from(end) }
|
|
148
|
+
: { ...event, kind, start: Temporal.PlainDate.from(start), end: Temporal.PlainDate.from(end) };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const Calendar = createCalendar<FeedEvent>()({
|
|
152
|
+
eventAdapter: defineEventAdapter<FeedEvent>()({
|
|
153
|
+
getId: (event) => event.id,
|
|
154
|
+
getTitle: (event) => event.title,
|
|
155
|
+
// A FeedEvent is already an interval: its kind, start and end.
|
|
156
|
+
getInterval: (event) => event,
|
|
157
|
+
getResourceIds: () => [],
|
|
158
|
+
}),
|
|
159
|
+
views: [createWeekView<FeedEvent>({ getEventColor: (event) => event.color })],
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
const weekRules = { firstDay: 1, minimalDays: 4, weekend: [6, 7] } as const;
|
|
163
|
+
|
|
164
|
+
interface FeedCalendarProps {
|
|
165
|
+
readonly events: readonly FeedEventJson[];
|
|
166
|
+
readonly now: string;
|
|
167
|
+
readonly timeZone: string;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export function FeedCalendar({ events, now, timeZone }: FeedCalendarProps) {
|
|
171
|
+
const feedEvents = useMemo(() => events.map(fromFeedJson), [events]);
|
|
172
|
+
const nowInstant = Temporal.Instant.from(now);
|
|
173
|
+
|
|
174
|
+
return (
|
|
175
|
+
<Calendar.Root
|
|
176
|
+
events={feedEvents}
|
|
177
|
+
now={nowInstant}
|
|
178
|
+
defaultAnchorDate={nowInstant.toZonedDateTimeISO(timeZone).toPlainDate()}
|
|
179
|
+
defaultTimeZone={timeZone}
|
|
180
|
+
locale="en-US"
|
|
181
|
+
weekRules={weekRules}
|
|
182
|
+
>
|
|
183
|
+
<Calendar.Frame className="flex h-[640px] flex-col rounded-xl border">
|
|
184
|
+
<CalendarToolbar calendar={Calendar} />
|
|
185
|
+
<Calendar.Viewport className="min-h-0 flex-1">
|
|
186
|
+
<Calendar.ActiveView />
|
|
187
|
+
</Calendar.Viewport>
|
|
188
|
+
</Calendar.Frame>
|
|
189
|
+
</Calendar.Root>
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
## Map ICS events to calendar events
|
|
195
|
+
|
|
196
|
+
ICS writes times in five ways, and the parser keeps them apart as `start.kind`. `toFeedEvent` turns
|
|
197
|
+
each into an event the calendar can show:
|
|
198
|
+
|
|
199
|
+
```ts title="feed-events.ts"
|
|
200
|
+
// app/team/feed-events.ts
|
|
201
|
+
import { getEventEnd, type IcsCalendar, type IcsDateValue, type IcsEvent } from "@moltenship/ics";
|
|
202
|
+
|
|
203
|
+
import type { CalendarEventColor } from "@/components/calendar/event-colors";
|
|
204
|
+
|
|
205
|
+
/** A feed event as plain JSON, ready to pass from a Server Component to a Client Component. */
|
|
206
|
+
export interface FeedEventJson {
|
|
207
|
+
readonly id: string;
|
|
208
|
+
readonly title: string;
|
|
209
|
+
readonly color: CalendarEventColor;
|
|
210
|
+
readonly kind: "timed" | "all-day";
|
|
211
|
+
/** An instant, such as "2026-10-12T07:00:00Z", or a date for an all-day event. */
|
|
212
|
+
readonly start: string;
|
|
213
|
+
readonly end: string;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// The calendar needs an end after the start; ICS allows events that take no time.
|
|
217
|
+
const minimumLength = { minutes: 30 };
|
|
218
|
+
|
|
219
|
+
/** The zone for floating and unresolved times: the feed's X-WR-TIMEZONE, else the viewer's. */
|
|
220
|
+
export function feedTimeZone(calendar: IcsCalendar, viewerTimeZone: string): string {
|
|
221
|
+
return calendar.timeZone ?? viewerTimeZone;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function toInstant(value: IcsDateValue, timeZone: string): Temporal.Instant {
|
|
225
|
+
switch (value.kind) {
|
|
226
|
+
case "utc":
|
|
227
|
+
return value.instant;
|
|
228
|
+
case "zoned":
|
|
229
|
+
return value.zoned.toInstant();
|
|
230
|
+
case "date":
|
|
231
|
+
return value.date.toZonedDateTime(timeZone).toInstant();
|
|
232
|
+
case "floating":
|
|
233
|
+
case "unresolved":
|
|
234
|
+
return value.wallTime.toZonedDateTime(timeZone).toInstant();
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** One event for the calendar, or `undefined` for a cancelled one. */
|
|
239
|
+
export function toFeedEvent(event: IcsEvent, timeZone: string): FeedEventJson | undefined {
|
|
240
|
+
if (event.status === "CANCELLED") return undefined;
|
|
241
|
+
|
|
242
|
+
const end = getEventEnd(event); // DTEND, else DTSTART + DURATION, in the start's kind
|
|
243
|
+
let times: Pick<FeedEventJson, "kind" | "start" | "end">;
|
|
244
|
+
if (event.start.kind === "date") {
|
|
245
|
+
const start = event.start.date;
|
|
246
|
+
const endDate = end.kind === "date" ? end.date : start;
|
|
247
|
+
const isAfter = Temporal.PlainDate.compare(endDate, start) > 0;
|
|
248
|
+
times = {
|
|
249
|
+
kind: "all-day",
|
|
250
|
+
start: start.toString(),
|
|
251
|
+
end: (isAfter ? endDate : start.add({ days: 1 })).toString(),
|
|
252
|
+
};
|
|
253
|
+
} else {
|
|
254
|
+
const start = toInstant(event.start, timeZone);
|
|
255
|
+
// A DTEND in UTC or a time zone keeps its instant, even after a floating start.
|
|
256
|
+
const absoluteEnd = event.end?.kind === "utc" || event.end?.kind === "zoned";
|
|
257
|
+
const endInstant = toInstant(absoluteEnd ? event.end! : end, timeZone);
|
|
258
|
+
const isAfter = Temporal.Instant.compare(endInstant, start) > 0;
|
|
259
|
+
times = {
|
|
260
|
+
kind: "timed",
|
|
261
|
+
start: start.toString(),
|
|
262
|
+
end: (isAfter ? endInstant : start.add(minimumLength)).toString(),
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
return {
|
|
267
|
+
// An override (RECURRENCE-ID) shares its series' UID, so its start keeps the ID unique.
|
|
268
|
+
id: event.recurrenceId === undefined ? event.uid : `${event.uid}/${times.start}`,
|
|
269
|
+
title: event.summary ?? "Busy",
|
|
270
|
+
// Free time (TRANSP:TRANSPARENT) and tentative events are gray, the rest blue.
|
|
271
|
+
color: event.transparency === "TRANSPARENT" || event.status === "TENTATIVE" ? "gray" : "blue",
|
|
272
|
+
...times,
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/** Every event of a parsed calendar, ready for the client. */
|
|
277
|
+
export function toFeedEvents(
|
|
278
|
+
calendar: IcsCalendar,
|
|
279
|
+
viewerTimeZone: string,
|
|
280
|
+
): readonly FeedEventJson[] {
|
|
281
|
+
const timeZone = feedTimeZone(calendar, viewerTimeZone);
|
|
282
|
+
return calendar.events.flatMap((event) => toFeedEvent(event, timeZone) ?? []);
|
|
283
|
+
}
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
| `kind` | Written as | Shown as |
|
|
287
|
+
| ------------ | ------------------------------ | ----------------------------------------------------- |
|
|
288
|
+
| `utc` | `DTSTART:20261012T090000Z` | A timed event at that instant |
|
|
289
|
+
| `zoned` | `DTSTART;TZID=Europe/Berlin:…` | A timed event at `zoned.toInstant()` |
|
|
290
|
+
| `date` | `DTSTART;VALUE=DATE:20261012` | An all-day event |
|
|
291
|
+
| `floating` | `DTSTART:20261012T090000` | A timed event at that wall time, in a zone you choose |
|
|
292
|
+
| `unresolved` | `DTSTART;TZID=Our Office:…` | The same as floating |
|
|
293
|
+
|
|
294
|
+
- **Floating times** have a clock time and no zone. A feed's `X-WR-TIMEZONE`, which Google adds,
|
|
295
|
+
says which zone the publisher meant, and the parser gives it to you as `calendar.timeZone`.
|
|
296
|
+
`feedTimeZone` uses it, and the viewer's time zone when the feed has none. The preview's feed has
|
|
297
|
+
none, so its focus time shows at the same clock time wherever you are.
|
|
298
|
+
- **The end.** `getEventEnd` returns DTEND, else DTSTART plus DURATION, else the RFC's default: the
|
|
299
|
+
next day for a date, the start itself for a time. It's always the same kind as the start.
|
|
300
|
+
- **No length.** ICS lets an event end when it starts, but a calendar event's end must come after
|
|
301
|
+
its start, so those events show as half an hour long, or one day for a date.
|
|
302
|
+
- **Cancelled events.** Feeds keep a cancelled event, with `STATUS:CANCELLED`, so that subscribers
|
|
303
|
+
can remove it. `toFeedEvent` skips it.
|
|
304
|
+
- **Colors.** Events marked free (`TRANSP:TRANSPARENT`) or tentative are gray, the rest blue. Change
|
|
305
|
+
the rule to suit your app, or color each feed differently.
|
|
306
|
+
|
|
307
|
+
## Recurring events
|
|
308
|
+
|
|
309
|
+
`@moltenship/ics` reads recurrence but doesn't expand it. A series is one event with its rule in
|
|
310
|
+
`recurrenceRule`, the occurrences it removes in `exceptionDates`, and extra ones in
|
|
311
|
+
`recurrenceDates`. A change to one occurrence is a separate event with the series' UID and a
|
|
312
|
+
`recurrenceId` that names the occurrence it replaces. So the mapping above shows:
|
|
313
|
+
|
|
314
|
+
- **A recurring event once,** at its DTSTART, which is the first occurrence.
|
|
315
|
+
- **A changed occurrence as its own event,** at its new time.
|
|
316
|
+
|
|
317
|
+
[`@moltenship/calendar-recurrence`](../api/recurrence.md) expands them. `recurringEventsFromIcs`
|
|
318
|
+
groups a calendar's events into series masters, overrides and one-off events, and an expander turns
|
|
319
|
+
them into the occurrences for the dates on screen. Those dates are known in the browser, so the
|
|
320
|
+
server sends the feed's text, a string, instead of mapped events. In the page above, have the
|
|
321
|
+
cached function return `await response.text()`, and pass it as `text`:
|
|
322
|
+
|
|
323
|
+
```tsx title="recurring-feed-calendar.tsx"
|
|
324
|
+
// app/team/recurring-feed-calendar.tsx
|
|
325
|
+
"use client";
|
|
326
|
+
|
|
327
|
+
import { createCalendar, weekRange, type PlainDateInterval } from "@moltenship/calendar";
|
|
328
|
+
import {
|
|
329
|
+
createCanonicalRecurrenceAdapter,
|
|
330
|
+
createOccurrenceAdapter,
|
|
331
|
+
createOccurrenceExpander,
|
|
332
|
+
recurringEventsFromIcs,
|
|
333
|
+
type RecurrenceOccurrence,
|
|
334
|
+
type RecurringCalendarEvent,
|
|
335
|
+
} from "@moltenship/calendar-recurrence";
|
|
336
|
+
import { parseIcs, type IcsEvent } from "@moltenship/ics";
|
|
337
|
+
import { useMemo, useState } from "react";
|
|
338
|
+
|
|
339
|
+
import { CalendarToolbar } from "@/components/calendar/calendar-toolbar";
|
|
340
|
+
import { createWeekView } from "@/components/calendar/time-grid-view/time-grid-view";
|
|
341
|
+
|
|
342
|
+
type FeedEvent = RecurringCalendarEvent<IcsEvent>;
|
|
343
|
+
type FeedOccurrence = RecurrenceOccurrence<FeedEvent, string>;
|
|
344
|
+
|
|
345
|
+
const adapters = createCanonicalRecurrenceAdapter<IcsEvent>();
|
|
346
|
+
|
|
347
|
+
const Calendar = createCalendar<FeedOccurrence>()({
|
|
348
|
+
eventAdapter: createOccurrenceAdapter(adapters.event),
|
|
349
|
+
views: [createWeekView<FeedOccurrence>()],
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
/** The feed's VEVENTs as series masters, overrides and one-off events. */
|
|
353
|
+
export function readRecurringFeed(text: string): readonly FeedEvent[] {
|
|
354
|
+
const { calendars } = parseIcs(text);
|
|
355
|
+
return calendars.flatMap((calendar) => {
|
|
356
|
+
// UTC times on all-day and floating series, such as an UNTIL, read in the feed's zone.
|
|
357
|
+
const { events, issues } = recurringEventsFromIcs(calendar.events, {
|
|
358
|
+
timeZone: calendar.timeZone,
|
|
359
|
+
});
|
|
360
|
+
for (const issue of issues) console.warn(`${issue.event.uid}: ${issue.message}`);
|
|
361
|
+
return events;
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
const weekRules = { firstDay: 1, minimalDays: 4, weekend: [6, 7] } as const;
|
|
366
|
+
|
|
367
|
+
interface RecurringFeedCalendarProps {
|
|
368
|
+
/** The feed's ICS text, which the server fetched. */
|
|
369
|
+
readonly text: string;
|
|
370
|
+
readonly now: string;
|
|
371
|
+
readonly timeZone: string;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
export function RecurringFeedCalendar({ text, now, timeZone }: RecurringFeedCalendarProps) {
|
|
375
|
+
const nowInstant = Temporal.Instant.from(now);
|
|
376
|
+
const today = nowInstant.toZonedDateTimeISO(timeZone).toPlainDate();
|
|
377
|
+
const events = useMemo(() => readRecurringFeed(text), [text]);
|
|
378
|
+
const [expander] = useState(() => createOccurrenceExpander(adapters));
|
|
379
|
+
const [range, setRange] = useState<PlainDateInterval>(() =>
|
|
380
|
+
weekRange(today, { weekStartsOn: weekRules.firstDay }),
|
|
381
|
+
);
|
|
382
|
+
const { occurrences } = useMemo(
|
|
383
|
+
() => expander.expand(events, { range, timeZone }),
|
|
384
|
+
[expander, events, range, timeZone],
|
|
385
|
+
);
|
|
386
|
+
|
|
387
|
+
return (
|
|
388
|
+
<Calendar.Root
|
|
389
|
+
events={occurrences}
|
|
390
|
+
onVisibleRangeChange={({ loadRange }) => setRange(loadRange)}
|
|
391
|
+
now={nowInstant}
|
|
392
|
+
defaultAnchorDate={today}
|
|
393
|
+
defaultTimeZone={timeZone}
|
|
394
|
+
locale="en-US"
|
|
395
|
+
weekRules={weekRules}
|
|
396
|
+
>
|
|
397
|
+
<Calendar.Frame className="flex h-[640px] flex-col rounded-xl border">
|
|
398
|
+
<CalendarToolbar calendar={Calendar} />
|
|
399
|
+
<Calendar.Viewport className="min-h-0 flex-1">
|
|
400
|
+
<Calendar.ActiveView />
|
|
401
|
+
</Calendar.Viewport>
|
|
402
|
+
</Calendar.Frame>
|
|
403
|
+
</Calendar.Root>
|
|
404
|
+
);
|
|
405
|
+
}
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
- **IDs.** Masters and one-off events keep their UID. An override gets the ID of the occurrence it
|
|
409
|
+
replaces, such as `standup@example.com::2026-10-20T07:00:00Z`, so it never collides with its
|
|
410
|
+
series.
|
|
411
|
+
- **Time zones.** A zoned series repeats on its zone's wall clock, so a 09:00 meeting stays at 09:00
|
|
412
|
+
across daylight saving time. Floating and all-day series stay floating and all-day, and the
|
|
413
|
+
calendar shows them that way. Pass the feed's `X-WR-TIMEZONE` as `timeZone`, so UTC dates on them,
|
|
414
|
+
such as an UNTIL, keep the feed's dates. Feeds without one, such as Exchange's, write the UNTIL of
|
|
415
|
+
an all-day series at the owner's midnight in UTC; without `timeZone`, the bridge reads such dates
|
|
416
|
+
in the zone most of the feed's TZIDs name, so the last day isn't lost. Pass `timeZone: "UTC"` to
|
|
417
|
+
read them in UTC, as the bridge did before.
|
|
418
|
+
- **Daylight saving time.** A DTSTART in the hour a zone skips moves forward by the gap. A series
|
|
419
|
+
keeps the length written up to DTEND: a weekly Sydney 02:30–03:30 meeting from the night clocks
|
|
420
|
+
go forward shows 03:30–04:30 that night, and 02:30–03:30 on later weeks.
|
|
421
|
+
- **Outlook's all-day events.** Older Outlook and Exchange exports write an all-day event from
|
|
422
|
+
midnight to midnight in a zone, with `X-MICROSOFT-CDO-ALLDAYEVENT:TRUE`. The bridge reads it as
|
|
423
|
+
all-day on the dates written, and its series' midnight dates as dates.
|
|
424
|
+
- **This and future.** Apple writes a change to an occurrence and every later one as an override
|
|
425
|
+
with `RANGE=THISANDFUTURE`. The bridge splits the series there, as Google Calendar does: the new
|
|
426
|
+
part starts at the override, with its title, and the rest of the rule moved the same way. Its
|
|
427
|
+
occurrences take the new part's IDs.
|
|
428
|
+
- **Cancelled events.** A cancelled override, with `STATUS:CANCELLED`, hides its occurrence. A
|
|
429
|
+
cancelled master is left out with its overrides.
|
|
430
|
+
- **Issues.** `recurringEventsFromIcs` reports what it changed or left out, such as an RDATE with a
|
|
431
|
+
date where the series has times, or a `RANGE=THISANDFUTURE` change the rule can't follow, which
|
|
432
|
+
then replaces one occurrence. The expander reports its own issues, such as a rule it can't
|
|
433
|
+
expand.
|
|
434
|
+
|
|
435
|
+
To expand on the server instead, call the expander there for a range you choose, and map each
|
|
436
|
+
occurrence's `interval` to JSON as `toFeedEvent` maps an event's times. The calendar then shows only
|
|
437
|
+
that range. [Show and edit recurring events](./recurring-events.md) covers expansion in
|
|
438
|
+
more detail.
|
|
439
|
+
|
|
440
|
+
## Time zones
|
|
441
|
+
|
|
442
|
+
A TZID is a name the file chooses, and the parser maps it to a time zone Temporal knows: an IANA
|
|
443
|
+
name such as `America/New_York`, a Windows or Outlook name such as `W. Europe Standard Time`, the
|
|
444
|
+
file's own VTIMEZONE, and a few more. A TZID that matches none of them isn't guessed. The time
|
|
445
|
+
stays `unresolved`, with its wall time and TZID, and `issues` gets an `unknown-time-zone` warning.
|
|
446
|
+
The mapping above then reads it in the zone you chose, like a floating time.
|
|
447
|
+
|
|
448
|
+
If you know what a feed's own names mean, say so with `resolveTimeZone`. The parser asks it first;
|
|
449
|
+
`undefined` falls through to the built-in rules:
|
|
450
|
+
|
|
451
|
+
```ts
|
|
452
|
+
const { calendars, issues } = parseIcs(text, {
|
|
453
|
+
resolveTimeZone: (tzid) => (tzid === "Our Office" ? "Europe/Lisbon" : undefined),
|
|
454
|
+
});
|
|
455
|
+
```
|
|
456
|
+
|
|
457
|
+
The [API reference](../api/ics.md) lists every rule, in order.
|
|
458
|
+
|
|
459
|
+
## Issues
|
|
460
|
+
|
|
461
|
+
Each issue has a `severity`, a `code`, a `message` and, when it comes from one, a `line`. An error
|
|
462
|
+
drops what it concerns, such as an event without DTSTART. A warning keeps a best-effort value.
|
|
463
|
+
|
|
464
|
+
Feeds you subscribe to break the rules more often than you'd think, so keep showing what parsed.
|
|
465
|
+
Log the issues, as the page does, and if people should know, pass a count of the errors to the
|
|
466
|
+
client and show it beside the calendar, such as "2 events couldn't be read".
|
|
467
|
+
|
|
468
|
+
Files that people upload are different: tell them what's wrong instead. `strict: true` throws an
|
|
469
|
+
`IcsParseError` with the errors in `issues`:
|
|
470
|
+
|
|
471
|
+
```ts
|
|
472
|
+
import { IcsParseError, parseIcs } from "@moltenship/ics";
|
|
473
|
+
|
|
474
|
+
function readUpload(text: string) {
|
|
475
|
+
try {
|
|
476
|
+
return { calendars: parseIcs(text, { strict: true }).calendars };
|
|
477
|
+
} catch (error) {
|
|
478
|
+
if (!(error instanceof IcsParseError)) throw error;
|
|
479
|
+
return { errors: error.issues.map((issue) => `Line ${issue.line ?? "?"}: ${issue.message}`) };
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
```
|
|
483
|
+
|
|
484
|
+
A parser from `createIcsParser` is also a [Standard Schema](https://standardschema.dev), so a form
|
|
485
|
+
or router library that takes one can validate the upload. The [API reference](../api/ics.md) covers
|
|
486
|
+
both.
|