import { Grid, Switch } from '@mui/material'; import { FC, useMemo } from 'react'; import api from '../../lib/api'; import ContainedButton from '../ContainedButton'; import FlexBox from '../FlexBox'; import handleAPIError from '../../lib/handleAPIError'; import MessageBox, { Message } from '../MessageBox'; import { Panel, PanelHeader } from '../Panels'; import Spinner from '../Spinner'; import { BodyText, HeaderText } from '../Text'; import useProtect from '../../hooks/useProtect'; import useProtectedState from '../../hooks/useProtectedState'; const StretchedButton: FC = (props) => ( ); const SimpleOperationsPanel: FC = ({ installTarget = 'disabled', onSubmit, title, }) => { const { protect } = useProtect(); const [message, setMessage] = useProtectedState( undefined, protect, ); const headerElement = useMemo( () => title ? ( {title} ) : ( ), [title], ); const messageElement = useMemo( () => message && ( { setMessage(undefined); }} /> ), [message, setMessage], ); return ( {headerElement} Install target { let actionText = 'disable'; let actionTextCap = 'Disable'; if (isChecked) { actionText = 'enable'; actionTextCap = 'Enable'; } onSubmit?.call(null, { actionProceedText: actionTextCap, content: ( Would you like to {actionText} "Install target" on this striker? It'll take a few moments to complete. ), onProceedAppend: () => { api .put( '/host/local', { isEnableInstallTarget: isChecked }, { params: { handler: 'install-target' } }, ) .catch((error) => { const emsg = handleAPIError(error); emsg.children = `Failed to ${actionText} "Install target". ${emsg.children}`; setMessage(emsg); }); }, titleText: `${actionTextCap} "Install target" on ${title}?`, }); }} /> { onSubmit?.call(null, { actionProceedText: 'Update', content: ( Would you like to update the operating system on this striker? It'll be placed into maintenance mode until the update completes. ), onProceedAppend: () => { api.put('/command/update-system').catch((error) => { const emsg = handleAPIError(error); emsg.children = `Failed to initiate system update. ${emsg.children}`; setMessage(emsg); }); }, titleText: `Update operating system on ${title}?`, }); }} > Update system Reconfigure striker { onSubmit?.call(null, { actionProceedText: 'Reboot', content: ( Would you like to reboot this striker? ), onProceedAppend: () => { api.put('/command/reboot-host').catch((error) => { const emsg = handleAPIError(error); emsg.children = `Failed to initiate system reboot. ${emsg.children}`; setMessage(emsg); }); }, titleText: `Reboot ${title}?`, }); }} > Reboot { onSubmit?.call(null, { actionProceedText: 'Shutdown', content: ( Would you like to shutdown this striker? ), onProceedAppend: () => { api.put('/command/poweroff-host').catch((error) => { const emsg = handleAPIError(error); emsg.children = `Failed to initiate system shutdown. ${emsg.children}`; setMessage(emsg); }); }, titleText: `Shutdown ${title}?`, }); }} > Shutdown {messageElement} ); }; export default SimpleOperationsPanel;