"use client"; import React, { useMemo, useState, useEffect } from "react"; import { Card, CardBody, CardHeader, Tabs, Tab, Listbox, ListboxItem, Skeleton, } from "@nextui-org/react"; import { useTranslation } from "@/shared/lib/useTranslation"; import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer, } from "recharts"; import { getAdminAnalytics, AdminAnalyticsData, TopServiceItem, } from "@/api/adminAnalytics.api"; import toast from "react-hot-toast"; // Chart data interface to match existing chart component interface ChartDataItem { date: string; users: number; revenue: number; calls: number; } // Transform API data to chart format const transformToChartData = ( analytics: AdminAnalyticsData ): ChartDataItem[] => { // Get the maximum length of days arrays to handle potential mismatches const maxLength = Math.max( analytics.user_register.days.length, analytics.user_pay.days.length, analytics.mcp_call.days.length ); const chartData: ChartDataItem[] = []; for (let i = 0; i < maxLength; i++) { // Format date for display (converting from YYYY-MM-DD to MMM DD) const userRegDay = analytics.user_register.days[i]; const payDay = analytics.user_pay.days[i]; const callDay = analytics.mcp_call.days[i]; // Use the first available date, format it nicely const rawDate = userRegDay?.stats_day || payDay?.stats_day || callDay?.stats_day; const formattedDate = rawDate ? new Date(rawDate).toLocaleDateString("en-US", { month: "short", day: "numeric", }) : `Day ${i + 1}`; chartData.push({ date: formattedDate, users: userRegDay ? parseInt(userRegDay.count as string) || 0 : 0, revenue: payDay ? payDay.count : 0, calls: callDay ? callDay.count : 0, }); } return chartData; }; // Render label + total value in tab header const TabTitle: React.FC<{ label: string; total: number | string; }> = ({ label, total }) => (