Skip to content

Commit d12bf63

Browse files
committed
Fix stackbit modal closing
1 parent afe3259 commit d12bf63

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

packages/app/src/app/components/CreateNewSandbox/CreateSandbox/Import/Import.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const copyToClipboard = (str: string) => {
4444
};
4545

4646
export const Import = () => {
47-
const { state } = useOvermind();
47+
const { state, actions } = useOvermind();
4848
const [error, setError] = useState(null);
4949
const [transformedUrl, setTransformedUrl] = useState('');
5050
const [url, setUrl] = useState('');
@@ -161,6 +161,7 @@ export const Import = () => {
161161
<StackbitButton
162162
style={{ fontSize: 11 }}
163163
username={state.user.username}
164+
closeModal={() => actions.modalClosed()}
164165
/>
165166
)}
166167
</Column>

packages/app/src/app/components/CreateNewSandbox/CreateSandbox/Import/Stackbit.tsx

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,47 @@ import history from 'app/utils/history';
33
import { Button } from '@codesandbox/common/lib/components/Button';
44
import { 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-
3535
interface 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

Comments
 (0)