Files
Ben Gotow 6760c0fd1e Implement Calendar event syncback for changes (#2574)
* Add implementation plan for calendar ICS helpers and recurring event dialog

This plan addresses two issues with calendar event drag persistence:

1. ICS data inconsistency - currently only cached fields are updated,
   not the underlying ICS data. Plan includes a new ics-event-helpers.ts
   module with functions to create, update, and modify ICS data.

2. Missing recurring event handling - no dialog asking whether to modify
   this occurrence or all occurrences. Plan includes a confirmation
   dialog component and integration with the drag persistence flow.

* Implement ICS helpers and recurring event dialog for calendar events

This change ensures calendar event modifications properly update the
underlying ICS data, not just the cached recurrenceStart/recurrenceEnd fields.

New files:
- ics-event-helpers.ts: Centralized helpers for ICS manipulation
  - createICSString: Create new ICS events from scratch
  - updateEventTimes: Update DTSTART/DTEND in existing ICS
  - createRecurrenceException: Create EXDATE + exception event
  - updateRecurringEventTimes: Shift entire recurring series
  - isRecurringEvent/getRecurrenceInfo: Query recurrence status

- recurring-event-dialog.ts: Dialog asking "this occurrence only"
  vs "all occurrences" when modifying recurring events

Modified files:
- mailspring-calendar.tsx: Updated _persistDragChange to use ICS helpers
  and show recurring event dialog. Added separate methods for simple
  event changes, single occurrence exceptions, and all-occurrences updates.
  Undo/redo now properly restores ICS data.

- calendar-event-popover.tsx: Updated saveEdits to use ICS helpers
  and handle recurring events with the same dialog flow.

- mailspring-exports.js/d.ts: Export ICSEventHelpers module

Fixes the issue where dragging events appeared to move them but the
underlying ICS data was not updated, causing sync inconsistencies.

* Add remaining work plan for calendar ICS implementation

Documents the prioritized list of remaining issues identified during
code review, including:
- High: recurring event deletion, quick event ICS generation, undo fix
- Medium: deprecated lifecycle, race conditions, timezone handling
- Low: validation, null checks, localization

Includes implementation details and recommended order of work.

* Add ICS generation, recurring deletion dialog, and fix deprecated lifecycle

Three improvements to calendar event handling:

1. Quick Event Creation (quick-event-popover.tsx):
   - Now generates proper ICS data using ICSEventHelpers.createICSString()
   - Sets ics and icsuid fields on new Event models
   - Exported generateUID() from ics-event-helpers.ts

2. Recurring Event Deletion (mailspring-calendar.tsx):
   - Added recurring event dialog to _onDeleteSelectedEvents
   - "This occurrence only" adds EXDATE to master event
   - "All occurrences" deletes the entire series
   - Added addExclusionDate() helper to ics-event-helpers.ts
   - Split deletion logic into _deleteEvent, _deleteOccurrence, _deleteEntireEvent

3. React Lifecycle Fix (mailspring-calendar.tsx):
   - Changed deprecated componentWillMount to componentDidMount
   - Improves React 18 compatibility

* Improve timezone handling and add validation in ICS helpers

Timezone improvements:
- createICALTime now properly preserves timezone from original events
- Uses convertToZone for correct timezone conversion instead of just
  attaching zone after creation
- formatDateOnly now uses local date (not UTC) for all-day events to
  prevent off-by-one day errors
- createICSString now accepts optional timezone parameter
- createRecurrenceException preserves master event's timezone

Validation improvements:
- Added validateTimestamps helper to check for valid positive numbers
- Validates that end time is not before start time
- Added null checks for VEVENT component in all functions that need it
- Functions now throw descriptive errors for malformed ICS

Also renamed formatDateTime to formatDateTimeUTC for clarity.

* Replace any types with proper TypeScript interfaces and extract recurring event utility

- Add UndoBlock interface to UndoRedoStore with public queueUndoBlock method
- Export UndoBlock from mailspring-exports for external use
- Replace `any[]` attendees type with EventAttendee interface
- Fix HTMLMetaElement cast in extractNotesFromDescription
- Add proper types to EventAttendeesInput props and methods
- Create recurring-event-actions.ts shared utility with:
  - modifySimpleEvent: Update non-recurring events
  - createOccurrenceException: Create exception for single occurrence
  - modifyAllOccurrences: Shift entire recurring series
  - modifyEventWithRecurringSupport: Main entry point with dialog handling
- Refactor mailspring-calendar.tsx to use shared utility (removes ~90 lines)
- Refactor calendar-event-popover.tsx to use shared utility (removes ~70 lines)
- Remove duplicate recurring event handling code from both components

* Use task-based undo for calendar events instead of custom UndoBlock

Following the established pattern from SyncbackMetadataTask, this change
enhances SyncbackEventTask to support undo by storing original event data:

- Add undoData and taskDescription attributes to SyncbackEventTask
- Add canBeUndone getter that returns true when undoData is provided
- Implement createUndoTask() that swaps current/original state (like redo)
- Update forUpdating() to accept undoData and description parameters

Update recurring-event-actions.ts to use task-based undo:
- Capture event snapshot before modifications
- Pass undoData to SyncbackEventTask.forUpdating() calls
- Pass description for undo toast display

Remove custom UndoBlock from UndoRedoStore:
- Remove exported UndoBlock interface
- Remove queueUndoBlock method
- Keep internal UndoBlock type for task-based undo

Update mailspring-calendar.tsx:
- Remove manual _registerUndoAction method
- Update keyboard handler to pass undoData to task
- Update drag handler to pass description to shared utility
- Undo is now automatically handled by UndoRedoStore._onQueue

This aligns with how other undoable tasks work (ChangeStarredTask,
SyncbackMetadataTask) and removes the need for custom undo handling.

* Add undo-redo task pattern documentation for future implementers

- Create docs/undo-redo-task-pattern.md with comprehensive guide
- Document toggle pattern (ChangeStarredTask) and snapshot pattern
- Include implementation steps, anti-patterns, and related files
- Update CLAUDE.md to reference the new documentation

* Fix recurring event handling and undo/redo issues in calendar

- Keyboard events (arrow keys) now show recurring event dialog like drag does
- Single occurrence deletion now supports undo via undoData
- Add newData snapshot to SyncbackEventTask for reliable redo
- Override createIdenticalTask to use captured snapshots

Fixes three PR review findings:
1. Arrow key changes bypassed recurring event dialog
2. _deleteOccurrence didn't pass undoData (not undoable)
3. Redo now uses captured snapshots instead of potentially stale event refs

* Improve TypeScript typing and add user error feedback

- Add type annotation to constructor props parameter
- Show error dialogs when calendar operations fail (delete, keyboard move, drag)
- Add documentation about Event.clone() deep clone requirement for undo/redo

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-01-20 10:28:02 -06:00
..