import { useState } from 'react' import { apiFetch } from '@/lib/http' import { cn } from '@/lib/utils' type ExportFormat = 'csv' | 'pdf' type ExportScope = 'queries' | 'topics' | 'health' | 'escalations' | 'full' const SCOPES: Array<{ id: ExportScope; label: string; desc: string }> = [ { id: 'queries', label: 'Query analytics', desc: 'Volume, success rate, response times' }, { id: 'topics', label: 'Top topics', desc: 'Most-queried subjects' }, { id: 'health', label: 'Knowledge health', desc: 'Coverage, freshness, accuracy scores' }, { id: 'escalations', label: 'Escalations', desc: 'Unresolved queries by frequency' }, { id: 'full', label: 'Full report', desc: 'All of the above combined' }, ] export function AnalyticsExport() { const [scope, setScope] = useState('full') const [format, setFormat] = useState('csv') const [dateRange, setDateRange] = useState('30d') const [exporting, setExporting] = useState(false) async function handleExport() { setExporting(true) try { const res = await apiFetch( `/api/analytics/export?scope=${scope}&format=${format}&date_range=${dateRange}`, ) const blob = await res.blob() const ext = format === 'pdf' ? 'pdf' : 'csv' const filename = `godspeed-analytics-${scope}-${dateRange}.${ext}` const url = URL.createObjectURL(blob) const a = document.createElement('a') a.href = url a.download = filename a.click() URL.revokeObjectURL(url) } finally { setExporting(false) } } return (

Export Analytics

{/* Scope */}
{SCOPES.map((s) => ( ))}
{/* Format + date range + button */}
{(['csv', 'pdf'] as const).map((f) => ( ))}
) }