import { isDesktopApplication } from '@/utils'; import { alertDialog } from '@Services/alertService'; import { STRING_IMPORT_SUCCESS, STRING_INVALID_IMPORT_FILE, STRING_UNSUPPORTED_BACKUP_FILE_VERSION, StringImportError } from '@/strings'; import { BackupFile } from '@node_modules/@standardnotes/snjs'; import { useState } from '@node_modules/preact/hooks'; import { WebApplication } from '@/ui_models/application'; import { JSXInternal } from '@node_modules/preact/src/jsx'; import TargetedEvent = JSXInternal.TargetedEvent; import { StateUpdater } from 'preact/hooks'; import { FC } from 'react'; type Props = { application: WebApplication; isBackupEncrypted: boolean; isEncryptionEnabled: boolean; setIsBackupEncrypted: StateUpdater; } const DataBackup: FC = ({ application, isBackupEncrypted, isEncryptionEnabled, setIsBackupEncrypted }) => { const [isImportDataLoading, setIsImportDataLoading] = useState(false); const downloadDataArchive = () => { application.getArchiveService().downloadBackup(isBackupEncrypted); }; const readFile = async (file: File): Promise => { return new Promise((resolve) => { const reader = new FileReader(); reader.onload = (e) => { try { const data = JSON.parse(e.target!.result as string); resolve(data); } catch (e) { application.alertService.alert(STRING_INVALID_IMPORT_FILE); } }; reader.readAsText(file); }); }; const performImport = async (data: BackupFile) => { setIsImportDataLoading(true); const result = await application.importData(data); setIsImportDataLoading(false); if (!result) { return; } let statusText = STRING_IMPORT_SUCCESS; if ('error' in result) { statusText = result.error; } else if (result.errorCount) { statusText = StringImportError(result.errorCount); } void alertDialog({ text: statusText }); }; const importFileSelected = async (event: TargetedEvent) => { const { files } = (event.target as HTMLInputElement); if (!files) { return; } const file = files[0]; const data = await readFile(file); if (!data) { return; } const version = data.version || data.keyParams?.version || data.auth_params?.version; if (!version) { await performImport(data); return; } if ( application.protocolService.supportedVersions().includes(version) ) { await performImport(data); } else { setIsImportDataLoading(false); void alertDialog({ text: STRING_UNSUPPORTED_BACKUP_FILE_VERSION }); } }; return ( <> {isImportDataLoading ? (
) : (
Data Backups
Download a backup of all your data.
{isEncryptionEnabled && (
)}
{isDesktopApplication() && (

Backups are automatically created on desktop and can be managed via the "Backups" top-level menu.

)}
)} ); }; export default DataBackup;