"use client"; import { useCallback, useEffect, useMemo, useState } from "react"; import Link from "next/link"; import { motion } from "motion/react"; import { Activity, AlertTriangle, ArrowRight, BarChart3, Brain, CheckCircle2, Clock3, Database, FileText, RefreshCw, Sparkles, TrendingUp, } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Card } from "@/components/ui/card"; import { getStatsAction } from "@/features/analytics/api/stats.action"; import { checkDeadlineNotifications } from "@/features/notifications/api/notification.action"; import dynamic from "next/dynamic"; // Dynamically import heavy charting libraries to dramatically improve initial load and rendering time const ContractStatusChart = dynamic(() => import("@/features/analytics/components/charts").then(mod => mod.ContractStatusChart), { ssr: false, loading: () =>
}); const ContractTypeChart = dynamic(() => import("@/features/analytics/components/charts").then(mod => mod.ContractTypeChart), { ssr: false, loading: () => }); const TrendChart = dynamic(() => import("@/features/analytics/components/charts").then(mod => mod.TrendChart), { ssr: false, loading: () => }); interface DashboardStats { totalContracts: number; analyzedContracts: number; processingContracts: number; uploadedContracts: number; failedContracts: number; analysisRate: number; } interface ChartData { byType: Array<{ type: string; count: number }>; byStatus: Array<{ status: string; count: number }>; trends: Array<{ date: string; count: number }>; } interface PremiumInfo { averagePremium: number; totalPremium: number; count: number; } interface RecentContract { id: string; title: string | null; type: string | null; createdAt: string; premium: number | null; } interface AILearningTelemetry { completedSamples: number; completedLast7Days: number; avgSummaryLength: number; avgExtractedTextLength: number; avgKeyPointsPerContract: number; learningScore: number; improvementHint: string; } interface StatsActionResult { success: boolean; stats?: DashboardStats; chartData?: ChartData; premiumInfo?: PremiumInfo; aiLearningTelemetry?: AILearningTelemetry; recentContracts?: RecentContract[]; error?: string; } const numberFormatter = new Intl.NumberFormat("en-US"); const currencyFormatter = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD", maximumFractionDigits: 2, }); const defaultStats: DashboardStats = { totalContracts: 0, analyzedContracts: 0, processingContracts: 0, uploadedContracts: 0, failedContracts: 0, analysisRate: 0, }; const formatLastUpdated = (date: Date | null): string => { if (!date) { return "Just now"; } const seconds = Math.max(1, Math.floor((Date.now() - date.getTime()) / 1000)); if (seconds < 60) return `${seconds}s ago`; const minutes = Math.floor(seconds / 60); if (minutes < 60) return `${minutes}m ago`; const hours = Math.floor(minutes / 60); return `${hours}h ago`; }; const clampPercent = (value: number): number => Math.max(0, Math.min(100, value)); export default function DashboardPage() { const [stats, setStats] = useState
A reliable command center for uploaded documents, AI analysis throughput, and portfolio quality across your BFSI workflow.
Pipeline Snapshot
{numberFormatter.format(stats.totalContracts)} files
Analyzed
{numberFormatter.format(stats.analyzedContracts)}
Pending
{numberFormatter.format(pendingContracts)}
Total Files
{numberFormatter.format(stats.totalContracts)}
Uploaded into your workspace
Analyzed
{numberFormatter.format(stats.analyzedContracts)}
Completed by AI pipeline
Pending Queue
{numberFormatter.format(pendingContracts)}
Uploaded and processing files
Failed
{numberFormatter.format(stats.failedContracts)}
Items needing re-analysis
{row.label}
{numberFormatter.format(row.value)}
Success Rate
{stats.analysisRate}%
Completed vs total files
Avg Premium
{currencyFormatter.format(premiumInfo.averagePremium)}
Across {numberFormatter.format(premiumInfo.count)} analyzed files
Total Premium
{currencyFormatter.format(premiumInfo.totalPremium)}
Portfolio value captured by AI
Completed Samples
{numberFormatter.format(aiLearningTelemetry.completedSamples)}
{numberFormatter.format(aiLearningTelemetry.completedLast7Days)}{" "} in last 7 days
Avg Summary Length
{numberFormatter.format(aiLearningTelemetry.avgSummaryLength)}
characters
Avg Extracted Text
{numberFormatter.format( aiLearningTelemetry.avgExtractedTextLength, )}
characters
Avg Key Points
{aiLearningTelemetry.avgKeyPointsPerContract.toFixed(1)}
items per analysis
{aiLearningTelemetry.improvementHint}
{contract.title || "Untitled contract"}
Premium:{" "} {contract.premium !== null ? currencyFormatter.format(contract.premium) : "Not detected"}
No recent analyses yet
Analyze a contract to populate this activity feed.
Upload and analyze contracts to unlock trend and distribution charts.