diff --git a/packages/app/src/app/overmind/namespaces/editor/actions.ts b/packages/app/src/app/overmind/namespaces/editor/actions.ts index 1e58148c8f7..a93b5b4afde 100755 --- a/packages/app/src/app/overmind/namespaces/editor/actions.ts +++ b/packages/app/src/app/overmind/namespaces/editor/actions.ts @@ -571,9 +571,10 @@ export const toggleEditorPreviewLayout: Action = ({ state, effects }) => { effects.vscode.resetLayout(); }; -export const previewActionReceived: Action<{ - action: any; -}> = ({ state, effects, actions }, { action }) => { +export const previewActionReceived: Action = ( + { actions, effects, state }, + action +) => { switch (action.action) { case 'notification': effects.notificationToast.add({ diff --git a/packages/app/src/app/pages/Sandbox/Editor/Content/Preview/index.tsx b/packages/app/src/app/pages/Sandbox/Editor/Content/Preview/index.tsx index 19b9821c122..356f9f04b3a 100644 --- a/packages/app/src/app/pages/Sandbox/Editor/Content/Preview/index.tsx +++ b/packages/app/src/app/pages/Sandbox/Editor/Content/Preview/index.tsx @@ -1,32 +1,55 @@ +import { ServerContainerStatus } from '@codesandbox/common/lib/types'; import BasePreview from '@codesandbox/common/lib/components/Preview'; import RunOnClick from '@codesandbox/common/lib/components/RunOnClick'; +import React, { FunctionComponent, useState } from 'react'; + import { useOvermind } from 'app/overmind'; -// @flow -import React, { FC, useState } from 'react'; type Props = { hidden?: boolean; + options: { + url?: string; + }; runOnClick?: boolean; - options: { url?: string }; }; - -const PreviewComponent: FC = props => { - const { state, actions, effects } = useOvermind(); - const [running, setRunning] = useState(!props.runOnClick); +export const Preview: FunctionComponent = ({ + hidden, + options, + runOnClick, +}) => { + const { + actions: { + editor: { errorsCleared, previewActionReceived, projectViewToggled }, + }, + effects: { + preview: { initializePreview }, + }, + state: { + editor: { + currentModule, + currentSandbox, + initialPath, + isInProjectView, + isResizing, + previewWindowVisible, + }, + preferences: { settings }, + server: { containerStatus, error, hasUnrecoverableError }, + }, + } = useOvermind(); + const [running, setRunning] = useState(!runOnClick); /** * Responsible for showing a message when something is happening with SSE. Only used * for server sandboxes right now, but we can extend it in the future. It would require * a better design if we want to use it for more though. */ - function getOverlayMessage() { - const { containerStatus, error, hasUnrecoverableError } = state.server; - - if (containerStatus === 'hibernated') { + const getOverlayMessage = () => { + if (containerStatus === ServerContainerStatus.HIBERNATED) { return 'The container has been hibernated because of inactivity, you can start it by refreshing the browser.'; } - if (containerStatus === 'stopped') { + if (containerStatus === ServerContainerStatus.STOPPED) { return 'Restarting the sandbox...'; } @@ -35,37 +58,28 @@ const PreviewComponent: FC = props => { } return undefined; - } - - const { options } = props; - - const completelyHidden = !state.editor.previewWindowVisible; + }; return running ? ( actions.editor.errorsCleared()} - onAction={action => actions.editor.previewActionReceived({ action })} - hide={props.hidden} - noPreview={completelyHidden} - onToggleProjectView={() => actions.editor.projectViewToggled()} - isResizing={state.editor.isResizing} + currentModule={currentModule} + hide={hidden} + initialPath={initialPath} + isInProjectView={isInProjectView} + isResizing={isResizing} + onAction={action => previewActionReceived(action)} + onClearErrors={() => errorsCleared()} + onMount={initializePreview} + noPreview={!previewWindowVisible} + onToggleProjectView={() => projectViewToggled()} overlayMessage={getOverlayMessage()} + previewSecret={currentSandbox.previewSecret} + privacy={currentSandbox.privacy} + sandbox={currentSandbox} + settings={settings} + url={options.url} /> ) : ( - { - setRunning(true); - }} - /> + setRunning(true)} /> ); }; -export const Preview = PreviewComponent;