"use client"; import React from "react"; import Link from "next/link"; import { CalendarRange, RefreshCw } from "lucide-react"; import { getMonths, getYears } from "@/lib/frontend/apiClient"; import { branchPath, monthPath, yearPath } from "@/lib/frontend/routes"; import { sortNumericStringsDesc } from "@/lib/frontend/explorer/sorters"; import { formatMonthLabel } from "@/lib/frontend/explorer/formatters"; import { mapExplorerError } from "@/lib/frontend/explorer/errorMapping"; import { buildLoginUrl, LOGIN_REASONS } from "@/lib/frontend/authRedirect"; import { useExplorerQuery } from "@/lib/frontend/hooks/useExplorerQuery"; import { useDebouncedVisibility } from "@/lib/frontend/hooks/useDebouncedVisibility"; import { LOADING_UI_DELAY_MS } from "@/lib/frontend/ui/uxTimings"; import ExplorerBreadcrumbs from "@/components/explorer/breadcrumbs/ExplorerBreadcrumbs"; import ExplorerPageShell from "@/components/explorer/ExplorerPageShell"; import ExplorerSectionCard from "@/components/explorer/ExplorerSectionCard"; import ExplorerLoading from "@/components/explorer/states/ExplorerLoading"; import ExplorerEmpty from "@/components/explorer/states/ExplorerEmpty"; import ExplorerError from "@/components/explorer/states/ExplorerError"; import ExplorerNotFound from "@/components/explorer/states/ExplorerNotFound"; import ForbiddenView from "@/components/system/ForbiddenView"; import { Button } from "@/components/ui/button"; export default function MonthsExplorer({ branch, year }) { const monthsLoadFn = React.useCallback( () => getMonths(branch, year), [branch, year], ); const monthsQuery = useExplorerQuery(monthsLoadFn, [monthsLoadFn]); const yearsLoadFn = React.useCallback(() => getYears(branch), [branch]); const yearsQuery = useExplorerQuery(yearsLoadFn, [yearsLoadFn]); const mapped = React.useMemo( () => mapExplorerError(monthsQuery.error), [monthsQuery.error], ); const showLoadingUi = useDebouncedVisibility( monthsQuery.status === "loading", { delayMs: LOADING_UI_DELAY_MS, minVisibleMs: 0, }, ); React.useEffect(() => { if (mapped?.kind !== "unauthenticated") return; const next = typeof window !== "undefined" ? `${window.location.pathname}${window.location.search}` : yearPath(branch, year); window.location.replace( buildLoginUrl({ reason: LOGIN_REASONS.EXPIRED, next }), ); }, [mapped?.kind, branch, year]); const yearOptions = yearsQuery.status === "success" && Array.isArray(yearsQuery.data?.years) ? yearsQuery.data.years : null; const breadcrumbsNode = ( ); const actions = ( { monthsQuery.retry(); yearsQuery.retry(); }} title="Aktualisieren" > Aktualisieren ); if (showLoadingUi) { return ( ); } if (monthsQuery.status === "loading") { return ( ); } if (monthsQuery.status === "error" && mapped) { if (mapped.kind === "forbidden") return ; if (mapped.kind === "notfound") { return ( ); } if (mapped.kind === "unauthenticated") { return ( ); } return ( ); } const months = Array.isArray(monthsQuery.data?.months) ? monthsQuery.data.months : []; const sorted = sortNumericStringsDesc(months); return ( {sorted.length} Monat{sorted.length === 1 ? "" : "e"} } > {sorted.length === 0 ? ( ) : ( {sorted.map((m) => ( {formatMonthLabel(m)} ))} )} ); }