Skip to content

Commit 64052fd

Browse files
authored
Robustify forking (codesandbox#369)
1 parent d9c9e24 commit 64052fd

File tree

5 files changed

+22
-81
lines changed

5 files changed

+22
-81
lines changed

packages/app/src/app/components/sandbox/CodeEditor/Tabs/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ export default class EditorTabs extends React.PureComponent<Props> {
161161
moduleObject[m.id] = m;
162162
});
163163

164-
tabs.forEach(tab => {
164+
tabs.filter(tab => moduleObject[tab.moduleId]).forEach(tab => {
165165
const module = moduleObject[tab.moduleId];
166166

167167
tabNamesObject[module.title] = tabNamesObject[module.title] || [];
@@ -179,6 +179,7 @@ export default class EditorTabs extends React.PureComponent<Props> {
179179
>
180180
{tabs
181181
.map(tab => ({ ...tab, module: moduleObject[tab.moduleId] }))
182+
.filter(tab => tab.module)
182183
.map((tab, i) => {
183184
const { module } = tab;
184185
const modulesWithName = tabNamesObject[module.title];

packages/app/src/app/store/entities/sandboxes/actions/fork.js

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { values } from 'lodash';
33

44
import { push } from 'react-router-redux';
55

6-
import type { Module, Directory } from 'common/types';
76
import { sandboxUrl } from 'common/utils/url-generator';
87

98
import { createAPIActions, doRequest } from '../../../api/actions';
@@ -16,35 +15,6 @@ import notificationActions from '../../../notifications/actions';
1615

1716
export const FORK_SANDBOX_API_ACTIONS = createAPIActions('SANDBOX', 'FORK');
1817

19-
/**
20-
* When you fork you get a 'copy' of the modules, these modules have the shortid
21-
* in common, so if we want to get the module that is equivalent we want to use
22-
* this
23-
*/
24-
export const getEquivalentModule = (module: Module, modules: Array<Module>) => {
25-
const newModule = modules.find(
26-
m => m.id !== module.id && m.shortid === module.shortid
27-
);
28-
29-
return newModule;
30-
};
31-
32-
/**
33-
* When you fork you get a 'copy' of the directories, these directories have the shortid
34-
* in common, so if we want to get the directory that is equivalent we want to use
35-
* this
36-
*/
37-
export const getEquivalentDirectory = (
38-
directory: Directory,
39-
directories: Array<Directory>
40-
) => {
41-
const newDirectory = directories.find(
42-
d => d.id !== directory.id && d.shortid === directory.shortid
43-
);
44-
45-
return newDirectory;
46-
};
47-
4818
export const forkSandbox = (id: string) => async (
4919
dispatch: Function,
5020
getState: Function
@@ -57,7 +27,17 @@ export const forkSandbox = (id: string) => async (
5727

5828
const currentSandbox = singleSandboxSelector(getState(), { id });
5929
if (currentSandbox) {
60-
data.currentModule = currentSandbox.currentModule;
30+
const currentModule = modulesSelector(getState())[
31+
currentSandbox.currentModule
32+
];
33+
if (currentModule) {
34+
const equivalentModule = data.modules.find(
35+
m => m.shortid === currentModule.shortid && m.sourceId === data.sourceId
36+
);
37+
if (equivalentModule) {
38+
data.currentModule = equivalentModule.id;
39+
}
40+
}
6141
}
6242
data.forked = true;
6343

@@ -73,7 +53,10 @@ export const forkSandbox = (id: string) => async (
7353
// Mark old modules as synced so there is no confirm when moving to new url
7454
dispatch(moduleActions.setModuleSynced(m.id));
7555

76-
const newModule = getEquivalentModule(m, modules) || m;
56+
const newModule =
57+
modules.find(
58+
m2 => m2.shortid === m.shortid && m2.sourceId === data.sourceId
59+
) || m;
7760
dispatch(moduleActions.setCode(newModule.id, m.code));
7861
});
7962

packages/app/src/app/store/entities/sandboxes/actions/fork.test.js

Lines changed: 0 additions & 42 deletions
This file was deleted.

packages/app/src/app/store/entities/sandboxes/entity.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ export default new schema.Entity(
1818
{
1919
processStrategy: sandbox => {
2020
const {
21-
currentModule,
21+
currentModule: hrefCurrentModule,
2222
initialPath,
2323
isInProjectView,
2424
isEditorScreen,
2525
isPreviewScreen,
2626
} = getSandboxOptions(document.location.href);
2727

28+
const currentModule = hrefCurrentModule || sandbox.currentModule;
29+
2830
const mainModule = findMainModule(
2931
sandbox.modules,
3032
sandbox.directories,

packages/app/src/app/store/entities/sandboxes/reducer.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @flow
22
import type { Sandbox } from 'common/types';
33
import { moveItem } from 'common/utils/array';
4-
import { mapValues } from 'lodash';
4+
import { mapValues, omit } from 'lodash';
55

66
import {
77
SET_NPM_DEPENDENCIES,
@@ -311,10 +311,7 @@ export default function reducer(
311311
owned: false,
312312
}));
313313
case DELETE_SANDBOX_API_ACTIONS.SUCCESS:
314-
return {
315-
...state,
316-
[action.meta.id]: null,
317-
};
314+
return omit(state, action.meta.id);
318315

319316
case SET_MODULE_SYNCED:
320317
return mapValues(state, s => ({

0 commit comments

Comments
 (0)