@@ -3,42 +3,47 @@ import history from 'app/utils/history';
3
3
import { Button } from '@codesandbox/common/lib/components/Button' ;
4
4
import { gitHubToSandboxUrl } from '@codesandbox/common/lib/utils/url-generator' ;
5
5
6
- window . addEventListener ( 'message' , receiveMessage , false ) ;
6
+ function openStackbit ( username : string , closeModal : ( ) => void ) {
7
+ const stackbitWindow = window . open (
8
+ `https://app.stackbit.com/wizard?ref=codesandbox&githubUser=${ username } &ssgExclusive=1&ssg=gatsby&cmsExclusive=netlifycms,forestry,nocms` ,
9
+ '_blank' ,
10
+ 'width=1210,height=800'
11
+ ) ;
12
+
13
+ window . addEventListener ( 'message' , receiveMessage , false ) ;
7
14
8
- function receiveMessage ( event ) {
9
- if ( event . origin === 'https://app.stackbit.com' && event . data ) {
10
- const data = JSON . parse ( event . data ) ;
15
+ function receiveMessage ( event ) {
16
+ if ( event . origin === 'https://app.stackbit.com' && event . data ) {
17
+ const data = JSON . parse ( event . data ) ;
11
18
12
- if (
13
- data . type === 'project-update' &&
14
- data . project &&
15
- data . project . repository &&
16
- data . project . repository . url
17
- ) {
18
- // @ts -ignore
19
- window . stackbitWindow . close ( ) ;
19
+ if (
20
+ data . type === 'project-update' &&
21
+ data . project &&
22
+ data . project . repository &&
23
+ data . project . repository . url
24
+ ) {
25
+ stackbitWindow . close ( ) ;
20
26
21
- history . push ( gitHubToSandboxUrl ( data . project . repository . url ) ) ;
27
+ closeModal ( ) ;
28
+ history . push ( gitHubToSandboxUrl ( data . project . repository . url ) ) ;
29
+ window . removeEventListener ( 'message' , receiveMessage , false ) ;
30
+ }
22
31
}
23
32
}
24
33
}
25
34
26
- function openStackbit ( username : string ) {
27
- // @ts -ignore
28
- window . stackbitWindow = window . open (
29
- `https://app.stackbit.com/wizard?ref=codesandbox&githubUser=${ username } &ssgExclusive=1&ssg=gatsby&cmsExclusive=netlifycms,forestry,nocms` ,
30
- '_blank' ,
31
- 'width=1210,height=800'
32
- ) ;
33
- }
34
-
35
35
interface Props {
36
36
username : string ;
37
+ closeModal : ( ) => void ;
37
38
style ?: React . CSSProperties ;
38
39
}
39
40
40
- export const StackbitButton = ( { username, style } : Props ) => (
41
- < Button style = { style } small onClick = { ( ) => openStackbit ( username ) } >
41
+ export const StackbitButton = ( { username, style, closeModal } : Props ) => (
42
+ < Button
43
+ style = { style }
44
+ small
45
+ onClick = { ( ) => openStackbit ( username , closeModal ) }
46
+ >
42
47
Generate Sandbox
43
48
</ Button >
44
49
) ;
0 commit comments