Skip to content

Commit 166db56

Browse files
committed
Make it possible to deduplicate any devtools tab
1 parent dd90ccd commit 166db56

File tree

11 files changed

+78
-62
lines changed

11 files changed

+78
-62
lines changed

packages/app/src/app/components/Preview/DevTools/Tabs/Tab/index.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,21 @@ import {
99
} from 'react-dnd';
1010
import CrossIcon from 'react-icons/lib/md/clear';
1111

12+
import { DevToolsTabPosition } from '@codesandbox/common/lib/types';
1213
import { Tab, CloseTab } from './elements';
1314
import { IViewType, Status } from '../..';
14-
import { ITabPosition } from '..';
1515
import { UnreadDevToolsCount } from './UnreadDevToolsCount';
1616

1717
export interface TabProps {
1818
active: boolean;
1919
pane: IViewType;
2020
onClick: (e: React.MouseEvent<HTMLDivElement>) => void;
2121
onMouseDown: (e: React.MouseEvent<HTMLDivElement>) => void;
22-
moveTab?: (currentPosition: ITabPosition, nextPosition: ITabPosition) => void;
23-
closeTab?: (pos: ITabPosition) => void;
22+
moveTab?: (
23+
currentPosition: DevToolsTabPosition,
24+
nextPosition: DevToolsTabPosition
25+
) => void;
26+
closeTab?: (pos: DevToolsTabPosition) => void;
2427
index: number;
2528
devToolIndex: number;
2629
canDrag: boolean;
@@ -146,11 +149,11 @@ const entryTarget = {
146149
return;
147150
}
148151

149-
const previousPosition: ITabPosition = {
152+
const previousPosition: DevToolsTabPosition = {
150153
tabPosition: sourceItem.index,
151154
devToolIndex: sourceItem.devToolIndex,
152155
};
153-
const nextPosition: ITabPosition = {
156+
const nextPosition: DevToolsTabPosition = {
154157
tabPosition: props.index,
155158
devToolIndex: props.devToolIndex,
156159
};

packages/app/src/app/components/Preview/DevTools/Tabs/TabDropZone/index.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import {
66
DropTargetMonitor,
77
DropTargetConnector,
88
} from 'react-dnd';
9+
import { DevToolsTabPosition } from '@codesandbox/common/lib/types';
910

10-
import { ITabPosition } from '..';
1111
import { PREVIEW_TAB_ID } from '../Tab';
1212

1313
const DropZone = styled.div<{ isOver: boolean }>`
@@ -26,7 +26,10 @@ const DropZone = styled.div<{ isOver: boolean }>`
2626
`;
2727

2828
export interface TabDropZoneProps {
29-
moveTab: (currentPosition: ITabPosition, nextPosition: ITabPosition) => void;
29+
moveTab: (
30+
currentPosition: DevToolsTabPosition,
31+
nextPosition: DevToolsTabPosition
32+
) => void;
3033
index: number;
3134
devToolIndex: number;
3235
}
@@ -61,7 +64,7 @@ const entryTarget = {
6164
return;
6265
}
6366

64-
const previousPosition: ITabPosition = {
67+
const previousPosition: DevToolsTabPosition = {
6568
tabPosition: sourceItem.index,
6669
devToolIndex: sourceItem.devToolIndex,
6770
};

packages/app/src/app/components/Preview/DevTools/Tabs/index.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
import React from 'react';
22
import Tooltip from '@codesandbox/common/lib/components/Tooltip';
33
import { ViewTab } from '@codesandbox/common/lib/templates/template';
4+
import { DevToolsTabPosition } from '@codesandbox/common/lib/types';
45

56
import { Status, IViews } from '..';
67
import { Actions, Container, Tabs } from './elements';
78
import { DraggableTab, PaneTab, TabProps } from './Tab';
89
import { TabDropZone, TabDropZoneProps } from './TabDropZone';
910
// import { AddTab } from './AddTab';
1011

11-
export interface ITabPosition {
12-
devToolIndex: number;
13-
tabPosition: number;
14-
}
15-
1612
export interface Props {
1713
hidden: boolean;
1814
currentPaneIndex: number;
1915
owned: boolean;
2016
setPane: (i: number) => void;
2117
devToolIndex: number;
22-
moveTab?: (prevPos: ITabPosition, newPos: ITabPosition) => void;
23-
closeTab?: (pos: ITabPosition) => void;
18+
moveTab?: (prevPos: DevToolsTabPosition, newPos: DevToolsTabPosition) => void;
19+
closeTab?: (pos: DevToolsTabPosition) => void;
2420
status?: { [title: string]: Status | undefined };
2521

2622
panes: ViewTab[];

packages/app/src/app/components/Preview/DevTools/index.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import { TemplateType } from '@codesandbox/common/lib/templates';
77
import { ViewConfig } from '@codesandbox/common/lib/templates/template';
88
import track from '@codesandbox/common/lib/utils/analytics';
99

10+
import { DevToolsTabPosition } from '@codesandbox/common/lib/types';
1011
import { console } from './Console';
11-
import { DevToolTabs, ITabPosition } from './Tabs';
12+
import { DevToolTabs } from './Tabs';
1213
import { problems } from './Problems';
1314
import { reactDevTools } from './React-Devtools';
1415
import { terminal } from './Terminal';
@@ -95,9 +96,12 @@ type Props = {
9596
primary: boolean;
9697
viewConfig: ViewConfig;
9798
devToolIndex: number;
98-
moveTab?: (prevPos: ITabPosition, nextPos: ITabPosition) => void;
99-
closeTab?: (pos: ITabPosition) => void;
100-
setPane: (pos: ITabPosition) => void;
99+
moveTab?: (
100+
prevPos: DevToolsTabPosition,
101+
nextPos: DevToolsTabPosition
102+
) => void;
103+
closeTab?: (pos: DevToolsTabPosition) => void;
104+
setPane: (pos: DevToolsTabPosition) => void;
101105
addedViews?: IViews;
102106
hideTabs?: boolean;
103107
currentDevToolIndex: number;

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
Sandbox,
1818
Tabs,
1919
WindowOrientation,
20+
DevToolsTabPosition,
2021
} from '@codesandbox/common/lib/types';
2122
import { getSandboxOptions } from '@codesandbox/common/lib/url';
2223
import { Derive } from 'app/overmind';
@@ -86,10 +87,7 @@ type State = {
8687
>;
8788
isAdvancedEditor: Derive<State, boolean>;
8889
shouldDirectoryBeOpen: Derive<State, (directoryShortid: string) => boolean>;
89-
currentDevToolsPosition: {
90-
devToolIndex: number;
91-
tabPosition: number;
92-
};
90+
currentDevToolsPosition: DevToolsTabPosition;
9391
sessionFrozen: boolean;
9492
};
9593

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

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from '@codesandbox/common/lib/types';
77
import { NotificationStatus } from '@codesandbox/notifications/lib/state';
88
import { getDevToolsTabPosition } from 'app/overmind/utils/server';
9+
import { ViewTab } from '@codesandbox/common/lib/templates/template';
910

1011
export const restartSandbox: Action = ({ effects }) => {
1112
effects.executor.emit('sandbox:restart');
@@ -180,30 +181,20 @@ type BrowserOptions = { title?: string; url?: string } & (
180181
| { url: string });
181182

182183
export const onBrowserTabOpened: Action<{
183-
options: BrowserOptions;
184-
}> = ({ actions }, { options }) => {
185-
actions.editor.onDevToolsTabAdded({
186-
tab: {
187-
id: 'codesandbox.browser',
188-
closeable: true,
189-
options,
190-
},
191-
});
192-
};
184+
closeable?: boolean;
185+
options?: BrowserOptions;
186+
}> = ({ actions, state }, { options, closeable }) => {
187+
const tab: ViewTab = {
188+
id: 'codesandbox.browser',
189+
};
190+
191+
if (typeof options !== 'undefined') {
192+
tab.options = options;
193+
}
193194

194-
export const onBrowserFromPortOpened: Action<{
195-
port: ServerPort;
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}`,
205-
},
206-
};
195+
if (typeof closeable !== 'undefined') {
196+
tab.closeable = closeable;
197+
}
207198

208199
const position = getDevToolsTabPosition({
209200
tabs: state.editor.devToolTabs,
@@ -216,3 +207,19 @@ export const onBrowserFromPortOpened: Action<{
216207
actions.editor.onDevToolsTabAdded({ tab });
217208
}
218209
};
210+
211+
export const onBrowserFromPortOpened: Action<{
212+
port: ServerPort;
213+
}> = ({ actions }, { port }) => {
214+
if (port.main) {
215+
actions.server.onBrowserTabOpened({});
216+
} else {
217+
actions.server.onBrowserTabOpened({
218+
closeable: true,
219+
options: {
220+
port: port.port,
221+
url: `https://${port.hostname}`,
222+
},
223+
});
224+
}
225+
};

packages/app/src/app/overmind/utils/server.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
ViewConfig,
33
ViewTab,
44
} from '@codesandbox/common/lib/templates/template';
5+
import { DevToolsTabPosition } from '@codesandbox/common/lib/types';
56

67
// eslint-disable-next-line consistent-return
78
export function getDevToolsTabPosition({
@@ -10,12 +11,7 @@ export function getDevToolsTabPosition({
1011
}: {
1112
tabs: ViewConfig[];
1213
tab: ViewTab;
13-
}):
14-
| {
15-
devToolIndex: number;
16-
tabPosition: number;
17-
}
18-
| undefined {
14+
}): DevToolsTabPosition | undefined {
1915
const serializedTab = JSON.stringify(tab);
2016

2117
for (let i = 0; i < tabs.length; i++) {

packages/app/src/app/pages/Sandbox/Editor/Content/utils.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ import {
33
ViewConfig,
44
ViewTab,
55
} from '@codesandbox/common/lib/templates/template';
6-
import { ITabPosition } from 'app/components/Preview/DevTools/Tabs';
6+
import { DevToolsTabPosition } from '@codesandbox/common/lib/types';
77

8-
const isEqual = (prevPos: ITabPosition, nextPos: ITabPosition) =>
8+
const isEqual = (prevPos: DevToolsTabPosition, nextPos: DevToolsTabPosition) =>
99
prevPos.devToolIndex === nextPos.devToolIndex &&
1010
prevPos.tabPosition === nextPos.tabPosition;
1111

1212
export function addDevToolsTab(
1313
tabs: ViewConfig[],
1414
newTab: ViewTab,
15-
position?: ITabPosition
15+
position?: DevToolsTabPosition
1616
) {
17-
const positionToAdd: ITabPosition = position || {
17+
const positionToAdd: DevToolsTabPosition = position || {
1818
devToolIndex: 0,
1919
tabPosition: tabs[0].views.length,
2020
};
@@ -31,8 +31,8 @@ export function addDevToolsTab(
3131

3232
export function moveDevToolsTab(
3333
tabs: ViewConfig[],
34-
prevPos: ITabPosition,
35-
nextPos: ITabPosition
34+
prevPos: DevToolsTabPosition,
35+
nextPos: DevToolsTabPosition
3636
) {
3737
if (isEqual(prevPos, nextPos)) {
3838
return tabs;
@@ -64,7 +64,10 @@ export function moveDevToolsTab(
6464
});
6565
}
6666

67-
export function closeDevToolsTab(tabs: ViewConfig[], closePos: ITabPosition) {
67+
export function closeDevToolsTab(
68+
tabs: ViewConfig[],
69+
closePos: DevToolsTabPosition
70+
) {
6871
// We want to do this immutable, to prevent conflicts while the file is changing
6972
return immer(tabs, draft => {
7073
const devTools = draft[closePos.devToolIndex];

packages/app/src/app/pages/Sandbox/Editor/Workspace/items/Server/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ export const Server: FunctionComponent = () => {
109109
style={{ position: 'relative' }}
110110
onClick={() =>
111111
onBrowserTabOpened({
112+
closeable: true,
112113
options: {
113114
url:
114115
sandbox.template === 'gridsome'

packages/app/src/embed/components/Content/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
ModuleError,
77
ModuleCorrection,
88
Settings,
9+
DevToolsTabPosition,
910
} from '@codesandbox/common/lib/types';
1011
import BasePreview from '@codesandbox/common/lib/components/Preview';
1112
import { CorrectionClearAction } from 'codesandbox-api/dist/types/actions/correction';
@@ -31,7 +32,6 @@ import {
3132
IViewType,
3233
DevToolProps,
3334
} from 'app/components/Preview/DevTools';
34-
import { ITabPosition } from 'app/components/Preview/DevTools/Tabs';
3535
import { clearCorrectionsFromAction } from 'app/utils/corrections';
3636

3737
import {
@@ -78,7 +78,7 @@ type State = {
7878
isInProjectView: boolean;
7979
dragging: boolean;
8080
running: boolean;
81-
currentDevToolPosition: ITabPosition;
81+
currentDevToolPosition: DevToolsTabPosition;
8282
};
8383

8484
export default class Content extends React.PureComponent<Props, State> {
@@ -104,7 +104,7 @@ export default class Content extends React.PureComponent<Props, State> {
104104
this.corrections = [];
105105
}
106106

107-
setPane = (pos: ITabPosition) => {
107+
setPane = (pos: DevToolsTabPosition) => {
108108
this.setState({ currentDevToolPosition: pos });
109109
};
110110

0 commit comments

Comments
 (0)