|
| 1 | +import Centered from '@codesandbox/common/lib/components/flex/Centered'; |
| 2 | +import Fullscreen from '@codesandbox/common/lib/components/flex/Fullscreen'; |
| 3 | +import Padding from '@codesandbox/common/lib/components/spacing/Padding'; |
| 4 | +import { getSandboxName } from '@codesandbox/common/lib/utils/get-sandbox-name'; |
| 5 | +import { inject, observer } from 'app/componentConnectors'; |
| 6 | +import { Skeleton } from 'app/components/Skeleton'; |
| 7 | +import { SubTitle } from 'app/components/SubTitle'; |
| 8 | +import { Title } from 'app/components/Title'; |
| 9 | +import { Navigation } from 'app/pages/common/Navigation'; |
| 10 | +import { SignInButton } from 'app/pages/common/SignInButton'; |
| 11 | +import { QuickActions } from 'app/pages/Sandbox/QuickActions'; |
| 12 | +import { hasAuthToken } from 'app/utils/user'; |
| 13 | +import * as React from 'react'; |
| 14 | +import Helmet from 'react-helmet'; |
| 15 | +import { Link } from 'react-router-dom'; |
| 16 | + |
| 17 | +import Editor from '../Sandbox/Editor'; |
| 18 | +import { BlinkingDot } from './BlinkingDot'; |
| 19 | + |
| 20 | +class LivePage extends React.Component { |
| 21 | + loggedIn = this.props.store.hasLogIn; |
| 22 | + |
| 23 | + UNSAFE_componentWillMount() { |
| 24 | + this.initializeLive(); |
| 25 | + } |
| 26 | + |
| 27 | + disconnectLive() { |
| 28 | + if (this.props.store.live.isLive) { |
| 29 | + this.props.signals.live.onNavigateAway({}); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + componentWillUnmount() { |
| 34 | + this.disconnectLive(); |
| 35 | + this.props.signals.editor.onNavigateAway({}); |
| 36 | + } |
| 37 | + |
| 38 | + initializeLive = () => { |
| 39 | + if (hasAuthToken()) { |
| 40 | + this.loggedIn = true; |
| 41 | + this.props.signals.live.roomJoined({ |
| 42 | + roomId: this.props.match.params.id, |
| 43 | + }); |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + componentDidUpdate(prevProps) { |
| 48 | + if ( |
| 49 | + prevProps.match.params.id !== this.props.match.params.id || |
| 50 | + (hasAuthToken() && !this.loggedIn) |
| 51 | + ) { |
| 52 | + this.disconnectLive(); |
| 53 | + this.initializeLive(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + getContent = () => { |
| 58 | + const { store } = this.props; |
| 59 | + |
| 60 | + if (!hasAuthToken()) { |
| 61 | + return ( |
| 62 | + <> |
| 63 | + <div |
| 64 | + style={{ |
| 65 | + fontWeight: 300, |
| 66 | + color: 'rgba(255, 255, 255, 0.5)', |
| 67 | + marginBottom: '1rem', |
| 68 | + fontSize: '1.5rem', |
| 69 | + }} |
| 70 | + > |
| 71 | + Sign in required |
| 72 | + </div> |
| 73 | + <Title style={{ fontSize: '1.25rem' }}> |
| 74 | + You need to sign in to join this session |
| 75 | + </Title> |
| 76 | + <br /> |
| 77 | + <div> |
| 78 | + <SignInButton /> |
| 79 | + </div> |
| 80 | + </> |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + if (store.live.error) { |
| 85 | + if (store.live.error === 'room not found') { |
| 86 | + return ( |
| 87 | + <> |
| 88 | + <div |
| 89 | + style={{ |
| 90 | + fontWeight: 300, |
| 91 | + color: 'rgba(255, 255, 255, 0.5)', |
| 92 | + marginBottom: '1rem', |
| 93 | + fontSize: '1.5rem', |
| 94 | + }} |
| 95 | + > |
| 96 | + Something went wrong |
| 97 | + </div> |
| 98 | + <Title style={{ fontSize: '1.25rem' }}> |
| 99 | + It seems like this session doesn |
| 100 | + {"'"}t exist or has been closed |
| 101 | + </Title> |
| 102 | + <br /> |
| 103 | + <Link to="/s">Create Sandbox</Link> |
| 104 | + </> |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + return ( |
| 109 | + <> |
| 110 | + <Title>An error occured while connecting to the live session:</Title> |
| 111 | + <SubTitle>{store.live.error}</SubTitle> |
| 112 | + <br /> |
| 113 | + <br /> |
| 114 | + <Link to="/s">Create Sandbox</Link> |
| 115 | + </> |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + if (!store.editor.currentSandbox) { |
| 120 | + return ( |
| 121 | + <> |
| 122 | + <Skeleton |
| 123 | + titles={[ |
| 124 | + { |
| 125 | + content: <BlinkingDot />, |
| 126 | + delay: 0, |
| 127 | + }, |
| 128 | + { |
| 129 | + content: 'Joining Live Session...', |
| 130 | + delay: 0.5, |
| 131 | + }, |
| 132 | + ]} |
| 133 | + /> |
| 134 | + </> |
| 135 | + ); |
| 136 | + } |
| 137 | + |
| 138 | + return null; |
| 139 | + }; |
| 140 | + |
| 141 | + render() { |
| 142 | + const { match, store } = this.props; |
| 143 | + |
| 144 | + // eslint-disable-next-line |
| 145 | + store.user; // Force observer call |
| 146 | + |
| 147 | + const content = this.getContent(); |
| 148 | + |
| 149 | + if (content) { |
| 150 | + return ( |
| 151 | + <Fullscreen> |
| 152 | + <Padding |
| 153 | + style={{ |
| 154 | + display: 'flex', |
| 155 | + flexDirection: 'column', |
| 156 | + width: '100vw', |
| 157 | + height: '100vh', |
| 158 | + }} |
| 159 | + margin={1} |
| 160 | + > |
| 161 | + <Navigation title="Live Session" /> |
| 162 | + <Centered |
| 163 | + style={{ flex: 1, width: '100%', height: '100%' }} |
| 164 | + horizontal |
| 165 | + vertical |
| 166 | + > |
| 167 | + {content} |
| 168 | + </Centered> |
| 169 | + </Padding> |
| 170 | + </Fullscreen> |
| 171 | + ); |
| 172 | + } |
| 173 | + |
| 174 | + const sandbox = store.editor.currentSandbox; |
| 175 | + |
| 176 | + return ( |
| 177 | + <> |
| 178 | + {sandbox && ( |
| 179 | + <Helmet> |
| 180 | + <title>{getSandboxName(sandbox)} - CodeSandbox</title> |
| 181 | + </Helmet> |
| 182 | + )} |
| 183 | + <Editor match={match} /> |
| 184 | + <QuickActions /> |
| 185 | + </> |
| 186 | + ); |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +// eslint-disable-next-line import/no-default-export |
| 191 | +export default inject('signals', 'store')(observer(LivePage)); |
0 commit comments