Updated api usage, corrected table designs
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import React, { useState, useRef, useEffect } from 'react';
|
||||
import React, { useState, useRef, useEffect, useCallback } from 'react';
|
||||
import { createPortal } from 'react-dom';
|
||||
import { Download, FileSpreadsheet, FileText, ChevronDown } from 'lucide-react';
|
||||
import { ExportColumn, exportToSpreadsheet, exportToPdf } from '../../utils/exportUtils';
|
||||
import { useAuth } from '../../auth/AuthProvider';
|
||||
@@ -25,24 +26,46 @@ export function ExportButton<T>({
|
||||
className = '',
|
||||
}: ExportButtonProps<T>) {
|
||||
const [isOpen, setIsOpen] = useState(false);
|
||||
const [menuPosition, setMenuPosition] = useState<{ top: number; right: number } | null>(null);
|
||||
const dropdownRef = useRef<HTMLDivElement>(null);
|
||||
const menuRef = useRef<HTMLDivElement>(null);
|
||||
const { user, claims, tenantId } = useAuth();
|
||||
|
||||
const isDisabled = !data || data.length === 0;
|
||||
|
||||
const updatePosition = useCallback(() => {
|
||||
if (!dropdownRef.current) return;
|
||||
const rect = dropdownRef.current.getBoundingClientRect();
|
||||
setMenuPosition({
|
||||
top: rect.bottom + 8,
|
||||
right: Math.max(16, window.innerWidth - rect.right),
|
||||
});
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
function handleClickOutside(event: MouseEvent) {
|
||||
if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) {
|
||||
const target = event.target as Node;
|
||||
if (
|
||||
dropdownRef.current &&
|
||||
!dropdownRef.current.contains(target) &&
|
||||
(!menuRef.current || !menuRef.current.contains(target))
|
||||
) {
|
||||
setIsOpen(false);
|
||||
}
|
||||
}
|
||||
|
||||
if (isOpen) {
|
||||
updatePosition();
|
||||
document.addEventListener('mousedown', handleClickOutside);
|
||||
window.addEventListener('resize', updatePosition);
|
||||
window.addEventListener('scroll', updatePosition, true);
|
||||
}
|
||||
return () => {
|
||||
document.removeEventListener('mousedown', handleClickOutside);
|
||||
window.removeEventListener('resize', updatePosition);
|
||||
window.removeEventListener('scroll', updatePosition, true);
|
||||
};
|
||||
}, [isOpen]);
|
||||
}, [isOpen, updatePosition]);
|
||||
|
||||
const handleExportSpreadsheet = () => {
|
||||
if (isDisabled) return;
|
||||
@@ -96,44 +119,56 @@ export function ExportButton<T>({
|
||||
<ChevronDown className={`w-3 h-3 transition-transform ${isOpen ? 'rotate-180' : ''}`} />
|
||||
</button>
|
||||
|
||||
{isOpen && !isDisabled && (
|
||||
<div className="absolute right-0 mt-2 w-56 bg-[#0d121f] border border-slate-700/90 rounded-xl shadow-2xl z-[99999] overflow-hidden backdrop-blur-md animate-in fade-in-50 zoom-in-95 ring-1 ring-black/40">
|
||||
<div className="p-2 border-b border-slate-800/80">
|
||||
<span className="text-[10px] font-semibold uppercase tracking-wider text-slate-400 px-2 block">
|
||||
Export Format ({data.length} items)
|
||||
</span>
|
||||
</div>
|
||||
<div className="p-1 space-y-0.5">
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleExportSpreadsheet}
|
||||
className="w-full flex items-center gap-2.5 px-3 py-2 text-xs text-left text-slate-200 hover:text-white hover:bg-emerald-500/15 rounded-lg transition-colors cursor-pointer group"
|
||||
>
|
||||
<div className="w-6 h-6 rounded-md bg-emerald-500/10 border border-emerald-500/20 flex items-center justify-center shrink-0 group-hover:bg-emerald-500/20">
|
||||
<FileSpreadsheet className="w-3.5 h-3.5 text-emerald-400" />
|
||||
</div>
|
||||
<div>
|
||||
<span className="font-semibold block leading-tight">Spreadsheet (.xlsx)</span>
|
||||
<span className="text-[10px] text-slate-400">Excel / Google Sheets</span>
|
||||
</div>
|
||||
</button>
|
||||
{isOpen && !isDisabled && menuPosition && typeof document !== 'undefined' &&
|
||||
createPortal(
|
||||
<div
|
||||
ref={menuRef}
|
||||
style={{
|
||||
position: 'fixed',
|
||||
top: `${menuPosition.top}px`,
|
||||
right: `${menuPosition.right}px`,
|
||||
zIndex: 999999,
|
||||
}}
|
||||
className="w-56 bg-[#0d121f] border border-slate-700/90 rounded-xl shadow-2xl overflow-hidden backdrop-blur-md animate-in fade-in-50 zoom-in-95 ring-1 ring-black/40"
|
||||
>
|
||||
<div className="p-2 border-b border-slate-800/80">
|
||||
<span className="text-[10px] font-semibold uppercase tracking-wider text-slate-400 px-2 block">
|
||||
Export Format ({data.length} items)
|
||||
</span>
|
||||
</div>
|
||||
<div className="p-1 space-y-0.5">
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleExportSpreadsheet}
|
||||
className="w-full flex items-center gap-2.5 px-3 py-2 text-xs text-left text-slate-200 hover:text-white hover:bg-emerald-500/15 rounded-lg transition-colors cursor-pointer group"
|
||||
>
|
||||
<div className="w-6 h-6 rounded-md bg-emerald-500/10 border border-emerald-500/20 flex items-center justify-center shrink-0 group-hover:bg-emerald-500/20">
|
||||
<FileSpreadsheet className="w-3.5 h-3.5 text-emerald-400" />
|
||||
</div>
|
||||
<div>
|
||||
<span className="font-semibold block leading-tight">Spreadsheet (.xlsx)</span>
|
||||
<span className="text-[10px] text-slate-400">Excel / Google Sheets</span>
|
||||
</div>
|
||||
</button>
|
||||
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleExportPdf}
|
||||
className="w-full flex items-center gap-2.5 px-3 py-2 text-xs text-left text-slate-200 hover:text-white hover:bg-blue-500/15 rounded-lg transition-colors cursor-pointer group"
|
||||
>
|
||||
<div className="w-6 h-6 rounded-md bg-blue-500/10 border border-blue-500/20 flex items-center justify-center shrink-0 group-hover:bg-blue-500/20">
|
||||
<FileText className="w-3.5 h-3.5 text-blue-400" />
|
||||
</div>
|
||||
<div>
|
||||
<span className="font-semibold block leading-tight">PDF Document (.pdf)</span>
|
||||
<span className="text-[10px] text-slate-400">Landscape with PFM Logo</span>
|
||||
</div>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleExportPdf}
|
||||
className="w-full flex items-center gap-2.5 px-3 py-2 text-xs text-left text-slate-200 hover:text-white hover:bg-blue-500/15 rounded-lg transition-colors cursor-pointer group"
|
||||
>
|
||||
<div className="w-6 h-6 rounded-md bg-blue-500/10 border border-blue-500/20 flex items-center justify-center shrink-0 group-hover:bg-blue-500/20">
|
||||
<FileText className="w-3.5 h-3.5 text-blue-400" />
|
||||
</div>
|
||||
<div>
|
||||
<span className="font-semibold block leading-tight">PDF Document (.pdf)</span>
|
||||
<span className="text-[10px] text-slate-400">Landscape with PFM Logo</span>
|
||||
</div>
|
||||
</button>
|
||||
</div>
|
||||
</div>,
|
||||
document.body
|
||||
)
|
||||
}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user