import { useCallback, useMemo, useState } from 'react'; import { toast } from 'react-toastify'; import DownloadIcon from '@mui/icons-material/GetApp'; import { Box, Button, Grid, Typography } from '@mui/material'; import * as SystemApi from 'api/system'; import { API, callAction } from 'api/app'; 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'; interface DownloadButton { key: string; type: string; label: string | number; isGridButton: boolean; } const DownloadUpload = () => { const { LL } = useI18nContext(); 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 downloadButtons: DownloadButton[] = useMemo( () => [ { key: 'settings', type: 'settings', label: LL.SETTINGS_OF(LL.APPLICATION()), isGridButton: true }, { key: 'customizations', type: 'customizations', label: LL.CUSTOMIZATIONS(), isGridButton: true }, { key: 'entities', type: 'entities', label: LL.CUSTOM_ENTITIES(0), isGridButton: true }, { key: 'schedule', type: 'schedule', label: LL.SCHEDULE(0), isGridButton: true }, { key: 'allvalues', type: 'allvalues', label: LL.ALLVALUES(), isGridButton: false } ], [LL] ); const handleDownload = useCallback( (type: string) => () => { void sendExportData(type); }, [sendExportData] ); if (restarting) { return ; } if (!data) { return ( ); } const gridButtons = downloadButtons.filter((btn) => btn.isGridButton); const standaloneButton = downloadButtons.find((btn) => !btn.isGridButton); return ( {LL.DOWNLOAD(0)} {LL.DOWNLOAD_SETTINGS_TEXT()}. {gridButtons.map((button) => ( ))} {LL.DOWNLOAD_SETTINGS_TEXT2()}. {standaloneButton && ( )} {LL.UPLOAD()} {LL.UPLOAD_TEXT()}. ); }; export default DownloadUpload;