11// @flow
22import * as React from 'react' ;
3- import type { Sandbox , Module } from 'common/lib/types' ;
3+ import { Sandbox , Module } from 'common/lib/types' ;
44import { listen , dispatch , registerFrame , resetState } from 'codesandbox-api' ;
55import { debounce } from 'lodash-es' ;
66import io from 'socket.io-client' ;
@@ -15,54 +15,59 @@ import { generateFileFromSandbox } from 'common/lib/templates/configuration/pack
1515
1616import Navigator from './Navigator' ;
1717import { Container , StyledFrame , Loading } from './elements' ;
18- import type { Settings } from '../CodeEditor/types' ;
18+ import { Settings } from '../CodeEditor/types' ;
1919
2020type Props = {
21- onInitialized : ( preview : BasePreview ) => void , // eslint-disable-line no-use-before-define
22- sandbox : Sandbox ,
23- extraModules : { [ path : string ] : { code : string , path : string } } ,
24- currentModule : Module ,
25- settings : Settings ,
26- initialPath : string ,
27- isInProjectView : boolean ,
28- onClearErrors : ( ) => void ,
29- onAction : ( action : Object ) => void ,
30- onOpenNewWindow : ( ) => void ,
31- onToggleProjectView : ( ) => void ,
32- isResizing : boolean ,
33- alignRight : ( ) => void ,
34- alignBottom : ( ) => void ,
35- onResize ?: ( height : number ) => void ,
36- showNavigation ?: boolean ,
37- inactive ?: boolean ,
38- dragging ?: boolean ,
39- hide : boolean ,
40- noPreview : boolean ,
41- alignDirection ?: 'right' | 'bottom' ,
42- delay ?: number ,
43- setServerStatus ?: ( status : string ) => void ,
44- syncSandbox ?: ( updates : any ) => void ,
21+ onInitialized : ( preview : BasePreview ) => ( ( ) => void ) ; // eslint-disable-line no-use-before-define
22+ sandbox : Sandbox ;
23+ extraModules : { [ path : string ] : { code : string ; path : string } } ;
24+ currentModule : Module ;
25+ settings : Settings ;
26+ initialPath : string ;
27+ isInProjectView : boolean ;
28+ onClearErrors : ( ) => void ;
29+ onAction : ( action : Object ) => void ;
30+ onOpenNewWindow : ( ) => void ;
31+ onToggleProjectView : ( ) => void ;
32+ isResizing : boolean ;
33+ alignRight : ( ) => void ;
34+ alignBottom : ( ) => void ;
35+ onResize ?: ( height : number ) => void ;
36+ showNavigation ?: boolean ;
37+ inactive ?: boolean ;
38+ dragging ?: boolean ;
39+ hide : boolean ;
40+ noPreview : boolean ;
41+ alignDirection ?: 'right' | 'bottom' ;
42+ delay ?: number ;
43+ setServerStatus ?: ( status : string ) => void ;
44+ syncSandbox ?: ( updates : any ) => void ;
45+ className ?: string ;
4546} ;
4647
4748type State = {
48- frameInitialized : boolean ,
49- history : Array < string > ,
50- historyPosition : number ,
51- urlInAddressBar : string ,
52- url : ? string ,
53- overlayMessage : ? string ,
54- hibernated : boolean ,
55- sseError : boolean ,
56- showScreenshot : boolean ,
49+ frameInitialized : boolean ;
50+ history : Array < string > ;
51+ historyPosition : number ;
52+ urlInAddressBar : string ;
53+ url : string | undefined ;
54+ overlayMessage : string | undefined ;
55+ hibernated : boolean ;
56+ sseError : boolean ;
57+ showScreenshot : boolean ;
5758} ;
5859
59- const getSSEUrl = ( id ?: string , initialPath ? : string = '' ) =>
60+ const getSSEUrl = ( id ?: string , initialPath : string = '' ) =>
6061 `https://${ id ? id + '.' : '' } sse.${
6162 process . env . NODE_ENV === 'development' ? 'codesandbox.io' : host ( )
6263 } ${ initialPath } `;
6364
64- const getDiff = ( a , b ) => {
65- const diff = { } ;
65+ interface IModulesByPath {
66+ [ path : string ] : { path : string ; code : null | string ; isBinary ?: boolean } ;
67+ }
68+
69+ const getDiff = ( a : IModulesByPath , b : IModulesByPath ) => {
70+ const diff : IModulesByPath = { } ;
6671
6772 Object . keys ( b )
6873 . filter ( p => {
@@ -148,15 +153,13 @@ function sseTerminalMessage(msg) {
148153class BasePreview extends React . Component < Props , State > {
149154 serverPreview : boolean ;
150155 lastSent : {
151- sandboxId : string ,
152- modules : {
153- [ path : string ] : any ,
154- } ,
155- ignoreNextUpdate : boolean ,
156+ sandboxId : string ;
157+ modules : IModulesByPath ;
158+ ignoreNextUpdate : boolean ;
156159 } ;
157- // TODO: Find typedefs for this
158- $socket : ? any ;
159- connectTimeout : ? number ;
160+
161+ $socket : SocketIOClient . Socket ;
162+ connectTimeout : number | undefined ;
160163 // indicates if the socket closing is initiated by us
161164 localClose : boolean ;
162165
@@ -203,7 +206,7 @@ class BasePreview extends React.Component<Props, State> {
203206 this . executeCode = debounce ( this . executeCode , 800 ) ;
204207 }
205208
206- window . openNewWindow = this . openNewWindow ;
209+ ( window as any ) . openNewWindow = this . openNewWindow ;
207210 }
208211
209212 initializeLastSent = ( ) => {
@@ -242,7 +245,7 @@ class BasePreview extends React.Component<Props, State> {
242245 this . $socket . close ( ) ;
243246 // we need this setTimeout() for socket open() to work immediately after close()
244247 setTimeout ( ( ) => {
245- this . connectTimeout = setTimeout ( ( ) => onTimeout ( this ) , 3000 ) ;
248+ this . connectTimeout = window . setTimeout ( ( ) => onTimeout ( this ) , 3000 ) ;
246249 this . $socket . open ( ) ;
247250 } , 0 ) ;
248251 }
@@ -253,7 +256,7 @@ class BasePreview extends React.Component<Props, State> {
253256 } ) ;
254257 this . $socket = socket ;
255258 if ( process . env . NODE_ENV === 'development' ) {
256- window . $socket = socket ;
259+ ( window as any ) . $socket = socket ;
257260 }
258261
259262 socket . on ( 'disconnect' , ( ) => {
@@ -379,11 +382,14 @@ class BasePreview extends React.Component<Props, State> {
379382 ( ) => this . $socket . close ( )
380383 ) ;
381384 } else {
382- window . showNotification ( `Sandbox Container: ${ message } ` , 'error' ) ;
385+ ( window as any ) . showNotification (
386+ `Sandbox Container: ${ message } ` ,
387+ 'error'
388+ ) ;
383389 }
384390 } ) ;
385391
386- this . connectTimeout = setTimeout ( ( ) => onTimeout ( this ) , 3000 ) ;
392+ this . connectTimeout = window . setTimeout ( ( ) => onTimeout ( this ) , 3000 ) ;
387393 socket . open ( ) ;
388394 }
389395 } ;
@@ -393,8 +399,8 @@ class BasePreview extends React.Component<Props, State> {
393399 delay : true ,
394400 } ;
395401
396- listener: ? Function ;
397- disposeInitializer: ? Function ;
402+ listener : ( ) => void ;
403+ disposeInitializer : ( ) => void ;
398404 initialPath : string ;
399405
400406 componentWillUnmount ( ) {
@@ -464,7 +470,7 @@ class BasePreview extends React.Component<Props, State> {
464470 this . handleRefresh ( ) ;
465471 } ;
466472
467- handleMessage = ( data : Object , source : any ) => {
473+ handleMessage = ( data : any , source : any ) => {
468474 if ( data && data . codesandbox ) {
469475 if ( data . type === 'initialized' && source ) {
470476 registerFrame (
@@ -547,8 +553,8 @@ class BasePreview extends React.Component<Props, State> {
547553 : getModulePath ( sandbox . modules , sandbox . directories , currentModule . id ) ;
548554 } ;
549555
550- getModulesToSend = ( ) => {
551- const modulesObject = { } ;
556+ getModulesToSend = ( ) : IModulesByPath => {
557+ const modulesObject : IModulesByPath = { } ;
552558 const sandbox = this . props . sandbox ;
553559
554560 sandbox . modules . forEach ( m => {
@@ -581,7 +587,7 @@ class BasePreview extends React.Component<Props, State> {
581587 const sandbox = this . props . sandbox ;
582588
583589 if ( settings . clearConsoleEnabled && ! this . serverPreview ) {
584- // $FlowIssue: Chrome behaviour
590+ // @ts -ignore Chrome behaviour
585591 console . clear ( '__internal__' ) ; // eslint-disable-line no-console
586592 dispatch ( { type : 'clear-console' } ) ;
587593 }
@@ -638,9 +644,9 @@ class BasePreview extends React.Component<Props, State> {
638644 sendUrl = ( ) => {
639645 const { urlInAddressBar } = this . state ;
640646
641- if ( document . getElementById ( 'sandbox' ) ) {
642- // $FlowIssue
643- document . getElementById ( 'sandbox' ) . src = urlInAddressBar ;
647+ const el = document . getElementById ( 'sandbox' ) ;
648+ if ( el ) {
649+ ( el as HTMLIFrameElement ) . src = urlInAddressBar ;
644650
645651 this . setState ( {
646652 history : [ urlInAddressBar ] ,
@@ -654,9 +660,9 @@ class BasePreview extends React.Component<Props, State> {
654660 const { history, historyPosition, urlInAddressBar } = this . state ;
655661 const url = history [ historyPosition ] || urlInAddressBar ;
656662
657- if ( document . getElementById ( 'sandbox' ) ) {
658- // $FlowIssue
659- document . getElementById ( 'sandbox' ) . src =
663+ const el = document . getElementById ( 'sandbox' ) ;
664+ if ( el ) {
665+ ( el as HTMLIFrameElement ) . src =
660666 url ||
661667 ( this . serverPreview
662668 ? getSSEUrl ( this . props . sandbox . id )
@@ -777,7 +783,6 @@ class BasePreview extends React.Component<Props, State> {
777783 alignBottom = { this . props . alignBottom }
778784 alignDirection = { this . props . alignDirection }
779785 isServer = { this . serverPreview }
780- owned = { sandbox . owned }
781786 />
782787 ) }
783788 { overlayMessage && < Loading > { overlayMessage } </ Loading > }
@@ -799,7 +804,6 @@ class BasePreview extends React.Component<Props, State> {
799804 }
800805 id = "sandbox"
801806 title = { sandbox . title || sandbox . id }
802- hideNavigation = { ! showNavigation }
803807 style = { {
804808 ...style ,
805809 zIndex : 1 ,
@@ -828,7 +832,6 @@ class BasePreview extends React.Component<Props, State> {
828832 } }
829833 >
830834 < div
831- alt = { this . props . sandbox . title }
832835 style = { {
833836 width : '100%' ,
834837 height : '100%' ,
0 commit comments