logComponent.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. componentDidMount() {
  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. var today = new Date();
  33. var h = today.getHours();
  34. var m = today.getMinutes();
  35. var s = today.getSeconds();
  36. return (
  37. <div id="log-console" ref={"log-console"} >
  38. {
  39. this.state.logs.map((l, i) => {
  40. return (
  41. <div key={i} className={"log" + (l.isError ? " error" : "")}>
  42. {h + ":" + m + ":" + s+ ": " + l.message}
  43. </div>
  44. )
  45. })
  46. }
  47. </div>
  48. );
  49. }
  50. }