121 lines
5.8 KiB
TypeScript
121 lines
5.8 KiB
TypeScript
"use client";
|
|
|
|
import { useState } from "react";
|
|
import { FileDown, Loader2, FileSpreadsheet, FileText } from "lucide-react";
|
|
import { useExport } from "@/hooks/useExport";
|
|
import { toast } from "react-hot-toast";
|
|
|
|
export function ExportPanel() {
|
|
const { exportSnapshot, exportAuditTrail, isLoading, error } = useExport();
|
|
|
|
const handleExportSnapshot = async (format: "csv" | "xlsx") => {
|
|
try {
|
|
await exportSnapshot(format);
|
|
const formatName = format === "csv" ? "CSV" : "Excel";
|
|
toast.success(`Inventory snapshot exported as ${formatName}`);
|
|
} catch (err) {
|
|
// Error handled by hook
|
|
}
|
|
};
|
|
|
|
const handleExportAuditTrail = async (format: "csv" | "xlsx") => {
|
|
try {
|
|
await exportAuditTrail(format);
|
|
const formatName = format === "csv" ? "CSV" : "Excel";
|
|
toast.success(`Audit trail exported as ${formatName}`);
|
|
} catch (err) {
|
|
// Error handled by hook
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div data-testid="admin-tab-exports" className="bg-surface/50 border border-slate-800/50 rounded-[2.5rem] p-4 md:p-6 flex flex-col shadow-2xl transition-all h-full">
|
|
<div className="flex items-center gap-3 mb-4 md:mb-6">
|
|
<div className="w-10 h-10 rounded-xl bg-indigo-500/10 flex items-center justify-center text-indigo-400 border border-indigo-500/20">
|
|
<FileDown size={20} />
|
|
</div>
|
|
<div>
|
|
<h2 className="text-xl font-normal text-white tracking-tight">Export & Reports</h2>
|
|
<p className="text-[10px] md:text-xs text-muted font-normal tracking-tight">Download system snapshots and action logs</p>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="grid md:grid-cols-2 gap-4">
|
|
{/* Inventory Snapshot Section */}
|
|
<div className="p-4 bg-background/40 border border-slate-800/40 rounded-2xl hover:border-primary/20 transition-all group flex flex-col justify-between">
|
|
<div className="mb-4">
|
|
<h3 className="text-sm font-normal text-secondary flex items-center gap-2">
|
|
<FileSpreadsheet size={14} className="text-primary" />
|
|
Inventory Snapshot
|
|
</h3>
|
|
<p className="text-xs text-muted mt-1 leading-relaxed">
|
|
Export current inventory state with all item details and locations.
|
|
</p>
|
|
</div>
|
|
|
|
<div className="grid grid-cols-2 gap-2">
|
|
<button
|
|
onClick={() => handleExportSnapshot("csv")}
|
|
disabled={isLoading}
|
|
className="px-5 py-1.5 bg-primary hover:bg-primary/90 text-white rounded-xl text-sm font-normal transition-all active:scale-95 shadow-xl shadow-primary/10 tracking-tight border border-primary/30 flex items-center justify-center gap-2 disabled:opacity-50"
|
|
aria-label="Export inventory snapshot as CSV"
|
|
>
|
|
{isLoading ? <Loader2 size={14} className="animate-spin" /> : <FileText size={14} />}
|
|
CSV
|
|
</button>
|
|
<button
|
|
onClick={() => handleExportSnapshot("xlsx")}
|
|
disabled={isLoading}
|
|
className="px-5 py-1.5 bg-primary hover:bg-primary/90 text-white rounded-xl text-sm font-normal transition-all active:scale-95 shadow-xl shadow-primary/10 tracking-tight border border-primary/30 flex items-center justify-center gap-2 disabled:opacity-50"
|
|
aria-label="Export inventory snapshot as Excel"
|
|
>
|
|
{isLoading ? <Loader2 size={14} className="animate-spin" /> : <FileSpreadsheet size={14} />}
|
|
Excel
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Audit Trail Section */}
|
|
<div className="p-4 bg-background/40 border border-slate-800/40 rounded-2xl hover:border-primary/20 transition-all group flex flex-col justify-between">
|
|
<div className="mb-4">
|
|
<h3 className="text-sm font-normal text-secondary flex items-center gap-2">
|
|
<FileText size={14} className="text-indigo-400" />
|
|
Audit Trail
|
|
</h3>
|
|
<p className="text-xs text-muted mt-1 leading-relaxed">
|
|
Complete history of system actions, changes, and user operations.
|
|
</p>
|
|
</div>
|
|
<div className="grid grid-cols-2 gap-2">
|
|
<button
|
|
onClick={() => handleExportAuditTrail("csv")}
|
|
disabled={isLoading}
|
|
className="px-5 py-1.5 bg-primary hover:bg-primary/90 text-white rounded-xl text-sm font-normal transition-all active:scale-95 shadow-xl shadow-primary/10 tracking-tight border border-primary/30 flex items-center justify-center gap-2 disabled:opacity-50"
|
|
aria-label="Export audit trail as CSV"
|
|
>
|
|
{isLoading ? <Loader2 size={14} className="animate-spin" /> : <FileText size={14} />}
|
|
CSV
|
|
</button>
|
|
<button
|
|
onClick={() => handleExportAuditTrail("xlsx")}
|
|
disabled={isLoading}
|
|
className="px-5 py-1.5 bg-primary hover:bg-primary/90 text-white rounded-xl text-sm font-normal transition-all active:scale-95 shadow-xl shadow-primary/10 tracking-tight border border-primary/30 flex items-center justify-center gap-2 disabled:opacity-50"
|
|
aria-label="Export audit trail as Excel"
|
|
>
|
|
{isLoading ? <Loader2 size={14} className="animate-spin" /> : <FileSpreadsheet size={14} />}
|
|
Excel
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{error && (
|
|
<div className="mt-4 p-3 bg-rose-500/10 border border-rose-500/20 rounded-xl text-rose-500 text-[10px] flex items-center gap-2 animate-in slide-in-from-top-2 duration-300">
|
|
<div className="w-1.5 h-1.5 bg-rose-500 rounded-full animate-pulse" />
|
|
<span className="font-normal truncate">Error: {error}</span>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|