diff --git a/packages/app/src/app/pages/Live/index.js b/packages/app/src/app/pages/Live/index.tsx similarity index 53% rename from packages/app/src/app/pages/Live/index.js rename to packages/app/src/app/pages/Live/index.tsx index 4935676cb27..66e9f071ff9 100644 --- a/packages/app/src/app/pages/Live/index.js +++ b/packages/app/src/app/pages/Live/index.tsx @@ -1,11 +1,11 @@ -import * as React from 'react'; -import { Link } from 'react-router-dom'; +import React, { useEffect, useCallback } from 'react'; +import { Link, RouteProps } from 'react-router-dom'; +import { useOvermind } from 'app/overmind'; import Helmet from 'react-helmet'; import { getSandboxName } from '@codesandbox/common/lib/utils/get-sandbox-name'; import Fullscreen from '@codesandbox/common/lib/components/flex/Fullscreen'; import Padding from '@codesandbox/common/lib/components/spacing/Padding'; import Centered from '@codesandbox/common/lib/components/flex/Centered'; -import { inject, observer } from 'app/componentConnectors'; import { Title } from 'app/components/Title'; import { SubTitle } from 'app/components/SubTitle'; import { Skeleton } from 'app/components/Skeleton'; @@ -16,46 +16,48 @@ import { hasAuthToken } from 'app/utils/user'; import Editor from '../Sandbox/Editor'; import { BlinkingDot } from './BlinkingDot'; -class LivePage extends React.Component { - loggedIn = this.props.store.hasLogIn; - - UNSAFE_componentWillMount() { - this.initializeLive(); - } - - disconnectLive() { - if (this.props.store.live.isLive) { - this.props.signals.live.onNavigateAway({}); +export const LivePage: React.FunctionComponent = ({ match }) => { + const { + state: { + hasLogIn, + live: { isLive, error, isLoading }, + editor: { currentSandbox }, + user, + }, + actions: { live, editor }, + } = useOvermind(); + let loggedIn = hasLogIn; + + const initializeLive = useCallback(() => { + if (hasAuthToken()) { + loggedIn = true; + live.roomJoined({ + roomId: match.params.id, + }); } - } + }, [match.params.id]); - componentWillUnmount() { - this.disconnectLive(); - this.props.signals.editor.onNavigateAway({}); - } + initializeLive(); - initializeLive = () => { - if (hasAuthToken()) { - this.loggedIn = true; - this.props.signals.live.roomJoined({ - roomId: this.props.match.params.id, - }); + const disconnectLive = () => { + if (isLive) { + live.onNavigateAway(); } }; - componentDidUpdate(prevProps) { - if ( - prevProps.match.params.id !== this.props.match.params.id || - (hasAuthToken() && !this.loggedIn) - ) { - this.disconnectLive(); - this.initializeLive(); + useEffect(() => { + if (hasAuthToken() && !loggedIn) { + disconnectLive(); + initializeLive(); } - } - getContent = () => { - const { store } = this.props; + return () => { + disconnectLive(); + editor.onNavigateAway(); + }; + }, [disconnectLive, editor, initializeLive, loggedIn, match.params.id]); + const getContent = () => { if (!hasAuthToken()) { return ( <> @@ -80,8 +82,8 @@ class LivePage extends React.Component { ); } - if (store.live.error) { - if (store.live.error === 'room not found') { + if (error) { + if (error === 'room not found') { return ( <>
An error occured while connecting to the live session: - {store.live.error} + {error}

Create Sandbox @@ -115,7 +117,7 @@ class LivePage extends React.Component { ); } - if (store.live.isLoading || !store.editor.currentSandbox) { + if (isLoading || !currentSandbox) { return ( <> - - - - {content} - - - - ); - } - - const sandbox = store.editor.currentSandbox; + const content = getContent(); + if (content) { return ( - <> - {sandbox && ( - - {getSandboxName(sandbox)} - CodeSandbox - - )} - - - + + + + + {content} + + + ); } -} -// eslint-disable-next-line import/no-default-export -export default inject('signals', 'store')(observer(LivePage)); + return ( + <> + {currentSandbox && ( + + {getSandboxName(currentSandbox)} - CodeSandbox + + )} + + + + ); +};