Feature: Event Management (CRUD)
Relevant source files
The following files were used as context for generating this wiki page:- packages/api/src/routers/events.ts
- apps/web/src/components/calendar/flows/create-event/use-create-action.tsx
- apps/web/src/components/calendar/flows/update-event/use-update-action.tsx
- apps/web/src/components/calendar/flows/delete-event/use-delete-action.tsx
- apps/web/src/components/event-form/event-form.tsx
- packages/api/src/schemas/events.ts
- apps/web/src/lib/schemas/event-form.ts
- apps/web/src/lib/hotkeys/event-hotkeys.tsx
- apps/web/src/components/create-event-input.tsx
- apps/web/src/components/calendar/hooks/use-event-collection.ts
- packages/google-calendar/src/resources/calendars/events.ts
The Event Management system is a core feature of the Analog application, providing the full lifecycle for calendar events: Create, Read, Update, and Delete (CRUD). It is designed to be provider-agnostic, with initial support for Google and Microsoft calendars, by using a standardized internal data model.
The system comprises backend tRPC API endpoints for data manipulation, Zod schemas for robust validation on both client and server, and a suite of React components and hooks in the frontend for user interaction. This includes a detailed event form, quick-creation inputs, and optimistic UI updates for a seamless user experience.
Data Models & Schemas
The system relies on Zod for schema definition and validation, ensuring data integrity from the frontend forms to the backend API.
Backend API Schemas
The backend defines strict schemas for creating and updating events. These schemas use temporal-zod to handle Temporal date/time objects correctly.
createEventInputSchema: Defines the shape of data required to create a new event.updateEventInputSchema: Extends the creation schema, adding fields likeetagfor concurrency control and an optionalresponseobject for updating attendee status.
The diagram below illustrates the main entities and their relationships within the event data model.
This ER diagram shows the core Event entity and its relationships with Attendee, Conference, and Recurrence data.
Sources: packages/api/src/schemas/events.ts:140-250
createEventInputSchema Fields
| Field | Type | Description |
|---|---|---|
id | string | A unique identifier for the event. |
title | string (optional) | The event title. |
start | Temporal instance | The start date/time of the event. |
end | Temporal instance | The end date/time of the event. |
allDay | boolean (optional) | Indicates if it's an all-day event. |
recurrence | recurrenceSchema (optional) | Recurrence rule for the event. |
description | string (optional) | The event description. |
location | string (optional) | The event location. |
accountId | string | The ID of the user's connected account. |
calendarId | string | The ID of the calendar to add the event to. |
providerId | google | microsoft | The calendar provider. |
attendees | array (optional) | A list of event attendees. |
conference | conferenceSchema (optional) | Conference details (e.g., Google Meet). |
Sources: packages/api/src/schemas/events.ts:200-224
Frontend Form Schema
The frontend uses a separate schema, eventFormSchema, to validate the EventForm component. This schema is tailored for form inputs, using string representations for dates and times, and includes UI-specific validation logic.
eventFormSchema: Validates fields liketitle,startDate,endDate,startTime,endTime, and performs cross-field validation (e.g., ensuringendDateis afterstartDate).
// apps/web/src/lib/schemas/event-form.ts:40-70
export const eventFormSchema = baseEventFormSchema.superRefine((data, ctx) => {
const { startDate, endDate, startTime, endTime, isAllDay } = data;
// Date relationship validation
if (endDate.compare(startDate) < 0) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "End date must be after or equal to start date",
path: ["endDate"],
});
}
if (isAllDay) return;
// ... time validation logic ...
// Time relationship validation (only when not all day)
if (isSameDay(startDate, endDate) && startTime && endTime) {
const startTimeObj = parseTime(startTime);
const endTimeObj = parseTime(endTime);
// Compare times directly using Time.compare method
if (endTimeObj.compare(startTimeObj) <= 0) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "End time must be after start time",
path: ["endTime"],
});
}
}
});
Sources: apps/web/src/lib/schemas/event-form.ts:40-70
API Endpoints (tRPC)
The backend exposes a tRPC router for event management, which serves as the bridge between the client and calendar providers like Google or Microsoft.
Sources: packages/api/src/routers/events.ts
| Procedure | Input Schema | Description |
|---|---|---|
list | z.object({ ... }) | Fetches a list of events for a given account and date range. |
create | createEventInputSchema | Creates a new event in the specified provider's calendar. |
update | updateEventInputSchema | Updates an existing event. |
delete | z.object({ ... }) | Deletes an event from the provider's calendar. |
Sources: packages/api/src/routers/events.ts:16-56
The procedures internally call provider-specific services (e.g., google.events.create) to perform the requested operations.
// packages/api/src/routers/events.ts:25-33
create: protectedProcedure
.input(createEventInputSchema)
.mutation(async ({ ctx, input }) => {
const provider = getProvider(ctx.session.user.id, input.accountId);
const result = await provider.events.create(input);
return result;
}),
Sources: packages/api/src/routers/events.ts:25-33
Frontend Implementation
The frontend uses a combination of custom hooks and components to interact with the event API and manage UI state.
Event Creation
Event creation can be initiated in several ways:
- Quick Create Input: The
CreateEventInputcomponent allows users to type event details in a natural language format (e.g., "Sales meeting at @22:00"). It uses@mentions to trigger suggestions for time, date, or duration. - Event Form: The
EventFormcomponent provides a detailed interface for creating or editing events, with fields for title, dates, attendees, location, etc. - Drag to Create: In week view, users can click and drag on the calendar grid to create a new draft event for that time slot.
- Hotkey: Pressing the
ckey opens the event creation form.
The use-create-action.tsx hook encapsulates the logic for calling the events.create tRPC mutation. It handles optimistic updates to the UI, providing immediate feedback to the user while the API call is in progress.
The following sequence diagram shows the event creation flow starting from the useCreateAction hook.
This diagram illustrates the optimistic UI update flow when creating an event.
Sources: apps/web/src/components/calendar/flows/create-event/use-create-action.tsx
Event Update
Updating an event follows a similar pattern using the useUpdateAction hook. When a user edits an event in the EventForm and saves, this hook is triggered. It performs an optimistic update in the TanStack Query cache and calls the events.update tRPC mutation.
Sources: apps/web/src/components/calendar/flows/update-event/use-update-action.tsx
Event Deletion
Events can be deleted by pressing the Backspace key when an event is selected or through a delete button in the UI. A confirmation dialog (DeleteEventConfirmation) is shown to prevent accidental deletion.
The useDeleteAction hook manages the deletion process. It optimistically removes the event from the UI and calls the events.delete tRPC mutation.
This diagram shows the flow for deleting an event, including user confirmation and optimistic UI updates.
Sources: apps/web/src/lib/hotkeys/event-hotkeys.tsx:59-67, apps/web/src/components/calendar/flows/delete-event/use-delete-action.tsx
Event Display (Read)
Events are fetched via the events.list tRPC procedure. On the frontend, the useEventCollection hook is responsible for processing and organizing the raw event data for efficient rendering in different calendar views (e.g., month, week, day). It pre-filters events by the visible date range and calculates their positions for grid-based layouts like the week view.
This diagram shows the data flow for displaying events.
This flowchart shows how raw event data is processed by useEventCollection before being rendered in different calendar views.
Sources: apps/web/src/components/calendar/hooks/use-event-collection.ts, apps/web/src/components/calendar/week-view/week-view.tsx:39-41
User Interaction & Hotkeys
The application provides keyboard shortcuts to streamline event management. The EventHotkeys component uses react-hotkeys-hook to bind keys to specific actions.
| Key | Action | Scope |
|---|---|---|
c | Create a new draft event and open the editor. | event |
backspace | Open the delete confirmation dialog for the selected event. | event |
esc | Unselect the currently selected event(s). | event |
j | Join the meeting of the selected event, if a conference link exists. | event |
Sources: apps/web/src/lib/hotkeys/event-hotkeys.tsx:18-23
Summary
The Event Management (CRUD) feature is a well-architected system that separates concerns between the backend API, data validation schemas, and frontend components. It leverages tRPC for type-safe API communication and TanStack Query with optimistic updates to provide a responsive and reliable user experience. The use of provider-agnostic abstractions and detailed schemas ensures the system is both robust and extensible.