Frontend State Management (Jotai)
Relevant source files
The following files were used as context for generating this wiki page:
- apps/web/src/atoms/store.ts
- apps/web/src/atoms/calendar-preferences.ts
- apps/web/src/atoms/selected-events.ts
- apps/web/src/atoms/view-preferences.ts
- apps/web/src/components/calendar/header/calendar-view-title.tsx
- apps/web/src/components/calendar/hooks/use-grid-layout.ts
- apps/web/src/components/calendar/week-view/week-view.tsx
The Analog web application utilizes Jotai for its frontend state management. Jotai is an atomic state management library for React, meaning that state is organized into small, isolated pieces called "atoms". This approach provides a flexible and scalable way to manage global and local state without the boilerplate of traditional state management libraries. State is composed by combining atoms, and components subscribe only to the atoms they need, which optimizes re-renders.
The state is primarily categorized into user preferences for the calendar and view, and the current selection state of events. Many atoms are persisted to localStorage to maintain user settings across sessions. A custom Jotai store is created to provide a unique state container for the application.
Sources: apps/web/package.json:80, apps/web/src/atoms/store.ts:3
Core Concepts
Custom Store
A custom Jotai store is created to encapsulate the application's state. This provides an isolated state container, which is beneficial for scenarios like server-side rendering or testing, preventing state from leaking between requests or tests.
// apps/web/src/atoms/store.ts:3
import { createStore } from "jotai";
export const store = createStore();
Sources: apps/web/src/atoms/store.ts:3-5
State Atoms
The application state is divided into logical groups of atoms, primarily for managing calendar settings, view preferences, and event selections.
Calendar and View Preferences
These atoms control the appearance and behavior of the calendar grid and its views. They are defined in calendar-preferences.ts and view-preferences.ts.
Calendar Settings
These atoms manage persistent settings related to the calendar's structure and default display.
| Atom | Description | Default Value |
|---|---|---|
calendarPreferencesAtom | An atom persisted to localStorage under the key analog.calendar. It stores user-defined preferences. | { weekStartsOn: 0, defaultView: "week", defaultTimeZone: "system" } |
calendarSettingsAtom | A derived atom that merges the user's calendarPreferencesAtom with a set of default values, ensuring a complete settings object is always available. | Merged object from calendarPreferencesAtom and defaults. |
Sources: apps/web/src/atoms/calendar-preferences.ts:12-32
The diagram below illustrates how the final calendarSettingsAtom is derived.
View Preferences
These atoms control the currently active view, date, and other transient UI toggles.
| Atom | Description | Initial Value |
|---|---|---|
viewAtom | Controls the current calendar view type. | "week" |
currentDateAtom | Holds the current date being displayed in the calendar, using Temporal.PlainDate. | Temporal.Now.plainDateISO() |
viewPreferencesAtom | An atom persisted to localStorage under the key analog.view. Stores UI toggles. | { showWeekends: true, showWeekNumbers: false } |
Sources: apps/web/src/atoms/view-preferences.ts:10-21
Event Selection State
This group of atoms, defined in selected-events.ts, manages which calendar events are currently selected by the user.
| Atom | Description | Type |
|---|---|---|
selectedEventsAtom | An atom persisted to localStorage (analog.selected-events) holding an array of selected event IDs. | string[] |
lastSelectedEventIdAtom | A session-persisted atom (analog.last-selected-event-id) tracking the ID of the last event selected. | `string |
hasSelectedEventsAtom | A derived, read-only atom that returns true if any events are selected. | boolean |
selectedEventIdsAtom | A derived, read-only atom that provides a Set<string> of selected event IDs for efficient lookups. | Set<string> |
toggleSelectEventAtom | A write-only atom to handle the logic of selecting, deselecting, and multi-selecting events. | (payload: { id: string; shiftKey: boolean }) => void |
Sources: apps/web/src/atoms/selected-events.ts:12-65
Event Selection Logic
The toggleSelectEventAtom contains the core logic for managing event selections. It supports single-item selection, toggling selection, and multi-selection using the shiftKey.
The following sequence diagram shows the flow when a user clicks on an event.
Usage in Components
Atoms are consumed within React components and hooks using Jotai's hooks like useAtomValue (for read-only access) and useSetAtom (for write-only access).
Example: CalendarViewTitle Component
The CalendarViewTitle component reads from both calendarSettingsAtom and viewPreferencesAtom to construct the correct title for the current calendar view.
// apps/web/src/components/calendar/header/calendar-view-title.tsx:28-30
export function CalendarViewTitle({ className }: CalendarViewTitleProps) {
const { currentDate, view } = useCalendarState();
const settings = useAtomValue(calendarSettingsAtom);
const viewPreferences = useAtomValue(viewPreferencesAtom);
// ... logic to generate title
}
Sources: apps/web/src/components/calendar/header/calendar-view-title.tsx:28-30
Example: useGridLayout Hook
The useGridLayout hook uses viewPreferencesAtom to dynamically adjust the CSS grid-template-columns property, hiding or showing weekend columns based on the showWeekends preference.
// apps/web/src/components/calendar/hooks/use-grid-layout.ts:26-28
export function useGridLayout(
days: Temporal.PlainDate[],
options: GridLayoutOptions = {},
) {
// ...
const viewPreferences = useAtomValue(viewPreferencesAtom);
const gridTemplateColumns = useMemo(() => {
const columnSizes = days.map((day) => {
const isDayVisible = viewPreferences.showWeekends || !isWeekend(day);
return isDayVisible ? "minmax(0,1fr)" : "0fr";
});
// ...
}, [days, viewPreferences.showWeekends, includeTimeColumn, timeColumnWidth]);
return gridTemplateColumns;
}
Sources: apps/web/src/components/calendar/hooks/use-grid-layout.ts:28-36
The diagram below shows how components and hooks subscribe to different atoms to build the UI.
Summary
The Analog frontend leverages Jotai's atomic state management to create a decoupled and maintainable architecture. By breaking down state into small, independent atoms for user preferences and UI state, the system allows for optimized re-renders and clear data flow. The use of derived atoms and atoms persisted to localStorage further enhances functionality by composing state and preserving user settings across sessions. This approach provides a robust foundation for managing the application's dynamic and interactive user interface.