import { useState } from 'react' import { useNavigate } from 'react-router-dom' import { useTranslation } from 'react-i18next' import { createBackup } from '../api/backups' import { useJSONImport } from '../hooks/useJSONImport' import ImportSuccessModal from '../components/dialogs/ImportSuccessModal' export default function ImportJSON() { const { t } = useTranslation() const navigate = useNavigate() const { preview, loading, error, upload, commit, committing, commitResult, clearCommitResult, cancel, reset, } = useJSONImport() const [content, setContent] = useState('') const [showReview, setShowReview] = useState(false) const [showSuccessModal, setShowSuccessModal] = useState(false) const [resolutions, setResolutions] = useState>({}) const [names] = useState>({}) const handleUpload = async () => { if (!content.trim()) { return } try { JSON.parse(content) } catch { alert(t('importJSON.invalidJSON')) return } try { await upload(content) setShowReview(true) } catch { // Error is handled by hook } } const handleFileUpload = async (e: React.ChangeEvent) => { const file = e.target.files?.[0] if (!file) return const text = await file.text() setContent(text) } const handleCommit = async () => { try { await createBackup() await commit(resolutions, names) setContent('') setShowReview(false) setShowSuccessModal(true) } catch { // Error is handled by hook } } const handleCloseSuccessModal = () => { setShowSuccessModal(false) clearCommitResult() } const handleCancel = async () => { if (confirm(t('importJSON.cancelConfirm'))) { try { await cancel() setShowReview(false) reset() } catch { // Error is handled by hook } } } const handleResolutionChange = (domain: string, resolution: string) => { setResolutions((prev) => ({ ...prev, [domain]: resolution })) } return (

{t('importJSON.title')}

{error && (
{error.message}
)} {!showReview && (

{t('importJSON.title')}

{t('importJSON.description')}

{t('importCaddy.orPasteContent')}