|
| 1 | +import { useState } from "react"; |
| 2 | +import { Card, CardContent, CardDescription, CardHeader, CardTitle, CardFooter } from "@/components/ui/card"; |
| 3 | +import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; |
| 4 | +import { Button } from "@/components/ui/button"; |
| 5 | +import { FileJson, FileText, Download } from "lucide-react"; |
| 6 | +import { useToast } from "@/hooks/use-toast"; |
| 7 | + |
| 8 | +export default function AdminExport() { |
| 9 | + const [entity, setEntity] = useState<string>("users"); |
| 10 | + const [range, setRange] = useState<string>("7d"); |
| 11 | + const [isExporting, setIsExporting] = useState<boolean>(false); |
| 12 | + const { toast } = useToast(); |
| 13 | + |
| 14 | + const handleExport = async (format: "json" | "pdf") => { |
| 15 | + try { |
| 16 | + setIsExporting(true); |
| 17 | + const url = `/api/admin/export/${entity}?range=${range}&format=${format}`; |
| 18 | + |
| 19 | + const response = await fetch(url); |
| 20 | + if (!response.ok) { |
| 21 | + throw new Error("Export failed"); |
| 22 | + } |
| 23 | + |
| 24 | + const blob = await response.blob(); |
| 25 | + const downloadUrl = window.URL.createObjectURL(blob); |
| 26 | + const link = document.createElement("a"); |
| 27 | + link.href = downloadUrl; |
| 28 | + link.download = `export_${entity}_${range}.${format}`; |
| 29 | + document.body.appendChild(link); |
| 30 | + link.click(); |
| 31 | + link.remove(); |
| 32 | + window.URL.revokeObjectURL(downloadUrl); |
| 33 | + |
| 34 | + toast({ |
| 35 | + title: "Export Successful", |
| 36 | + description: `Successfully exported ${entity} as ${format.toUpperCase()}`, |
| 37 | + }); |
| 38 | + } catch (error: any) { |
| 39 | + toast({ |
| 40 | + title: "Export Failed", |
| 41 | + description: error.message, |
| 42 | + variant: "destructive", |
| 43 | + }); |
| 44 | + } finally { |
| 45 | + setIsExporting(false); |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + return ( |
| 50 | + <div className="container mx-auto py-8 px-4 max-w-4xl space-y-8"> |
| 51 | + <div> |
| 52 | + <h1 className="text-3xl font-bold tracking-tight">Data Export Center</h1> |
| 53 | + <p className="text-muted-foreground">Download raw platform data for external analysis or compliance.</p> |
| 54 | + </div> |
| 55 | + |
| 56 | + <Card> |
| 57 | + <CardHeader> |
| 58 | + <CardTitle>Export Configuration</CardTitle> |
| 59 | + <CardDescription>Select the data entity and time range to export.</CardDescription> |
| 60 | + </CardHeader> |
| 61 | + <CardContent className="space-y-6"> |
| 62 | + <div className="space-y-4"> |
| 63 | + <div className="space-y-2"> |
| 64 | + <label className="text-sm font-medium">Data Entity</label> |
| 65 | + <Select value={entity} onValueChange={setEntity}> |
| 66 | + <SelectTrigger> |
| 67 | + <SelectValue placeholder="Select Data Type" /> |
| 68 | + </SelectTrigger> |
| 69 | + <SelectContent> |
| 70 | + <SelectItem value="users">Users & Accounts</SelectItem> |
| 71 | + <SelectItem value="orders">Audit Orders & Payments</SelectItem> |
| 72 | + <SelectItem value="reviews">PR Reviews & Analysis</SelectItem> |
| 73 | + <SelectItem value="logs">System Access Logs</SelectItem> |
| 74 | + </SelectContent> |
| 75 | + </Select> |
| 76 | + </div> |
| 77 | + |
| 78 | + <div className="space-y-2"> |
| 79 | + <label className="text-sm font-medium">Time Range</label> |
| 80 | + <Select value={range} onValueChange={setRange}> |
| 81 | + <SelectTrigger> |
| 82 | + <SelectValue placeholder="Select Time Range" /> |
| 83 | + </SelectTrigger> |
| 84 | + <SelectContent> |
| 85 | + <SelectItem value="7d">Last 7 Days</SelectItem> |
| 86 | + <SelectItem value="15d">Last 15 Days</SelectItem> |
| 87 | + <SelectItem value="30d">Last 30 Days</SelectItem> |
| 88 | + <SelectItem value="all">All Time</SelectItem> |
| 89 | + </SelectContent> |
| 90 | + </Select> |
| 91 | + </div> |
| 92 | + </div> |
| 93 | + </CardContent> |
| 94 | + <CardFooter className="flex gap-4"> |
| 95 | + <Button |
| 96 | + variant="outline" |
| 97 | + className="w-full flex items-center gap-2" |
| 98 | + onClick={() => handleExport("json")} |
| 99 | + disabled={isExporting} |
| 100 | + > |
| 101 | + <FileJson className="w-4 h-4" /> |
| 102 | + Export as JSON |
| 103 | + </Button> |
| 104 | + <Button |
| 105 | + className="w-full flex items-center gap-2" |
| 106 | + onClick={() => handleExport("pdf")} |
| 107 | + disabled={isExporting} |
| 108 | + > |
| 109 | + <FileText className="w-4 h-4" /> |
| 110 | + Export as PDF |
| 111 | + </Button> |
| 112 | + </CardFooter> |
| 113 | + </Card> |
| 114 | + </div> |
| 115 | + ); |
| 116 | +} |
0 commit comments