import { useCallback, useMemo, useState } from 'react'; import { toast } from 'react-toastify'; import CancelIcon from '@mui/icons-material/Cancel'; import DownloadIcon from '@mui/icons-material/GetApp'; import WarningIcon from '@mui/icons-material/Warning'; import { Button, Dialog, DialogActions, DialogContent, DialogTitle, Grid, Typography } from '@mui/material'; import * as SystemApi from 'api/system'; import { API, callAction } from 'api/app'; import { dialogStyle } from '@/CustomTheme'; import { useRequest } from 'alova/client'; import type { APIcall } from 'app/main/types'; import SystemMonitor from 'app/status/SystemMonitor'; import { FormLoader, SectionContent, SingleUpload, useLayoutTitle } from 'components'; import { useI18nContext } from 'i18n/i18n-react'; import { saveFile } from 'utils'; const DownloadUpload = () => { const { LL } = useI18nContext(); const [confirmBackup, setConfirmBackup] = useState(false); const [restarting, setRestarting] = useState(false); const { send: sendExportData } = useRequest( (type: string) => callAction({ action: 'export', param: type }), { immediate: false } ) .onSuccess((event) => { saveFile(event.data, event.args[0], '.json'); toast.info(LL.DOWNLOAD_SUCCESSFUL()); }) .onError((error) => { toast.error(String(error.error?.message || 'An error occurred')); }); const { send: sendAPI } = useRequest((data: APIcall) => API(data), { immediate: false }); const { data, send: loadData, error } = useRequest(SystemApi.readSystemStatus); const doRestart = useCallback(async () => { setRestarting(true); try { await sendAPI({ device: 'system', cmd: 'restart', id: 0 }); } catch (error) { toast.error((error as Error).message); setRestarting(false); } }, [sendAPI]); useLayoutTitle(LL.DOWNLOAD_UPLOAD()); const handleCloseBackupDialog = useCallback(() => { setConfirmBackup(false); }, []); const renderBackupDialog = useMemo( () => ( {LL.DOWNLOAD_SYSTEM_BACKUP()}   {LL.WARNING_SYSTEM_BACKUP()} ), [confirmBackup, handleCloseBackupDialog, LL] ); const handleDownload = useCallback( (type: string) => () => { void sendExportData(type); setConfirmBackup(false); }, [sendExportData] ); if (restarting) { return ; } if (!data) { return ( ); } return ( {renderBackupDialog} {LL.DOWNLOAD(0)} {LL.DOWNLOAD_SETTINGS_TEXT()}: {LL.DOWNLOAD_SETTINGS_TEXT2()}: {LL.UPLOAD()} {LL.UPLOAD_TEXT()}: ); }; export default DownloadUpload;