Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

13.9 KB
Raw Blame
import './LemonCalendar.scss'

import clsx from 'clsx'
import { useValues } from 'kea'
import { Ref, forwardRef, useEffect, useState } from 'react'

import { IconChevronLeft, IconChevronRight } from '@posthog/icons'

import { dayjs } from 'lib/dayjs'
import { LemonButton, LemonButtonProps } from 'lib/lemon-ui/LemonButton'
import { ScrollableShadows } from 'lib/lemon-ui/ScrollableShadows/ScrollableShadows'
import { range } from 'lib/utils/arrays'
import { teamLogic } from 'scenes/teamLogic'

export interface LemonCalendarProps {
    /** Fired if a calendar cell is clicked */
    onDateClick?: (date: dayjs.Dayjs) => void
    /** First day of the leftmost month on display. */
    leftmostMonth?: dayjs.Dayjs
    /** Called if the user changed the month in the calendar */
    onLeftmostMonthChanged?: (date: dayjs.Dayjs) => void
    /** Describe the selection state of each date; the calendar owns the resulting styling */
    getDateState?: (opts: GetDateStateOpts) => LemonCalendarDateState
    /** Describe the selection state of each time cell; the calendar owns the resulting styling */
    getTimeState?: (opts: GetTimeStateOpts) => LemonCalendarTimeState
    /** Number of months */
    months?: number
    /** 0 or unset for Sunday, 1 for Monday. */
    weekStartDay?: number
    /** Set the time granularity of the calendar */
    granularity?: 'day' | 'hour' | 'minute'
    /** Use 24-hour format instead of 12-hour with AM/PM */
    use24HourFormat?: boolean
}

export interface GetDateStateOpts {
    date: dayjs.Dayjs
    dayIndex: number
    weekIndex: number
}

/**
 * A decoupled description of how a single date should render, mirroring Quill's calendar vocabulary.
 * `selected` takes precedence: when set, the range flags (`isStart`/`isEnd`/`isBetween`) are ignored.
 */
export interface LemonCalendarDateState {
    /** Disable the date and explain why on hover */
    disabledReason?: string
    /** Render as a selected single date; wins over the range flags below */
    selected?: boolean
    /** Render as the start boundary of a range */
    isStart?: boolean
    /** Render as the end boundary of a range */
    isEnd?: boolean
    /** Render as a date sitting between the range boundaries */
    isBetween?: boolean
}

export interface GetTimeStateOpts {
    unit: 'h' | 'm' | 'a'
    value: number | string
}

/** A decoupled description of how a single time cell should render. */
export interface LemonCalendarTimeState {
    active?: boolean
    disabledReason?: string
    onClick?: () => void
}

export function timeDataAttr({ unit, value }: GetTimeStateOpts): string {
    return `${value}-${unit}`
}

function timeStateToButtonProps(state: LemonCalendarTimeState, opts: GetTimeStateOpts): LemonButtonProps {
    return {
        active: state.active,
        disabledReason: state.disabledReason,
        onClick: state.onClick,
        className: 'rounded-none',
        'data-attr': timeDataAttr(opts),
    }
}

function dateStateToButtonProps(
    state: LemonCalendarDateState,
    defaultProps: LemonButtonProps,
    dayIndex: number
): LemonButtonProps {
    const props: LemonButtonProps = { ...defaultProps, disabledReason: state.disabledReason }
    const isFirstDayOfWeek = dayIndex === 0
    const isLastDayOfWeek = dayIndex === 6

    if (state.selected) {
        return { ...props, status: 'default', type: 'primary' }
    }

    if (state.isStart || state.isEnd) {
        return {
            ...props,
            className:
                state.isStart && state.isEnd
                    ? props.className
                    : clsx(
                          props.className,
                          {
                              'rounded-r-none': state.isStart && !isLastDayOfWeek,
                              'rounded-l-none': state.isEnd && !isFirstDayOfWeek,
                          },
                          'LemonCalendar__range--boundary'
                      ),
            type: 'primary',
        }
    }

    if (state.isBetween) {
        return {
            ...props,
            className: clsx(
                props.className,
                isFirstDayOfWeek ? 'rounded-r-none' : isLastDayOfWeek ? 'rounded-l-none' : 'rounded-none'
            ),
            active: true,
        }
    }

    return props
}

const dayLabels = ['su', 'mo', 'tu', 'we', 'th', 'fr', 'sa']

export const LemonCalendar = forwardRef(function LemonCalendar(
    { granularity = 'day', use24HourFormat = false, ...props }: LemonCalendarProps,
    ref: Ref<HTMLDivElement>
): JSX.Element {
    const { weekStartDay: teamWeekStartDay } = useValues(teamLogic)

    const months = Math.max(props.months ?? 1, 1)
    const weekStartDay = props.weekStartDay ?? teamWeekStartDay
    const today = dayjs().startOf('day')

    const [leftmostMonth, setLeftmostMonth] = useState<dayjs.Dayjs>(() =>
        (props.leftmostMonth ?? today).startOf('month')
    )
    useEffect(() => {
        if (props.leftmostMonth && props.leftmostMonth.isSame(leftmostMonth, 'd')) {
            setLeftmostMonth(props.leftmostMonth)
        }
    }, [props.leftmostMonth]) // oxlint-disable-line react-hooks/exhaustive-deps

    const timeButtonProps = (opts: GetTimeStateOpts): LemonButtonProps | undefined => {
        const timeState = props.getTimeState?.(opts)
        return timeState ? timeStateToButtonProps(timeState, opts) : undefined
    }

    return (
        <div
            ref={ref}
            className={clsx(
                'LemonCalendar relative flex items-start gap-4 tabular-nums',
                `LemonCalendar--${granularity}`
            )}
            data-attr="lemon-calendar"
        >
            {range(0, months).map((month) => {
                const startOfMonth = leftmostMonth.add(month, 'month').startOf('month')
                // need to add a day because of https://github.com/iamkun/dayjs/issues/2007
                // calling endOf('month') on startOfMonth goes to the end of the previous month
                const endOfMonth = startOfMonth.add(1, 'day').endOf('month')
                const firstDay = startOfMonth.subtract((startOfMonth.day() - weekStartDay + 7) % 7, 'days')
                const lastDay = endOfMonth.add((((weekStartDay + 6) % 7) - endOfMonth.day() + 7) % 7, 'days')
                const weeks = lastDay.diff(firstDay, 'week') + 1
                const showLeftMonth = month === 0
                const showRightMonth = month + 1 === months

                return (
                    <table className="LemonCalendar__month" key={month} data-attr="lemon-calendar-month">
                        <thead>
                            <tr className="LemonCalendar__month-header">
                                <th className="relative">
                                    {showLeftMonth && (
                                        <LemonButton
                                            fullWidth
                                            data-attr="lemon-calendar-month-previous"
                                            className="absolute-left"
                                            onClick={() => {
                                                const newDate = leftmostMonth.subtract(1, 'month')
                                                setLeftmostMonth(newDate)
                                                props.onLeftmostMonthChanged?.(newDate)
                                            }}
                                            icon={<IconChevronLeft />}
                                        />
                                    )}
                                </th>
                                <th
                                    className="relative font-title font-semibold text-secondary uppercase cursor-default text-center"
                                    data-attr={`lemon-calendar-month-title-${month}`}
                                    colSpan={5}
                                >
                                    {startOfMonth.format('MMMM')} {startOfMonth.year()}
                                </th>
                                <th className="relative">
                                    {showRightMonth && (
                                        <LemonButton
                                            fullWidth
                                            data-attr="lemon-calendar-month-next"
                                            className="absolute-right"
                                            onClick={() => {
                                                const newDate = leftmostMonth.add(1, 'month')
                                                setLeftmostMonth(newDate)
                                                props.onLeftmostMonthChanged?.(newDate)
                                            }}
                                            icon={<IconChevronRight />}
                                        />
                                    )}
                                </th>
                            </tr>
                            <tr>
                                {range(0, 7).map((day) => (
                                    <th key={day} className="py-2 text-xs font-bold text-secondary uppercase">
                                        {dayLabels[firstDay.add(day, 'day').day()]}
                                    </th>
                                ))}
                            </tr>
                        </thead>
                        <tbody>
                            {range(0, weeks).map((week) => (
                                <tr key={week} data-attr="lemon-calendar-week">
                                    {range(0, 7).map((day) => {
                                        const date = firstDay.add(week * 7 + day, 'day')
                                        const defaultProps: LemonButtonProps = {
                                            className: clsx('flex-col', {
                                                'opacity-25': date.isBefore(startOfMonth) || date.isAfter(endOfMonth),
                                                LemonCalendar__today: date.isSame(today, 'd'),
                                            }),
                                        }

                                        const dateState = props.getDateState?.({
                                            dayIndex: day,
                                            weekIndex: week,
                                            date,
                                        })
                                        const buttonProps = dateState
                                            ? dateStateToButtonProps(dateState, defaultProps, day)
                                            : defaultProps
                                        return (
                                            <td key={day}>
                                                <LemonButton
                                                    fullWidth
                                                    center
                                                    data-attr="lemon-calendar-day"
                                                    onClick={() => props.onDateClick?.(date)}
                                                    {...buttonProps}
                                                >
                                                    {date.date()}
                                                </LemonButton>
                                            </td>
                                        )
                                    })}
                                </tr>
                            ))}
                        </tbody>
                    </table>
                )
            })}
            {granularity != 'day' && (
                <div className="LemonCalendar__time absolute top-0 bottom-0 right-0 flex divide-x border-l">
                    <ScrollableShadows direction="vertical">
                        {(use24HourFormat ? range(0, 24) : [12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]).map((hour) => {
                            const buttonProps = timeButtonProps({ unit: 'h', value: hour })

                            return (
                                <LemonButton fullWidth key={hour} {...buttonProps}>
                                    <span className="w-full text-center px-2">{String(hour).padStart(2, '0')}</span>
                                </LemonButton>
                            )
                        })}
                        <div className="LemonCalendar__time--scroll-spacer" />
                    </ScrollableShadows>
                    {granularity === 'minute' && (
                        <ScrollableShadows direction="vertical">
                            {range(0, 60).map((minute) => {
                                const buttonProps = timeButtonProps({ unit: 'm', value: minute })
                                return (
                                    <LemonButton fullWidth key={minute} {...buttonProps}>
                                        <span className="w-full text-center px-2">
                                            {String(minute).padStart(2, '0')}
                                        </span>
                                    </LemonButton>
                                )
                            })}
                            <div className="LemonCalendar__time--scroll-spacer" />
                        </ScrollableShadows>
                    )}
                    {!use24HourFormat && (
                        <div>
                            <LemonButton fullWidth {...timeButtonProps({ unit: 'a', value: 'am' })}>
                                <span className="w-full text-center">AM</span>
                            </LemonButton>
                            <LemonButton fullWidth {...timeButtonProps({ unit: 'a', value: 'pm' })}>
                                <span className="w-full text-center">PM</span>
                            </LemonButton>
                        </div>
                    )}
                </div>
            )}
        </div>
    )
})