Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 6x 6x 3x 3x 3x 11x 11x 11x 1x 1x 1x 1x 1x 11x 11x 11x 1x 11x 11x 1x 11x 11x 11x 257x 209x 174x 58x 11x 11x 11x 9x 108x 1x 1x 1x 1x 9x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 132x 132x 132x 8x 8x 8x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 8x 8x 8x 8x 8x 124x 103x 21x 132x 132x 132x 11x 11x 11x 11x | 'use client';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { CURRENT_YEAR, ENV, MIN_YEAR } from '@/constants';
import { BaseVariant } from '@/types';
import {
getDecadeYears,
parseDateFromYYYYMMDD,
toggleYearSelection,
} from '@/utils/calendar';
import { combineClasses } from '@shared/utils';
import { Button } from '@/components/Calendar/Shadcn/button';
import { DecadeNavigator } from '@/components/DecadeNavigator';
import '../styles/year-calendar.css';
// Same color palette as MonthCalendar
const selectionColors = ['bg-blue-500', 'bg-secondary-350', 'bg-secondary-400'];
interface YearCalendarProps {
selectedYears?: number[];
minYear?: number;
maxYear?: number; // default = current year
onYearsChange?: (years: number[]) => void;
className?: string;
variant?: BaseVariant;
}
const variantTheme = {
[BaseVariant.PRIMARY]: {
root: 'year-calendar',
yearGrid: 'year-calendar-grid',
yearButton: 'year-calendar-button',
yearButtonSelected: 'year-calendar-button-selected',
yearButtonDisabled: 'year-calendar-button-disabled',
yearButtonDefault: 'year-calendar-button-default',
},
} as const;
export const YearCalendar = ({
selectedYears = [],
minYear = MIN_YEAR,
maxYear = CURRENT_YEAR,
onYearsChange,
className,
variant = BaseVariant.PRIMARY,
}: YearCalendarProps) => {
const theme = variantTheme[variant];
const [selected, setSelected] = useState<number[]>(selectedYears);
const goLiveDate = parseDateFromYYYYMMDD(ENV.GO_LIVE_DATE);
const goLiveYear = goLiveDate ? goLiveDate.getFullYear() : MIN_YEAR;
const firstSelectedYear = selectedYears[0] ?? CURRENT_YEAR;
// Track decade separately
const [decadeStart, setDecadeStart] = useState(
() => Math.floor(firstSelectedYear / 10) * 10,
);
const decadeYears = useMemo(() => getDecadeYears(decadeStart), [decadeStart]);
useEffect(() => {
setSelected(selectedYears);
if (selectedYears.length > 0) {
const yearToShow = selectedYears[0]!;
setDecadeStart(Math.floor(yearToShow / 10) * 10);
}
}, [selectedYears]);
const toggleYear = useCallback(
(year: number) => {
if (selected.includes(year)) return;
const updated = toggleYearSelection({ selected, year, minYear, maxYear });
setSelected(updated);
onYearsChange?.(updated);
},
[selected, minYear, maxYear, onYearsChange],
);
const handleNextDecade = useCallback(() => {
setDecadeStart((prev) => prev + 10);
}, []);
const handlePreviousDecade = useCallback(() => {
setDecadeStart((prev) => prev - 10);
}, []);
const isYearDisabled = useCallback(
(year: number) =>
year > maxYear ||
year < minYear ||
year < goLiveYear ||
year === CURRENT_YEAR,
[maxYear, minYear, goLiveYear],
);
const yearClickHandlers = useMemo(() => {
return decadeYears.map((year) => {
return () => {
const isSelected = selected.includes(year);
if (isYearDisabled(year) || isSelected) return;
toggleYear(year);
};
});
}, [decadeYears, selected, minYear, maxYear, toggleYear]);
return (
<div
data-testid="year-calendar"
data-variant={variant}
className={combineClasses(theme.root, className)}
>
{/* Decade Navigation */}
<DecadeNavigator
decadeStart={decadeStart}
minYear={minYear}
maxYear={maxYear}
onPrevious={handlePreviousDecade}
onNext={handleNextDecade}
/>
{/* Year Grid */}
<div className={theme.yearGrid}>
{decadeYears.map((year, index) => {
const isSelected = selected.includes(year);
let bgColor = '';
if (isSelected) {
const order = selected.indexOf(year);
if (order >= 0) bgColor = selectionColors[order] ?? '';
}
return (
<Button
data-testid="year-option"
key={year}
variant="ghost"
onClick={yearClickHandlers[index]}
disabled={isYearDisabled(year)}
className={combineClasses(
theme.yearButton,
isSelected
? combineClasses(
bgColor,
`hover:${bgColor}`,
theme.yearButtonSelected,
)
: isYearDisabled(year)
? theme.yearButtonDisabled
: theme.yearButtonDefault,
)}
>
{year}
</Button>
);
})}
</div>
</div>
);
};
|