import React, { useMemo } from 'react'; import { useDashboardStats, useReports, useAgentQueue, useAIInsights, useAuth } from '@nexus/shared'; import styles from './DashboardOverview.module.css'; interface DashboardOverviewProps { workspaceId?: string; } export const DashboardOverview: React.FC = ({ workspaceId }) => { const { user } = useAuth(); const { stats, loading: statsLoading, refresh: refreshStats } = useDashboardStats(workspaceId); const { daily, loading: reportsLoading, refresh: refreshReports } = useReports(workspaceId); const { tickets: queueTickets, loading: queueLoading, refresh: refreshQueue } = useAgentQueue(workspaceId); const { insights, loading: insightsLoading, refresh: refreshInsights } = useAIInsights(workspaceId); // Manual Refresh logic — no auto-polling as per user requirement. const { volumeArray, timeLabels, categoryData } = useMemo(() => { if (!daily || daily.length === 0) { return { volumeArray: new Array(30).fill(0), timeLabels: new Array(30).fill(''), categoryData: [] }; } const vol = daily.map(d => d.total); const labels = daily.map(d => d.label); // Aggregate categories across all 30 days const catMap: Record = {}; daily.forEach(d => { Object.entries(d.categories).forEach(([cat, count]) => { catMap[cat] = (catMap[cat] || 0) + count; }); }); const categories = Object.entries(catMap) .map(([name, count]) => ({ name, count })) .sort((a, b) => b.count - a.count); return { volumeArray: vol, timeLabels: labels, categoryData: categories }; }, [daily]); // Agent Workload from backend - Map to UI requirements const agentWorkload = useMemo(() => { if (!stats?.personnel_load) return []; return stats.personnel_load.map(p => ({ name: p.name, active: p.count, avatar: p.name.split(' ').map(n => n[0]).join('').slice(0, 2).toUpperCase() })); }, [stats?.personnel_load]); const maxH = Math.max(...volumeArray, 1); // Only show the global loading screen if we don't have data yet const isInitialStatsLoading = statsLoading && !stats; const isInitialQueueLoading = queueLoading && queueTickets.length === 0; const handleFullRefresh = () => { refreshStats(); refreshReports(); refreshQueue(); refreshInsights(); }; return (
{/* 1. High-Impact Mission Banner */}
Good morning, {user?.full_name || 'COMMANDER ADMIN'}
AI MISSION SUMMARY
{insightsLoading ? (
) : (

{insights?.summary || "Analyzing workspace operational stream... Stand by for telemetry summary."}

)}
ACTIVE LOAD
{isInitialQueueLoading ? (
) : (
{queueTickets.filter(t => ['OPEN', 'ACKNOWLEDGED', 'IN_PROGRESS'].includes(t.status)).length}
)}
Open Cases
BREACH RISK
{isInitialQueueLoading ? (
) : (
{stats?.performance?.sla_breached_count || 0}
)}
SLA Breaches
CSAT STABILITY
{isInitialStatsLoading ? (
) : (
{(stats?.performance?.csat_score ?? 0).toFixed(1)}
)}
Quality Pulse
{/* 2. Operational Intelligence Grid */}
{/* Suggested Actions */}

Suggested Actions

{insightsLoading ? ( [1, 2, 3].map(i => (
)) ) : ( (insights?.actions || ["Reviewing P0 assignments", "Analyzing capacity trends", "Balancing workload"]).map((action, i) => (
⚡
{action}
Real-time priority nudge
)) )}
{/* Volume Trend */}

Last 30 Days Volume

{isInitialStatsLoading ? ( new Array(30).fill(0).map((_, i) => (
)) ) : ( volumeArray.map((v, i) => (
{i % 5 === 0 && {timeLabels[i]}}
)) )}

Categorical Distribution

{categoryData.length === 0 ? (
No data reported
) : ( categoryData.map(cat => (
{cat.name}
c.count), 1)) * 100, 100)}%` }} />
{cat.count}
)) )}
{/* Agent Capacity */}

Active Personnel Load

{isInitialQueueLoading ? ( [1, 2, 3, 4, 5].map(i => (
)) ) : ( agentWorkload.map(a => (
{a.avatar}
{a.name} {a.active} tickets
= 10 ? 'var(--error)' : a.active >= 5 ? 'var(--warning)' : 'var(--success)' }} />
)) )}
); };