Skip to content

Commit 23a1f88

Browse files
SaraVieiraCompuIves
authored andcommitted
React DevTools (codesandbox#2252)
* add syubmodule * add tab * make work * deletre submodule * add sandbox * clean up file * clean up elements * remove console * Update yarn.lock * Add hidden state * Make name for react devtools more specific * Rename React Devtools to ⚛️ Components * Use useState for current devtools * Use new batched React render * Remove second argument from root.render * change color of devtools according to theme * Retry new react dom render * Update react to rc * Open file in CodeSandbox * Fix view source for React template * bump to [email protected] * fix yarn integrety * only show devtools in react * Remove debug code * Upgrade devtools * Fix types * Move existing implementations to new helper function * Fix missing rename
1 parent d9a6584 commit 23a1f88

File tree

30 files changed

+259
-89
lines changed

30 files changed

+259
-89
lines changed

packages/app/package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@
187187
"http-browserify": "^1.7.0",
188188
"https-browserify": "^1.0.0",
189189
"humps": "CompuIves/humps",
190-
"immer": "^0.8.5",
190+
"immer": "^3.2.0",
191191
"immutability-helper": "^2.6.6",
192192
"instantsearch.css": "^7.1.0",
193193
"is-url": "^1.2.2",
@@ -227,17 +227,19 @@
227227
"qs": "^6.5.0",
228228
"querystring": "^0.2.0",
229229
"rc-slider": "^8.2.0",
230-
"react": "^16.8.6",
230+
"react": "^16.9.0",
231231
"react-addons-css-transition-group": "^15.6.0",
232232
"react-apollo": "^2.5.6",
233233
"react-color": "^2.17.3",
234234
"react-day-picker": "^7.2.4",
235+
"react-devtools-inline": "^4.0.0",
235236
"react-dnd": "^7.0.2",
236237
"react-dnd-html5-backend": "^2.4.1",
237-
"react-dom": "^16.8.6",
238+
"react-dom": "^16.9.0",
238239
"react-draggable": "^3.0.5",
239240
"react-error-overlay": "^1.0.10",
240241
"react-icons": "^2.2.7",
242+
"react-input-autosize": "^2.2.1",
241243
"react-inspector": "^2.2.0",
242244
"react-instantsearch": "^5.3.2",
243245
"react-loadable": "^3.3.1",

packages/app/src/app/components/CodeEditor/VSCode/index.tsx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,8 @@ class MonacoEditor extends React.Component<Props> implements Editor {
236236
},
237237
]);
238238

239-
setupCodeSandboxAPIListener() {
240-
// @ts-ignore
241-
return listen(({ action, type, code, path, lineNumber, column }) => {
239+
setupCodeSandboxAPIListener = () =>
240+
listen(({ action, type, code, path, lineNumber, column }: any) => {
242241
if (type === 'add-extra-lib') {
243242
// TODO: bring this func back
244243
// const dtsPath = `${path}.d.ts`;
@@ -254,17 +253,18 @@ class MonacoEditor extends React.Component<Props> implements Editor {
254253
if (lineNumber || column) {
255254
options.selection = {
256255
startLineNumber: lineNumber,
257-
startColumn: column,
256+
startColumn: column || 0,
258257
};
259258
}
260259

261-
this.editor.codeEditorService.openCodeEditor({
262-
resource: this.monaco.Uri.file('/sandbox' + path),
263-
options,
264-
});
260+
if (this.editor) {
261+
this.editor.codeEditorService.openCodeEditor({
262+
resource: this.monaco.Uri.file('/sandbox' + path),
263+
options,
264+
});
265+
}
265266
}
266267
});
267-
}
268268

269269
modelListeners: {
270270
[path: string]: {
@@ -933,7 +933,9 @@ class MonacoEditor extends React.Component<Props> implements Editor {
933933
),
934934
options: {
935935
inlineClassName: classification.type
936-
? `${classification.kind} ${classification.type}-of-${classification.parentKind}`
936+
? `${classification.kind} ${classification.type}-of-${
937+
classification.parentKind
938+
}`
937939
: classification.kind,
938940
},
939941
}));
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import styled from 'styled-components';
2+
3+
export const Container = styled.div`
4+
background-color: ${props =>
5+
props.theme['panel.background'] || props.theme.background2};
6+
width: 100%;
7+
height: 100%;
8+
color: ${props =>
9+
props.theme['editor.foreground'] || 'rgba(255, 255, 255, 0.8)'};
10+
`;
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import React, { useEffect, useState, useContext } from 'react';
2+
import { initialize } from 'react-devtools-inline/frontend';
3+
import { dispatch, actions } from 'codesandbox-api';
4+
import { ThemeContext } from 'styled-components';
5+
6+
import { Container } from './elements';
7+
import { DevToolProps } from '..';
8+
9+
const DevTools = (props: DevToolProps) => {
10+
const [ReactDevTools, setDevTools] = useState(null);
11+
const theme = useContext(ThemeContext);
12+
13+
useEffect(() => {
14+
const iframe = document.getElementById(
15+
'sandbox-preview'
16+
) as HTMLIFrameElement;
17+
const contentWindow = iframe.contentWindow;
18+
19+
setDevTools(initialize(contentWindow));
20+
iframe.onload = () => {
21+
contentWindow.postMessage(
22+
{
23+
type: 'activate',
24+
},
25+
'*'
26+
);
27+
};
28+
}, []);
29+
30+
if (props.hidden) {
31+
return null;
32+
}
33+
34+
const browserTheme = theme.light ? 'light' : 'dark';
35+
36+
function viewElementSourceFunction(id: string, data: any) {
37+
const { source } = data;
38+
39+
if (source) {
40+
dispatch(actions.editor.openModule(source.fileName, source.lineNumber));
41+
}
42+
}
43+
44+
return (
45+
<Container>
46+
{ReactDevTools && (
47+
<ReactDevTools
48+
viewElementSourceFunction={viewElementSourceFunction}
49+
browserTheme={browserTheme}
50+
/>
51+
)}
52+
</Container>
53+
);
54+
};
55+
56+
export default {
57+
id: 'codesandbox.react-devtools',
58+
title: 'React DevTools',
59+
Content: DevTools,
60+
actions: [],
61+
};

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { ViewConfig } from '@codesandbox/common/lib/templates/template';
99
import console from './Console';
1010
import Tabs, { ITabPosition } from './Tabs';
1111
import problems from './Problems';
12+
import reactDevTools from './React-Devtools';
1213
import terminal from './Terminal';
1314
import tests from './Tests';
1415
import { Container, Header, ContentContainer } from './elements';
@@ -77,6 +78,7 @@ const VIEWS: IViews = {
7778
[problems.id]: problems,
7879
[tests.id]: tests,
7980
[terminal.id]: terminal,
81+
[reactDevTools.id]: reactDevTools,
8082
};
8183

8284
type Props = {

packages/app/src/app/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { render } from 'react-dom';
2+
import { unstable_createSyncRoot as createSyncRoot } from 'react-dom';
33
import { ThemeProvider } from 'styled-components';
44
import { Router } from 'react-router-dom';
55
import { ApolloProvider } from 'react-apollo';
@@ -120,7 +120,8 @@ async function boot(state, signals, overmind) {
120120
});
121121

122122
try {
123-
render(
123+
const root = createSyncRoot(rootEl);
124+
root.render(
124125
<Signals.Provider value={signals}>
125126
<Store.Provider value={state}>
126127
<Provider store={state} signals={signals}>
@@ -137,8 +138,7 @@ async function boot(state, signals, overmind) {
137138
</ApolloProvider>
138139
</Provider>
139140
</Store.Provider>
140-
</Signals.Provider>,
141-
rootEl
141+
</Signals.Provider>
142142
);
143143
} catch (e) {
144144
logError(e);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
} from '@codesandbox/common/lib/types';
1313
import { NotificationStatus } from '@codesandbox/notifications/lib/state';
1414
import { generateFileFromSandbox as generatePackageJsonFromSandbox } from '@codesandbox/common/lib/templates/configuration/package-json';
15-
import { parseConfigurations } from './utils/parse-configurations';
15+
import { parseSandboxConfigurations } from '@codesandbox/common/lib/templates/configuration/parse-sandbox-configurations';
1616
import { defaultOpenedModule, mainModule } from './utils/main-module';
1717
import getItems from './utils/items';
1818
import { createOptimisticModule } from './utils/common';
@@ -146,7 +146,7 @@ export const setCurrentSandbox: Action<Sandbox> = (
146146
state.editor.currentId = sandbox.id;
147147

148148
let currentModuleShortid = state.editor.currentModuleShortid;
149-
const parsedConfigs = parseConfigurations(sandbox);
149+
const parsedConfigs = parseSandboxConfigurations(sandbox);
150150
const main = mainModule(sandbox, parsedConfigs);
151151

152152
state.editor.mainModuleShortid = main.shortid;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from '@codesandbox/common/lib/types';
1212
import { generateFileFromSandbox } from '@codesandbox/common/lib/templates/configuration/package-json';
1313
import { dirname } from 'path';
14-
import { parseConfigurations } from '../../utils/parse-configurations';
14+
import { parseSandboxConfigurations } from '@codesandbox/common/lib/templates/configuration/parse-sandbox-configurations';
1515
import {
1616
getModulePath,
1717
getDirectoryPath,
@@ -190,7 +190,7 @@ export const state: State = {
190190
const state: State = this;
191191

192192
return state.currentSandbox
193-
? parseConfigurations(state.currentSandbox)
193+
? parseSandboxConfigurations(state.currentSandbox)
194194
: null;
195195
},
196196
get mainModule() {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ export const recoverFiles: Action = ({ effects, actions, state }) => {
4949
});
5050

5151
effects.notificationToast.add({
52-
message: `We recovered ${recoveredList.length} unsaved files from a previous session`,
52+
message: `We recovered ${
53+
recoveredList.length
54+
} unsaved files from a previous session`,
5355
status: NotificationStatus.NOTICE,
5456
});
5557
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ export const createPrClicked: AsyncAction = async ({ state, effects }) => {
101101

102102
const user = state.user;
103103
const git = state.editor.currentSandbox.originalGit;
104-
const url = `https://github.com/${git.username}/${git.repo}/compare/${git.branch}...${user.username}:${pr.newBranch}?expand=1`;
104+
const url = `https://github.com/${git.username}/${git.repo}/compare/${
105+
git.branch
106+
}...${user.username}:${pr.newBranch}?expand=1`;
105107

106108
state.git.pr.prURL = url;
107109

0 commit comments

Comments
 (0)