move restarting to parent component - #1920

This commit is contained in:
proddy
2024-08-16 13:48:28 +02:00
parent d3a7ab8fc1
commit 5404537da8
3 changed files with 53 additions and 52 deletions

View File

@@ -1,6 +1,8 @@
import { useState } from 'react';
import { toast } from 'react-toastify';
import DownloadIcon from '@mui/icons-material/GetApp';
import PowerSettingsNewIcon from '@mui/icons-material/PowerSettingsNew';
import { Box, Button, Divider, Link, Typography } from '@mui/material';
import * as SystemApi from 'api/system';
@@ -15,8 +17,10 @@ import { getDevVersion, getStableVersion } from 'api/system';
import { useRequest } from 'alova/client';
import type { APIcall } from 'app/main/types';
import RestartMonitor from 'app/status/RestartMonitor';
import {
FormLoader,
MessageBox,
SectionContent,
SingleUpload,
useLayoutTitle
@@ -26,6 +30,9 @@ import { useI18nContext } from 'i18n/i18n-react';
const DownloadUpload = () => {
const { LL } = useI18nContext();
const [restarting, setRestarting] = useState<boolean>(false);
const [restartNeeded, setRestartNeeded] = useState<boolean>(false);
const { send: sendSettings } = useRequest(getSettings(), {
immediate: false
}).onSuccess((event) => {
@@ -41,7 +48,7 @@ const DownloadUpload = () => {
const { send: sendEntities } = useRequest(getEntities(), {
immediate: false
}).onSuccess((event) => {
saveFile(event.data, 'entities.json');
saveFile(event.data, 'custom_entities.json');
});
const { send: sendSchedule } = useRequest(getSchedule(), {
@@ -65,6 +72,20 @@ const DownloadUpload = () => {
error
} = useRequest(SystemApi.readHardwareStatus);
const { send: restartCommand } = useRequest(SystemApi.restart(), {
immediate: false
});
const restart = async () => {
await restartCommand()
.then(() => {
setRestarting(true);
})
.catch((error: Error) => {
toast.error(error.message);
});
};
// called immediately to get the latest version, on page load
// set immediate to false to avoid calling the API on page load and GH blocking while testing!
const { data: latestVersion } = useRequest(getStableVersion, {
@@ -100,14 +121,14 @@ const DownloadUpload = () => {
return data.esp_platform;
};
const saveFile = (json: unknown, endpoint: string) => {
const saveFile = (json: unknown, filename: string) => {
const anchor = document.createElement('a');
anchor.href = URL.createObjectURL(
new Blob([JSON.stringify(json, null, 2)], {
type: 'text/plain'
})
);
anchor.download = 'emsesp_' + endpoint;
anchor.download = 'emsesp_' + filename;
anchor.click();
URL.revokeObjectURL(anchor.href);
toast.info(LL.DOWNLOAD_SUCCESSFUL());
@@ -284,12 +305,27 @@ const DownloadUpload = () => {
)}
</Box>
<SingleUpload />
<SingleUpload setRestartNeeded={setRestartNeeded} />
{restartNeeded && (
<MessageBox mt={2} level="warning" message={LL.RESTART_TEXT(0)}>
<Button
startIcon={<PowerSettingsNewIcon />}
variant="contained"
color="error"
onClick={restart}
>
{LL.RESTART()}
</Button>
</MessageBox>
)}
</>
);
};
return <SectionContent>{content()}</SectionContent>;
return (
<SectionContent>{restarting ? <RestartMonitor /> : content()}</SectionContent>
);
};
export default DownloadUpload;