Skip to content

Commit c3430fe

Browse files
authored
Fix creating a sandbox in overmind from dashboard as a team (codesandbox#2455)
1 parent 221ffb3 commit c3430fe

File tree

7 files changed

+52
-38
lines changed

7 files changed

+52
-38
lines changed

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -218,17 +218,8 @@ export const refetchSandboxInfo: AsyncAction = async ({
218218
sandbox.userLiked = updatedSandbox.userLiked;
219219
sandbox.title = updatedSandbox.title;
220220
sandbox.team = updatedSandbox.team;
221-
222-
state.live.isTeam = Boolean(sandbox.team);
223-
224-
if (sandbox.roomId === updatedSandbox.roomId) {
225-
return;
226-
}
227-
228221
sandbox.roomId = updatedSandbox.roomId;
229-
await actions.live.internal.disconnect();
230-
if (updatedSandbox.owned && updatedSandbox.roomId) {
231-
await actions.live.internal.initialize(sandbox.roomId);
232-
}
222+
223+
await actions.editor.internal.initializeLiveSandbox(sandbox);
233224
}
234225
};

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ export default {
7272
})),
7373
};
7474
},
75-
forkSandbox(id: string): Promise<Sandbox> {
75+
forkSandbox(id: string, body?: unknown): Promise<Sandbox> {
7676
const url = id.includes('/')
7777
? `/sandboxes/fork/${id}`
7878
: `/sandboxes/${id}/fork`;
7979

80-
return api.post(url, {});
80+
return api.post(url, body || {});
8181
},
8282
createModule(sandboxId: string, module: Module): Promise<Module> {
8383
return api.post(`/sandboxes/${sandboxId}/modules`, {

packages/app/src/app/overmind/factories.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ export const withOwnedSandbox = <T>(
7777
'This sandbox is frozen, and will be forked. Do you want to continue?'
7878
))
7979
) {
80-
await actions.editor.internal.forkSandbox(state.editor.currentId);
80+
await actions.editor.internal.forkSandbox({
81+
sandboxId: state.editor.currentId,
82+
});
8183
}
8284

8385
return continueAction(context, payload);

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ export const searchChanged: Action<{ search: string }> = (
6464
state.dashboard.filters.search = search;
6565
};
6666

67-
export const createSandboxClicked: AsyncAction<{ sandboxId: string }> = (
68-
{ actions },
69-
{ sandboxId }
70-
) => actions.editor.internal.forkSandbox(sandboxId);
67+
export const createSandboxClicked: AsyncAction<{
68+
sandboxId: string;
69+
body: { collectionId: string | undefined };
70+
}> = ({ actions }, { sandboxId, body }) =>
71+
actions.editor.internal.forkSandbox({ sandboxId, body });

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ export const sandboxChanged: AsyncAction<{ id: string }> = withLoadApp<{
7676
const sandbox = await effects.api.getSandbox(newId);
7777

7878
actions.internal.updateCurrentSandbox(sandbox);
79+
state.editor.currentId = newId;
80+
state.editor.isLoading = false;
81+
82+
await actions.editor.internal.initializeLiveSandbox(sandbox);
7983

8084
return;
8185
}
@@ -104,13 +108,9 @@ export const sandboxChanged: AsyncAction<{ id: string }> = withLoadApp<{
104108

105109
actions.internal.ensurePackageJSON();
106110

107-
if (sandbox.owned && sandbox.roomId) {
108-
if (sandbox.team) {
109-
state.live.isTeam = true;
110-
}
111+
await actions.editor.internal.initializeLiveSandbox(sandbox);
111112

112-
await actions.live.internal.initialize(sandbox.roomId);
113-
} else if (sandbox.owned) {
113+
if (sandbox.owned && !state.live.isLive) {
114114
actions.files.internal.recoverFiles();
115115
}
116116

@@ -231,7 +231,9 @@ export const forkSandboxClicked: AsyncAction = async ({
231231
return;
232232
}
233233

234-
await actions.editor.internal.forkSandbox(state.editor.currentId);
234+
await actions.editor.internal.forkSandbox({
235+
sandboxId: state.editor.currentId,
236+
});
235237
};
236238

237239
export const likeSandboxToggled: AsyncAction<{

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

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
ModuleTab,
66
TabType,
77
ServerContainerStatus,
8+
Sandbox,
89
} from '@codesandbox/common/lib/types';
910
import getTemplateDefinition from '@codesandbox/common/lib/templates';
1011
import { getTemplate as computeTemplate } from 'codesandbox-import-utils/lib/create-sandbox/templates';
@@ -29,6 +30,28 @@ export const ensureSandboxId: Action<string, string> = ({ state }, id) => {
2930
return matchingSandboxId || id;
3031
};
3132

33+
export const initializeLiveSandbox: AsyncAction<Sandbox> = async (
34+
{ state, actions },
35+
sandbox
36+
) => {
37+
state.live.isTeam = Boolean(sandbox.team);
38+
39+
if (state.live.isLive) {
40+
const roomChanged = state.live.roomInfo.roomId !== sandbox.roomId;
41+
42+
if (!roomChanged) {
43+
// In this case we don't need to initialize new live session, we reuse the existing one
44+
return;
45+
}
46+
47+
await actions.live.internal.disconnect();
48+
}
49+
50+
if (sandbox.owned && sandbox.roomId) {
51+
await actions.live.internal.initialize(sandbox.roomId);
52+
}
53+
};
54+
3255
export const setModuleSavedCode: Action<{
3356
moduleShortid: string;
3457
savedCode: string;
@@ -255,10 +278,10 @@ export const setModuleCode: Action<{
255278
}
256279
};
257280

258-
export const forkSandbox: AsyncAction<string> = async (
259-
{ state, effects, actions },
260-
id
261-
) => {
281+
export const forkSandbox: AsyncAction<{
282+
sandboxId: string;
283+
body?: { collectionId: string | undefined };
284+
}> = async ({ state, effects, actions }, { sandboxId: id, body }) => {
262285
const templateDefinition = getTemplateDefinition(
263286
state.editor.currentSandbox ? state.editor.currentSandbox.template : null
264287
);
@@ -275,7 +298,7 @@ export const forkSandbox: AsyncAction<string> = async (
275298
try {
276299
state.editor.isForkingSandbox = true;
277300

278-
const forkedSandbox = await effects.api.forkSandbox(id);
301+
const forkedSandbox = await effects.api.forkSandbox(id, body);
279302

280303
// Copy over any unsaved code
281304
if (state.editor.currentSandbox) {

standalone-packages/vscode-textmate/package-lock.json

Lines changed: 3 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)