/**
* Backup & Reset section — move the whole settings tree in or out, or reset
* it to defaults.
*
* Export downloads the `/aureon/v1/export` payload as a JSON file. Import reads
* a chosen file and POSTs it to `/aureon/v1/import`, which runs it through the
* normal sanitizer before persisting; the panel then reloads so every section
* reflects the imported values.
*
* Two cards rather than the Export/Import tabs this used to have. There are only
* two of them, and a tab bar spent a click hiding half of a screen that fits on
* one — worse, it hid the *warning* on the import side from anyone who only ever
* opened Export.
*
* Import asks before it runs. It replaces every setting on the site and there's
* no undo, so the one thing this screen must not do is treat "picked the wrong
* file in the OS dialog" as consent — the file is read and parsed first, so the
* prompt can name what's actually in it before anything is written.
*
* Reset sits under both as a full-width card: it is the third whole-site action
* and the only one with nothing to pick, so it confirms first and then POSTs to
* `/aureon/v1/reset`, reloading the same way Import does.
*/
import { useState, useRef } from '@wordpress/element';
import apiFetch from '@wordpress/api-fetch';
import { __, sprintf } from '@wordpress/i18n';
import { Button, Notice } from '@wordpress/components';
import {
DatabaseBackup,
Download,
Upload,
FileUp,
BookOpen,
ArrowUpRight,
Info,
Check,
Lightbulb,
RotateCcw,
} from 'lucide-react';
import FieldGroup from '../controls/field-group';
import ConfirmDialog from '../confirm-dialog';
import SectionIntro from '../controls/section-intro';
interface ToolsSchema {
label: string;
}
interface ToolsSectionProps {
schema?: ToolsSchema;
}
const DOCS_URL = 'https://aureonwp.com/docs/import-export/';
// What an export actually carries. Listed rather than summarised in a sentence
// because the question a reader has before trusting a backup is "does it include
// X?", and a list is what you can scan for X.
const INCLUDED = [
__(
'Theme design settings (colors, typography, layout, header, footer)',
'aureon'
),
__(
'All module settings, including those added by companion plugins',
'aureon'
),
__( 'Custom CSS and advanced options', 'aureon' ),
];
const TIPS = [
__( 'Use export before updating or making major changes', 'aureon' ),
__( 'You can import settings from any Aureon-enabled site', 'aureon' ),
__( 'Keep a backup file for future use', 'aureon' ),
];
type Feedback = { status: 'success' | 'error'; message: string } | null;
// A file staged for import: parsed and held until the reader confirms.
type Staged = { name: string; size: number; data: unknown } | null;
export default function ToolsSection( { schema }: ToolsSectionProps ) {
const [ busy, setBusy ] = useState( false );
const [ feedback, setFeedback ] = useState< Feedback >( null );
const [ staged, setStaged ] = useState< Staged >( null );
const [ dragging, setDragging ] = useState( false );
const [ confirmingReset, setConfirmingReset ] = useState( false );
const fileRef = useRef< HTMLInputElement >( null );
if ( ! schema ) {
return null;
}
const handleExport = async () => {
setBusy( true );
setFeedback( null );
try {
const data = await apiFetch( { path: '/aureon/v1/export' } );
const blob = new window.Blob( [ JSON.stringify( data, null, 2 ) ], {
type: 'application/json',
} );
const url = window.URL.createObjectURL( blob );
const anchor = document.createElement( 'a' );
const stamp = new Date().toISOString().slice( 0, 10 );
anchor.href = url;
anchor.download = `aureon-settings-${ stamp }.json`;
document.body.appendChild( anchor );
anchor.click();
anchor.remove();
window.URL.revokeObjectURL( url );
setFeedback( {
status: 'success',
message: __( 'Settings exported.', 'aureon' ),
} );
} catch ( err ) {
setFeedback( {
status: 'error',
message: __( 'Export failed.', 'aureon' ),
} );
} finally {
setBusy( false );
}
};
// Reading and parsing happens on pick; writing waits for the confirm. A file
// that isn't valid JSON is rejected here, before the reader is asked to
// approve overwriting everything with it.
const stageFile = async ( file: File ) => {
setFeedback( null );
try {
const parsed = JSON.parse( await file.text() );
if ( ! parsed || 'object' !== typeof parsed ) {
throw new Error( 'not an object' );
}
setStaged( { name: file.name, size: file.size, data: parsed } );
} catch ( err ) {
setFeedback( {
status: 'error',
message: __(
'That file isn’t a valid Aureon export.',
'aureon'
),
} );
}
// Clear the input so picking the same file again still fires a change.
if ( fileRef.current ) {
fileRef.current.value = '';
}
};
const runImport = async () => {
if ( ! staged ) {
return;
}
const data = staged.data;
setStaged( null );
setBusy( true );
setFeedback( null );
try {
await apiFetch( {
path: '/aureon/v1/import',
method: 'POST',
data: data as Record< string, unknown >,
} );
setFeedback( {
status: 'success',
message: __( 'Settings imported. Reloading…', 'aureon' ),
} );
window.setTimeout( () => window.location.reload(), 800 );
} catch ( err ) {
setFeedback( {
status: 'error',
message: __(
'Import failed — is this a valid Aureon export file?',
'aureon'
),
} );
setBusy( false );
}
};
const runReset = async () => {
setConfirmingReset( false );
setBusy( true );
setFeedback( null );
try {
await apiFetch( { path: '/aureon/v1/reset', method: 'POST' } );
setFeedback( {
status: 'success',
message: __( 'Settings reset. Reloading…', 'aureon' ),
} );
window.setTimeout( () => window.location.reload(), 800 );
} catch ( err ) {
setFeedback( {
status: 'error',
message: __( 'Reset failed. Nothing was changed.', 'aureon' ),
} );
setBusy( false );
}
};
const onDrop = ( event: React.DragEvent ) => {
event.preventDefault();
setDragging( false );
const file = event.dataTransfer.files?.[ 0 ];
if ( file ) {
stageFile( file );
}
};
return (
{ __(
'Back up, restore or reset your theme settings.',
'aureon'
) }
{ __( 'Drop a .json file here', 'aureon' ) }
{ __( 'or', 'aureon' ) }
{ __(
'Export the settings from your current site and import them on the new site. It’s a quick and easy way to keep the same design and configuration.',
'aureon'
) }
{ __( 'Quick tips', 'aureon' ) }
{ schema.label }
{ INCLUDED.map( ( item ) => (
{ __( 'Need to move your site?', 'aureon' ) }
{ TIPS.map( ( tip ) => (
{ title }
{ text }