Skip to content

Commit bbf9557

Browse files
christianalfoniCompuIves
authored andcommitted
refactored sandbox page to overmind (codesandbox#3180)
* refactored sandbox page to overmind * fix changing sandbox
1 parent 2550785 commit bbf9557

File tree

3 files changed

+74
-87
lines changed

3 files changed

+74
-87
lines changed

packages/app/src/app/overmind/namespaces/live/actions.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,11 @@ export const onSessionCloseClicked: Action = ({ actions, effects }) => {
171171
actions.live.internal.reset();
172172
};
173173

174-
export const onNavigateAway: Action = ({ actions }) => {
175-
actions.live.internal.disconnect();
176-
actions.live.internal.reset();
174+
export const onNavigateAway: Action = ({ actions, state }) => {
175+
if (state.live.isLive) {
176+
actions.live.internal.disconnect();
177+
actions.live.internal.reset();
178+
}
177179
};
178180

179181
export const onToggleNotificationsHidden: Action = ({ state }) => {

packages/app/src/app/pages/Sandbox/index.js renamed to packages/app/src/app/pages/Sandbox/index.tsx

Lines changed: 68 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,31 @@ import Centered from '@codesandbox/common/lib/components/flex/Centered';
33
import Fullscreen from '@codesandbox/common/lib/components/flex/Fullscreen';
44
import Padding from '@codesandbox/common/lib/components/spacing/Padding';
55
import { getSandboxName } from '@codesandbox/common/lib/utils/get-sandbox-name';
6-
import { inject, observer } from 'app/componentConnectors';
76
import { Skeleton } from 'app/components/Skeleton';
87
import { Title } from 'app/components/Title';
8+
import { useOvermind } from 'app/overmind';
99
import { GithubIntegration } from 'app/pages/common/GithubIntegration';
1010
import { Navigation } from 'app/pages/common/Navigation';
1111
import { NotFound } from 'app/pages/common/NotFound';
1212
import { QuickActions } from 'app/pages/Sandbox/QuickActions';
13-
import React from 'react';
14-
import Helmet from 'react-helmet';
13+
import React, { useEffect } from 'react';
14+
import { Helmet } from 'react-helmet';
1515
import { Link } from 'react-router-dom';
1616

1717
import Editor from './Editor';
1818

19-
class SandboxPage extends React.Component {
20-
UNSAFE_componentWillMount() {
19+
interface Props {
20+
match: {
21+
params: {
22+
id: string;
23+
};
24+
};
25+
}
26+
27+
export const Sandbox: React.FC<Props> = ({ match }) => {
28+
const { state, actions } = useOvermind();
29+
30+
useEffect(() => {
2131
if (window.screen.availWidth < 800) {
2232
if (!document.location.search.includes('from-embed')) {
2333
const addedSign = document.location.search ? '&' : '?';
@@ -26,45 +36,26 @@ class SandboxPage extends React.Component {
2636
addedSign +
2737
'codemirror=1';
2838
} else {
29-
this.props.signals.preferences.codeMirrorForced();
39+
actions.preferences.codeMirrorForced();
3040
}
3141
}
3242

33-
this.fetchSandbox();
34-
}
35-
36-
disconnectLive() {
37-
if (this.props.store.live.isLive) {
38-
this.props.signals.live.onNavigateAway({});
39-
}
40-
}
41-
42-
componentWillUnmount() {
43-
this.disconnectLive();
44-
this.props.signals.editor.onNavigateAway({});
45-
}
46-
47-
fetchSandbox = () => {
48-
const { id } = this.props.match.params;
49-
50-
this.props.signals.editor.sandboxChanged({ id });
51-
};
52-
53-
componentDidUpdate(prevProps) {
54-
if (prevProps.match.params.id !== this.props.match.params.id) {
55-
this.disconnectLive();
56-
this.fetchSandbox();
57-
}
58-
}
43+
actions.editor.sandboxChanged({ id: match.params.id });
44+
}, [actions.editor, actions.preferences, match.params, match.params.id]);
5945

60-
getContent() {
61-
const { store } = this.props;
46+
useEffect(
47+
() => () => {
48+
actions.live.onNavigateAway();
49+
},
50+
[actions.live]
51+
);
6252

63-
const { hasLogIn } = store;
53+
function getContent() {
54+
const { hasLogIn } = state;
6455

65-
if (store.editor.error) {
66-
const isGithub = this.props.match.params.id.includes('github');
67-
const hasPrivateAccess = store.user && store.user.integrations.github;
56+
if (state.editor.error) {
57+
const isGithub = match.params.id.includes('github');
58+
const hasPrivateAccess = state.user && state.user.integrations.github;
6859

6960
return (
7061
<>
@@ -79,7 +70,7 @@ class SandboxPage extends React.Component {
7970
Something went wrong
8071
</div>
8172
<Title style={{ fontSize: '1.25rem', marginBottom: 0 }}>
82-
{store.editor.error}
73+
{state.editor.error}
8374
</Title>
8475
<br />
8576
<div style={{ display: 'flex', maxWidth: 400, width: '100%' }}>
@@ -113,11 +104,11 @@ class SandboxPage extends React.Component {
113104
);
114105
}
115106

116-
if (store.editor.notFound) {
107+
if (state.editor.notFound) {
117108
return <NotFound />;
118109
}
119110

120-
if (store.editor.isLoading || !store.editor.currentSandbox) {
111+
if (state.editor.isLoading || !state.editor.currentSandbox) {
121112
return (
122113
<>
123114
<Skeleton
@@ -139,50 +130,44 @@ class SandboxPage extends React.Component {
139130
return null;
140131
}
141132

142-
render() {
143-
const { match, store } = this.props;
144-
145-
const content = this.getContent();
146-
147-
if (content) {
148-
return (
149-
<Fullscreen>
150-
<Padding
151-
style={{
152-
display: 'flex',
153-
flexDirection: 'column',
154-
width: '100vw',
155-
height: '100vh',
156-
}}
157-
margin={1}
158-
>
159-
{store.editor.isLoading ? null : (
160-
<Navigation title="Sandbox Editor" />
161-
)}
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;
133+
const content = getContent();
175134

135+
if (content) {
176136
return (
177-
<>
178-
<Helmet>
179-
<title>{getSandboxName(sandbox)} - CodeSandbox</title>
180-
</Helmet>
181-
<Editor match={match} />
182-
<QuickActions />
183-
</>
137+
<Fullscreen>
138+
<Padding
139+
style={{
140+
display: 'flex',
141+
flexDirection: 'column',
142+
width: '100vw',
143+
height: '100vh',
144+
}}
145+
margin={1}
146+
>
147+
{state.editor.isLoading ? null : (
148+
<Navigation title="Sandbox Editor" />
149+
)}
150+
<Centered
151+
style={{ flex: 1, width: '100%', height: '100%' }}
152+
horizontal
153+
vertical
154+
>
155+
{content}
156+
</Centered>
157+
</Padding>
158+
</Fullscreen>
184159
);
185160
}
186-
}
187161

188-
export default inject('signals', 'store')(observer(SandboxPage));
162+
const sandbox = state.editor.currentSandbox;
163+
164+
return (
165+
<>
166+
<Helmet>
167+
<title>{getSandboxName(sandbox)} - CodeSandbox</title>
168+
</Helmet>
169+
<Editor />
170+
<QuickActions />
171+
</>
172+
);
173+
};

packages/app/src/app/pages/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import Dashboard from './Dashboard';
1515
import { DevAuthPage } from './DevAuth';
1616
import { Container, Content } from './elements';
1717
import { NewSandbox } from './NewSandbox';
18-
import Sandbox from './Sandbox';
18+
import { Sandbox } from './Sandbox';
1919

2020
const routeDebugger = _debug('cs:app:router');
2121

0 commit comments

Comments
 (0)