2022-11-25 20:25:03 +00:00
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
import * as React from 'react';
|
2022-06-20 21:26:47 +00:00
|
|
|
import { Button } from '@/components/elements/button/index';
|
|
|
|
import Can from '@/components/elements/Can';
|
|
|
|
import { ServerContext } from '@/state/server';
|
2022-06-21 22:43:59 +00:00
|
|
|
import { PowerAction } from '@/components/server/console/ServerConsoleContainer';
|
2022-06-20 21:26:47 +00:00
|
|
|
import { Dialog } from '@/components/elements/dialog';
|
|
|
|
|
|
|
|
interface PowerButtonProps {
|
|
|
|
className?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ({ className }: PowerButtonProps) => {
|
2022-06-26 19:13:52 +00:00
|
|
|
const [open, setOpen] = useState(false);
|
2022-11-25 20:25:03 +00:00
|
|
|
const status = ServerContext.useStoreState(state => state.status.value);
|
|
|
|
const instance = ServerContext.useStoreState(state => state.socket.instance);
|
2022-06-20 21:26:47 +00:00
|
|
|
|
|
|
|
const killable = status === 'stopping';
|
2022-06-26 19:13:52 +00:00
|
|
|
const onButtonClick = (
|
|
|
|
action: PowerAction | 'kill-confirmed',
|
2022-11-25 20:25:03 +00:00
|
|
|
e: React.MouseEvent<HTMLButtonElement, MouseEvent>,
|
2022-06-26 19:13:52 +00:00
|
|
|
): void => {
|
2022-06-20 21:26:47 +00:00
|
|
|
e.preventDefault();
|
|
|
|
if (action === 'kill') {
|
|
|
|
return setOpen(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (instance) {
|
|
|
|
setOpen(false);
|
|
|
|
instance.send('set state', action === 'kill-confirmed' ? 'kill' : action);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-06-26 01:29:41 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (status === 'offline') {
|
|
|
|
setOpen(false);
|
|
|
|
}
|
2022-06-26 19:13:52 +00:00
|
|
|
}, [status]);
|
2022-06-26 01:29:41 +00:00
|
|
|
|
2022-06-20 21:26:47 +00:00
|
|
|
return (
|
|
|
|
<div className={className}>
|
|
|
|
<Dialog.Confirm
|
|
|
|
open={open}
|
|
|
|
hideCloseIcon
|
|
|
|
onClose={() => setOpen(false)}
|
|
|
|
title={'Forcibly Stop Process'}
|
|
|
|
confirm={'Continue'}
|
|
|
|
onConfirmed={onButtonClick.bind(this, 'kill-confirmed')}
|
|
|
|
>
|
|
|
|
Forcibly stopping a server can lead to data corruption.
|
|
|
|
</Dialog.Confirm>
|
|
|
|
<Can action={'control.start'}>
|
|
|
|
<Button
|
2022-06-27 23:56:26 +00:00
|
|
|
className={'flex-1'}
|
2022-06-20 21:26:47 +00:00
|
|
|
disabled={status !== 'offline'}
|
|
|
|
onClick={onButtonClick.bind(this, 'start')}
|
|
|
|
>
|
|
|
|
Start
|
|
|
|
</Button>
|
|
|
|
</Can>
|
|
|
|
<Can action={'control.restart'}>
|
2022-06-27 23:56:26 +00:00
|
|
|
<Button.Text className={'flex-1'} disabled={!status} onClick={onButtonClick.bind(this, 'restart')}>
|
2022-06-20 21:26:47 +00:00
|
|
|
Restart
|
|
|
|
</Button.Text>
|
|
|
|
</Can>
|
|
|
|
<Can action={'control.stop'}>
|
|
|
|
<Button.Danger
|
2022-06-27 23:56:26 +00:00
|
|
|
className={'flex-1'}
|
2022-06-20 21:26:47 +00:00
|
|
|
disabled={status === 'offline'}
|
|
|
|
onClick={onButtonClick.bind(this, killable ? 'kill' : 'stop')}
|
|
|
|
>
|
|
|
|
{killable ? 'Kill' : 'Stop'}
|
|
|
|
</Button.Danger>
|
|
|
|
</Can>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|