From 1ae374069cbe293c0fba4b5445d8d620a2d3e723 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Thu, 5 Sep 2019 23:05:24 -0700 Subject: [PATCH] Still completely broken terminal... --- .../scripts/components/server/Console.tsx | 145 ++++++++---------- 1 file changed, 60 insertions(+), 85 deletions(-) diff --git a/resources/scripts/components/server/Console.tsx b/resources/scripts/components/server/Console.tsx index 5b1bcc1d..ce2514bf 100644 --- a/resources/scripts/components/server/Console.tsx +++ b/resources/scripts/components/server/Console.tsx @@ -1,10 +1,8 @@ -import React, { createRef } from 'react'; +import React, { createRef, useEffect } from 'react'; import { Terminal } from 'xterm'; import * as TerminalFit from 'xterm/lib/addons/fit/fit'; import SpinnerOverlay from '@/components/elements/SpinnerOverlay'; -import { connect } from 'react-redux'; -import { Websocket } from '@/plugins/Websocket'; -import { ServerStore } from '@/state/server'; +import { ServerContext } from '@/state/server'; const theme = { background: 'transparent', @@ -27,94 +25,71 @@ const theme = { brightWhite: '#ffffff', }; -interface Props { - connected: boolean; - instance: Websocket | null; -} +const terminal = new Terminal({ + disableStdin: true, + cursorStyle: 'underline', + allowTransparency: true, + fontSize: 12, + fontFamily: 'Menlo, Monaco, Consolas, monospace', + rows: 30, + theme: theme, +}); -class Console extends React.PureComponent> { - ref = createRef(); - terminal = new Terminal({ - disableStdin: true, - cursorStyle: 'underline', - allowTransparency: true, - fontSize: 12, - fontFamily: 'Menlo, Monaco, Consolas, monospace', - rows: 30, - theme: theme, - }); +export default () => { + const ref = createRef(); + const connected = ServerContext.useStoreState(state => state.socket.connected); + const instance = ServerContext.useStoreState(state => state.socket.instance); - componentDidMount () { - if (this.ref.current) { - this.terminal.open(this.ref.current); - this.terminal.clear(); + const handleConsoleOutput = (line: string) => terminal.writeln( + line.replace(/(?:\r\n|\r|\n)$/im, '') + '\u001b[0m', + ); + + useEffect(() => { + if (ref.current) { + terminal.open(ref.current); // @see https://github.com/xtermjs/xterm.js/issues/2265 // @see https://github.com/xtermjs/xterm.js/issues/2230 - TerminalFit.fit(this.terminal); + TerminalFit.fit(terminal); + } + }, [ ref.current ]); + + useEffect(() => { + if (connected && instance) { + terminal.clear(); + + instance + .addListener('stats', data => console.log(JSON.parse(data))) + .addListener('console output', handleConsoleOutput); + + instance.send('send logs'); } - if (this.props.connected && this.props.instance) { - this.listenForEvents(); - } - } + return () => { + instance && instance + .removeListener('console output', handleConsoleOutput) + .removeAllListeners('stats'); + }; + }, [ connected, instance ]); - componentDidUpdate (prevProps: Readonly>) { - if (!prevProps.connected && this.props.connected) { - this.listenForEvents(); - } - } - - componentWillUnmount () { - if (this.props.instance) { - this.props.instance.removeListener('server log', this.handleServerLog); - this.props.instance.removeListener('server log', this.handleConsoleOutput); - } - } - - listenForEvents () { - const instance = this.props.instance!; - - instance.addListener('server log', this.handleServerLog); - instance.addListener('console output', this.handleConsoleOutput); - instance.send('send logs'); - } - - handleServerLog = (lines: string[]) => lines.forEach(data => { - return data.split(/\n/g).forEach(line => this.terminal.writeln(line + '\u001b[0m')); - }); - - handleConsoleOutput = (line: string) => this.terminal.writeln( - line.replace(/(?:\r\n|\r|\n)$/im, '') + '\u001b[0m' - ); - - render () { - return ( -
- -
-
-
-
-
$
-
- -
+ return ( +
+ +
+
+
+
+
$
+
+
- ); - } -} - -export default connect( - (state: ServerStore) => ({ - connected: state.socket.connected, - instance: state.socket.instance, - }), -)(Console); +
+ ); +};