feat: season filter + chart bands on Dashboard and Comparison
All checks were successful
Deploy HiHala Dashboard / deploy (push) Successful in 7s
All checks were successful
Deploy HiHala Dashboard / deploy (push) Successful in 7s
Dashboard: - Season dropdown filter (filters data by season date range) - Revenue trend chart shows colored annotation bands for each season - All downstream memos use season-filtered data Comparison: - Season presets in period selector (optgroup) - Auto-compares with same season from previous hijri year if defined - Season preset persists start/end dates in URL Added chartjs-plugin-annotation for chart bands. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -17,7 +17,7 @@ import {
|
||||
getMuseumsForDistrict,
|
||||
getLatestYear
|
||||
} from '../services/dataService';
|
||||
import type { MuseumRecord, ComparisonProps, DateRangeFilters } from '../types';
|
||||
import type { MuseumRecord, ComparisonProps, DateRangeFilters, Season } from '../types';
|
||||
|
||||
interface PresetDateRange {
|
||||
start: string;
|
||||
@@ -63,7 +63,7 @@ const generatePresetDates = (year: number): PresetDates => ({
|
||||
'full': { start: `${year}-01-01`, end: `${year}-12-31` }
|
||||
});
|
||||
|
||||
function Comparison({ data, showDataLabels, setShowDataLabels, includeVAT, setIncludeVAT }: ComparisonProps) {
|
||||
function Comparison({ data, seasons, showDataLabels, setShowDataLabels, includeVAT, setIncludeVAT }: ComparisonProps) {
|
||||
const { t } = useLanguage();
|
||||
const [searchParams, setSearchParams] = useSearchParams();
|
||||
|
||||
@@ -95,7 +95,10 @@ function Comparison({ data, showDataLabels, setShowDataLabels, includeVAT, setIn
|
||||
if (urlPreset && urlPreset !== 'custom' && dates[urlPreset]) {
|
||||
return dates[urlPreset].start;
|
||||
}
|
||||
return searchParams.get('from') || `${year}-01-01`;
|
||||
// Season presets store from/to in URL
|
||||
const fromParam = searchParams.get('from');
|
||||
if (fromParam) return fromParam;
|
||||
return `${year}-01-01`;
|
||||
});
|
||||
const [endDate, setEndDateState] = useState(() => {
|
||||
const urlPreset = searchParams.get('preset');
|
||||
@@ -105,7 +108,10 @@ function Comparison({ data, showDataLabels, setShowDataLabels, includeVAT, setIn
|
||||
if (urlPreset && urlPreset !== 'custom' && dates[urlPreset]) {
|
||||
return dates[urlPreset].end;
|
||||
}
|
||||
return searchParams.get('to') || `${year}-01-31`;
|
||||
// Season presets store from/to in URL
|
||||
const toParam = searchParams.get('to');
|
||||
if (toParam) return toParam;
|
||||
return `${year}-01-31`;
|
||||
});
|
||||
const [filters, setFiltersState] = useState(() => ({
|
||||
district: searchParams.get('district') || 'all',
|
||||
@@ -123,7 +129,7 @@ function Comparison({ data, showDataLabels, setShowDataLabels, includeVAT, setIn
|
||||
const params = new URLSearchParams();
|
||||
if (newPreset && newPreset !== 'jan') params.set('preset', newPreset);
|
||||
if (newYear && newYear !== latestYear) params.set('year', newYear.toString());
|
||||
if (newPreset === 'custom') {
|
||||
if (newPreset === 'custom' || newPreset.startsWith('season-')) {
|
||||
if (newFrom) params.set('from', newFrom);
|
||||
if (newTo) params.set('to', newTo);
|
||||
}
|
||||
@@ -136,7 +142,7 @@ function Comparison({ data, showDataLabels, setShowDataLabels, includeVAT, setIn
|
||||
const setSelectedYear = (year: number) => {
|
||||
setSelectedYearState(year);
|
||||
const newDates = generatePresetDates(year);
|
||||
if (preset !== 'custom' && newDates[preset]) {
|
||||
if (preset !== 'custom' && !preset.startsWith('season-') && newDates[preset]) {
|
||||
setStartDateState(newDates[preset].start);
|
||||
setEndDateState(newDates[preset].end);
|
||||
}
|
||||
@@ -145,7 +151,15 @@ function Comparison({ data, showDataLabels, setShowDataLabels, includeVAT, setIn
|
||||
|
||||
const setPreset = (value: string) => {
|
||||
setPresetState(value);
|
||||
if (value !== 'custom' && presetDates[value]) {
|
||||
if (value.startsWith('season-')) {
|
||||
const seasonId = parseInt(value.replace('season-', ''));
|
||||
const season = seasons.find(s => s.Id === seasonId);
|
||||
if (season) {
|
||||
setStartDateState(season.StartDate);
|
||||
setEndDateState(season.EndDate);
|
||||
updateUrl(value, season.StartDate, season.EndDate, filters, selectedYear);
|
||||
}
|
||||
} else if (value !== 'custom' && presetDates[value]) {
|
||||
setStartDateState(presetDates[value].start);
|
||||
setEndDateState(presetDates[value].end);
|
||||
updateUrl(value, null, null, filters, selectedYear);
|
||||
@@ -227,13 +241,29 @@ function Comparison({ data, showDataLabels, setShowDataLabels, includeVAT, setIn
|
||||
const availableMuseums = useMemo(() => getMuseumsForDistrict(data, filters.district), [data, filters.district]);
|
||||
|
||||
// Year-over-year comparison: same dates, previous year
|
||||
const ranges = useMemo(() => ({
|
||||
curr: { start: startDate, end: endDate },
|
||||
prev: {
|
||||
// For season presets, try to find the same season name from the previous hijri year
|
||||
const ranges = useMemo(() => {
|
||||
const curr = { start: startDate, end: endDate };
|
||||
let prev = {
|
||||
start: startDate.replace(/^(\d{4})/, (_: string, y: string) => String(parseInt(y) - 1)),
|
||||
end: endDate.replace(/^(\d{4})/, (_: string, y: string) => String(parseInt(y) - 1))
|
||||
};
|
||||
|
||||
if (preset.startsWith('season-')) {
|
||||
const seasonId = parseInt(preset.replace('season-', ''));
|
||||
const currentSeason = seasons.find(s => s.Id === seasonId);
|
||||
if (currentSeason) {
|
||||
const prevSeason = seasons.find(
|
||||
s => s.Name === currentSeason.Name && s.HijriYear === currentSeason.HijriYear - 1
|
||||
);
|
||||
if (prevSeason) {
|
||||
prev = { start: prevSeason.StartDate, end: prevSeason.EndDate };
|
||||
}
|
||||
}
|
||||
}
|
||||
}), [startDate, endDate]);
|
||||
|
||||
return { curr, prev };
|
||||
}, [startDate, endDate, preset, seasons]);
|
||||
|
||||
const prevData = useMemo(() =>
|
||||
filterDataByDateRange(data, ranges.prev.start, ranges.prev.end, filters),
|
||||
@@ -559,9 +589,18 @@ function Comparison({ data, showDataLabels, setShowDataLabels, includeVAT, setIn
|
||||
<option value="h1">{t('time.h1')}</option>
|
||||
<option value="h2">{t('time.h2')}</option>
|
||||
<option value="full">{t('time.fullYear')}</option>
|
||||
{seasons.length > 0 && (
|
||||
<optgroup label={t('comparison.seasons') || 'Seasons'}>
|
||||
{seasons.map(s => (
|
||||
<option key={s.Id} value={`season-${s.Id}`}>
|
||||
{s.Name} {s.HijriYear}
|
||||
</option>
|
||||
))}
|
||||
</optgroup>
|
||||
)}
|
||||
</select>
|
||||
</FilterControls.Group>
|
||||
{preset !== 'custom' && (
|
||||
{preset !== 'custom' && !preset.startsWith('season-') && (
|
||||
<FilterControls.Group label={t('filters.year')}>
|
||||
<select value={selectedYear} onChange={e => setSelectedYear(parseInt(e.target.value))}>
|
||||
{availableYears.map(y => (
|
||||
@@ -570,7 +609,7 @@ function Comparison({ data, showDataLabels, setShowDataLabels, includeVAT, setIn
|
||||
</select>
|
||||
</FilterControls.Group>
|
||||
)}
|
||||
{preset === 'custom' && (
|
||||
{(preset === 'custom' || preset.startsWith('season-')) && (
|
||||
<>
|
||||
<FilterControls.Group label={t('comparison.from')}>
|
||||
<input type="date" value={startDate} onChange={e => setStartDate(e.target.value)} />
|
||||
|
||||
Reference in New Issue
Block a user