import { useState, useCallback, useRef } from "preact/hooks"; import { useT } from "@shared/i18n/context"; import type { ImportDiff } from "@shared/hooks/use-proxy-assignments"; interface ImportExportProps { onExport: () => Promise>; onImportPreview: (data: Array<{ email: string; proxyId: string }>) => Promise; onApplyImport: (assignments: Array<{ accountId: string; proxyId: string }>) => Promise; } export function ImportExport({ onExport, onImportPreview, onApplyImport }: ImportExportProps) { const t = useT(); const [showImport, setShowImport] = useState(false); const [diff, setDiff] = useState(null); const [importing, setImporting] = useState(false); const fileRef = useRef(null); const handleExport = useCallback(async () => { const data = await onExport(); const blob = new Blob([JSON.stringify(data, null, 2)], { type: "application/json" }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = "proxy-assignments.json"; a.click(); URL.revokeObjectURL(url); }, [onExport]); const handleFileSelect = useCallback(async () => { const file = fileRef.current?.files?.[0]; if (!file) return; try { const text = await file.text(); const parsed = JSON.parse(text) as Array<{ email: string; proxyId: string }>; if (!Array.isArray(parsed)) { alert("Invalid format: expected array of { email, proxyId }"); return; } setImporting(true); const result = await onImportPreview(parsed); setDiff(result); setImporting(false); } catch { alert("Failed to parse JSON file"); } }, [onImportPreview]); const handleApply = useCallback(async () => { if (!diff || diff.changes.length === 0) return; setImporting(true); await onApplyImport( diff.changes.map((c) => ({ accountId: c.accountId, proxyId: c.to })), ); setImporting(false); setDiff(null); setShowImport(false); }, [diff, onApplyImport]); const handleClose = useCallback(() => { setShowImport(false); setDiff(null); if (fileRef.current) fileRef.current.value = ""; }, []); return ( <> {/* Buttons */}
{/* Import Modal */} {showImport && (
e.stopPropagation()} >

{t("importPreview")}

{!diff ? (

{t("importFile")}

{importing && (

Loading...

)}
) : (
{diff.changes.length === 0 ? (

{t("noChanges")}

) : ( <>

{diff.changes.length} {t("changesCount")} {diff.unchanged > 0 && ( ({diff.unchanged} unchanged) )}

{diff.changes.map((c) => (
{c.email} {c.from} {c.to}
))}
)}
)} {/* Actions */}
{diff && diff.changes.length > 0 && ( )}
)} ); }