logComponent.tsx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import * as React from "react";
  2. import { GlobalState } from '../../globalState';
  3. import * as ReactDOM from 'react-dom';
  4. require("./log.scss");
  5. interface ILogComponentProps {
  6. globalState: GlobalState;
  7. }
  8. export class LogEntry {
  9. constructor(public message: string, public isError: boolean) {
  10. }
  11. }
  12. export class LogComponent extends React.Component<ILogComponentProps, { logs: LogEntry[] }> {
  13. constructor(props: ILogComponentProps) {
  14. super(props);
  15. this.state = { logs: [] };
  16. }
  17. componentWillMount() {
  18. this.props.globalState.onLogRequiredObservable.add(log => {
  19. let currentLogs = this.state.logs;
  20. currentLogs.push(log);
  21. this.setState({ logs: currentLogs });
  22. });
  23. }
  24. componentDidUpdate() {
  25. const logConsole = ReactDOM.findDOMNode(this.refs["log-console"]) as HTMLElement;
  26. if (!logConsole) {
  27. return;
  28. }
  29. logConsole.scrollTop = logConsole.scrollHeight;
  30. }
  31. render() {
  32. return (
  33. <div id="log-console" ref={"log-console"} >
  34. {
  35. this.state.logs.map((l, i) => {
  36. return (
  37. <div key={i} className={"log" + (l.isError ? " error" : "")}>
  38. {l.message}
  39. </div>
  40. )
  41. })
  42. }
  43. </div>
  44. );
  45. }
  46. }