2019-09-18 05:33:14 +00:00
|
|
|
import React, { lazy, useEffect, useState } from 'react';
|
2019-07-10 04:25:57 +00:00
|
|
|
import { ServerContext } from '@/state/server';
|
2019-09-18 04:59:35 +00:00
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
|
|
|
import { faServer } from '@fortawesome/free-solid-svg-icons/faServer';
|
|
|
|
import { faCircle } from '@fortawesome/free-solid-svg-icons/faCircle';
|
|
|
|
import classNames from 'classnames';
|
2019-09-18 05:33:14 +00:00
|
|
|
import styled from 'styled-components';
|
|
|
|
import { faMemory } from '@fortawesome/free-solid-svg-icons/faMemory';
|
|
|
|
import { faMicrochip } from '@fortawesome/free-solid-svg-icons/faMicrochip';
|
|
|
|
import { bytesToHuman } from '@/helpers';
|
|
|
|
import Spinner from '@/components/elements/Spinner';
|
|
|
|
|
|
|
|
const GreyBox = styled.div`
|
|
|
|
${tw`mt-4 shadow-md bg-neutral-700 rounded p-3 flex text-xs`}
|
|
|
|
`;
|
|
|
|
|
|
|
|
const ChunkedConsole = lazy(() => import('@/components/server/Console'));
|
2019-06-29 05:17:29 +00:00
|
|
|
|
2019-06-29 23:59:50 +00:00
|
|
|
export default () => {
|
2019-09-18 05:33:14 +00:00
|
|
|
const [ memory, setMemory ] = useState(0);
|
|
|
|
const [ cpu, setCpu ] = useState(0);
|
|
|
|
|
2019-09-18 04:59:35 +00:00
|
|
|
const server = ServerContext.useStoreState(state => state.server.data!);
|
2019-07-10 04:25:57 +00:00
|
|
|
const status = ServerContext.useStoreState(state => state.status.value);
|
2019-06-29 23:59:50 +00:00
|
|
|
|
2019-09-18 05:33:14 +00:00
|
|
|
const { connected, instance } = ServerContext.useStoreState(state => state.socket);
|
|
|
|
|
|
|
|
const statsListener = (data: string) => {
|
|
|
|
let stats: any = {};
|
|
|
|
try {
|
|
|
|
stats = JSON.parse(data);
|
|
|
|
} catch (e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setMemory(stats.memory_bytes);
|
|
|
|
setCpu(stats.cpu_absolute);
|
|
|
|
};
|
|
|
|
|
2019-09-18 06:12:38 +00:00
|
|
|
const sendPowerCommand = (command: 'start' | 'stop' | 'restart' | 'kill') => {
|
|
|
|
instance && instance.send('set state', command);
|
|
|
|
};
|
|
|
|
|
2019-09-18 05:33:14 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (!connected || !instance) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
instance.addListener('stats', statsListener);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
instance.removeListener('stats', statsListener);
|
|
|
|
};
|
|
|
|
}, [ connected ]);
|
|
|
|
|
2019-06-29 23:59:50 +00:00
|
|
|
return (
|
|
|
|
<div className={'my-10 flex'}>
|
2019-09-18 04:59:35 +00:00
|
|
|
<div className={'flex-1 ml-4'}>
|
|
|
|
<div className={'rounded shadow-md bg-neutral-700'}>
|
|
|
|
<div className={'bg-neutral-900 rounded-t p-3 border-b border-black'}>
|
|
|
|
<p className={'text-sm uppercase'}>
|
|
|
|
<FontAwesomeIcon icon={faServer} className={'mr-1 text-neutral-300'}/> {server.name}
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
<div className={'p-3'}>
|
|
|
|
<p className={'text-xs uppercase'}>
|
|
|
|
<FontAwesomeIcon
|
|
|
|
icon={faCircle}
|
2019-09-18 05:33:14 +00:00
|
|
|
fixedWidth={true}
|
2019-09-18 04:59:35 +00:00
|
|
|
className={classNames('mr-1', {
|
|
|
|
'text-red-500': status === 'offline',
|
2019-09-18 05:33:14 +00:00
|
|
|
'text-yellow-500': [ 'running', 'offline' ].indexOf(status) < 0,
|
2019-09-18 04:59:35 +00:00
|
|
|
'text-green-500': status === 'running',
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
{status}
|
|
|
|
</p>
|
2019-09-18 05:33:14 +00:00
|
|
|
<p className={'text-xs mt-2'}>
|
|
|
|
<FontAwesomeIcon
|
|
|
|
icon={faMemory}
|
|
|
|
fixedWidth={true}
|
|
|
|
className={'mr-1'}
|
|
|
|
/>
|
|
|
|
{bytesToHuman(memory)}
|
|
|
|
<span className={'text-neutral-500'}>/ {server.limits.memory} MB</span>
|
|
|
|
</p>
|
|
|
|
<p className={'text-xs mt-2'}>
|
|
|
|
<FontAwesomeIcon
|
|
|
|
icon={faMicrochip}
|
|
|
|
fixedWidth={true}
|
|
|
|
className={'mr-1'}
|
|
|
|
/>
|
|
|
|
{cpu.toFixed(2)} %
|
|
|
|
</p>
|
2019-09-18 04:59:35 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2019-09-18 05:33:14 +00:00
|
|
|
<GreyBox className={'justify-center'}>
|
2019-09-18 06:12:38 +00:00
|
|
|
<button
|
|
|
|
className={'btn btn-secondary btn-xs mr-2'}
|
|
|
|
disabled={status !== 'offline'}
|
|
|
|
onClick={e => {
|
|
|
|
e.preventDefault();
|
|
|
|
sendPowerCommand('start');
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Start
|
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
className={'btn btn-secondary btn-xs mr-2'}
|
|
|
|
onClick={e => {
|
|
|
|
e.preventDefault();
|
|
|
|
sendPowerCommand('restart');
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Restart
|
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
className={'btn btn-red btn-xs'}
|
|
|
|
disabled={status === 'offline'}
|
|
|
|
onClick={e => {
|
|
|
|
e.preventDefault();
|
|
|
|
sendPowerCommand(status === 'stopping' ? 'kill' : 'stop');
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Stop
|
|
|
|
</button>
|
2019-09-18 05:33:14 +00:00
|
|
|
</GreyBox>
|
2019-09-18 04:59:35 +00:00
|
|
|
</div>
|
2019-09-18 05:33:14 +00:00
|
|
|
<React.Suspense
|
|
|
|
fallback={
|
|
|
|
<div className={'mx-4 w-3/4 mr-4 flex items-center justify-center'}>
|
|
|
|
<Spinner centered={true} size={'normal'}/>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<div className={'mx-4 w-3/4 mr-4'}>
|
|
|
|
<ChunkedConsole/>
|
|
|
|
</div>
|
|
|
|
</React.Suspense>
|
2019-06-29 23:14:32 +00:00
|
|
|
</div>
|
2019-06-29 23:59:50 +00:00
|
|
|
);
|
|
|
|
};
|