sessionstack.tsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import {Component} from 'react';
  2. import {findDOMNode} from 'react-dom';
  3. const ASPECT_RATIO = 16 / 9;
  4. type Props = {
  5. data: {
  6. session_url?: string;
  7. };
  8. };
  9. type State = {
  10. showIframe: boolean;
  11. height?: number;
  12. width?: number;
  13. };
  14. class SessionStackContextType extends Component<Props, State> {
  15. state: State = {
  16. showIframe: false,
  17. };
  18. componentDidMount() {
  19. // eslint-disable-next-line react/no-find-dom-node
  20. const domNode = findDOMNode(this) as HTMLElement;
  21. this.parentNode = domNode.parentNode as HTMLElement;
  22. window.addEventListener('resize', () => this.setIframeSize(), false);
  23. this.setIframeSize();
  24. }
  25. componentWillUnmount() {
  26. window.removeEventListener('resize', () => this.setIframeSize(), false);
  27. }
  28. parentNode?: HTMLElement;
  29. getTitle = () => 'SessionStack';
  30. setIframeSize() {
  31. if (this.state.showIframe || !this.parentNode) {
  32. return;
  33. }
  34. const parentWidth = this.parentNode.clientWidth;
  35. this.setState({
  36. width: parentWidth,
  37. height: parentWidth / ASPECT_RATIO,
  38. });
  39. }
  40. playSession() {
  41. this.setState({
  42. showIframe: true,
  43. });
  44. this.setIframeSize();
  45. }
  46. render() {
  47. const {session_url} = this.props.data;
  48. if (!session_url) {
  49. return <h4>Session not found.</h4>;
  50. }
  51. return (
  52. <div className="panel-group">
  53. {this.state.showIframe ? (
  54. <iframe
  55. src={session_url}
  56. sandbox="allow-scripts allow-same-origin"
  57. width={this.state.width}
  58. height={this.state.height}
  59. />
  60. ) : (
  61. <button
  62. className="btn btn-default"
  63. type="button"
  64. onClick={() => this.playSession()}
  65. >
  66. Play session
  67. </button>
  68. )}
  69. </div>
  70. );
  71. }
  72. }
  73. export default SessionStackContextType;