forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerConsole.tsx
More file actions
152 lines (136 loc) · 5.67 KB
/
ServerConsole.tsx
File metadata and controls
152 lines (136 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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 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 SuspenseSpinner from '@/components/elements/SuspenseSpinner';
type PowerAction = 'start' | 'stop' | 'restart' | 'kill';
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'));
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 (
<button
className={'btn btn-red btn-xs'}
disabled={status === 'offline'}
onClick={e => {
e.preventDefault();
onPress(clicked ? 'kill' : 'stop');
setClicked(true);
}}
>
{clicked ? 'Kill' : 'Stop'}
</button>
);
};
export default () => {
const [ memory, setMemory ] = useState(0);
const [ cpu, setCpu ] = 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);
};
const sendPowerCommand = (command: PowerAction) => {
instance && instance.send('set state', command);
};
useEffect(() => {
if (!connected || !instance) {
return;
}
instance.addListener('stats', statsListener);
return () => {
instance.removeListener('stats', statsListener);
};
}, [ connected ]);
return (
<div className={'my-10 flex'}>
<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}
fixedWidth={true}
className={classNames('mr-1', {
'text-red-500': status === 'offline',
'text-yellow-500': [ 'running', 'offline' ].indexOf(status) < 0,
'text-green-500': status === 'running',
})}
/>
{status}
</p>
<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>
</div>
</div>
<GreyBox className={'justify-center'}>
<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>
<StopOrKillButton onPress={action => sendPowerCommand(action)}/>
</GreyBox>
</div>
<SuspenseSpinner>
<div className={'mx-4 w-3/4 mr-4'}>
<ChunkedConsole/>
</div>
</SuspenseSpinner>
</div>
);
};