terminal-app.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import React from 'react';
  2. import { connect } from 'react-redux';
  3. import { ThemeProvider } from 'styled-components';
  4. import commonTheme from 'weaveworks-ui-components/lib/theme';
  5. import Terminal from './terminal';
  6. import GlobalStyle from './global-style';
  7. import defaultTheme from '../themes/default';
  8. import { receiveControlPipeFromParams, hitEsc } from '../actions/app-actions';
  9. const ESC_KEY_CODE = 27;
  10. class TerminalApp extends React.Component {
  11. constructor(props, context) {
  12. super(props, context);
  13. const paramString = window.location.hash.split('/').pop();
  14. const params = JSON.parse(decodeURIComponent(paramString));
  15. this.props.receiveControlPipeFromParams(
  16. params.pipe.id, params.pipe.raw,
  17. params.pipe.resizeTtyControl
  18. );
  19. this.state = {
  20. statusBarColor: params.statusBarColor,
  21. title: params.title,
  22. titleBarColor: params.titleBarColor
  23. };
  24. this.onKeyUp = this.onKeyUp.bind(this);
  25. }
  26. componentDidMount() {
  27. window.addEventListener('keyup', this.onKeyUp);
  28. }
  29. componentWillUnmount() {
  30. window.removeEventListener('keyup', this.onKeyUp);
  31. }
  32. onKeyUp(ev) {
  33. if (ev.keyCode === ESC_KEY_CODE) {
  34. this.props.hitEsc();
  35. }
  36. }
  37. componentWillReceiveProps(nextProps) {
  38. if (!nextProps.controlPipe) {
  39. window.close();
  40. }
  41. }
  42. render() {
  43. const style = {borderTop: `4px solid ${this.state.titleBarColor}`};
  44. return (
  45. <ThemeProvider theme={{...commonTheme, scope: defaultTheme }}>
  46. <>
  47. <GlobalStyle />
  48. <div className="terminal-app" style={style}>
  49. {this.props.controlPipe && (
  50. <Terminal
  51. pipe={this.props.controlPipe}
  52. titleBarColor={this.state.titleBarColor}
  53. statusBarColor={this.state.statusBarColor}
  54. title={this.state.title}
  55. embedded={false} />
  56. )}
  57. </div>
  58. </>
  59. </ThemeProvider>
  60. );
  61. }
  62. }
  63. function mapStateToProps(state) {
  64. return {
  65. controlPipe: state.get('controlPipes').last()
  66. };
  67. }
  68. export default connect(
  69. mapStateToProps,
  70. { hitEsc, receiveControlPipeFromParams }
  71. )(TerminalApp);