sessionstack.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import {Component} from 'react';
  2. import {uniqueId} from 'sentry/utils/guid';
  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. window.addEventListener('resize', () => this.setIframeSize(), false);
  20. this.setIframeSize();
  21. }
  22. componentWillUnmount() {
  23. window.removeEventListener('resize', () => this.setIframeSize(), false);
  24. }
  25. iframeContainerId = uniqueId();
  26. getTitle = () => 'SessionStack';
  27. setIframeSize() {
  28. const parentNode = document.getElementById(this.iframeContainerId)?.parentElement;
  29. if (!this.state.showIframe || !parentNode) {
  30. return;
  31. }
  32. const parentWidth = parentNode.clientWidth;
  33. this.setState({
  34. width: parentWidth,
  35. height: parentWidth / ASPECT_RATIO,
  36. });
  37. }
  38. playSession() {
  39. this.setState({
  40. showIframe: true,
  41. });
  42. this.setIframeSize();
  43. }
  44. render() {
  45. const {session_url} = this.props.data;
  46. if (!session_url) {
  47. return <h4>Session not found.</h4>;
  48. }
  49. return (
  50. <div className="panel-group" id={this.iframeContainerId}>
  51. {this.state.showIframe ? (
  52. <iframe
  53. src={session_url}
  54. sandbox="allow-scripts allow-same-origin"
  55. width={this.state.width}
  56. height={this.state.height}
  57. />
  58. ) : (
  59. <button
  60. className="btn btn-default"
  61. type="button"
  62. onClick={() => this.playSession()}
  63. >
  64. Play session
  65. </button>
  66. )}
  67. </div>
  68. );
  69. }
  70. }
  71. export default SessionStackContextType;