import { useState, useEffect } from 'react' import { Link, Navigate } from 'react-router-dom' import { BarChart3, Users, Target, Clock, TrendingUp } from 'lucide-react' import { AreaChart, Area, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer, } from 'recharts' import { Spinner } from '@/components/common/Spinner' import { EmptyState } from '@/components/common/EmptyState' import { AnalyticsIllustration } from '@/components/common/EmptyStateIllustrations' import { analyticsApi } from '@/api' import { usePermissions } from '@/hooks/usePermissions' import { toast } from '@/lib/toast' import type { TeamAnalyticsResponse, AnalyticsPeriod } from '@/types' const CHART_COLORS = { resolved: '#34d399', escalated: '#f87171', workaround: '#fbbf24', unresolved: '#94a3b8', } const PERIOD_OPTIONS: { value: AnalyticsPeriod; label: string }[] = [ { value: '7d', label: 'Last 7 days' }, { value: '30d', label: 'Last 30 days' }, { value: '90d', label: 'Last 90 days' }, ] export default function TeamAnalyticsPage() { const { isAccountOwner, isSuperAdmin } = usePermissions() const [period, setPeriod] = useState('30d') const [data, setData] = useState(null) const [loading, setLoading] = useState(true) useEffect(() => { if (!isAccountOwner && !isSuperAdmin) return // eslint-disable-next-line react-hooks/set-state-in-effect setLoading(true) analyticsApi .getTeamAnalytics(period) .then(setData) .catch(console.error) .finally(() => setLoading(false)) }, [period, isAccountOwner, isSuperAdmin]) useEffect(() => { if (!isAccountOwner && !isSuperAdmin) { toast.info('Viewing your personal analytics', { id: 'analytics-redirect' }) } }, [isAccountOwner, isSuperAdmin]) if (!isAccountOwner && !isSuperAdmin) { return } if (loading) { return (
) } if (!data) { return (
} title="Analytics unavailable" description="Failed to load analytics data. Please try again." />
) } if (data.summary.total_sessions === 0) { return (
} title="Track your troubleshooting performance" description="Analytics show resolution times, common paths, and team efficiency. Data appears automatically as you complete sessions." action={ Run Your First Session } learnMoreLink="/guides/analytics" />
) } const { summary, time_series, top_flows, top_engineers } = data return (
{/* Header */}

Team Analytics

My Stats
{/* Stat Cards */}
{/* Area Chart — Sessions over Time */}

Sessions Over Time

{ const d = new Date(value) return `${d.getMonth() + 1}/${d.getDate()}` }} /> { const d = new Date(String(value)) return d.toLocaleDateString(undefined, { month: 'short', day: 'numeric', year: 'numeric', }) }} /> {/* Chart legend */}
{Object.entries(CHART_COLORS).map(([key, color]) => (
{key}
))}
{/* Two-Column: Top Flows & Top Engineers */}
{/* Top Flows */}

Top Flows

{top_flows.length === 0 ? (

No flow data for this period.

) : (
{top_flows.map((flow) => ( ))}
Name Sessions Completion Median
{flow.name} {flow.sessions} {(flow.completion_rate * 100).toFixed(1)}% {flow.median_duration_minutes} min
)}
{/* Top Engineers */}

Top Engineers

{top_engineers.length === 0 ? (

No engineer data for this period.

) : (
{top_engineers.map((eng) => ( ))}
Name Sessions Completion Median
{eng.name} {eng.sessions} {(eng.completion_rate * 100).toFixed(1)}% {eng.median_duration_minutes} min
)}
) } function StatCard({ icon: Icon, label, value, }: { icon: React.ComponentType<{ size?: number; className?: string }> label: string value: string }) { return (
{label}

{value}

) }