@@ -3,42 +3,47 @@ import history from 'app/utils/history';
33import { Button } from '@codesandbox/common/lib/components/Button' ;
44import { gitHubToSandboxUrl } from '@codesandbox/common/lib/utils/url-generator' ;
55
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 ) ;
714
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 ) ;
1118
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 ( ) ;
2026
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+ }
2231 }
2332 }
2433}
2534
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-
3535interface Props {
3636 username : string ;
37+ closeModal : ( ) => void ;
3738 style ?: React . CSSProperties ;
3839}
3940
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+ >
4247 Generate Sandbox
4348 </ Button >
4449) ;
0 commit comments