import { useEffect, useState } from "react"; import { CartesianGrid, Legend, Line, LineChart, Tooltip, XAxis, YAxis, } from "recharts"; import { useSharedContext } from "@/lib/shared-context"; import { PRData, WeeklyCount } from "@/app/Interfaces/interface"; export function PRLineChartData({ args }: any) { const { prData } = useSharedContext(); const [lineData, setLineData] = useState([]); const [xarr, setXarr] = useState([]); useEffect(() => { console.log(args); if (args?.items) { const allKeys = new Set(); args?.items ?.flat() ?.forEach(({ accessorKey }: any) => allKeys.add(accessorKey)); const merged: any = {}; args?.items?.flat()?.forEach(({ name, value, accessorKey }: any) => { if (!merged[name]) { merged[name] = { name }; allKeys.forEach((key: any) => { merged[name][key] = 0; // initialize all keys with 0 }); } merged[name][accessorKey] = value; }); const result = Object.values(merged); console.log(result, "result"); console.log( args?.items?.map((item: any) => item[0]?.accessorKey), "xarr", ); setLineData(result); setXarr(args?.items?.map((item: any) => item[0]?.accessorKey)); } }, [args?.items]); function groupPRsByWeek(prs: PRData[]): WeeklyCount[] { const weekMap: Record = {}; prs.forEach((pr) => { const date = new Date(pr.createdAt); const day = date.getUTCDay(); // 0 (Sun) to 6 (Sat) const diffToMonday = (day + 6) % 7; // get difference to previous Monday const monday = new Date(date); monday.setUTCDate(date.getUTCDate() - diffToMonday); monday.setUTCHours(0, 0, 0, 0); // normalize to midnight const mondayStr = monday.toISOString().split("T")[0]; weekMap[mondayStr] = (weekMap[mondayStr] || 0) + 1; }); return Object.entries(weekMap) .map(([week, count]) => ({ week, count })) .sort((a, b) => a.week.localeCompare(b.week)); } const chartColors = [ "hsl(12, 76%, 61%)", "hsl(173, 58%, 39%)", "hsl(43, 74%, 66%)", "hsl(27, 87%, 67%)", "hsl(12, 76%, 61%)", "hsl(173, 58%, 39%)", "hsl(43, 74%, 66%)", "hsl(27, 87%, 67%)", "hsl(12, 76%, 61%)", "hsl(173, 58%, 39%)", "hsl(43, 74%, 66%)", "hsl(27, 87%, 67%)", "hsl(12, 76%, 61%)", "hsl(173, 58%, 39%)", "hsl(43, 74%, 66%)", "hsl(27, 87%, 67%)", "hsl(197, 37%, 24%)", ]; return (

Trend Distribution

{/* } /> */} {xarr.map((item: any, index: number) => { return ( ); })} {/* */} {/* */}
); } const CustomPieTooltip = ({ active, payload }: any) => { if (active && payload && payload.length) { const { week, count } = payload[0].payload; return (
{`${week} - ${count}`}
); } return null; };