'use client'; /* Origin: marketing-stats (96S1), verbatim. */ import { useState, useRef, useEffect } from 'react'; import { CalendarDays, ChevronDown, X } from 'lucide-react'; import { MonthGrid } from './MonthGrid'; import { businessToday, formatUtcMonthLabel, isFutureBusinessMonth, utcMonthStart } from '@/lib/dateUtils'; import { useDismiss } from '@/lib/use-dismiss'; interface MonthPickerProps { // YYYY-MM, or null/undefined for no selection. value: string | null | undefined; onChange: (value: string | null) => void; placeholder?: string; className?: string; // Show the "X" clear affordance / "Clear" footer action. Turn off when the // consumer always needs a month selected (e.g. a required field). allowClear?: boolean; // Locks the field: the popup can't be opened and the value can't be cleared. // For form fields that are part of a record's identity and therefore // read-only while editing (see balances/BalanceForm.tsx). disabled?: boolean; // Which months are greyed out in the grid. Defaults to "no future months", // the rule every filter on this app uses. A form can tighten it — e.g. // balances also excludes the CURRENT month, since a month's closing balance // isn't final until the month has ended. isMonthDisabled?: (year: number, month: number) => boolean; } function parseMonthValue(value: string | null | undefined): { year: number; month: number } | null { if (!value) return null; const [y, m] = value.split('-').map(Number); if (!y || !m) return null; return { year: y, month: m - 1 }; } function formatMonthValue(year: number, month: number): string { return `${year}-${String(month + 1).padStart(2, '0')}`; } // The same formatter MonthRangePicker uses, fed a UTC-anchored month start so // the two triggers cannot spell the same month differently. function formatLabel(year: number, month: number): string { return formatUtcMonthLabel(utcMonthStart(year, month)); } export default function MonthPicker({ value, onChange, placeholder = 'Select month', className = '', allowClear = true, disabled = false, isMonthDisabled = isFutureBusinessMonth, }: MonthPickerProps) { const [isOpen, setIsOpen] = useState(false); const parsed = parseMonthValue(value); // The year currently browsed in the grid — independent of the applied // value, so browsing to a different year doesn't select anything on its own. // Defaults to the current GMT+8 year (the reporting timezone), not the // browser's — see dateUtils' BUSINESS_TIMEZONE notes. const [viewYear, setViewYear] = useState(parsed?.year ?? businessToday().year); const popupRef = useRef(null); useEffect(() => { if (parsed) setViewYear(parsed.year); }, [value]); useDismiss(popupRef, isOpen, () => setIsOpen(false)); const handlePickMonth = (month: number) => { if (isMonthDisabled(viewYear, month)) return; onChange(formatMonthValue(viewYear, month)); setIsOpen(false); }; const handleClear = () => { onChange(null); setIsOpen(false); }; return (
{isOpen && !disabled && (
handlePickMonth(m)} isMonthDisabled={isMonthDisabled} /> {allowClear && (
)}
)}
); }