"use client"; import { useEffect, useMemo, useState } from "react"; import { useSearchParams, useRouter } from "next/navigation"; import DowntimeParetoCard from "@/components/analytics/DowntimeParetoCard"; import { usePathname } from "next/navigation"; import { DOWNTIME_RANGES, coerceDowntimeRange, type DowntimeRange } from "@/lib/analytics/downtimeRange"; type MachineLite = { id: string; name: string; siteName?: string | null; // optional for later }; export default function DowntimeParetoReportClient() { const sp = useSearchParams(); const router = useRouter(); const pathname = usePathname(); const [range, setRange] = useState(coerceDowntimeRange(sp.get("range"))); const [machineId, setMachineId] = useState(sp.get("machineId") || ""); const [machines, setMachines] = useState([]); const [loadingMachines, setLoadingMachines] = useState(true); // Keep URL in sync (so deep-links work) useEffect(() => { const qs = new URLSearchParams(); if (range) qs.set("range", range); if (machineId) qs.set("machineId", machineId); const next = `${pathname}?${qs.toString()}`; const current = `${pathname}?${sp.toString()}`; // avoid needless replace loops if (next !== current) router.replace(next); // eslint-disable-next-line react-hooks/exhaustive-deps }, [range, machineId, pathname]); useEffect(() => { let cancelled = false; async function loadMachines() { setLoadingMachines(true); try { // Use whatever endpoint you already have for listing machines: // If you don’t have one, easiest is GET /api/machines returning [{id,name}] const res = await fetch("/api/machines", { credentials: "include" }); const json = await res.json(); if (!cancelled && res.ok) setMachines(json.machines ?? json ?? []); } finally { if (!cancelled) setLoadingMachines(false); } } loadMachines(); return () => { cancelled = true; }; }, []); const machineOptions = useMemo(() => { return [{ id: "", name: "All machines" }, ...machines]; }, [machines]); return (
Downtime Pareto
Org-wide report with drilldown
); }