Skip to content

Commit dd6eea0

Browse files
christianalfoniSaraVieira
authored andcommitted
Fixed saveCode sync (codesandbox#2416)
* Fixed saveCode sync * fixed devtool tabs changes
1 parent 8648f42 commit dd6eea0

File tree

4 files changed

+43
-25
lines changed

4 files changed

+43
-25
lines changed

packages/app/src/app/overmind/effects/api/index.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import apiFactory, { Api, ApiConfig } from './apiFactory';
21
import {
32
CurrentUser,
43
Dependency,
@@ -14,14 +13,14 @@ import {
1413
GitInfo,
1514
GitCommit,
1615
GitPr,
17-
RoomInfo,
1816
PaymentDetails,
1917
Profile,
2018
UserSandbox,
2119
} from '@codesandbox/common/lib/types';
2220
import { TemplateType } from '@codesandbox/common/lib/templates';
2321
import { client } from 'app/graphql/client';
2422
import { LIST_TEMPLATES } from 'app/pages/Dashboard/queries';
23+
import apiFactory, { Api, ApiConfig } from './apiFactory';
2524

2625
let api: Api;
2726

@@ -60,8 +59,17 @@ export default {
6059
getDependency(name: string): Promise<Dependency> {
6160
return api.get(`/dependencies/${name}@latest`);
6261
},
63-
getSandbox(id: string): Promise<Sandbox> {
64-
return api.get(`/sandboxes/${id}`);
62+
async getSandbox(id: string): Promise<Sandbox> {
63+
const sandbox = await api.get<Sandbox>(`/sandboxes/${id}`);
64+
65+
// We need to add savedCode property to track it
66+
return {
67+
...sandbox,
68+
modules: sandbox.modules.map(module => ({
69+
...module,
70+
savedCode: null,
71+
})),
72+
};
6573
},
6674
forkSandbox(id: string): Promise<Sandbox> {
6775
const url = id.includes('/')

packages/app/src/app/overmind/namespaces/editor/actions.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ export const onDevToolsTabAdded: Action<{
580580
}> = ({ state, actions }, { tab }) => {
581581
const devToolTabs = state.editor.devToolTabs;
582582
const { devTools: newDevToolTabs, position } = addDevToolsTabUtil(
583-
devToolTabs,
583+
json(devToolTabs),
584584
tab
585585
);
586586

@@ -599,7 +599,11 @@ export const onDevToolsTabMoved: Action<{
599599
nextPos: any;
600600
}> = ({ state, actions }, { prevPos, nextPos }) => {
601601
const devToolTabs = state.editor.devToolTabs;
602-
const newDevToolTabs = moveDevToolsTabUtil(devToolTabs, prevPos, nextPos);
602+
const newDevToolTabs = moveDevToolsTabUtil(
603+
json(devToolTabs),
604+
prevPos,
605+
nextPos
606+
);
603607
const code = JSON.stringify({ preview: newDevToolTabs }, null, 2);
604608

605609
actions.editor.internal.updateDevtools({
@@ -614,7 +618,7 @@ export const onDevToolsTabClosed: Action<{
614618
}> = ({ state, actions }, { pos }) => {
615619
const devToolTabs = state.editor.devToolTabs;
616620
const closePos = pos;
617-
const newDevToolTabs = closeDevToolsTabUtil(devToolTabs, closePos);
621+
const newDevToolTabs = closeDevToolsTabUtil(json(devToolTabs), closePos);
618622
const code = JSON.stringify({ preview: newDevToolTabs }, null, 2);
619623

620624
actions.editor.internal.updateDevtools({

packages/app/src/app/overmind/namespaces/editor/internalActions.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,10 @@ export const setCurrentModule: Action<Module> = ({ state }, module) => {
290290
state.editor.currentTabId = null;
291291

292292
const tabs = state.editor.tabs as ModuleTab[];
293-
const tab = tabs.find(tab => tab.moduleShortid === module.shortid);
293+
const tab = tabs.find(tabItem => tabItem.moduleShortid === module.shortid);
294294

295295
if (!tab) {
296-
const dirtyTabIndex = tabs.findIndex(tab => tab.dirty);
296+
const dirtyTabIndex = tabs.findIndex(tabItem => tabItem.dirty);
297297
const newTab: ModuleTab = {
298298
type: TabType.MODULE,
299299
moduleShortid: module.shortid,
@@ -340,16 +340,19 @@ export const updateDevtools: AsyncAction<{
340340

341341
if (devtoolsModule) {
342342
await actions.editor.internal.saveCode({
343-
code: devtoolsModule.code,
343+
code,
344344
moduleShortid: devtoolsModule.shortid,
345345
cbID: null,
346346
});
347347
} else {
348348
await actions.files.createModulesByPath({
349-
'/.codesandbox/workspace.json': {
350-
content: code,
351-
isBinary: false,
349+
files: {
350+
'/.codesandbox/workspace.json': {
351+
content: code,
352+
isBinary: false,
353+
},
352354
},
355+
cbID: null,
353356
});
354357
}
355358
} else {

packages/app/src/app/overmind/namespaces/files/actions.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as internalActions from './internalActions';
21
import { AsyncAction } from 'app/overmind';
32
import { getModulePath } from '@codesandbox/common/lib/sandbox/modules';
43
import getDefinition from '@codesandbox/common/lib/templates';
@@ -11,6 +10,7 @@ import { INormalizedModules } from 'codesandbox-import-util-types';
1110
import { ModuleTab } from '@codesandbox/common/lib/types';
1211
import { createOptimisticModule } from 'app/overmind/utils/common';
1312
import { withOwnedSandbox } from 'app/overmind/factories';
13+
import * as internalActions from './internalActions';
1414

1515
export const internal = internalActions;
1616

@@ -275,10 +275,11 @@ export const massCreateModules: AsyncAction<{
275275
modules: any;
276276
directories: any;
277277
directoryShortid: string;
278+
cbID?: string;
278279
}> = withOwnedSandbox(
279280
async (
280281
{ state, effects, actions },
281-
{ modules, directories, directoryShortid }
282+
{ modules, directories, directoryShortid, cbID }
282283
) => {
283284
const sandboxId = state.editor.currentId;
284285

@@ -300,11 +301,10 @@ export const massCreateModules: AsyncAction<{
300301
if (state.live.isCurrentEditor) {
301302
effects.live.sendMassCreatedModules(data.modules, data.directories);
302303
}
303-
// Where is the id?
304-
// effects.vscode.callCallback()
304+
305+
effects.vscode.callCallback(cbID);
305306
} catch (error) {
306-
// Where is the id and message?
307-
// effects.vscode.callCallbackError()
307+
effects.vscode.callCallbackError(cbID);
308308
effects.notificationToast.error('Unable to create new files');
309309
}
310310
}
@@ -381,13 +381,15 @@ export const moduleCreated: AsyncAction<{
381381

382382
export const moduleDeleted: AsyncAction<{
383383
moduleShortid: string;
384-
}> = async ({ state, effects }, { moduleShortid }) => {
384+
}> = async ({ state, effects, actions }, { moduleShortid }) => {
385385
const sandbox = state.editor.currentSandbox;
386386
const moduleToDeleteIndex = sandbox.modules.findIndex(
387387
module => module.shortid === moduleShortid
388388
);
389389
const removedModule = sandbox.modules.splice(moduleToDeleteIndex, 1)[0];
390390

391+
actions.editor.internal.setCurrentModule(state.editor.mainModule);
392+
391393
try {
392394
await effects.api.deleteModule(sandbox.id, moduleShortid);
393395

@@ -400,10 +402,10 @@ export const moduleDeleted: AsyncAction<{
400402
}
401403
};
402404

403-
export const createModulesByPath: AsyncAction<INormalizedModules> = async (
404-
{ state, actions },
405-
files
406-
) => {
405+
export const createModulesByPath: AsyncAction<{
406+
cbID: string;
407+
files: INormalizedModules;
408+
}> = async ({ state, actions }, { files, cbID }) => {
407409
const sandbox = state.editor.currentSandbox;
408410

409411
const { modules, directories } = denormalize(files, sandbox.directories);
@@ -412,11 +414,12 @@ export const createModulesByPath: AsyncAction<INormalizedModules> = async (
412414
modules,
413415
directories,
414416
directoryShortid: null,
417+
cbID,
415418
});
416419
};
417420

418421
export const syncSandbox: AsyncAction<any[]> = async (
419-
{ state, effects, actions },
422+
{ state, effects },
420423
updates
421424
) => {
422425
const id = state.editor.currentId;

0 commit comments

Comments
 (0)