import { useState } from "react"; import { useNavigate } from "react-router-dom"; import { Container, Card, Button } from "react-bootstrap"; import CustomIcon from "../CustomIcon/CustomIcon"; import PlaceholderCard from "../PlaceholderCard/PlaceholderCard"; import StatsCard from "../StatsCard/StatsCard"; //React Query import { useQuery } from "@tanstack/react-query"; //Services import dashboardService from "../../services/Dashboard"; function parseMetricViewIdFromReportUrl(reportViewUrl) { if (!reportViewUrl || typeof reportViewUrl !== "string") return null; try { const u = new URL( reportViewUrl.startsWith("http") ? reportViewUrl : `https://placeholder.local${reportViewUrl.startsWith("/") ? "" : "/"}${reportViewUrl}` ); return u.searchParams.get("id"); } catch { const m = String(reportViewUrl).match(/[?&]id=([^&]+)/); return m ? decodeURIComponent(m[1]) : null; } } function PerformanceStatsContainer() { const navigate = useNavigate(); const [isRefresh, setIsRefresh] = useState(false); /** get all actions */ const fetchDashboardUserStats = async ({ queryKey }) => { const [key, serviceInput] = queryKey; // console.log("serviceInput - ", serviceInput); try { const statsRes = await dashboardService.getDashboardUserStats( serviceInput ); if (statsRes.data.status === "success") { console.log("## Dashboard User Stats Res --", statsRes); const stats = statsRes.data?.data?.stats; return stats; } return null; } catch (error) { throw new Error("Error fetching quick actions"); } }; const { isFetching, isPending, isError, data: userStats, error, refetch, } = useQuery({ queryKey: ["dashboardUserStatsQuery", { isRefresh: isRefresh }], queryFn: fetchDashboardUserStats, }); async function handleReloadStats() { setIsRefresh(true); // triggers refetch with a new queryKey // refetch({ isRefresh: isRefresh }); } const handleStatsCardClick = (item) => { const metricViewId = item?.id ?? item?.metric_view_id ?? parseMetricViewIdFromReportUrl(item?.report_view_url); if (!metricViewId) { console.warn("Performance stat has no metric view id in report_view_url"); return; } const title = item?.value_title || item?.title || "Metric report"; const aggType = String(item?.agg_type || "").toLowerCase(); const defaultTab = aggType === "team" ? "Detail" : aggType === "individual" ? "Trend" : "Detail"; navigate("/metric-reports", { state: { metricViewId, reportTitle: title, pageTitle: title, defaultTab, }, }); }; if (isPending) { return (
{[0, 1].map((_, index) => ( ))}
); } if (isError) { return ( No more stats ); } return ( <> {isFetching && userStats?.length === 0 ? ( <>
{[0, 1].map((_, index) => ( ))}
) : ( <> {userStats?.length > 0 && (

Performance stats

{userStats.map((item, index) => ( ))}
)} )} ); } export default PerformanceStatsContainer;