import React, { lazy, useEffect, useState } from 'react'; import { ServerContext } from '@/state/server'; 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'; import { faMemory } from '@fortawesome/free-solid-svg-icons/faMemory'; import { faMicrochip } from '@fortawesome/free-solid-svg-icons/faMicrochip'; import { faHdd } from '@fortawesome/free-solid-svg-icons/faHdd'; import { bytesToHuman } from '@/helpers'; import SuspenseSpinner from '@/components/elements/SuspenseSpinner'; import TitledGreyBox from '@/components/elements/TitledGreyBox'; import Can from '@/components/elements/Can'; import PageContentBlock from '@/components/elements/PageContentBlock'; import ContentContainer from '@/components/elements/ContentContainer'; type PowerAction = 'start' | 'stop' | 'restart' | 'kill'; const ChunkedConsole = lazy(() => import(/* webpackChunkName: "console" */'@/components/server/Console')); const ChunkedStatGraphs = lazy(() => import(/* webpackChunkName: "graphs" */'@/components/server/StatGraphs')); const StopOrKillButton = ({ onPress }: { onPress: (action: PowerAction) => void }) => { const [ clicked, setClicked ] = useState(false); const status = ServerContext.useStoreState(state => state.status.value); useEffect(() => { setClicked(state => [ 'stopping' ].indexOf(status) < 0 ? false : state); }, [ status ]); return ( { e.preventDefault(); onPress(clicked ? 'kill' : 'stop'); setClicked(true); }} > {clicked ? 'Kill' : 'Stop'} ); }; export default () => { const [ memory, setMemory ] = useState(0); const [ cpu, setCpu ] = useState(0); const [ disk, setDisk ] = useState(0); const server = ServerContext.useStoreState(state => state.server.data!); const status = ServerContext.useStoreState(state => state.status.value); 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); setDisk(stats.disk_bytes); }; const sendPowerCommand = (command: PowerAction) => { instance && instance.send('set state', command); }; useEffect(() => { if (!connected || !instance) { return; } instance.addListener('stats', statsListener); return () => { instance.removeListener('stats', statsListener); }; }, [ instance, connected ]); return ( {status} {cpu.toFixed(2)} % {bytesToHuman(memory)} / {server.limits.memory} MB {bytesToHuman(disk)} / {server.limits.disk} MB {!server.isInstalling ? { e.preventDefault(); sendPowerCommand('start'); }} > Start { e.preventDefault(); sendPowerCommand('restart'); }} > Restart sendPowerCommand(action)}/> : This server is currently running its installation process and most actions are unavailable. } ); };
{status}
{cpu.toFixed(2)} %
{bytesToHuman(memory)} / {server.limits.memory} MB
{bytesToHuman(disk)} / {server.limits.disk} MB
This server is currently running its installation process and most actions are unavailable.