import { useState } from "react"; import { useTranslation } from "react-i18next"; import { Link } from "react-router-dom"; import { ArrowLeft } from "lucide-react"; import PeriodSelector from "../components/dashboard/PeriodSelector"; import CompareModeTabs from "../components/reports/CompareModeTabs"; import CompareSubModeToggle from "../components/reports/CompareSubModeToggle"; import CompareReferenceMonthPicker from "../components/reports/CompareReferenceMonthPicker"; import ComparePeriodTable from "../components/reports/ComparePeriodTable"; import ComparePeriodChart from "../components/reports/ComparePeriodChart"; import CompareBudgetView from "../components/reports/CompareBudgetView"; import ViewModeToggle, { readViewMode, type ViewMode } from "../components/reports/ViewModeToggle"; import { useCompare, comparisonMeta } from "../hooks/useCompare"; import { useReportsPeriod } from "../hooks/useReportsPeriod"; const STORAGE_KEY = "reports-viewmode-compare"; function formatMonthLabel(year: number, month: number, language: string): string { const date = new Date(year, month - 1, 1); return new Intl.DateTimeFormat(language === "fr" ? "fr-CA" : "en-CA", { month: "long", year: "numeric", }).format(date); } export default function ReportsComparePage() { const { t, i18n } = useTranslation(); const { period, setPeriod, from, to, setCustomDates } = useReportsPeriod(); const { mode, subMode, setMode, setSubMode, setReferencePeriod, year, month, rows, isLoading, error, } = useCompare(); const [viewMode, setViewMode] = useState(() => readViewMode(STORAGE_KEY)); const preserveSearch = typeof window !== "undefined" ? window.location.search : ""; const { previousYear, previousMonth: prevMonth } = comparisonMeta(subMode, year, month); // Monthly block labels: a specific month on both sides. For MoM the // previous = previous month of same year; for YoY the previous = same // month one year earlier (comparisonMeta already resolves both). const currentLabel = formatMonthLabel(year, month, i18n.language); const previousLabel = formatMonthLabel(previousYear, prevMonth, i18n.language); // Cumulative YTD block labels. For MoM both sides live in the same year // but the previous side only covers up to the end of the previous month, // so we surface the end-of-window month label on each side. For YoY we // compare Jan→refMonth across two different years; the year + month label // makes the window boundary unambiguous. const cumulativeCurrentLabel = subMode === "mom" ? `→ ${formatMonthLabel(year, month, i18n.language)}` : `${String(year)} → ${formatMonthLabel(year, month, i18n.language)}`; const cumulativePreviousLabel = subMode === "mom" ? `→ ${formatMonthLabel(previousYear, prevMonth, i18n.language)}` : `${String(previousYear)} → ${formatMonthLabel(previousYear, prevMonth, i18n.language)}`; const showActualControls = mode === "actual"; return (

{t("reports.hub.compare")}

{showActualControls && ( )}
{showActualControls && ( )}
{error && (
{error}
)} {mode === "budget" ? ( ) : viewMode === "chart" ? ( ) : ( )}
); }