Skip to content

Commit 4f295f7

Browse files
authored
Remove loading indicator for sandboxes (codesandbox#2251)
* Loading indicator for sandboxes * Fix by letting updateSandbox update the module * Fix pressing back after loading sandbox
1 parent 8466914 commit 4f295f7

File tree

5 files changed

+24
-5
lines changed

5 files changed

+24
-5
lines changed

packages/app/src/app/components/CodeEditor/VSCode/index.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,7 @@ class MonacoEditor extends React.Component<Props> implements Editor {
511511
errors?: ModuleError[],
512512
corrections?: ModuleCorrection[]
513513
) => {
514-
const oldModule = this.currentModule;
515-
this.swapDocuments(oldModule, newModule);
514+
this.swapDocuments(newModule);
516515

517516
this.currentModule = newModule;
518517
this.currentTitle = newModule.title;
@@ -594,8 +593,8 @@ class MonacoEditor extends React.Component<Props> implements Editor {
594593
): Promise<null> =>
595594
new Promise(resolve => {
596595
this.sandbox = newSandbox;
597-
this.currentModule = newCurrentModule;
598596
this.dependencies = dependencies;
597+
this.changeModule(newCurrentModule, [], []);
599598

600599
// Do in setTimeout, since disposeModules is async
601600
setTimeout(() => {
@@ -983,13 +982,13 @@ class MonacoEditor extends React.Component<Props> implements Editor {
983982
module.id
984983
);
985984

986-
if (this.getCurrentModelPath() !== path) {
985+
if (path && this.getCurrentModelPath() !== path) {
987986
this.editor.openFile(path);
988987
}
989988
}
990989
};
991990

992-
swapDocuments = (currentModule: Module, nextModule: Module) => {
991+
swapDocuments = (nextModule: Module) => {
993992
this.openModule(nextModule);
994993
};
995994

packages/app/src/app/components/CodeEditor/types.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export interface Editor {
5858
) => any;
5959
changeCode?: (code: string, moduleId?: string) => any;
6060
currentModule?: Module;
61+
sandbox?: Sandbox;
6162
setTSConfig?: (tsConfig: Object) => void;
6263
setReceivingCode?: (receivingCode: boolean) => void;
6364
applyOperations?: (operations: { [moduleShortid: string]: any }) => void;

packages/app/src/app/components/CodeEditor/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export interface Editor {
6161
) => any;
6262
changeCode?: (code: string, moduleId?: string) => any;
6363
currentModule?: Module;
64+
sandbox?: Sandbox;
6465
setTSConfig?: (tsConfig: Object) => void;
6566
setReceivingCode?: (receivingCode: boolean) => void;
6667
applyOperations?: (operations: { [moduleShortid: string]: any }) => void;

packages/app/src/app/pages/Sandbox/Editor/Content/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,13 +295,25 @@ class EditorPreview extends React.Component<Props, State> {
295295
}
296296

297297
const editorModule = editor.currentModule;
298+
const currentSandbox = editor.sandbox;
298299
const changeModule = editor.changeModule;
299300
if (
300301
(!editorModule || newModule.id !== editorModule.id) &&
301302
changeModule
302303
) {
303304
const errors = store.editor.errors.map(e => e);
304305
const corrections = store.editor.corrections.map(e => e);
306+
307+
if (
308+
currentSandbox.id !== store.editor.currentSandbox.id &&
309+
editor.changeSandbox
310+
) {
311+
// This means that the sandbox will be updated soon in the editor itself, which will
312+
// cause the module to change anyway. We don't want to continue here because the new sandbox
313+
// has not yet been initialized in the editor, but it's trying already to update the module.
314+
return;
315+
}
316+
305317
changeModule(newModule, errors, corrections);
306318
} else if (editor.changeCode) {
307319
// Only code changed from outside the editor

packages/app/src/app/store/sequences.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ export const setSandbox = [
138138
{
139139
true: [],
140140
false: [
141+
set(props`oldId`, state`editor.currentId`),
141142
set(state`editor.currentId`, props`sandbox.id`),
142143
actions.setCurrentModuleShortid,
143144
actions.setMainModuleShortid,
@@ -149,6 +150,11 @@ export const setSandbox = [
149150
resetServerState,
150151
setupExecutor,
151152
syncFilesToFS,
153+
154+
// Remove the old sandbox because it's stale with the changes the user did on it (for example,
155+
// the user might have changed code of a file and then forked. We didn't revert the code back
156+
// to its old state so if the user opens this sandbox again it shows wrong code)
157+
unset(state`editor.sandboxes.${props`oldId`}`),
152158
],
153159
},
154160
];

0 commit comments

Comments
 (0)