Recurrence Rule Handling
Relevant source files
The following files were used as context for generating this wiki page:
- packages/api/src/utils/recurrences/parse.ts
- packages/api/src/utils/recurrences/export.ts
- packages/api/src/schemas/events.ts
- apps/web/src/components/event-form/recurrences/recurrence-dialog.tsx
- apps/web/src/components/event-form/recurrences/recurrence-field.tsx
- apps/web/src/components/event-form/recurrences/recurrence-suggestions.ts
- packages/api/src/providers/calendars/google-calendar/events.ts
The Recurrence Rule Handling system in Analog provides a comprehensive mechanism for defining, parsing, and exporting event recurrence patterns. It is designed to be compatible with the iCalendar specification (RFC 5545), primarily using RRULE, RDATE, and EXDATE properties. This system serves as a bridge between the user-facing components in the web application, the backend API, and external calendar providers like Google Calendar.
The core of the system is a standardized Recurrence object, which is used throughout the application. Utility functions are provided to convert this object to and from iCalendar-compliant string formats. The frontend offers a user-friendly dialog for creating custom rules, along with intelligent suggestions for common recurrence patterns.
Data Model: recurrenceSchema
The central data structure for recurrence is defined by recurrenceSchema using Zod. This schema validates and types the recurrence object attached to an event. It closely mirrors the properties available in the iCalendar RRULE format.
Sources: packages/api/src/schemas/events.ts:213-255
Schema Diagram
This diagram illustrates the structure of the recurrenceSchema object.
Fields
The table below details the properties of the recurrenceSchema.
| Field | Type | Description | iCalendar Part |
|---|---|---|---|
freq | Enum | Required. The frequency of the recurrence. | FREQ |
interval | number (int, >=1) | The interval at which the recurrence rule repeats. | INTERVAL |
count | number (int, >=1) | The number of occurrences. | COUNT |
until | PlainDate, Instant, ZonedDateTime | The end date/time of the recurrence. | UNTIL |
byDay | string[] | Specifies a list of days of the week. | BYDAY |
byMonth | number[] | Specifies a list of months of the year. | BYMONTH |
byMonthDay | number[] | Specifies a list of days of the month. | BYMONTHDAY |
byYearDay | number[] | Specifies a list of days of the year. | BYYEARDAY |
byWeekNo | number[] | Specifies a list of week numbers of the year. | BYWEEKNO |
byHour | number[] | Specifies a list of hours of the day. | BYHOUR |
byMinute | number[] | Specifies a list of minutes of the hour. | BYMINUTE |
bySecond | number[] | Specifies a list of seconds of the minute. | BYSECOND |
bySetPos | number[] | Specifies a list of positional offsets into the set of occurrences. | BYSETPOS |
wkst | Enum | The week start day. | WKST |
rDate | dateInputSchema[] | A list of additional recurrence dates. | RDATE |
exDate | dateInputSchema[] | A list of exception dates. | EXDATE |
Sources: packages/api/src/schemas/events.ts:213-255
iCalendar String Parsing
The system provides utilities to parse iCalendar property strings (RRULE, RDATE, EXDATE) into a structured Recurrence object. This is essential for interpreting data from external calendar providers.
Sources: packages/api/src/utils/recurrences/parse.ts
Parsing Flow
The fromRecurrenceProperties function orchestrates the parsing of an array of iCalendar strings.
Key Parsing Functions
fromRRule(rrule: string, timeZone: string): Parses a singleRRULE:string. It splits the string by;and then by=to extract key-value pairs, mapping them to theRecurrenceobject fields. It handles various data types, including integers, dates, and arrays.
Sources: packages/api/src/utils/recurrences/parse.ts:89-266fromRDate(rDateString: string, timeZone: string): Parses aRDATE:string, splitting by,to get individual date strings, which are then parsed intoTemporalobjects.
Sources: packages/api/src/utils/recurrences/parse.ts:275-299fromExDate(exDateString: string, timeZone: string): Similar tofromRDate, but forEXDATE:strings.
Sources: packages/api/src/utils/recurrences/parse.ts:308-332fromRecurrenceProperties(properties: string[], timeZone: string): The main entry point that takes an array of strings (e.g., from a Google Calendar event) and combines the results of the above functions into a single, completeRecurrenceobject.
Sources: packages/api/src/utils/recurrences/parse.ts:340-379
iCalendar String Exporting
To communicate with external calendar providers, the internal Recurrence object must be converted back into iCalendar-compliant strings.
Sources: packages/api/src/utils/recurrences/export.ts
Exporting Flow
The toRecurrenceProperties function converts a Recurrence object into an array of iCalendar strings.
Key Exporting Functions
toRRule(recurrence: Recurrence): Constructs theRRULE:string by iterating over theRecurrenceobject's properties and joining them with;.
Sources: packages/api/src/utils/recurrences/export.ts:17-43toRDate(rDates: ...): Constructs theRDATE:string by formatting an array ofTemporaldates and joining them with,.
Sources: packages/api/src/utils/recurrences/export.ts:49-59toExDate(exDates: ...): Similar totoRDate, but forEXDATE:strings.
Sources: packages/api/src/utils/recurrences/export.ts:65-75toRecurrenceProperties(recurrence: Recurrence): The main entry point that calls the above functions to generate a complete array of iCalendar property strings, ready to be sent to an external API.
Sources: packages/api/src/utils/recurrences/export.ts:81-99
Frontend Implementation
The web application provides a user-friendly interface for creating and managing recurrence rules, composed of several React components.
RecurrenceField Component
This component is the main entry point for the user. It displays the human-readable description of the current recurrence rule (e.g., "Every week on Monday") or "Repeat" if none is set. It uses the rrule-temporal library's toText function to generate this description. It also acts as a trigger for a dropdown menu with suggestions and a "Custom" option that opens the RecurrenceDialog.
Sources: apps/web/src/components/event-form/recurrences/recurrence-field.tsx:34-62
RecurrenceDialog Component
This dialog allows users to define complex, custom recurrence rules. It uses @tanstack/react-form to manage form state for properties like frequency, interval, end conditions (never, on a date, or after a number of occurrences), and days of the week.
Upon submission, the form's state is parsed using a Zod schema (formRecurrenceSchema) and the resulting Recurrence object is passed up via the onChange prop.
Sources: apps/web/src/components/event-form/recurrences/recurrence-dialog.tsx:68-121
Recurrence Suggestions
To simplify common use cases, the system generates a list of recurrence suggestions based on the event's start date. The generateRecurrenceSuggestions function creates options like "Every day", "Every weekday", and "Every month on the 15th". Each suggestion includes a human-readable label and a pre-configured Recurrence object.
Sources: apps/web/src/components/event-form/recurrences/recurrence-suggestions.ts:31-114
Provider Integration (Google Calendar)
The recurrence system is integrated with calendar providers to ensure seamless synchronization. The google-calendar/events.ts provider serves as a clear example.
- Parsing from Google: When fetching an event from Google Calendar, the
recurrencearray in the API response (containingRRULE,RDATE,EXDATEstrings) is passed tofromRecurrencePropertiesto create the internalRecurrenceobject.
Sources: packages/api/src/providers/calendars/google-calendar/events.ts:108-117 - Exporting to Google: When creating or updating an event, the internal
Recurrenceobject is passed totoRecurrenceProperties. The resulting array of strings is then sent in therecurrencefield of the request body to the Google Calendar API.
Sources: packages/api/src/providers/calendars/google-calendar/events.ts:255-257
This sequence diagram shows the flow of creating a recurring event and syncing it with Google Calendar.