misc_pterodactyl-panel/resources/scripts/components/server/Console.tsx

106 lines
3.4 KiB
TypeScript
Raw Normal View History

import React, { createRef, useEffect, useMemo } from 'react';
import { ITerminalOptions, Terminal } from 'xterm';
import * as TerminalFit from 'xterm/lib/addons/fit/fit';
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
2019-09-06 06:05:24 +00:00
import { ServerContext } from '@/state/server';
const theme = {
background: 'transparent',
cursor: 'transparent',
black: '#000000',
red: '#E54B4B',
green: '#9ECE58',
yellow: '#FAED70',
blue: '#396FE2',
magenta: '#BB80B3',
cyan: '#2DDAFD',
white: '#d0d0d0',
brightBlack: 'rgba(255, 255, 255, 0.2)',
brightRed: '#FF5370',
brightGreen: '#C3E88D',
brightYellow: '#FFCB6B',
brightBlue: '#82AAFF',
brightMagenta: '#C792EA',
brightCyan: '#89DDFF',
brightWhite: '#ffffff',
};
const terminalProps: ITerminalOptions = {
2019-09-06 06:05:24 +00:00
disableStdin: true,
cursorStyle: 'underline',
allowTransparency: true,
fontSize: 12,
fontFamily: 'Menlo, Monaco, Consolas, monospace',
rows: 30,
theme: theme,
};
2019-09-06 06:05:24 +00:00
export default () => {
const ref = createRef<HTMLDivElement>();
const terminal = useMemo(() => new Terminal({ ...terminalProps }), []);
2019-09-06 06:05:24 +00:00
const connected = ServerContext.useStoreState(state => state.socket.connected);
const instance = ServerContext.useStoreState(state => state.socket.instance);
2019-09-06 06:05:24 +00:00
const handleConsoleOutput = (line: string) => terminal.writeln(
line.replace(/(?:\r\n|\r|\n)$/im, '') + '\u001b[0m',
);
2019-09-18 05:54:23 +00:00
const handleCommandKeydown = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key !== 'Enter' || (e.key === 'Enter' && e.currentTarget.value.length < 1)) {
return;
}
instance && instance.send('send command', e.currentTarget.value);
e.currentTarget.value = '';
};
2019-09-06 06:05:24 +00:00
useEffect(() => {
if (ref.current && !terminal.element) {
2019-09-06 06:05:24 +00:00
terminal.open(ref.current);
// @see https://github.com/xtermjs/xterm.js/issues/2265
// @see https://github.com/xtermjs/xterm.js/issues/2230
2019-09-06 06:05:24 +00:00
TerminalFit.fit(terminal);
}
2019-09-06 06:05:24 +00:00
}, [ ref.current ]);
2019-09-06 06:05:24 +00:00
useEffect(() => {
if (connected && instance) {
terminal.clear();
2019-09-18 05:54:23 +00:00
instance.addListener('console output', handleConsoleOutput);
2019-09-06 06:05:24 +00:00
instance.send('send logs');
}
2019-09-06 06:05:24 +00:00
return () => {
2019-09-18 05:54:23 +00:00
instance && instance.removeListener('console output', handleConsoleOutput);
2019-09-06 06:05:24 +00:00
};
}, [ connected, instance ]);
2019-09-06 06:05:24 +00:00
return (
<div className={'text-xs font-mono relative'}>
<SpinnerOverlay visible={!connected} size={'large'}/>
<div
className={'rounded-t p-2 bg-black overflow-scroll w-full'}
style={{
minHeight: '16rem',
maxHeight: '32rem',
2019-09-06 06:05:24 +00:00
}}
>
<div id={'terminal'} ref={ref}/>
</div>
<div className={'rounded-b bg-neutral-900 text-neutral-100 flex'}>
<div className={'flex-no-shrink p-2 font-bold'}>$</div>
<div className={'w-full'}>
2019-09-18 05:54:23 +00:00
<input
type={'text'}
disabled={!instance || !connected}
className={'bg-transparent text-neutral-100 p-2 pl-0 w-full'}
onKeyDown={e => handleCommandKeydown(e)}
/>
</div>
</div>
2019-09-06 06:05:24 +00:00
</div>
);
};