Feature: Calendar Views and Navigation
Relevant source files
The following files were used as context for generating this wiki page:
- apps/web/src/components/calendar/day-view/day-view.tsx
- apps/web/src/components/calendar/week-view/week-view.tsx
- apps/web/src/components/calendar/month-view/month-view.tsx
- apps/web/src/components/calendar/agenda-view/agenda-view.tsx
- apps/web/src/components/calendar/utils/date-time.ts
- apps/web/src/components/calendar-view.tsx
- apps/web/src/components/calendar/header/calendar-view-title.tsx
- apps/web/src/components/calendar/hooks/use-event-collection.ts
The Calendar Views and Navigation feature is the core of the Analog application's user interface, responsible for displaying calendar events in multiple layouts and allowing users to navigate through time. It is designed as a modular system where a central component, CalendarView, dynamically renders one of four distinct views: Month, Week, Day, or Agenda.
This system is orchestrated through a combination of React components, state management with Jotai, and a set of utility functions for date manipulation and event processing. The architecture ensures that each view is optimized for its specific purpose, from a high-level monthly overview to a detailed daily schedule, while sharing common logic for navigation and data handling.
Architecture Overview
The calendar system is built around a primary CalendarView component that acts as a controller, selecting which specific view component to render based on the application's state.
This diagram shows the high-level component hierarchy.
Key Architectural Components
| Component / Hook | Description | Source File |
|---|---|---|
CalendarView | The main container that wraps all calendar views. It filters events and conditionally renders the active view component (MonthView, WeekView, etc.). | apps/web/src/components/calendar-view.tsx |
CalendarHeader | A shared header component that displays navigation controls (Next, Previous, Today) and the dynamic view title. | apps/web/src/components/calendar-view.tsx:81 |
useCalendarState | A custom hook that provides access to the current calendar state, including the selected view and currentDate. | apps/web/src/components/calendar-view.tsx:30 |
useEventCollection | A crucial hook that processes and organizes raw event data into a structure optimized for the currently active view (e.g., calculating positions for overlapping events). | apps/web/src/components/calendar/hooks/use-event-collection.ts |
| Jotai State Atoms | A set of global state atoms (calendarSettingsAtom, viewPreferencesAtom, etc.) used to manage user settings and preferences across the application. | apps/web/src/components/calendar-view.tsx:16-20 |
Navigation and State
Navigation is handled by a set of pure functions that calculate the next or previous date based on the current view. The application state, managed by Jotai, is updated, triggering a re-render of the calendar.
Navigation Logic
The core navigation logic resides in navigateToPrevious and navigateToNext functions. These functions receive the current date and the active view, and return a new Temporal.PlainDate object.
// Source: apps/web/src/components/calendar/utils/date-time.ts:21-38
export function navigateToPrevious(
currentDate: Temporal.PlainDate,
view: CalendarView,
): Temporal.PlainDate {
switch (view) {
case "month":
return currentDate.subtract({ months: 1 });
case "week":
return currentDate.subtract({ weeks: 1 });
case "day":
return currentDate.subtract({ days: 1 });
case "agenda":
return currentDate.subtract({ days: AgendaDaysToShow });
default:
return currentDate;
}
}
The following diagram illustrates the navigation flow.
View Title Generation
The title displayed in the CalendarHeader (e.g., "August 2024") is dynamically generated by the getViewTitleData function. This function, like the navigation logic, is view-aware and formats the title appropriately for month, week, day, or agenda views. The CalendarViewTitle component consumes this data to render the final title, including short and full versions for responsive display.
Sources: apps/web/src/components/calendar/utils/date-time.ts:110-129, apps/web/src/components/calendar/header/calendar-view-title.tsx:30-34
Calendar Views
The application provides four distinct calendar views, each tailored for a different level of detail.
Month View
The MonthView provides a high-level overview of an entire month in a grid format.
- Structure: It renders a grid of weeks and days, ensuring the display starts on the user-configured first day of the week. Days outside the current month are visually de-emphasized.
- Event Handling: It uses the
useMultiDayOverflowhook to manage the layout of events. This hook calculates which events can be displayed within the available space in each week's row and which should be collapsed into an "overflow indicator". - Components:
MonthViewHeader: Displays the names of the weekdays.MonthViewWeek: Renders a single week row.MonthViewDay: Renders a single day cell, including the date number and any overflow indicators.PositionedEvent: Renders a single event, positioned correctly across multiple days within a week.
The diagram below shows how events are processed for the month view.
Week View
The WeekView displays a detailed 7-day (or 5-day, if weekends are hidden) schedule with a timeline.
- Structure: A grid with days of the week as columns and hours of the day as rows. It includes a dedicated "all-day" section at the top.
- Event Handling: It uses
useEventCollectionwithviewType: 'week', which leveragescalculateWeekViewEventPositionsto compute the exact coordinates and dimensions for each timed event, correctly handling overlaps. All-day events are handled separately and displayed in theWeekViewAllDaySection. - Components:
WeekViewHeader: Displays the date and day for each column.WeekViewAllDaySection: A dedicated area for all-day and multi-day spanning events.WeekViewDayColumn: Represents a single day's column, containing its timed events.Timeline: Renders the hourly time slots on the side.
- Features: Automatically scrolls to the current time upon loading via the
useScrollToCurrentTimehook.
Sources: apps/web/src/components/calendar/week-view/week-view.tsx, apps/web/src/components/calendar/hooks/use-event-collection.ts:101-118
Day View
The DayView is a specialized version of the WeekView, focused on a single day.
- Structure: Identical to a single column of the
WeekView, showing a timeline with timed events. - Event Handling: It calls
useEventCollectionwithviewType: 'day'to get positioned events for that specific day. - Note: The implementation of
DayViewis a direct consumer of the same event positioning logic used byWeekView, but scoped to a single day.
Sources: apps/web/src/components/calendar-view.tsx:42-48, apps/web/src/components/calendar/hooks/use-event-collection.ts:120-136
Agenda View
The AgendaView presents a chronological list of upcoming events.
- Structure: A vertical list of events, grouped by day. It shows events for a configurable number of upcoming days, defined by the
AgendaDaysToShowconstant. - Logic: It iterates through the upcoming days, filters the events that occur on each day, and renders them under a formatted date header. If no events are found in the period, it displays an "EmptyAgenda" message.
- Components:
EventItem: A reusable component to display a single event's details in a list format.
Sources: apps/web/src/components/calendar/agenda-view/agenda-view.tsx
Data Flow and Event Processing
The useEventCollection hook is the centerpiece of the calendar's data processing pipeline. It acts as an adapter, transforming a flat list of event items into a data structure tailored for the specific requirements of each view.
This sequence diagram shows the data flow when a user switches views.
Conclusion
The Calendar Views and Navigation feature in Analog is a well-architected system that effectively separates concerns. State and navigation logic are centralized, while each view component is responsible for its own rendering and layout. The useEventCollection hook serves as a powerful abstraction, decoupling the view components from the complex logic of event positioning and organization. This modular design makes the system maintainable and extensible, allowing for future enhancements or the addition of new view types with minimal impact on existing functionality.