Skip to content

Commit b807721

Browse files
MichaelDeBoeychristianalfoni
authored andcommitted
🔨 Switch Preview to use useOvermind (codesandbox#3284)
1 parent 6c6b744 commit b807721

File tree

2 files changed

+56
-41
lines changed
  • packages/app/src/app

2 files changed

+56
-41
lines changed

‎packages/app/src/app/overmind/namespaces/editor/actions.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -571,9 +571,10 @@ export const toggleEditorPreviewLayout: Action = ({ state, effects }) => {
571571
effects.vscode.resetLayout();
572572
};
573573

574-
export const previewActionReceived: Action<{
575-
action: any;
576-
}> = ({ state, effects, actions }, { action }) => {
574+
export const previewActionReceived: Action<any> = (
575+
{ actions, effects, state },
576+
action
577+
) => {
577578
switch (action.action) {
578579
case 'notification':
579580
effects.notificationToast.add({

‎packages/app/src/app/pages/Sandbox/Editor/Content/Preview/index.tsx

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,55 @@
1+
import { ServerContainerStatus } from '@codesandbox/common/lib/types';
12
import BasePreview from '@codesandbox/common/lib/components/Preview';
23
import RunOnClick from '@codesandbox/common/lib/components/RunOnClick';
4+
import React, { FunctionComponent, useState } from 'react';
5+
36
import { useOvermind } from 'app/overmind';
4-
// @flow
5-
import React, { FC, useState } from 'react';
67

78
type Props = {
89
hidden?: boolean;
10+
options: {
11+
url?: string;
12+
};
913
runOnClick?: boolean;
10-
options: { url?: string };
1114
};
12-
13-
const PreviewComponent: FC<Props> = props => {
14-
const { state, actions, effects } = useOvermind();
15-
const [running, setRunning] = useState(!props.runOnClick);
15+
export const Preview: FunctionComponent<Props> = ({
16+
hidden,
17+
options,
18+
runOnClick,
19+
}) => {
20+
const {
21+
actions: {
22+
editor: { errorsCleared, previewActionReceived, projectViewToggled },
23+
},
24+
effects: {
25+
preview: { initializePreview },
26+
},
27+
state: {
28+
editor: {
29+
currentModule,
30+
currentSandbox,
31+
initialPath,
32+
isInProjectView,
33+
isResizing,
34+
previewWindowVisible,
35+
},
36+
preferences: { settings },
37+
server: { containerStatus, error, hasUnrecoverableError },
38+
},
39+
} = useOvermind();
40+
const [running, setRunning] = useState(!runOnClick);
1641

1742
/**
1843
* Responsible for showing a message when something is happening with SSE. Only used
1944
* for server sandboxes right now, but we can extend it in the future. It would require
2045
* a better design if we want to use it for more though.
2146
*/
22-
function getOverlayMessage() {
23-
const { containerStatus, error, hasUnrecoverableError } = state.server;
24-
25-
if (containerStatus === 'hibernated') {
47+
const getOverlayMessage = () => {
48+
if (containerStatus === ServerContainerStatus.HIBERNATED) {
2649
return 'The container has been hibernated because of inactivity, you can start it by refreshing the browser.';
2750
}
2851

29-
if (containerStatus === 'stopped') {
52+
if (containerStatus === ServerContainerStatus.STOPPED) {
3053
return 'Restarting the sandbox...';
3154
}
3255

@@ -35,37 +58,28 @@ const PreviewComponent: FC<Props> = props => {
3558
}
3659

3760
return undefined;
38-
}
39-
40-
const { options } = props;
41-
42-
const completelyHidden = !state.editor.previewWindowVisible;
61+
};
4362

4463
return running ? (
4564
<BasePreview
46-
onMount={effects.preview.initializePreview}
47-
sandbox={state.editor.currentSandbox}
48-
privacy={state.editor.currentSandbox.privacy}
49-
previewSecret={state.editor.currentSandbox.previewSecret}
50-
currentModule={state.editor.currentModule}
51-
settings={state.preferences.settings}
52-
initialPath={state.editor.initialPath}
53-
url={options.url}
54-
isInProjectView={state.editor.isInProjectView}
55-
onClearErrors={() => actions.editor.errorsCleared()}
56-
onAction={action => actions.editor.previewActionReceived({ action })}
57-
hide={props.hidden}
58-
noPreview={completelyHidden}
59-
onToggleProjectView={() => actions.editor.projectViewToggled()}
60-
isResizing={state.editor.isResizing}
65+
currentModule={currentModule}
66+
hide={hidden}
67+
initialPath={initialPath}
68+
isInProjectView={isInProjectView}
69+
isResizing={isResizing}
70+
onAction={action => previewActionReceived(action)}
71+
onClearErrors={() => errorsCleared()}
72+
onMount={initializePreview}
73+
noPreview={!previewWindowVisible}
74+
onToggleProjectView={() => projectViewToggled()}
6175
overlayMessage={getOverlayMessage()}
76+
previewSecret={currentSandbox.previewSecret}
77+
privacy={currentSandbox.privacy}
78+
sandbox={currentSandbox}
79+
settings={settings}
80+
url={options.url}
6281
/>
6382
) : (
64-
<RunOnClick
65-
onClick={() => {
66-
setRunning(true);
67-
}}
68-
/>
83+
<RunOnClick onClick={() => setRunning(true)} />
6984
);
7085
};
71-
export const Preview = PreviewComponent;

0 commit comments

Comments
 (0)