import React, { useState } from 'react'; import { StatusBadge, PriorityBadge } from './Badges'; import { useReports, useServiceNarrative, NexusMarkdown } from '@nexus/shared'; import { Skeleton } from './Skeleton'; const COLORS = { brand: "#1E40AF", brandLight: "rgba(30, 64, 175, 0.1)", gray50: "#F9FAFB", gray100: "#F3F4F6", gray200: "#E5E7EB", gray400: "#9CA3AF", gray500: "#6B7280", gray700: "#374151", gray800: "#1F2937", gray900: "#111827", }; interface AnalyticsProps { tickets: any[]; } const Sparkline: React.FC<{ data: number[], color: string }> = ({ data, color }) => { if (!data || data.length === 0) return null; const max = Math.max(...data, 1); const width = 100; const height = 30; const divisor = data.length > 1 ? data.length - 1 : 1; const points = data.map((v, i) => `${(i / divisor) * width},${height - (v / max) * height}`).join(' '); return ( ); }; export const Analytics: React.FC = ({ tickets }) => { const { volume, sla, csat, loading: reportsLoading, refresh: refreshReports } = useReports(); const { narrative, loading: narrativeLoading, refresh: refreshNarrative } = useServiceNarrative(); const [isResummarizing, setIsResummarizing] = useState(false); const handleResummarize = async () => { setIsResummarizing(true); try { await refreshNarrative(); await refreshReports(); } catch (err) { console.error("Resummarization failed:", err); } finally { setIsResummarizing(false); } }; const resolvedCount = tickets.filter(t => t.status === "RESOLVED" || t.status === "CLOSED").length; const total = tickets.length; const volumeValues = volume ? Object.values(volume) : []; const categoryCounts = tickets.reduce((acc: any, t) => { const cat = t.category || "Uncategorized"; acc[cat] = (acc[cat] || 0) + 1; return acc; }, {}); const sortedCategories = Object.entries(categoryCounts).sort((a: any, b: any) => b[1] - a[1]); const priorityCounts = tickets.reduce((acc: any, t) => { const rawPrio = (t.priority || "P2").toUpperCase(); const PRIO_MAP: Record = { "P0": "URGENT", "P1": "HIGH", "P2": "MEDIUM", "P3": "LOW" }; const prio = PRIO_MAP[rawPrio] || rawPrio; acc[prio] = (acc[prio] || 0) + 1; return acc; }, {}); const resolvedTickets = tickets.filter(t => t.resolved_at && (t.status === "RESOLVED" || t.status === "CLOSED")); let avgResHours = 0; if (resolvedTickets.length > 0) { const totalHours = resolvedTickets.reduce((acc, t) => { const start = new Date(t.created_at).getTime(); const end = new Date(t.resolved_at!).getTime(); return acc + (end - start) / (1000 * 60 * 60); }, 0); avgResHours = totalHours / resolvedTickets.length; } const showReportsLoading = reportsLoading && !volume; const showNarrativeLoading = (narrativeLoading || isResummarizing) && !narrative; return ( Workspace Analytics Real-time operational metrics and historical trends {[ { label: "Active Tickets", value: showReportsLoading ? : total - resolvedCount, footer: showReportsLoading ? : `${total} Total Tickets`, color: "hsl(217, 91%, 60%)", icon: "📊", viz: showReportsLoading ? : 0 ? volumeValues : [0,0,0]} color="hsl(217, 91%, 60%)" /> }, { label: "SLA Compliance", value: showReportsLoading ? : (sla ? `${Math.round(sla.sla_compliance_rate * 100)}%` : "100%"), footer: showReportsLoading ? : `${sla?.breached_tickets || 0} Rule Breaches`, color: "hsl(142, 71%, 45%)", icon: "⚡", viz: showReportsLoading ? : }, { label: "Avg. Resolution", value: showReportsLoading ? : (avgResHours > 0 ? `${avgResHours.toFixed(1)}h` : "—"), footer: showReportsLoading ? : `${resolvedCount} Resolved Tickets`, color: "hsl(262, 83%, 58%)", icon: "✅", viz: showReportsLoading ? : Operational Threshold: 24h }, { label: "CSAT Score", value: showReportsLoading ? : (csat ? csat.average_rating.toFixed(1) : "—"), footer: showReportsLoading ? : (csat?.total_responses ? `${csat.total_responses} Total Ratings` : "No Ratings Yet"), color: "hsl(38, 92%, 50%)", icon: "⭐", viz: showReportsLoading ? : {"★".repeat(Math.floor(csat?.average_rating || 0)) + "☆".repeat(5 - Math.floor(csat?.average_rating || 0))} }, ].map(s => ( {s.label} {s.icon} {s.value} {s.viz} {s.footer} ))} Ticket Volume by Category Distribution Analysis {sortedCategories.length > 0 ? sortedCategories.map(([cat, count]: any) => ( {cat} {count} )) : No categorical data available} Priority Distribution {["URGENT", "HIGH", "MEDIUM", "LOW"].map(prio => { const count = priorityCounts[prio] || 0; const height = total > 0 ? (count / total) * 140 : 0; return ( {prio} {count} ); })} Nexus Workspace Intelligence AI-Synthesized Report 🔄 {showNarrativeLoading ? ( ) : ( )} Operational Activity History {["Ticket", "Subject", "Status", "Priority", "Duration"].map(h => ( {h} ))} {tickets.slice(0, 10).map(t => ( {t.id?.slice(0, 8)} {t.title} {t.resolved_at ? `${Math.round((new Date(t.resolved_at).getTime() - new Date(t.created_at).getTime()) / (1000 * 60 * 60))}h` : "—"} ))} ); };
Real-time operational metrics and historical trends