Skip to content

Commit 1aff458

Browse files
authored
Merge pull request #136 from carlosms/issue-131
Issue 131
2 parents b7767b0 + 42414e4 commit 1aff458

File tree

6 files changed

+135
-77
lines changed

6 files changed

+135
-77
lines changed

frontend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"react-helmet": "^5.2.0",
1515
"react-scripts": "1.1.4",
1616
"react-split-pane": "^0.1.77",
17+
"react-switch": "^3.0.4",
1718
"react-table": "^6.8.2",
1819
"uast-viewer": "^0.0.4"
1920
},

frontend/src/App.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import initButtonStyles from './utils/bootstrap';
99
import Sidebar from './components/Sidebar';
1010
import QueryBox from './components/QueryBox';
1111
import TabbedResults from './components/TabbedResults';
12-
import CodeViewer from './components/CodeViewer';
1312
import api from './api';
1413
import { STATUS_LOADING, STATUS_ERROR, STATUS_SUCCESS } from './state/query';
1514
import './App.less';
@@ -66,7 +65,6 @@ class App extends Component {
6665
this.handleSetActiveResult = this.handleSetActiveResult.bind(this);
6766
this.handleReload = this.handleReload.bind(this);
6867

69-
this.showCode = this.showCode.bind(this);
7068
this.showUAST = this.showUAST.bind(this);
7169

7270
this.uniqueKey = 0;
@@ -235,14 +233,6 @@ FROM ( SELECT MONTH(committer_when) as month,
235233
this.setState({ showModal: false, modalTitle: null, modalContent: null });
236234
}
237235

238-
showCode(code) {
239-
this.setState({
240-
showModal: true,
241-
modalTitle: 'Source code',
242-
modalContent: <CodeViewer code={code} languages={this.state.languages} />
243-
});
244-
}
245-
246236
showUAST(uast) {
247237
this.setState({
248238
showModal: true,
@@ -392,8 +382,8 @@ FROM ( SELECT MONTH(committer_when) as month,
392382
handleResetHistory={this.handleResetHistory}
393383
handleSetActiveResult={this.handleSetActiveResult}
394384
handleReload={this.handleReload}
395-
showCode={this.showCode}
396385
showUAST={this.showUAST}
386+
languages={this.state.languages}
397387
/>
398388
</SplitPane>
399389
</Row>

frontend/src/components/CodeViewer.js

Lines changed: 63 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
import React, { Component } from 'react';
2+
import { Modal } from 'react-bootstrap';
23
import PropTypes from 'prop-types';
34
import SplitPane from 'react-split-pane';
45
import UASTViewer, { Editor, withUASTEditor } from 'uast-viewer';
6+
import Switch from 'react-switch';
57
import api from '../api';
68
import './CodeViewer.less';
79

8-
function EditorPane({
9-
languages,
10-
language,
11-
showUast,
12-
handleLangChange,
13-
handleShowUastChange,
14-
editorProps
15-
}) {
10+
function EditorPane({ languages, language, handleLangChange, editorProps }) {
1611
return (
1712
<div className="editor-pane">
1813
Language:{' '}
@@ -24,14 +19,6 @@ function EditorPane({
2419
</option>
2520
))}
2621
</select>
27-
<label>
28-
<input
29-
type="checkbox"
30-
checked={showUast}
31-
onChange={handleShowUastChange}
32-
disabled={!language}
33-
/>UAST
34-
</label>
3522
<Editor {...editorProps} theme="default" />
3623
</div>
3724
);
@@ -43,30 +30,24 @@ EditorPane.propTypes = {
4330
id: PropTypes.string.isRequired,
4431
name: PropTypes.string.isRequired
4532
})
46-
),
33+
).isRequired,
4734
language: PropTypes.string,
48-
showUast: PropTypes.bool,
4935
handleLangChange: PropTypes.func.isRequired,
50-
handleShowUastChange: PropTypes.func.isRequired,
5136
editorProps: PropTypes.object
5237
};
5338

5439
function EditorUASTSpitPane({
5540
languages,
5641
editorProps,
5742
uastViewerProps,
58-
showUast,
59-
handleLangChange,
60-
handleShowUastChange
43+
handleLangChange
6144
}) {
6245
return (
6346
<SplitPane split="vertical" defaultSize={250} minSize={175}>
6447
<EditorPane
6548
languages={languages}
6649
language={editorProps.languageMode}
67-
showUast={showUast}
6850
handleLangChange={handleLangChange}
69-
handleShowUastChange={handleShowUastChange}
7051
editorProps={editorProps}
7152
/>
7253
{uastViewerProps.uast ? <UASTViewer {...uastViewerProps} /> : <div />}
@@ -78,9 +59,7 @@ EditorUASTSpitPane.propTypes = {
7859
languages: EditorPane.propTypes.languages,
7960
editorProps: PropTypes.object,
8061
uastViewerProps: PropTypes.object,
81-
showUast: PropTypes.bool,
82-
handleLangChange: PropTypes.func.isRequired,
83-
handleShowUastChange: PropTypes.func.isRequired
62+
handleLangChange: PropTypes.func.isRequired
8463
};
8564

8665
const EditorWithUAST = withUASTEditor(EditorUASTSpitPane);
@@ -156,51 +135,74 @@ class CodeViewer extends Component {
156135

157136
render() {
158137
const { loading, language, showUast, uast, error } = this.state;
138+
const { showModal, onHide, code, languages } = this.props;
159139

160140
if (loading) {
161141
return 'loading';
162142
}
163143

164-
if (showUast) {
165-
return (
166-
<div className="code-viewer">
167-
<EditorWithUAST
168-
languages={this.props.languages}
169-
code={this.props.code}
170-
languageMode={language}
171-
showUast={showUast}
172-
handleLangChange={this.handleLangChange}
173-
handleShowUastChange={this.handleShowUastChange}
174-
uast={uast}
175-
/>
176-
{error ? (
177-
<div className="error">
178-
<button onClick={this.removeError} className="close">
179-
close
180-
</button>
181-
{error}
182-
</div>
183-
) : null}
184-
</div>
185-
);
186-
}
187-
188144
return (
189-
<EditorPane
190-
languages={this.props.languages}
191-
language={language}
192-
showUast={showUast}
193-
handleLangChange={this.handleLangChange}
194-
handleShowUastChange={this.handleShowUastChange}
195-
editorProps={{ code: this.props.code, languageMode: language }}
196-
/>
145+
<Modal show={showModal} onHide={onHide} bsSize="large">
146+
<Modal.Header closeButton>
147+
<Modal.Title>
148+
CODE
149+
<Switch
150+
checked={showUast}
151+
onChange={this.handleShowUastChange}
152+
disabled={!language}
153+
checkedIcon={<span className="switch-text checked">UAST</span>}
154+
uncheckedIcon={
155+
<span className="switch-text unchecked">UAST</span>
156+
}
157+
width={100}
158+
handleDiameter={20}
159+
className={`code-toggler ${showUast ? 'checked' : 'unchecked'}`}
160+
aria-label="Toggle UAST view"
161+
/>
162+
</Modal.Title>
163+
</Modal.Header>
164+
<Modal.Body>
165+
{showUast ? (
166+
<div className="code-viewer">
167+
<EditorWithUAST
168+
languages={languages}
169+
code={code}
170+
languageMode={language}
171+
showUast={showUast}
172+
handleLangChange={this.handleLangChange}
173+
handleShowUastChange={this.handleShowUastChange}
174+
uast={uast}
175+
/>
176+
{error ? (
177+
<div className="error">
178+
<button onClick={this.removeError} className="close">
179+
close
180+
</button>
181+
{error}
182+
</div>
183+
) : null}
184+
</div>
185+
) : (
186+
<EditorPane
187+
languages={languages}
188+
language={language}
189+
showUast={showUast}
190+
handleLangChange={this.handleLangChange}
191+
handleShowUastChange={this.handleShowUastChange}
192+
editorProps={{ code, languageMode: language }}
193+
/>
194+
)}
195+
</Modal.Body>
196+
</Modal>
197197
);
198198
}
199199
}
200200

201201
CodeViewer.propTypes = {
202-
code: PropTypes.string.isRequired,
203-
languages: EditorPane.propTypes.languages
202+
code: PropTypes.string,
203+
languages: EditorPane.propTypes.languages,
204+
showModal: PropTypes.bool.isRequired,
205+
onHide: PropTypes.func.isRequired
204206
};
205207

206208
export default CodeViewer;

frontend/src/components/CodeViewer.less

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@import '../variables.less';
2+
13
.code-viewer {
24
position: relative;
35
height: 100%;
@@ -21,3 +23,34 @@
2123
.editor-pane {
2224
height: 100%;
2325
}
26+
27+
.code-toggler {
28+
margin-left: 40px;
29+
30+
.switch-text {
31+
font-size: 18px;
32+
font-weight: @btn-font-weight;
33+
}
34+
35+
.switch-text.checked {
36+
color: @btn-gbpl-tertiary-color;
37+
padding-left: 28px;
38+
}
39+
40+
.switch-text.unchecked {
41+
color: #979797;
42+
margin-left: -29px;
43+
}
44+
45+
&.checked {
46+
.react-switch-bg {
47+
background: @tertiary !important;
48+
}
49+
}
50+
51+
&.unchecked {
52+
.react-switch-bg {
53+
background: #e1e1e1 !important;
54+
}
55+
}
56+
}

frontend/src/components/TabbedResults.js

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import PropTypes from 'prop-types';
44
import DivTabs from './DivTabs';
55
import ResultsTable from './ResultsTable';
66
import HistoryTable from './HistoryTable';
7+
import CodeViewer from './CodeViewer';
78
import './TabbedResults.less';
89
import PencilIcon from '../icons/edit-query-tab-name.svg';
910
import CloseIcon from '../icons/close-query-tab.svg';
@@ -104,10 +105,14 @@ class TabbedResults extends Component {
104105
super(props);
105106
this.state = {
106107
activeKey: 0,
107-
nTabs: 0
108+
nTabs: 0,
109+
codeModalShow: false,
110+
codeModalContent: null
108111
};
109112

110113
this.handleSelect = this.handleSelect.bind(this);
114+
this.showCode = this.showCode.bind(this);
115+
this.handleModalClose = this.handleModalClose.bind(this);
111116
}
112117

113118
static getDerivedStateFromProps(nextProps, prevState) {
@@ -137,8 +142,23 @@ class TabbedResults extends Component {
137142
this.props.handleSetActiveResult(activeKey);
138143
}
139144

145+
showCode(code) {
146+
this.setState({
147+
codeModalShow: true,
148+
codeModalContent: code
149+
});
150+
}
151+
152+
handleModalClose() {
153+
this.setState({
154+
codeModalShow: false,
155+
codeModalContent: null
156+
});
157+
}
158+
140159
render() {
141-
const { showCode, showUAST, history } = this.props;
160+
const { codeModalShow, codeModalContent } = this.state;
161+
const { showUAST, history, languages } = this.props;
142162

143163
return (
144164
<div className="results-padding full-height full-width">
@@ -181,7 +201,7 @@ class TabbedResults extends Component {
181201
content = (
182202
<ResultsTable
183203
response={query.response}
184-
showCode={showCode}
204+
showCode={this.showCode}
185205
showUAST={showUAST}
186206
/>
187207
);
@@ -266,6 +286,12 @@ class TabbedResults extends Component {
266286
</Tab>
267287
)}
268288
</DivTabs>
289+
<CodeViewer
290+
showModal={codeModalShow}
291+
code={codeModalContent}
292+
onHide={this.handleModalClose}
293+
languages={languages}
294+
/>
269295
</div>
270296
);
271297
}
@@ -284,8 +310,8 @@ TabbedResults.propTypes = {
284310
handleResetHistory: PropTypes.func.isRequired,
285311
handleSetActiveResult: PropTypes.func.isRequired,
286312
handleReload: PropTypes.func.isRequired,
287-
showCode: PropTypes.func.isRequired,
288-
showUAST: PropTypes.func.isRequired
313+
showUAST: PropTypes.func.isRequired,
314+
languages: CodeViewer.propTypes.languages
289315
};
290316

291317
export default TabbedResults;

frontend/yarn.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5944,6 +5944,12 @@ react-style-proptype@^3.0.0:
59445944
dependencies:
59455945
prop-types "^15.5.4"
59465946

5947+
react-switch@^3.0.4:
5948+
version "3.0.4"
5949+
resolved "https://registry.yarnpkg.com/react-switch/-/react-switch-3.0.4.tgz#52958b2736b4d21b4f0050997955c5365019ca7b"
5950+
dependencies:
5951+
prop-types "^15.6.1"
5952+
59475953
react-table@^6.8.2:
59485954
version "6.8.2"
59495955
resolved "https://registry.yarnpkg.com/react-table/-/react-table-6.8.2.tgz#3a5aefabc85953300d16786fa307c30610db9adc"

0 commit comments

Comments
 (0)