import React, { useState, useEffect, useCallback, useMemo } from 'react'; import { useSearchParams } from 'react-router-dom'; import OverviewTab from '../components/agent-analytics/OverviewTab'; import AnalyticsTab from '../components/agent-analytics/AnalyticsTab'; import ConversationsTab from '../components/agent-analytics/ConversationsTab'; import GapsTab from '../components/agent-analytics/GapsTab'; import InsightsTab from '../components/agent-analytics/InsightsTab'; import LiveSyncBanner from '../components/agent-analytics/LiveSyncBanner'; import PlanStatusCard from '../components/PlanStatusCard'; import { fetchAgentDashboard, fetchAiInsights, } from '../service/agent-analytics/agent-analytics.service'; import { getDateRangeFromPreset } from '../components/agent-analytics/helpers'; import type { IAgentDashboardData, IInsightsResponse, IInsight, } from '../service/agent-analytics/agent-analytics.interface'; type Tab = { id: string; label: string; badge?: string; }; const DATE_PRESETS = [ { id: 'today', label: 'Today' }, { id: '7d', label: '7 Days' }, { id: '30d', label: '30 Days' }, { id: 'custom', label: 'Custom' }, ] as const; const VALID_TABS = [ 'overview', 'analytics', 'conversations', 'gaps', 'insights', ] as const; const AgentAnalytics = (): JSX.Element => { const [searchParams, setSearchParams] = useSearchParams(); const tabParam = searchParams.get('tab'); const initialTab = tabParam && (VALID_TABS as readonly string[]).includes(tabParam) ? tabParam : 'overview'; const [selectedTab, setSelectedTab] = useState(initialTab); const [datePreset, setDatePreset] = useState('30d'); const [customStart, setCustomStart] = useState(''); const [customEnd, setCustomEnd] = useState(''); const [showCustom, setShowCustom] = useState(false); const [insightCount, setInsightCount] = useState(0); const [dashData, setDashData] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const dateRange = useMemo(() => { if (datePreset === 'custom' && customStart && customEnd) { return { start: customStart, end: customEnd }; } return getDateRangeFromPreset(datePreset); }, [datePreset, customStart, customEnd]); const fetchDashboard = useCallback(() => { setLoading(true); setError(null); fetchAgentDashboard(dateRange.start, dateRange.end) .then(d => { setDashData(d); setLoading(false); }) .catch(e => { setError(e.message); setLoading(false); }); }, [dateRange.start, dateRange.end]); useEffect(() => { fetchDashboard(); }, [fetchDashboard]); useEffect(() => { if (tabParam && (VALID_TABS as readonly string[]).includes(tabParam)) { setSelectedTab(tabParam); } }, [tabParam]); const handleSelectTab = useCallback( (tabId: string): void => { setSelectedTab(tabId); setSearchParams( prev => { const next = new URLSearchParams(prev); next.set('tab', tabId); return next; }, { replace: true } ); }, [setSearchParams] ); const tabs: Tab[] = useMemo( () => [ { id: 'overview', label: 'Overview' }, { id: 'analytics', label: 'Analytics' }, { id: 'conversations', label: 'Conversations' }, { id: 'gaps', label: 'Gaps' }, { id: 'insights', label: 'Insights', badge: insightCount > 0 ? String(insightCount) : undefined, }, ], [insightCount] ); useEffect(() => { fetchAiInsights() .then((data: IInsightsResponse | null) => { const activeInsights: IInsight[] = ( data?.report?.insights || [] ).filter((insight: IInsight) => !insight.dismissed); setInsightCount(activeInsights.length); }) .catch(() => {}); }, []); return (

Agent Analytics

Monitor your AI agent{`'`}s performance, conversations, and impact.

{showCustom && (
setCustomStart(e.target.value)} className="w-[130px] rounded-md border-0 py-1.5 text-xs text-gray-900 ring-1 ring-inset ring-gray-200 focus:ring-2 focus:ring-inset focus:ring-gray-900" /> to setCustomEnd(e.target.value)} className="w-[130px] rounded-md border-0 py-1.5 text-xs text-gray-900 ring-1 ring-inset ring-gray-200 focus:ring-2 focus:ring-inset focus:ring-gray-900" />
)}
{DATE_PRESETS.map(p => ( ))}
{selectedTab === 'overview' && ( )} {selectedTab === 'analytics' && ( )} {selectedTab === 'conversations' && ( )} {selectedTab === 'gaps' && ( )} {selectedTab === 'insights' && ( )}
); }; export default AgentAnalytics;