import { useEffect, useState } from 'react'; import { orderBookingClient } from '../api/clients'; import { ORDER_BOOKING } from '../api/config'; import { Spinner } from '../components/reusable/Spinner'; import { EmptyState } from '../components/reusable/EmptyState'; import { StatsTiles } from '../components/reusable/StatsTiles'; import { TrendingUp } from 'lucide-react'; import { BarChart, Bar, XAxis, YAxis, CartesianGrid, Tooltip as RechartsTooltip, ResponsiveContainer, PieChart, Pie, Cell, Legend } from 'recharts'; interface Tile { tile_uid: string; key: string; value: string | number; } interface ChartRow { dimension: string | null; value: number; } interface ChartData { chart_uid: string; key: string; rows: ChartRow[]; } const COLORS = ['#6366f1', '#10b981', '#f59e0b', '#ef4444', '#8b5cf6', '#06b6d4', '#f97316']; export function AnalyticsPage() { const [tiles, setTiles] = useState([]); const [charts, setCharts] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { let live = true; async function run() { try { setLoading(true); const res = await orderBookingClient.recordView(ORDER_BOOKING.recordViews.ANALYTICS, { limit: 100, sortBy: 'instance_id', sortDir: 'desc' }); if (live) { setTiles((res.tile_values || []) as Tile[]); setCharts((res.chart_data || []) as ChartData[]); } } catch (e: any) { if (live) setError(e.message || 'Failed to load analytics'); } finally { if (live) setLoading(false); } } run(); return () => { live = false; }; }, []); if (loading) return
; if (error) return ; const renderChart = (chart: ChartData, index: number) => { const data = chart.rows.map(r => ({ name: r.dimension || 'Unknown', value: r.value })); const title = chart.key.split('_').map(w => w.charAt(0).toUpperCase() + w.slice(1)).join(' '); // Decide chart type based on key if (chart.key === 'status_overview') { return (

{title}

{data.map((_, index) => ( ))}
); } return (

{title}

); }; return (

Analytics Overview

{/* Tiles Grid */} {/* Charts Grid */}
{charts.map((chart, index) => renderChart(chart, index))}
); }