import { useState, useRef } from "react";
import SectionCard from "./SectionCard";
import Toggle from "./Toggle";
import SaveToast from "./SaveToast";
import { getAjaxUrl, getNonce } from "../../utils/api";
import { getAdminConfig } from "../../utils/helpers";
const EXPORT_ACTION = "ai_image_export_settings";
const IMPORT_ACTION = "ai_image_import_settings";
function TipBox({ children, variant = "info" }) {
const styles =
variant === "warning"
? "bg-amber-50/80 border-amber-100 text-amber-950"
: "bg-gray-50 border-gray-100 text-gray-600";
return (
{children}
);
}
export default function ExportImportSection() {
const config = getAdminConfig();
const version = config.version || "1.0.0";
const fileInputRef = useRef(null);
// Off by default: the export is a plain-text file that usually lands in a
// synced Downloads folder, so secrets leave the site only on request.
const [includeApiKeysExport, setIncludeApiKeysExport] = useState(false);
const [importApiKeys, setImportApiKeys] = useState(true);
const [selectedFile, setSelectedFile] = useState(null);
const [exporting, setExporting] = useState(false);
const [importing, setImporting] = useState(false);
const [message, setMessage] = useState({ type: null, text: "" });
const handleExport = async () => {
setMessage({ type: null, text: "" });
setExporting(true);
try {
const formData = new FormData();
formData.append("action", EXPORT_ACTION);
formData.append("nonce", getNonce());
formData.append("include_api_keys", includeApiKeysExport ? "1" : "0");
const res = await fetch(getAjaxUrl(), { method: "POST", body: formData });
const data = await res.json();
if (!data.success || !data.data?.bundle) {
throw new Error(data.data?.message || "Export failed.");
}
const json = JSON.stringify(data.data.bundle, null, 2);
const blob = new Blob([json], { type: "application/json" });
const url = URL.createObjectURL(blob);
const stamp = new Date().toISOString().slice(0, 10);
const link = document.createElement("a");
link.href = url;
link.download = `ai-image-settings-v${version}-${stamp}.json`;
document.body.appendChild(link);
link.click();
link.remove();
URL.revokeObjectURL(url);
setMessage({ type: "success", text: "Settings exported. Your download should start shortly." });
} catch (err) {
setMessage({ type: "error", text: err?.message || "Export failed." });
} finally {
setExporting(false);
}
};
const handleFileChange = (e) => {
const file = e.target.files?.[0];
setSelectedFile(file || null);
setMessage({ type: null, text: "" });
};
const handleImport = async () => {
if (!selectedFile) {
setMessage({ type: "error", text: "Please choose a JSON settings file to import." });
return;
}
const confirmed = window.confirm(
"Importing will overwrite your current plugin settings on this site. Continue?"
);
if (!confirmed) return;
setMessage({ type: null, text: "" });
setImporting(true);
try {
const fileText = await selectedFile.text();
JSON.parse(fileText);
const formData = new FormData();
formData.append("action", IMPORT_ACTION);
formData.append("nonce", getNonce());
formData.append("data", fileText);
formData.append("import_api_keys", importApiKeys ? "1" : "0");
const res = await fetch(getAjaxUrl(), { method: "POST", body: formData });
const data = await res.json();
if (!data.success) {
throw new Error(data.data?.message || "Import failed.");
}
setMessage({ type: "success", text: data.data?.message || "Settings imported." });
setSelectedFile(null);
if (fileInputRef.current) {
fileInputRef.current.value = "";
}
setTimeout(() => {
window.location.reload();
}, 1200);
} catch (err) {
const msg =
err instanceof SyntaxError
? "Invalid JSON file. Please select a valid export from this plugin."
: err?.message || "Import failed.";
setMessage({ type: "error", text: msg });
} finally {
setImporting(false);
}
};
return (
setMessage({ type: null, text: "" })} />
Export settings
Include API keys
Off by default. Turning this on writes your OpenAI, Gemini, Pexels, Unsplash, Pixabay and
Giphy keys into the downloaded file as plain text — anyone who opens that file can use them.
Tip: Export before major changes so you can
restore your setup quickly.
Import settings
Upload a JSON file previously exported from Instant Image Generator.
Import API keys from file
When off, provider toggles and other settings import but existing API keys on this site are kept.