Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 19x 19x 19x 19x 19x 19x 19x 19x 21x 21x 21x 21x 19x 19x 19x 19x 19x 19x 19x 19x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 1x 1x | 'use client';
import React, { useState, useRef, useEffect, useCallback } from 'react';
import { cn } from '@/lib/core';
import { clientLogger } from '@/lib/logging/clientLogger';
import { Button } from '@/components/ui/Button';
import { Icon, type IconName } from '@/components/ui/icons';
type ExportFormat = 'csv' | 'json';
type TimeRange = '1h' | '6h' | '24h' | '7d' | '30d';
export interface ExportButtonProps {
/** Current time range for the export */
timeRange?: TimeRange;
/** Optional path filter */
path?: string;
/** Custom class name */
className?: string;
/** Whether the button is disabled */
disabled?: boolean;
/** Callback after export starts */
onExportStart?: (format: ExportFormat) => void;
/** Callback after export completes */
onExportComplete?: (format: ExportFormat, success: boolean) => void;
}
interface ExportOption {
format: ExportFormat;
label: string;
description: string;
icon: IconName;
}
const exportOptions: ExportOption[] = [
{
format: 'csv',
label: 'Export as CSV',
description: 'Spreadsheet-compatible format',
icon: 'document',
},
{
format: 'json',
label: 'Export as JSON',
description: 'Raw data for developers',
icon: 'code',
},
];
/**
* ExportButton Component
*
* A dropdown button for exporting performance data in various formats.
* Handles the export API call and file download.
*
* @example
* ```tsx
* <ExportButton
* timeRange="24h"
* path="/api/products"
* onExportComplete={(format, success) => {
* if (success) toast.success(`Exported as ${format}`);
* }}
* />
* ```
*/
export function ExportButton({
timeRange = '24h',
path,
className,
disabled,
onExportStart,
onExportComplete,
}: ExportButtonProps) {
const [isOpen, setIsOpen] = useState(false);
const [exporting, setExporting] = useState<ExportFormat | null>(null);
const dropdownRef = useRef<HTMLDivElement>(null);
// Close on outside click
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) {
setIsOpen(false);
}
};
if (isOpen) {
document.addEventListener('mousedown', handleClickOutside);
}
return () => {
document.removeEventListener('mousedown', handleClickOutside);
};
}, [isOpen]);
// Handle escape key
useEffect(() => {
const handleEscape = (event: KeyboardEvent) => {
if (event.key === 'Escape') {
setIsOpen(false);
}
};
if (isOpen) {
document.addEventListener('keydown', handleEscape);
}
return () => {
document.removeEventListener('keydown', handleEscape);
};
}, [isOpen]);
const handleExport = useCallback(
async (format: ExportFormat) => {
setExporting(format);
setIsOpen(false);
onExportStart?.(format);
try {
const params = new URLSearchParams({
format,
timeRange,
});
if (path) params.append('path', path);
const response = await fetch(`/api/admin/monitoring/performance/export?${params}`);
if (!response.ok) {
throw new Error(`Export failed: ${response.statusText}`);
}
// Get filename from Content-Disposition header or generate one
const disposition = response.headers.get('Content-Disposition');
let filename = `performance-export.${format}`;
if (disposition) {
const match = disposition.match(/filename="?([^"]+)"?/);
if (match) {
filename = match[1];
}
}
// Create download
const blob = await response.blob();
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
onExportComplete?.(format, true);
} catch (error) {
clientLogger.error('Export error', error instanceof Error ? error : new Error(String(error)), { format, timeRange, path });
onExportComplete?.(format, false);
} finally {
setExporting(null);
}
},
[timeRange, path, onExportStart, onExportComplete]
);
const isDisabled = disabled || exporting !== null;
return (
<div ref={dropdownRef} className={cn('relative inline-block', className)}>
<Button
type="button"
variant="outline"
size="sm"
onClick={() => setIsOpen(!isOpen)}
disabled={isDisabled}
aria-haspopup="menu"
aria-expanded={isOpen}
leftIcon={
exporting ? (
<Icon name="spinner" size={16} className="animate-spin" />
) : (
<Icon name="download" size={16} />
)
}
rightIcon={
!exporting && <Icon name="chevron-down" size={14} className={cn('transition-transform', isOpen && 'rotate-180')} />
}
>
{exporting ? 'Exporting...' : 'Export'}
</Button>
{isOpen && (
<div
className={cn(
'absolute right-0 z-20 mt-1 w-56',
'bg-white dark:bg-gray-800',
'border border-gray-200 dark:border-gray-700',
'rounded-lg shadow-lg',
'overflow-hidden',
'animate-fadeIn'
)}
role="menu"
aria-label="Export format options"
>
{exportOptions.map((option) => (
<button
key={option.format}
type="button"
onClick={() => handleExport(option.format)}
disabled={isDisabled}
className={cn(
'flex items-start gap-3 w-full px-4 py-3 text-left',
'hover:bg-gray-50 dark:hover:bg-gray-700',
'transition-colors',
'disabled:opacity-50 disabled:cursor-not-allowed',
'focus:outline-none focus:bg-gray-50 dark:focus:bg-gray-700'
)}
role="menuitem"
>
<Icon
name={option.icon}
size={18}
className="mt-0.5 text-gray-500 dark:text-gray-400"
/>
<div>
<div className="font-medium text-gray-900 dark:text-gray-100">
{option.label}
</div>
<div className="text-sm text-gray-500 dark:text-gray-400">
{option.description}
</div>
</div>
</button>
))}
</div>
)}
</div>
);
}
export default ExportButton;
|