import { ChevronLeftIcon, ChevronRightIcon } from "lucide-react"; import { useEffect, useMemo, useState } from "react"; import { useTranslation } from "react-i18next"; import { buttonVariants } from "@/components/ui/button"; import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; import { cn } from "@/lib/utils"; import { useTranslate } from "@/utils/i18n"; import { CalendarLink } from "./CalendarLink"; import { buildCalendarPath, buildMonthKey, getMonthOfDate } from "./paths"; export interface MonthPickerProps { /** `YYYY-MM` currently shown. */ month: string; monthLabel: string; /** `YYYY-MM-DD` */ today: string; } const MONTHS = Array.from({ length: 12 }, (_, index) => index + 1); const TEXT_CONTROL_CLASSES = cn(buttonVariants({ variant: "quiet", size: "sm" })); const ICON_CONTROL_CLASSES = cn(buttonVariants({ variant: "quiet", size: "icon-compact" })); /** * The month title is the trigger; the popup is a year stepper over the twelve months, the * shown month filled (it is the `aria-current` month) and the current month marked. Months * are links so the browser owns history and middle-click. */ export const MonthPicker = ({ month, monthLabel, today }: MonthPickerProps) => { const t = useTranslate(); const { i18n } = useTranslation(); const [open, setOpen] = useState(false); const shownYear = Number(month.slice(0, 4)); const [year, setYear] = useState(shownYear); const currentMonth = getMonthOfDate(today); // Reopening lands on the shown month's year, not wherever the stepper was left. useEffect(() => { if (open) setYear(shownYear); }, [open, shownYear]); const monthShortLabels = useMemo( () => MONTHS.map((value) => new Date(2000, value - 1, 1).toLocaleString(i18n.language, { month: "short" })), [i18n.language], ); return ( {monthLabel}
{year}
{MONTHS.map((value) => { const key = buildMonthKey(year, value); const isShown = key === month; const isCurrent = key === currentMonth; return ( setOpen(false)} > {monthShortLabels[value - 1]} {isCurrent && ( ); })}
); };