Skip to content

Commit dd90ccd

Browse files
yeion7CompuIves
authored andcommitted
fix prevent open new main ports (codesandbox#2723)
* add type * add utils to find tab position * check port position
1 parent 687949b commit dd90ccd

File tree

3 files changed

+57
-12
lines changed

3 files changed

+57
-12
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { getSandboxOptions } from '@codesandbox/common/lib/url';
2222
import { Derive } from 'app/overmind';
2323
import immer from 'immer';
2424

25+
import { ViewConfig } from '@codesandbox/common/lib/templates/template';
2526
import { mainModule as getMainModule } from '../../utils/main-module';
2627
import { parseConfigurations } from '../../utils/parse-configurations';
2728

@@ -35,7 +36,7 @@ type State = {
3536
};
3637
// TODO: What is this really? Could not find it in Cerebral, but
3738
// EditorPreview is using it... weird stuff
38-
devToolTabs: Derive<State, any[]>;
39+
devToolTabs: Derive<State, ViewConfig[]>;
3940
isLoading: boolean;
4041
notFound: boolean;
4142
error: string | null;

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

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
ServerPort,
66
} from '@codesandbox/common/lib/types';
77
import { NotificationStatus } from '@codesandbox/notifications/lib/state';
8+
import { getDevToolsTabPosition } from 'app/overmind/utils/server';
89

910
export const restartSandbox: Action = ({ effects }) => {
1011
effects.executor.emit('sandbox:restart');
@@ -192,17 +193,26 @@ export const onBrowserTabOpened: Action<{
192193

193194
export const onBrowserFromPortOpened: Action<{
194195
port: ServerPort;
195-
}> = ({ actions }, { port }) => {
196-
actions.editor.onDevToolsTabAdded({
197-
tab: port.main
198-
? { id: 'codesandbox.browser' }
199-
: {
200-
id: 'codesandbox.browser',
201-
closeable: true,
202-
options: {
203-
port: port.port,
204-
url: `https://${port.hostname}`,
205-
},
196+
}> = ({ actions, state }, { port }) => {
197+
const tab = port.main
198+
? { id: 'codesandbox.browser' }
199+
: {
200+
id: 'codesandbox.browser',
201+
closeable: true,
202+
options: {
203+
port: port.port,
204+
url: `https://${port.hostname}`,
206205
},
206+
};
207+
208+
const position = getDevToolsTabPosition({
209+
tabs: state.editor.devToolTabs,
210+
tab,
207211
});
212+
213+
if (position) {
214+
actions.editor.onDevToolsPositionChanged({ position });
215+
} else {
216+
actions.editor.onDevToolsTabAdded({ tab });
217+
}
208218
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import {
2+
ViewConfig,
3+
ViewTab,
4+
} from '@codesandbox/common/lib/templates/template';
5+
6+
// eslint-disable-next-line consistent-return
7+
export function getDevToolsTabPosition({
8+
tabs,
9+
tab,
10+
}: {
11+
tabs: ViewConfig[];
12+
tab: ViewTab;
13+
}):
14+
| {
15+
devToolIndex: number;
16+
tabPosition: number;
17+
}
18+
| undefined {
19+
const serializedTab = JSON.stringify(tab);
20+
21+
for (let i = 0; i < tabs.length; i++) {
22+
const view = tabs[i];
23+
24+
for (let j = 0; j < view.views.length; j++) {
25+
const tabFounded = view.views[j];
26+
if (JSON.stringify(tabFounded) === serializedTab) {
27+
return {
28+
devToolIndex: i,
29+
tabPosition: j,
30+
};
31+
}
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)