1
+ import { ServerContainerStatus } from '@codesandbox/common/lib/types' ;
1
2
import BasePreview from '@codesandbox/common/lib/components/Preview' ;
2
3
import RunOnClick from '@codesandbox/common/lib/components/RunOnClick' ;
4
+ import React , { FunctionComponent , useState } from 'react' ;
5
+
3
6
import { useOvermind } from 'app/overmind' ;
4
- // @flow
5
- import React , { FC , useState } from 'react' ;
6
7
7
8
type Props = {
8
9
hidden ?: boolean ;
10
+ options : {
11
+ url ?: string ;
12
+ } ;
9
13
runOnClick ?: boolean ;
10
- options : { url ?: string } ;
11
14
} ;
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 ) ;
16
41
17
42
/**
18
43
* Responsible for showing a message when something is happening with SSE. Only used
19
44
* for server sandboxes right now, but we can extend it in the future. It would require
20
45
* a better design if we want to use it for more though.
21
46
*/
22
- function getOverlayMessage ( ) {
23
- const { containerStatus, error, hasUnrecoverableError } = state . server ;
24
-
25
- if ( containerStatus === 'hibernated' ) {
47
+ const getOverlayMessage = ( ) => {
48
+ if ( containerStatus === ServerContainerStatus . HIBERNATED ) {
26
49
return 'The container has been hibernated because of inactivity, you can start it by refreshing the browser.' ;
27
50
}
28
51
29
- if ( containerStatus === 'stopped' ) {
52
+ if ( containerStatus === ServerContainerStatus . STOPPED ) {
30
53
return 'Restarting the sandbox...' ;
31
54
}
32
55
@@ -35,37 +58,28 @@ const PreviewComponent: FC<Props> = props => {
35
58
}
36
59
37
60
return undefined ;
38
- }
39
-
40
- const { options } = props ;
41
-
42
- const completelyHidden = ! state . editor . previewWindowVisible ;
61
+ } ;
43
62
44
63
return running ? (
45
64
< 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 ( ) }
61
75
overlayMessage = { getOverlayMessage ( ) }
76
+ previewSecret = { currentSandbox . previewSecret }
77
+ privacy = { currentSandbox . privacy }
78
+ sandbox = { currentSandbox }
79
+ settings = { settings }
80
+ url = { options . url }
62
81
/>
63
82
) : (
64
- < RunOnClick
65
- onClick = { ( ) => {
66
- setRunning ( true ) ;
67
- } }
68
- />
83
+ < RunOnClick onClick = { ( ) => setRunning ( true ) } />
69
84
) ;
70
85
} ;
71
- export const Preview = PreviewComponent ;
0 commit comments