@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,583 @@
|
|
|
1
|
+
# Show and edit recurring events
|
|
2
|
+
|
|
3
|
+
Expand series for the dates on screen, and ask which events a change applies to.
|
|
4
|
+
|
|
5
|
+
Source: https://time.molten.sh/docs/guides/recurring-events
|
|
6
|
+
|
|
7
|
+
> Live example: see it at https://time.molten.sh/docs/guides/recurring-events
|
|
8
|
+
|
|
9
|
+
These events repeat: a stand-up every weekday, with Wednesday's skipped and Thursday's moved an
|
|
10
|
+
hour later, a 1:1 every other week, planning on eight Mondays, and a release day every Friday. Drag
|
|
11
|
+
an occurrence, or select one and delete it, and the calendar asks whether the change applies to
|
|
12
|
+
this event, this and following events, or all events. The change waits on screen while it asks.
|
|
13
|
+
|
|
14
|
+
This guide builds that calendar with `@moltenship/calendar-recurrence`. It expands each series into
|
|
15
|
+
its occurrences for the dates on screen, and turns a change to one occurrence into what to save.
|
|
16
|
+
[Recurring events](../concepts/recurrence.md) explains the model behind it.
|
|
17
|
+
|
|
18
|
+
## Install
|
|
19
|
+
|
|
20
|
+
The recurrence package is separate from the calendar. It doesn't use React, so you can also expand
|
|
21
|
+
series on a server.
|
|
22
|
+
|
|
23
|
+
Like the calendar, `@moltenship/calendar-recurrence` is on npm as a release candidate for 1.0.0.
|
|
24
|
+
|
|
25
|
+
**pnpm**
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pnpm add @moltenship/calendar-recurrence
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
**npm**
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
npm install @moltenship/calendar-recurrence
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
**yarn**
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
yarn add @moltenship/calendar-recurrence
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
**bun**
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
bun add @moltenship/calendar-recurrence
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
The dialog that asks for the scope is UI source, like the views. Add it with the shadcn CLI, as
|
|
50
|
+
described in [UI source](../installation.md#ui-source-optional). It brings shadcn's `button` and
|
|
51
|
+
`dialog`:
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
npx shadcn@latest add https://time.molten.sh/r/recurrence-scope-dialog.json
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Wire the adapters
|
|
58
|
+
|
|
59
|
+
The calendar shows occurrences, not your events. A `RecurrenceOccurrence` is one occurrence of a
|
|
60
|
+
series, or a one-off event passed through, and `createOccurrenceAdapter` turns your event adapter
|
|
61
|
+
into the calendar's adapter for them. A recurrence adapter beside it reads what makes an event a
|
|
62
|
+
series' master, or an override of one occurrence. Together, they're your `RecurrenceAdapters`.
|
|
63
|
+
|
|
64
|
+
An app without an event type of its own can store `RecurringCalendarEvent`s: core's
|
|
65
|
+
[`CalendarEvent`](../concepts/events.md#records-in-times-own-shape) with an optional `recurrence`
|
|
66
|
+
and `override`. `createCanonicalRecurrenceAdapter` returns both adapters for them:
|
|
67
|
+
|
|
68
|
+
```tsx title="recurrence-calendar.tsx"
|
|
69
|
+
"use client";
|
|
70
|
+
|
|
71
|
+
import { createCalendar } from "@moltenship/calendar";
|
|
72
|
+
import {
|
|
73
|
+
createCanonicalRecurrenceAdapter,
|
|
74
|
+
createOccurrenceAdapter,
|
|
75
|
+
type RecurrenceOccurrence,
|
|
76
|
+
type RecurringCalendarEvent,
|
|
77
|
+
} from "@moltenship/calendar-recurrence";
|
|
78
|
+
|
|
79
|
+
import type { CalendarEventColor } from "@/components/calendar/event-colors";
|
|
80
|
+
import { createWeekView } from "@/components/calendar/time-grid-view/time-grid-view";
|
|
81
|
+
|
|
82
|
+
export interface TeamEventData {
|
|
83
|
+
readonly color: CalendarEventColor;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* A series' master has a `recurrence`, an event that replaces one of its occurrences has an
|
|
88
|
+
* `override`, and any other event happens once.
|
|
89
|
+
*/
|
|
90
|
+
export type TeamEvent = RecurringCalendarEvent<TeamEventData>;
|
|
91
|
+
|
|
92
|
+
/** What the calendar shows: one occurrence of a series, or a one-off event. */
|
|
93
|
+
export type TeamOccurrence = RecurrenceOccurrence<TeamEvent, string>;
|
|
94
|
+
|
|
95
|
+
export const adapters = createCanonicalRecurrenceAdapter<TeamEventData>();
|
|
96
|
+
|
|
97
|
+
/** Lucide's "repeat" icon, for the chips of repeating events. */
|
|
98
|
+
const repeatIcon = (
|
|
99
|
+
<svg
|
|
100
|
+
fill="none"
|
|
101
|
+
stroke="currentColor"
|
|
102
|
+
strokeLinecap="round"
|
|
103
|
+
strokeLinejoin="round"
|
|
104
|
+
strokeWidth={2}
|
|
105
|
+
viewBox="0 0 24 24"
|
|
106
|
+
>
|
|
107
|
+
<path d="m17 2 4 4-4 4" />
|
|
108
|
+
<path d="M3 11v-1a4 4 0 0 1 4-4h14" />
|
|
109
|
+
<path d="m7 22-4-4 4-4" />
|
|
110
|
+
<path d="M21 13v1a4 4 0 0 1-4 4H3" />
|
|
111
|
+
</svg>
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
export const Calendar = createCalendar<TeamOccurrence>()({
|
|
115
|
+
// Occurrences are the calendar's events. It reads their titles through your adapter.
|
|
116
|
+
eventAdapter: createOccurrenceAdapter(adapters.event),
|
|
117
|
+
views: [
|
|
118
|
+
createWeekView<TeamOccurrence>({
|
|
119
|
+
// The icon marks a repeating event on screen, and the description says so to screen readers.
|
|
120
|
+
describeEvent: (occurrence) => (occurrence.recurrence ? "repeating event" : undefined),
|
|
121
|
+
getEventColor: (occurrence) => occurrence.event.data.color,
|
|
122
|
+
getEventIcon: (occurrence) => (occurrence.recurrence ? repeatIcon : null),
|
|
123
|
+
}),
|
|
124
|
+
],
|
|
125
|
+
});
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
Views receive occurrences, so `getEventColor` reads the color from `occurrence.event`, the event
|
|
129
|
+
the occurrence shows: the master, the override, or the one-off event. `occurrence.recurrence` is set
|
|
130
|
+
on occurrences of a series, so `getEventIcon` marks their chips with a repeat icon, and
|
|
131
|
+
`describeEvent` adds "repeating event" to their accessible names, since the icon is decorative.
|
|
132
|
+
|
|
133
|
+
### Your own event type
|
|
134
|
+
|
|
135
|
+
With your own event type, write the recurrence adapter yourself. `getRecurrence` returns a master's
|
|
136
|
+
rule, exception dates and extra dates, and `getOverride` returns the series and original start an
|
|
137
|
+
override replaces:
|
|
138
|
+
|
|
139
|
+
```ts title="meetings.ts"
|
|
140
|
+
import { createCalendar, defineEventAdapter } from "@moltenship/calendar";
|
|
141
|
+
import {
|
|
142
|
+
createOccurrenceAdapter,
|
|
143
|
+
parseRecurrenceRule,
|
|
144
|
+
type RecurrenceAdapters,
|
|
145
|
+
type RecurrenceOccurrence,
|
|
146
|
+
} from "@moltenship/calendar-recurrence";
|
|
147
|
+
|
|
148
|
+
export interface Meeting {
|
|
149
|
+
readonly id: string;
|
|
150
|
+
readonly title: string;
|
|
151
|
+
readonly start: Temporal.Instant;
|
|
152
|
+
readonly end: Temporal.Instant;
|
|
153
|
+
/** Where it was booked. A series repeats on this zone's wall clock. */
|
|
154
|
+
readonly timeZone: string;
|
|
155
|
+
/** On a series' first meeting: an RRULE, such as "FREQ=WEEKLY;BYDAY=MO,WE". */
|
|
156
|
+
readonly repeats?: string;
|
|
157
|
+
/** Starts of meetings the series skips. */
|
|
158
|
+
readonly skips?: readonly Temporal.Instant[];
|
|
159
|
+
/** On a meeting that replaces one of a series: the series, and the start it replaces. */
|
|
160
|
+
readonly replaces?: { readonly seriesId: string; readonly start: Temporal.Instant };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export const adapters: RecurrenceAdapters<Meeting, string, never> = {
|
|
164
|
+
event: defineEventAdapter<Meeting>()({
|
|
165
|
+
getId: (meeting) => meeting.id,
|
|
166
|
+
getTitle: (meeting) => meeting.title,
|
|
167
|
+
getInterval: (meeting) => ({
|
|
168
|
+
kind: "timed",
|
|
169
|
+
start: meeting.start,
|
|
170
|
+
end: meeting.end,
|
|
171
|
+
sourceTimeZone: meeting.timeZone,
|
|
172
|
+
}),
|
|
173
|
+
getResourceIds: () => [],
|
|
174
|
+
}),
|
|
175
|
+
recurrence: {
|
|
176
|
+
getRecurrence: (meeting) =>
|
|
177
|
+
meeting.repeats === undefined
|
|
178
|
+
? undefined
|
|
179
|
+
: { rule: parseRecurrenceRule(meeting.repeats).rule, exceptionDates: meeting.skips },
|
|
180
|
+
getOverride: (meeting) =>
|
|
181
|
+
meeting.replaces && {
|
|
182
|
+
seriesId: meeting.replaces.seriesId,
|
|
183
|
+
recurrenceId: meeting.replaces.start,
|
|
184
|
+
},
|
|
185
|
+
},
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
export const Calendar = createCalendar<RecurrenceOccurrence<Meeting, string>>()({
|
|
189
|
+
eventAdapter: createOccurrenceAdapter(adapters.event),
|
|
190
|
+
});
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
- A master's `getInterval` is its first occurrence. A timed series repeats on the wall clock of
|
|
194
|
+
`sourceTimeZone`, so a 09:00 meeting stays at 09:00 across daylight saving time.
|
|
195
|
+
- `parseRecurrenceRule` reads RRULE text, with or without the `RRULE:` prefix. It never throws:
|
|
196
|
+
check its `issues` when you save the text, and show an error then.
|
|
197
|
+
- The expander reads each event once while it's the same object, so parsing in the adapter is
|
|
198
|
+
cheap. Replace events rather than changing them.
|
|
199
|
+
|
|
200
|
+
## Expand the dates on screen
|
|
201
|
+
|
|
202
|
+
A series can be endless, so expand it only for the dates the calendar needs. The calendar reports
|
|
203
|
+
them through `onVisibleRangeChange`:
|
|
204
|
+
|
|
205
|
+
```ts title="use-occurrences.ts"
|
|
206
|
+
import type { CalendarVisibleRange, PlainDateInterval } from "@moltenship/calendar";
|
|
207
|
+
import { createOccurrenceExpander } from "@moltenship/calendar-recurrence";
|
|
208
|
+
import { useMemo, useState } from "react";
|
|
209
|
+
|
|
210
|
+
import { adapters, type TeamEvent } from "./recurrence-calendar";
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* The occurrences of `events` on the dates the calendar asks for. Pass `occurrences` to `events`
|
|
214
|
+
* on `Root`, and `onVisibleRangeChange` to the prop of that name.
|
|
215
|
+
*/
|
|
216
|
+
export function useOccurrences(
|
|
217
|
+
events: readonly TeamEvent[],
|
|
218
|
+
timeZone: string,
|
|
219
|
+
// The dates the calendar opens on, so its first render isn't empty.
|
|
220
|
+
initialRange: PlainDateInterval,
|
|
221
|
+
) {
|
|
222
|
+
// One expander for as long as the calendar is mounted: it keeps its work for series that
|
|
223
|
+
// didn't change, and returns the same occurrence objects for them.
|
|
224
|
+
const [expander] = useState(() => createOccurrenceExpander(adapters));
|
|
225
|
+
const [range, setRange] = useState(initialRange);
|
|
226
|
+
const expansion = useMemo(
|
|
227
|
+
() => expander.expand(events, { range, timeZone }),
|
|
228
|
+
[expander, events, range, timeZone],
|
|
229
|
+
);
|
|
230
|
+
|
|
231
|
+
return {
|
|
232
|
+
issues: expansion.issues,
|
|
233
|
+
occurrences: expansion.occurrences,
|
|
234
|
+
onVisibleRangeChange: ({ loadRange }: CalendarVisibleRange) => setRange(loadRange),
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
- **One expander.** Create it once, in `useState` as here or at module scope. It keeps its work for
|
|
240
|
+
each series between calls, and gives back the same occurrence objects for events that didn't
|
|
241
|
+
change, so the calendar and its views skip them.
|
|
242
|
+
- **`useMemo` over events and range.** Expansion runs again only when your events or the dates
|
|
243
|
+
change. A new array holding the same event objects is cheap: unchanged series reuse their work.
|
|
244
|
+
- **`loadRange`, not `range`.** Scrolling views render a page of days on either side of the ones
|
|
245
|
+
they show, so a swipe never reveals empty days. `loadRange` includes those pages; `range` is only
|
|
246
|
+
what's visible. In paged views the two are the same.
|
|
247
|
+
- **The first render.** The calendar reports its range after it first renders, so the hook starts
|
|
248
|
+
from the week it opens on, from `weekRange`. Without a first guess, the first frame has no
|
|
249
|
+
events.
|
|
250
|
+
- **Time zones.** `timeZone` is the calendar's zone, which places the range's dates for timed
|
|
251
|
+
series. Series don't repeat in it. They repeat in their own `sourceTimeZone`, or, for events
|
|
252
|
+
without one, in the expander's `defaultTimeZone`, which is UTC unless you set it.
|
|
253
|
+
|
|
254
|
+
`expansion.issues` lists what the expander couldn't read, such as an invalid rule or an override
|
|
255
|
+
whose series isn't loaded, with each event and its position. Bad data never throws: the rest of
|
|
256
|
+
the series and the other events still show. Log the issues, as you would
|
|
257
|
+
[diagnostics](../concepts/events.md#invalid-records).
|
|
258
|
+
|
|
259
|
+
## Ask which events a change applies to
|
|
260
|
+
|
|
261
|
+
When someone drags an occurrence, the calendar calls `onEventChange` with a change whose `event` is
|
|
262
|
+
the occurrence. `useRecurrenceScopePrompt` from the dialog you added returns `ask`, which opens the
|
|
263
|
+
dialog and resolves with the scope picked, and `dialog`, to render once:
|
|
264
|
+
|
|
265
|
+
```tsx title="team-calendar.tsx"
|
|
266
|
+
async function onEventChange(change: OccurrenceChange) {
|
|
267
|
+
// A new event, or a change to a one-off event, needs no question.
|
|
268
|
+
let scope: RecurrenceEditScope = "this";
|
|
269
|
+
if (isRecurringChange(change)) {
|
|
270
|
+
const scopes = recurrenceEditScopes(change, adapters, { timeZone });
|
|
271
|
+
// None applies, so there's nothing to ask: tell the user why instead. Throwing rolls the
|
|
272
|
+
// change back.
|
|
273
|
+
if (scopes.length === 0) throw new Error("This repeating event can't change that way.");
|
|
274
|
+
const picked = await ask({
|
|
275
|
+
action: change.cause === "delete" ? "delete" : "edit",
|
|
276
|
+
eventTitle: change.event.event.title,
|
|
277
|
+
scopes,
|
|
278
|
+
});
|
|
279
|
+
// An `AbortError` is announced as a cancel, not as a change that couldn't be saved.
|
|
280
|
+
if (picked === null) throw new DOMException("Cancelled", "AbortError");
|
|
281
|
+
scope = picked;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const edit = resolveRecurrenceEdit(change, scope, adapters, {
|
|
285
|
+
// Picked now, so the IDs of a "following" edit's new series are known.
|
|
286
|
+
newSeriesId: crypto.randomUUID(),
|
|
287
|
+
occurrences,
|
|
288
|
+
// The zone the calendar moves occurrences on, so a series dragged by days keeps its time.
|
|
289
|
+
timeZone,
|
|
290
|
+
});
|
|
291
|
+
if (edit.type === "rejected") throw new Error(edit.message);
|
|
292
|
+
|
|
293
|
+
// Save the edit on your server here. The change stays pending until this function returns.
|
|
294
|
+
if (edit.type === "event") {
|
|
295
|
+
setEvents((current) => applyEventChange(current, edit.change));
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
setEvents((current) =>
|
|
299
|
+
applyRecurrenceEdit(current, edit, { createId: () => crypto.randomUUID() }),
|
|
300
|
+
);
|
|
301
|
+
// "following" and "all" give the occurrences new IDs. The selection and keyboard focus
|
|
302
|
+
// follow them, and the calendar moves focus to the occurrence under its new ID.
|
|
303
|
+
setSelection((current) => renamed(current, edit.occurrenceIds));
|
|
304
|
+
setFocus((current) => renamed(current, edit.occurrenceIds));
|
|
305
|
+
}
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
1. **`isRecurringChange`** says whether the change is to an occurrence of a series. A new event, or
|
|
309
|
+
a change to a one-off event, needs no question.
|
|
310
|
+
2. **`ask`** opens the dialog. `action` picks its wording for an edit or a delete, and `scopes`
|
|
311
|
+
comes from **`recurrenceEditScopes`**, which offers only the scopes that apply: no "This and
|
|
312
|
+
following events" on a series' first occurrence, and only "This event" when the rule can't
|
|
313
|
+
follow the change, such as the 28th of each month dragged to a 29th. When none applies, the list
|
|
314
|
+
is empty and there's nothing to ask: tell the user why, and roll the change back.
|
|
315
|
+
3. **Cancel** resolves `null`. Throwing rolls the change back, as any error in `onEventChange`
|
|
316
|
+
does. Throw an `AbortError`, as the handler does, and the calendar's announcements say the
|
|
317
|
+
change was cancelled rather than that it couldn't be saved.
|
|
318
|
+
4. **`resolveRecurrenceEdit`** turns the change into an edit: what to save for that scope. Pass
|
|
319
|
+
the calendar's `timeZone` to it and to `recurrenceEditScopes`, so the change is replayed on the
|
|
320
|
+
series' own clock: a series dragged by days keeps its time of day when the calendar's zone and
|
|
321
|
+
the series' change DST on different dates, and one dragged an hour later moves an hour. If you
|
|
322
|
+
keep deleted occurrences as cancelled overrides, as Google's API does, pass them as `overrides`
|
|
323
|
+
too. A
|
|
324
|
+
change that needs no scope comes back as `type: "event"`, with your event unwrapped from the
|
|
325
|
+
occurrence, to apply as you would without recurrence. A scope that can't apply comes back as
|
|
326
|
+
`type: "rejected"`, with a `reason` and a `message`.
|
|
327
|
+
5. **Save** the edit, below. The change stays pending until `onEventChange` returns.
|
|
328
|
+
|
|
329
|
+
While the dialog is open, the change waits on screen as pending: the dragged occurrence shows where
|
|
330
|
+
it was dropped. Only that occurrence shows the change. The calendar keeps pending changes per event
|
|
331
|
+
and knows nothing about series, so for "This and following events" or "All events" the rest of the
|
|
332
|
+
series moves when your events change, and the calendar keeps showing the change until then.
|
|
333
|
+
|
|
334
|
+
### Deleting
|
|
335
|
+
|
|
336
|
+
The views don't delete events; your own UI does, with `proposeEventChange`. The change goes through
|
|
337
|
+
the same `onEventChange`, and `ask` uses the dialog's wording for a delete:
|
|
338
|
+
|
|
339
|
+
```tsx title="team-calendar.tsx"
|
|
340
|
+
/** The selected occurrence's title, and a button that deletes it through `onEventChange`. */
|
|
341
|
+
function SelectedEvent({ occurrence }: { readonly occurrence: TeamOccurrence | undefined }) {
|
|
342
|
+
const { proposeEventChange } = Calendar.useCalendarActions();
|
|
343
|
+
|
|
344
|
+
return (
|
|
345
|
+
<div className="flex min-h-12 shrink-0 items-center gap-3 border-t px-4 py-2 text-sm">
|
|
346
|
+
{occurrence === undefined ? (
|
|
347
|
+
<span className="text-muted-foreground">Click an event to select it.</span>
|
|
348
|
+
) : (
|
|
349
|
+
<>
|
|
350
|
+
<span className="min-w-0 truncate font-medium">{occurrence.event.title}</span>
|
|
351
|
+
<Button
|
|
352
|
+
className="ms-auto"
|
|
353
|
+
onClick={() => void proposeEventChange({ cause: "delete", eventId: occurrence.id })}
|
|
354
|
+
size="sm"
|
|
355
|
+
variant="outline"
|
|
356
|
+
>
|
|
357
|
+
Delete
|
|
358
|
+
</Button>
|
|
359
|
+
</>
|
|
360
|
+
)}
|
|
361
|
+
</div>
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
## Save the edit
|
|
367
|
+
|
|
368
|
+
A series edit lists its writes as plain data, so you can send it to a server as it is or apply it
|
|
369
|
+
to your own store. If your events are `RecurringCalendarEvent`s, `applyRecurrenceEdit` applies it
|
|
370
|
+
and returns the new array, as `onEventChange` above does. `createId` names the events it creates:
|
|
371
|
+
overrides, and the series a "This and following events" edit starts when you didn't pick its ID.
|
|
372
|
+
|
|
373
|
+
With your own event type or a database, write the same steps yourself:
|
|
374
|
+
|
|
375
|
+
| Field | What to write |
|
|
376
|
+
| ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
377
|
+
| `series` | `"update"`: the master at `placement`, its first occurrence, with `recurrence` in place of the stored one. `"delete"`: remove the master. |
|
|
378
|
+
| `newSeries` | For "This and following events": a new master, copied from `from` apart from its ID, at `placement`, with `recurrence`, and `id` if you gave one. |
|
|
379
|
+
| `override` | The edited occurrence's override: `"create"` one from the master `from`, `"update"` the stored one, or `"delete"` it. |
|
|
380
|
+
| `otherOverrides` | Call `recurrenceOverrideEdit(edit, recurrenceId)` for each override of `seriesId` you store, loaded or not, and keep, delete or rekey it. |
|
|
381
|
+
|
|
382
|
+
The edit describes placement and recurrence only. If the change also edited the event's title or
|
|
383
|
+
other fields, copy them yourself. [The reference](../api/recurrence.md#recurrenceedit) describes
|
|
384
|
+
each write.
|
|
385
|
+
|
|
386
|
+
A "This event" delete adds the occurrence's start to the master's exception dates, which every
|
|
387
|
+
iCalendar store understands. Google's API records a deleted occurrence as a cancelled instance
|
|
388
|
+
instead; this package reads both.
|
|
389
|
+
|
|
390
|
+
## Keep the selection
|
|
391
|
+
|
|
392
|
+
An occurrence's ID is its series' ID and its original start, such as
|
|
393
|
+
`standup::2026-10-14T07:30:00Z`. It's stable across views, time zones and reloads, and when "This
|
|
394
|
+
event" moves the occurrence, since its override keeps the original start.
|
|
395
|
+
|
|
396
|
+
"This and following events" and "All events" change the original starts, and so the IDs.
|
|
397
|
+
`edit.occurrenceIds` maps old IDs to new ones, and the handler above moves the selection and the
|
|
398
|
+
keyboard focus with it: select an occurrence in the preview, drag it with "All events", and it
|
|
399
|
+
stays selected.
|
|
400
|
+
|
|
401
|
+
- Control `focus` with `onFocusChange`, and map it before `onEventChange` returns. `ask` resolves
|
|
402
|
+
once the dialog has closed and focus is back on the occurrence, and the calendar then moves focus
|
|
403
|
+
to the occurrence under its new ID. Left alone, focus would name the old ID, which no longer
|
|
404
|
+
shows.
|
|
405
|
+
|
|
406
|
+
- Pass `occurrences`, the expansion you give the calendar, so the map covers every occurrence on
|
|
407
|
+
screen, not only the dragged one.
|
|
408
|
+
- Pass `newSeriesId` for "This and following events", so the map can name the new series'
|
|
409
|
+
occurrences. Pick it before you save, as you would for an optimistic create.
|
|
410
|
+
- Map anything else that holds occurrence IDs the same way, such as an open details panel or a URL.
|
|
411
|
+
- Read an occurrence's series from `occurrence.recurrence`, which keeps the series ID's type. Use
|
|
412
|
+
`parseOccurrenceId` only for IDs you stored.
|
|
413
|
+
|
|
414
|
+
## Put it together
|
|
415
|
+
|
|
416
|
+
The whole calendar: the occurrences go to `events`, the handlers to `Root`, and the dialog renders
|
|
417
|
+
beside it. Clicks on events select them, through the `data-event-id` every view puts on its
|
|
418
|
+
events, and `SelectedEvent` is the component from [Deleting](#deleting).
|
|
419
|
+
|
|
420
|
+
```tsx title="team-calendar.tsx"
|
|
421
|
+
"use client";
|
|
422
|
+
|
|
423
|
+
import {
|
|
424
|
+
eventIdOf,
|
|
425
|
+
weekRange,
|
|
426
|
+
type CalendarEventChange,
|
|
427
|
+
type CalendarFocus,
|
|
428
|
+
type CalendarSelection,
|
|
429
|
+
type EventId,
|
|
430
|
+
} from "@moltenship/calendar";
|
|
431
|
+
import {
|
|
432
|
+
applyRecurrenceEdit,
|
|
433
|
+
isRecurringChange,
|
|
434
|
+
recurrenceEditScopes,
|
|
435
|
+
resolveRecurrenceEdit,
|
|
436
|
+
type RecurrenceEditScope,
|
|
437
|
+
} from "@moltenship/calendar-recurrence";
|
|
438
|
+
import { useState, type MouseEvent } from "react";
|
|
439
|
+
|
|
440
|
+
import { calendarAnnouncements } from "@/components/calendar/announcements";
|
|
441
|
+
import { CalendarToolbar } from "@/components/calendar/calendar-toolbar";
|
|
442
|
+
import { useRecurrenceScopePrompt } from "@/components/calendar/recurrence-scope-dialog/use-recurrence-scope-prompt";
|
|
443
|
+
import { Button } from "@/components/ui/button";
|
|
444
|
+
|
|
445
|
+
import { adapters, Calendar, type TeamEvent, type TeamOccurrence } from "./recurrence-calendar";
|
|
446
|
+
import { useOccurrences } from "./use-occurrences";
|
|
447
|
+
|
|
448
|
+
type OccurrenceChange = CalendarEventChange<TeamOccurrence, string, string>;
|
|
449
|
+
|
|
450
|
+
/** A selection or focus on an occurrence, moved to the occurrence's new ID after an edit. */
|
|
451
|
+
function renamed<T extends CalendarSelection | CalendarFocus>(
|
|
452
|
+
current: T | null,
|
|
453
|
+
ids: ReadonlyMap<EventId, string>,
|
|
454
|
+
): T | null {
|
|
455
|
+
return current?.kind === "event"
|
|
456
|
+
? { ...current, eventId: ids.get(current.eventId) ?? current.eventId }
|
|
457
|
+
: current;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
const weekRules = { firstDay: 1, minimalDays: 4, weekend: [6, 7] } as const;
|
|
461
|
+
|
|
462
|
+
interface TeamCalendarProps {
|
|
463
|
+
readonly initialEvents: readonly TeamEvent[];
|
|
464
|
+
readonly timeZone: string;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
export function TeamCalendar({ initialEvents, timeZone }: TeamCalendarProps) {
|
|
468
|
+
const [today] = useState(() => Temporal.Now.plainDateISO(timeZone));
|
|
469
|
+
const [events, setEvents] = useState(initialEvents);
|
|
470
|
+
const [selection, setSelection] = useState<CalendarSelection | null>(null);
|
|
471
|
+
const [focus, setFocus] = useState<CalendarFocus | null>(null);
|
|
472
|
+
const { occurrences, onVisibleRangeChange } = useOccurrences(
|
|
473
|
+
events,
|
|
474
|
+
timeZone,
|
|
475
|
+
weekRange(today, { weekStartsOn: weekRules.firstDay }),
|
|
476
|
+
);
|
|
477
|
+
const { ask, dialog } = useRecurrenceScopePrompt();
|
|
478
|
+
|
|
479
|
+
async function onEventChange(change: OccurrenceChange) {
|
|
480
|
+
// A new event, or a change to a one-off event, needs no question.
|
|
481
|
+
let scope: RecurrenceEditScope = "this";
|
|
482
|
+
if (isRecurringChange(change)) {
|
|
483
|
+
const scopes = recurrenceEditScopes(change, adapters, { timeZone });
|
|
484
|
+
// None applies, so there's nothing to ask: tell the user why instead. Throwing rolls the
|
|
485
|
+
// change back.
|
|
486
|
+
if (scopes.length === 0) throw new Error("This repeating event can't change that way.");
|
|
487
|
+
const picked = await ask({
|
|
488
|
+
action: change.cause === "delete" ? "delete" : "edit",
|
|
489
|
+
eventTitle: change.event.event.title,
|
|
490
|
+
scopes,
|
|
491
|
+
});
|
|
492
|
+
// An `AbortError` is announced as a cancel, not as a change that couldn't be saved.
|
|
493
|
+
if (picked === null) throw new DOMException("Cancelled", "AbortError");
|
|
494
|
+
scope = picked;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
const edit = resolveRecurrenceEdit(change, scope, adapters, {
|
|
498
|
+
// Picked now, so the IDs of a "following" edit's new series are known.
|
|
499
|
+
newSeriesId: crypto.randomUUID(),
|
|
500
|
+
occurrences,
|
|
501
|
+
// The zone the calendar moves occurrences on, so a series dragged by days keeps its time.
|
|
502
|
+
timeZone,
|
|
503
|
+
});
|
|
504
|
+
if (edit.type === "rejected") throw new Error(edit.message);
|
|
505
|
+
|
|
506
|
+
// Save the edit on your server here. The change stays pending until this function returns.
|
|
507
|
+
if (edit.type === "event") {
|
|
508
|
+
setEvents((current) => applyEventChange(current, edit.change));
|
|
509
|
+
return;
|
|
510
|
+
}
|
|
511
|
+
setEvents((current) =>
|
|
512
|
+
applyRecurrenceEdit(current, edit, { createId: () => crypto.randomUUID() }),
|
|
513
|
+
);
|
|
514
|
+
// "following" and "all" give the occurrences new IDs. The selection and keyboard focus
|
|
515
|
+
// follow them, and the calendar moves focus to the occurrence under its new ID.
|
|
516
|
+
setSelection((current) => renamed(current, edit.occurrenceIds));
|
|
517
|
+
setFocus((current) => renamed(current, edit.occurrenceIds));
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
// Apps open an event by delegating clicks on `[data-event-id]`.
|
|
521
|
+
function selectClicked(event: MouseEvent) {
|
|
522
|
+
const element = (event.target as Element).closest("[data-event-id]");
|
|
523
|
+
const eventId = element === null ? null : eventIdOf(element);
|
|
524
|
+
setSelection(eventId === null ? null : { eventId, kind: "event" });
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
const selected =
|
|
528
|
+
selection?.kind === "event"
|
|
529
|
+
? occurrences.find((occurrence) => occurrence.id === selection.eventId)
|
|
530
|
+
: undefined;
|
|
531
|
+
|
|
532
|
+
return (
|
|
533
|
+
<>
|
|
534
|
+
<Calendar.Root
|
|
535
|
+
events={occurrences}
|
|
536
|
+
focus={focus}
|
|
537
|
+
onFocusChange={setFocus}
|
|
538
|
+
onEventChange={onEventChange}
|
|
539
|
+
onVisibleRangeChange={onVisibleRangeChange}
|
|
540
|
+
selection={selection}
|
|
541
|
+
onSelectionChange={setSelection}
|
|
542
|
+
defaultAnchorDate={today}
|
|
543
|
+
defaultTimeZone={timeZone}
|
|
544
|
+
locale="en-US"
|
|
545
|
+
weekRules={weekRules}
|
|
546
|
+
>
|
|
547
|
+
<Calendar.Frame className="flex h-full flex-col">
|
|
548
|
+
<CalendarToolbar calendar={Calendar} className="border-b" />
|
|
549
|
+
<Calendar.Viewport className="min-h-0 flex-1" onClick={selectClicked}>
|
|
550
|
+
<Calendar.ActiveView />
|
|
551
|
+
</Calendar.Viewport>
|
|
552
|
+
<SelectedEvent occurrence={selected} />
|
|
553
|
+
<Calendar.Announcer messages={calendarAnnouncements} />
|
|
554
|
+
</Calendar.Frame>
|
|
555
|
+
</Calendar.Root>
|
|
556
|
+
{dialog}
|
|
557
|
+
</>
|
|
558
|
+
);
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
/** A change to a one-off event, or a new event, as any calendar applies it. */
|
|
562
|
+
function applyEventChange(
|
|
563
|
+
events: readonly TeamEvent[],
|
|
564
|
+
change: CalendarEventChange<TeamEvent, string, string>,
|
|
565
|
+
): readonly TeamEvent[] {
|
|
566
|
+
if (change.cause === "delete") return events.filter((event) => event.id !== change.eventId);
|
|
567
|
+
const { resourceIds: _resourceIds, ...placement } = change.proposed;
|
|
568
|
+
if (change.cause === "create") {
|
|
569
|
+
const id = crypto.randomUUID();
|
|
570
|
+
return [...events, { data: { color: "blue" }, id, title: "New event", ...placement }];
|
|
571
|
+
}
|
|
572
|
+
return events.map((event) =>
|
|
573
|
+
event.id === change.eventId
|
|
574
|
+
? { data: event.data, id: event.id, title: event.title, ...placement }
|
|
575
|
+
: event,
|
|
576
|
+
);
|
|
577
|
+
}
|
|
578
|
+
```
|
|
579
|
+
|
|
580
|
+
## Recipes
|
|
581
|
+
|
|
582
|
+
- [Google Calendar events](./google-calendar.md): Read masters, changed and cancelled occurrences, and RRULE, EXDATE and RDATE lines from the Calendar API.
|
|
583
|
+
- [An ICS feed](./ics.md#recurring-events): Group a parsed feed into series with recurringEventsFromIcs, and expand it.
|