Skip to content
Source
Sign inSign up
TanStack/time

feat: calendar core #10

Openbart-krakowski(GitHub profile, opens in a new tab)wants to merge 176 commits intov0 fromrefs/pull/10/headopened

Calendar Core

Summary

The CalendarCore class provides a set of functionalities for managing calendar events, view modes, and period navigation. This class is designed to be used in various calendar applications where precise date management and event handling are required.

Motivation

The motivation behind this class is to provide a reusable and efficient core for calendar functionalities that can be integrated into different applications, ensuring consistent and simplified management of calendar events, view modes, and period navigation.

Parameters

  • weekStartsOn?: number
    • An optional number that specifies the day of the week that the calendar should start on. It defaults to 1 (Monday).
  • events?: TEvent[]
    • An optional array of events that the calendar should display.
  • viewMode: ViewMode
    • An object that specifies the initial view mode of the calendar.
  • locale?: Parameters<Temporal.PlainDate['toLocaleString']>['0']
    • An optional string that specifies the locale to use for formatting dates and times.

Returns

  • getDaysWithEvents(): Array<Day<TEvent>>
    • Returns an array of days in the current period with their associated events.
  • getDaysNames(): string[]
    • Returns an array of strings representing the names of the days of the week based on the locale and week start day.
  • changeViewMode(newViewMode: ViewMode): void
    • Changes the view mode of the calendar.
  • goToPreviousPeriod(): void
    • Navigates to the previous period based on the current view mode.
  • goToNextPeriod(): void
    • Navigates to the next period based on the current view mode.
  • goToCurrentPeriod(): void
    • Navigates to the current period.
  • goToSpecificPeriod(date: Temporal.PlainDate): void
    • Navigates to a specific period based on the provided date.
  • updateCurrentTime(): void
    • Updates the current time.
  • getEventProps(id: Event['id']): { style: CSSProperties } | null
    • Retrieves the style properties for a specific event based on its ID.
  • getCurrentTimeMarkerProps(): { style: CSSProperties; currentTime: string | undefined }
    • Retrieves the style properties and current time for the current time marker.
  • groupDaysBy(props: Omit<GroupDaysByProps<TEvent>, 'weekStartsOn'>): (Day<TEvent> | null)[][]
    • Groups the days in the current period by a specified unit. The fillMissingDays parameter can be used to fill in missing days with previous or next month's days.

Example Usage

import { CalendarCore, Event } from '@tanstack/time';
import { Temporal } from '@js-temporal/polyfill';

interface MyEvent extends Event {
  location: string;
}

const events: MyEvent[] = [
  {
    id: '1',
    startDate: Temporal.PlainDateTime.from('2024-06-10T09:00'),
    endDate: Temporal.PlainDateTime.from('2024-06-10T10:00'),
    title: 'Event 1',
    location: 'Room 101',
  },
  {
    id: '2',
    startDate: Temporal.PlainDateTime.from('2024-06-12T11:00'),
    endDate: Temporal.PlainDateTime.from('2024-06-12T12:00'),
    title: 'Event 2',
    location: 'Room 202',
  },
];

const calendarCore = new CalendarCore<MyEvent>({
  weekStartsOn: 1,
  viewMode: { value: 1, unit: 'month' },
  events,
  locale: 'en-US',
});

// Get days with events
const daysWithEvents = calendarCore.getDaysWithEvents();
console.log(daysWithEvents);

// Change view mode to week
calendarCore.changeViewMode({ value: 1, unit: 'week' });

// Navigate to the next period
calendarCore.goToNextPeriod();

// Update current time
calendarCore.updateCurrentTime();

// Get event properties
const eventProps = calendarCore.getEventProps('1');
console.log(eventProps);

// Get current time marker properties
const currentTimeMarkerProps = calendarCore.getCurrentTimeMarkerProps();
console.log(currentTimeMarkerProps);

// Group days by week
const groupedDays = calendarCore.groupDaysBy({ days: daysWithEvents, unit: 'week' });
console.log(groupedDays);

useCalendar hook

Summary

The useCalendar hook provides a comprehensive set of functionalities for managing calendar events, view modes, and period navigation.

Motivation

The motivation behind this hook is to provide a reusable and efficient way to integrate calendar functionalities into React components, ensuring consistent and simplified management of calendar events, view modes, and period navigation.

Parameters

  • weekStartsOn?: number
    • This parameter is an optional number that specifies the day of the week that the calendar should start on. It defaults to 0, which is Sunday.
  • events: Event[]
    • This parameter is an array of events that the calendar should display.
  • viewMode: 'month' | 'week' | number
    • This parameter is a string that specifies the initial view mode of the calendar. It can be either 'month', 'week', or a number representing the number of days in a custom view mode.
  • locale?: string
    • This parameter is an optional string that specifies the locale to use for formatting dates and times. It defaults to the system locale.
  • onChangeViewMode?: ({ value: number; unit: "month" | "week" | "day"; }) => void
    • This parameter is an optional callback function that is called when the view mode of the calendar changes. It receives the new view mode as an argument.
  • onChangeViewMode?: (viewMode: value: number; unit: "month" | "week" | "day";) => void
    • This parameter is an optional callback function that is called when the view mode of the calendar changes. It receives the new view mode as an argument.
  • reducer?: (state: CalendarState, action: CalendarAction) => CalendarState
    • This parameter is an optional custom reducer function that can be used to manage the state of the calendar.

Returns

  • firstDayOfPeriod: Temporal.PlainDate
    • This value represents the first day of the current period displayed by the calendar.
  • currentPeriod: string
    • This value represents a string that describes the current period displayed by the calendar.
  • goToPreviousPeriod: MouseEventHandler<HTMLButtonElement>
    • This function is a click event handler that navigates to the previous period.
  • goToNextPeriod: MouseEventHandler<HTMLButtonElement>
    • This function is a click event handler that navigates to the next period.
  • goToCurrentPeriod: MouseEventHandler<HTMLButtonElement>
    • This function is a click event handler that navigates to the current period.
  • goToSpecificPeriod: (date: Temporal.PlainDate) => void
    • This function is a callback function that is called when a date is selected in the calendar. It receives the selected date as an argument.
  • days: Day[]
    • This value represents an array of days in the current period displayed by the calendar.
  • daysNames: string[]
    • This value represents an array of strings that contain the names of the days of the week.
  • viewMode: 'month' | 'week' | number
    • This value represents the current view mode of the calendar.
  • changeViewMode: (newViewMode: 'month' | 'week' | number) => void
    • This function is used to change the view mode of the calendar.
  • getEventProps: (id: string) => { style: CSSProperties } | null
    • This function is used to retrieve the style properties for a specific event based on its ID.
  • getEventProps: (id: string) => { style: CSSProperties } | null
    • This function is used to retrieve the style properties for a specific event based on its ID.
  • getEventProps: (id: string) => { style: CSSProperties } | null
    • This function is used to retrieve the style properties for a specific event based on its ID.
  • getCurrentTimeMarkerProps: () => { style: CSSProperties, currentTime: Temporal.PlainTime }
    • This function is used to retrieve the style properties and current time for the current time marker.
  • isPending: boolean
    • This value represents whether the calendar is in a pending state.
  • groupDaysBy: ({ days: Day[], unit: 'week' | 'month', fillMissingDays?: boolean }) => Day[][]
    • This function is used to group the days in the current period by a specified unit. The fillMissingDays parameter can be used to fill in missing days with previous or next month's days.

Example Usage

const CalendarComponent = ({ events }) => {
  const {
    firstDayOfPeriod,
    currentPeriod,
    goToPreviousPeriod,
    goToNextPeriod,
    goToCurrentPeriod,
    goToSpecificPeriod,
    changeViewMode,
    days,
    daysNames,
    viewMode,
    getEventProps,
    getCurrentTimeMarkerProps,
    groupDaysBy,
  } = useCalendar({
    weekStartsOn: 1,
    viewMode: { value: 1, unit: 'month' },
    locale: 'en-US',
    onChangeViewMode: (newViewMode) => console.log('View mode changed:', newViewMode),
  });

  return (
    <div className="calendar-container">
      <div className="calendar-header">
        <button onClick={goToPreviousPeriod}>Previous</button>
        <button onClick={goToCurrentPeriod}>Today</button>
        <button onClick={goToNextPeriod}>Next</button>
      </div>
      <div className="calendar-view-mode">
        <button onClick={() => changeViewMode({ value: 1, unit: 'month' })}>Month View</button>
        <button onClick={() => changeViewMode({ value: 1, unit: 'week' })}>Week View</button>
        <button onClick={() => changeViewMode({ value: 3, unit: 'day' })}>3-Day View</button>
        <button onClick={() => changeViewMode({ value: 1, unit: 'day' })}>1-Day View</button>
      </div>
      <table className="calendar-table">
        {viewMode.unit === 'month' && (
          groupDaysBy(days, 'months').map((month, monthIndex) => (
            <tbody key={monthIndex} className="calendar-month">
              <tr>
                <th colSpan={7} className="calendar-month-name">
                  {month[0]?.date.toLocaleString('default', { month: 'long' })}{' '}
                  {month[0]?.date.year}
                </th>
              </tr>
              <tr>
                {daysNames.map((dayName, index) => (
                  <th key={index} className="calendar-day-name">
                    {dayName}
                  </th>
                ))}
              </tr>
              {groupDaysBy(month, 'weeks').map((week, weekIndex) => (
                <tr key={weekIndex} className="calendar-week">
                  {week.map((day) => (
                    <td
                      key={day.date.toString()}
                      className={`calendar-day ${day.isToday ? 'today' : ''
                        } ${day.isInCurrentPeriod ? 'current' : 'not-current'}`}
                    >
                      <div className="calendar-date">{day.date.day}</div>
                      <div className="calendar-events">
                        {day.events.map((event) => (
                          <div
                            key={event.id}
                            className="calendar-event"
                            style={getEventProps(event.id)?.style}
                          >
                            {event.title}
                          </div>
                        ))}
                      </div>
                    </td>
                  ))}
                </tr>
              ))}
            </tbody>
          ))
        )}

        {viewMode.unit === 'week' && (
          <tbody className="calendar-week">
            <tr>
              {daysNames.map((dayName, index) => (
                <th key={index} className="calendar-day-name">
                  {dayName}
                </th>
              ))}
            </tr>
            {groupDaysBy(days, 'weeks').map((week, weekIndex) => (
              <tr key={weekIndex}>
                {week.map((day) => (
                  <td
                    key={day.date.toString()}
                    className={`calendar-day ${day.isToday ? 'today' : ''
                      } ${day.isInCurrentPeriod ? 'current' : 'not-current'}`}
                  >
                    <div className="calendar-date">{day.date.day}</div>
                    <div className="calendar-events">
                      {day.events.map((event) => (
                        <div
                          key={event.id}
                          className="calendar-event"
                          style={getEventProps(event.id)?.style}
                        >
                          {event.title}
                        </div>
                      ))}
                    </div>
                  </td>
                ))}
              </tr>
            ))}
          </tbody>
        )}

        {viewMode.unit === 'day' && (
          <tbody className="calendar-day">
            <tr>
              {daysNames.map((dayName, index) => (
                <th key={index} className="calendar-day-name">
                  {dayName}
                </th>
              ))}
            </tr>
            <tr>
              {days.map((day) => (
                <td
                  key={day.date.toString()}
                  className={`calendar-day ${day.isToday ? 'today' : ''
                    } ${day.isInCurrentPeriod ? 'current' : 'not-current'}`}
                >
                  <div className="calendar-date">{day.date.day}</div>
                  <div className="calendar-events">
                    {day.events.map((event) => (
                      <div
                        key={event.id}
                        className="calendar-event"
                        style={getEventProps(event.id)?.style}
                      >
                        {event.title}
                      </div>
                    ))}
                  </div>
                </td>
              ))}
            </tr>
          </tbody>
        )}
      </table>
    </div>
  );
};
    1. Still figuring out nx and TS, so having trouble visualizing some of this. I've put down some notes on things, and hopefully they make sense. There is a lot here, so I'm sure I missed some stuff.

      A big thing is to consider the core idea laid out in the intro wiki entry as well as the core idea around TanStack projects where we deal in data and state, and let the developer decide how they render the UI. Not to say that we won't provide any UI type props at all, but that we have to carefully think through what those things might be (likely ARIA type props and event handlers).

      Please take all of this feedback only as constructive criticism. I really appreciate any assistance with this project. While I'm new to nx and TS, I've been a JS developer for 25+ years now, and have been working on scheduling applications for the better part of 20 years, so I'm speaking to you from years of experience in this area.

      If you have questions on anything, please reach out.

      @@ -0,0 +1,21 @@
      1+import { Temporal } from "@js-temporal/polyfill";
      2+import type { Day, Event } from "./types";
      3+
      4+interface GroupDaysByBaseProps<TEvent extends Event = Event> {
      5+ days: (Day<TEvent> | null)[];
      6+ weekStartsOn: number;
      7+}
      8+
      9+type GroupDaysByMonthProps<TEvent extends Event = Event> = GroupDaysByBaseProps<TEvent> & {
      10+ unit: 'month';
      11+ fillMissingDays?: never;
      12+};
      13+
      14+type GroupDaysByWeekProps<TEvent extends Event = Event> = GroupDaysByBaseProps<TEvent> & {
      15+ unit: 'week';
      16+ fillMissingDays?: boolean;
      17+};
      18+
      19+export type GroupDaysByProps<TEvent extends Event = Event> = GroupDaysByMonthProps<TEvent> | GroupDaysByWeekProps<TEvent>;
      20+
      21+export const groupDaysBy = <TEvent extends Event>({
      cutterbl(GitHub profile, opens in a new tab)commented

      This is an interesting method. An Event has both a 'start' and an 'end', meaning an Event can span units. For any given unit the 'start' may exist in one 'week' or 'month', while the 'end' may exist in another.

      The logic is a little more complex for both unit scenarios, and may be better served as two separate methods, even if those methods are called within this method.

      Also I would suggest sorting the array of 'days' before looping. This is relatively easy with Temporal's 'compare()' methods.

      Yeah, this method is intended only to group, for example, years into months and months into weeks. I think we can create a separate method for generating the date range.

      @@ -0,0 +118,21 @@
      118+ currentTime: Temporal.Now.plainDateTimeISO(this.options.timeZone),
      119+ })
      120+ }
      121+
      122+ private getFirstDayOfMonth() {
      123+ return getFirstDayOfMonth(
      124+ this.store.state.currentPeriod
      125+ .toString({ calendarName: 'auto' })
      126+ .substring(0, 7),
      127+ )
      128+ }
      129+
      130+ private getFirstDayOfWeek() {
      131+ return getFirstDayOfWeek(
      132+ this.store.state.currentPeriod.toString(),
      133+ this.options.locale,
      134+ )
      135+ }
      136+
      137+ private getCalendarDays() {
      138+ const start =
      cutterbl(GitHub profile, opens in a new tab)commented

      Another spot where 'startOf' helper methods would come in real handy

      @@ -0,0 +132,21 @@
      132+ this.store.state.currentPeriod.toString(),
      133+ this.options.locale,
      134+ )
      135+ }
      136+
      137+ private getCalendarDays() {
      138+ const start =
      139+ this.store.state.viewMode.unit === 'month'
      140+ ? this.getFirstDayOfMonth().subtract({
      141+ days:
      142+ (this.getFirstDayOfMonth().dayOfWeek -
      143+ (this.getFirstDayOfWeek().dayOfWeek + 1) +
      144+ 7) %
      145+ 7,
      146+ })
      147+ : this.store.state.currentPeriod
      148+
      149+ let end
      150+ switch (this.store.state.viewMode.unit) {
      151+ case 'month': {
      152+ const lastDayOfMonth = this.getFirstDayOfMonth()
      cutterbl(GitHub profile, opens in a new tab)commented

      and 'endOf' helper methods

    2. I wrote something up on this. This is the reasoning behind the validateDate in the utils, where a Date must be a Date, string (RFC 3339) or number (epoch). Simple types are easier to memoize against, whereas Date or Temporal objects are not.

    3. See the discussion in the time-maintainers Discord channel

      @@ -1,9 +1,18 @@
      11 import type { Temporal } from '@js-temporal/polyfill'
      22
      3+type ViewUnit = 'month' | 'week' | 'day' | 'workWeek' | 'decade' | 'year'
      4+
      5+interface EndOfParams {
      6+ date: Temporal.ZonedDateTime
      7+ unit: ViewUnit
      8+ viewModeValue?: number
      9+ firstDayOfWeek?: number
      10+}
      11+
      312 /**
      413 * Helper function to get the end of a given temporal unit.
      5- * @param date {Temporal.ZonedDateTime} - The date for which the end of the unit is needed.
      6- * @param unit {Unit} - The unit for which to find the end ('day', 'week', 'month', 'year', 'workWeek', 'decade').
      14+ * @param {EndOfParams} params - The parameters for the endOf function.
      715 * @returns {Temporal.ZonedDateTime} The end of the given unit.
      816 */
      9-export const endOf = (date: Temporal.ZonedDateTime, unit: 'day' | 'week' | 'month' | 'year' | 'workWeek' | 'decade'): Temporal.ZonedDateTime => {
      17+export function endOf({
      18+ date,
      cutterbl(GitHub profile, opens in a new tab)commented

      Like the direction here. I started a discussion in the time-maintainers discord around these types of methods...

Sign in to join this conversation.