Skip to content

Commit 924aaaf

Browse files
committed
refactor with react hooks
1 parent 31df2da commit 924aaaf

18 files changed

Lines changed: 313 additions & 309 deletions

‎src/App.js‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
import 'whatwg-fetch';
22
import ReactDOM from 'react-dom';
33
import MainContainer from './containers/MainContainer';
4+
import CopybookContainer from './containers/CopybookContainer';
5+
import NoticeContainer from './containers/NoticeContainer';
6+
import NavBarContainer from './containers/NavBarContainer';
47

5-
ReactDOM.render(
6-
<MainContainer/>,
7-
document.querySelector('.app')
8-
);
8+
function App() {
9+
return (
10+
<>
11+
<NavBarContainer/>
12+
<MainContainer/>
13+
<CopybookContainer/>
14+
<NoticeContainer/>
15+
</>
16+
);
17+
}
18+
19+
ReactDOM.render(<App/>, document.querySelector('.app'));

‎src/components/Copybook.js‎

Lines changed: 54 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,44 @@
1-
import React from 'react';
1+
import {useEffect, useRef} from 'react';
22
import {Button, Dropdown, Modal} from 'semantic-ui-react';
33
import Loading from "./Loading";
44

5-
export default class Copybook extends React.Component {
6-
code = React.createRef();
7-
editor = React.createRef();
8-
visible = false;
5+
export default function Copybook(props) {
6+
const codeEl = useRef(null);
7+
const editorEl = useRef(null);
98

10-
constructor(props) {
11-
super(props);
12-
}
9+
useEffect(() => {
10+
renderPrettyPrint();
11+
}, [props.copybookFileContent, props.copybookVisible]);
1312

14-
componentDidUpdate(prevProps, prevState, snapshot) {
15-
if (prevProps.copybookFileContent != this.props.copybookFileContent
16-
|| (!this.visiable && this.props.copybookVisible)) {
17-
this.renderPrettyPrint();
18-
this.visible = true;
19-
}
13+
function handleClose() {
14+
props.onCloseCopybook();
2015
}
2116

22-
componentDidMount() {
23-
this.renderPrettyPrint();
24-
}
25-
26-
handleClose = () => {
27-
this.visiable = false;
28-
this.props.onCloseCopybook();
29-
}
30-
31-
handleDropdownChange = (e, { searchQuery, value }) => {
32-
if (value != this.props.copybookSelectedFile.path) {
33-
this.props.onRequestCopybookFile(
34-
this.props.copybookFileList.find(f => f.path === value)
17+
function handleDropdownChange(e, { searchQuery, value }) {
18+
if (value != props.copybookSelectedFile.path) {
19+
props.onRequestCopybookFile(
20+
props.copybookFileList.find(f => f.path === value)
3521
);
3622
}
3723
}
3824

39-
renderPrettyPrint = () => {
25+
function renderPrettyPrint() {
4026
setTimeout(() => {
41-
if (this.editor.current) {
42-
this.editor.current.innerHTML = '';
27+
if (editorEl.current) {
28+
editorEl.current.innerHTML = '';
4329
}
44-
if (this.code.current) {
45-
this.code.current.classList.remove('prettyprinted');
30+
if (codeEl.current) {
31+
codeEl.current.classList.remove('prettyprinted');
4632
setTimeout(() => PR.prettyPrint(), 100);
4733
}
48-
}, this.code.current ? 0 : 1000);
34+
}, codeEl.current ? 0 : 1000);
4935
}
5036

51-
renderDropdownItem() {
52-
if (!this.props.copybookFileList) {
37+
function renderDropdownItem() {
38+
if (!props.copybookFileList) {
5339
return null;
5440
}
55-
return this.props.copybookFileList.map((file, idx) => {
41+
return props.copybookFileList.map((file, idx) => {
5642
return {
5743
key: file.path,
5844
value: file.path,
@@ -61,47 +47,43 @@ export default class Copybook extends React.Component {
6147
});
6248
}
6349

64-
render() {
65-
if (!this.props.copybookVisible
66-
|| !this.props.copybookFileList
67-
|| !this.props.copybookFileContent) {
68-
return (
69-
<Modal open={this.props.copybookVisible} onClose={this.handleClose}
70-
centered={false} closeIcon className='copybook fix-modal' size='large'>
71-
<Modal.Header>
72-
<div className='copybook__title'>Daily Algorithm Copybook</div>
73-
</Modal.Header>
74-
<Modal.Content>
75-
<Loading/>
76-
<pre><code className='prettyprint' ref={this.code}></code></pre>
77-
</Modal.Content>
78-
</Modal>
79-
);
80-
}
81-
const copybookSelectedFile = this.props.copybookSelectedFile;
50+
if (!props.copybookVisible || !props.copybookFileList || !props.copybookFileContent) {
8251
return (
83-
<Modal open={this.props.copybookVisible} onClose={this.handleClose}
84-
centered={false} closeIcon className='copybook fix-modal' size='large'>
52+
<Modal open={props.copybookVisible} onClose={handleClose}
53+
centered={false} closeIcon className={props.className} size='large'>
8554
<Modal.Header>
86-
<div className='copybook__title'>Daily Algorithm Copybook</div>
87-
<Button size='tiny' as='a' basic
88-
href={this.props.copybookSelectedFile.link}
89-
target='_blank'>View In GitHub</Button>
90-
<Dropdown
91-
search
92-
selection
93-
onChange={this.handleDropdownChange}
94-
value={copybookSelectedFile.path}
95-
options={this.renderDropdownItem()}/>
55+
<div className='title'>Daily Algorithm Copybook</div>
9656
</Modal.Header>
9757
<Modal.Content>
98-
{this.props.requestingCopybook ? <Loading/> : ''}
99-
<pre>
100-
<code className='prettyprint' ref={this.code}>{this.props.copybookFileContent.content}</code>
101-
<div className='copybook__editor' contentEditable={true} ref={this.editor}></div>
102-
</pre>
58+
<Loading/>
59+
<pre><code className='prettyprint' ref={codeEl}></code></pre>
10360
</Modal.Content>
10461
</Modal>
105-
)
62+
);
10663
}
64+
65+
return (
66+
<Modal open={props.copybookVisible} onClose={handleClose}
67+
centered={false} closeIcon className={props.className} size='large'>
68+
<Modal.Header>
69+
<div className='title'>Daily Algorithm Copybook</div>
70+
<Button size='tiny' as='a' basic
71+
href={props.copybookSelectedFile.link}
72+
target='_blank'>View In GitHub</Button>
73+
<Dropdown
74+
search
75+
selection
76+
onChange={handleDropdownChange}
77+
value={props.copybookSelectedFile.path}
78+
options={renderDropdownItem()}/>
79+
</Modal.Header>
80+
<Modal.Content>
81+
{props.copybookRequesting ? <Loading/> : ''}
82+
<pre>
83+
<code className='prettyprint' ref={codeEl}>{props.copybookFileContent.content}</code>
84+
<div className='editor' contentEditable={true} ref={editorEl}></div>
85+
</pre>
86+
</Modal.Content>
87+
</Modal>
88+
)
10789
}

‎src/components/NavBar.js‎

Lines changed: 0 additions & 31 deletions
This file was deleted.

‎src/components/SourceCode.js‎

Lines changed: 52 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,74 @@
1-
import React from 'react';
1+
import {useEffect} from 'react';
22
import {Modal, Button, Dropdown, Label} from 'semantic-ui-react';
33
import * as Tools from '../utils/Tools';
44
import Loading from "./Loading";
55

6-
export default class SourceCode extends React.Component {
7-
code = React.createRef();
8-
mark = null;
9-
visible = false;
10-
constructor(props) {
11-
super(props);
12-
}
6+
export default function SourceCode(props) {
7+
const codeEl = React.createRef();
8+
let mark = null;
139

14-
componentDidUpdate(prevProps, prevState, snapshot) {
15-
if (prevProps.sourceCode != this.props.sourceCode || (!this.visiable && this.props.sourceCodeVisible)) {
16-
this.renderPrettyPrint();
17-
this.visible = true;
18-
}
19-
}
10+
useEffect(() => {
11+
renderPrettyPrint();
12+
}, [props.sourceCode, props.sourceCodeVisible]);
2013

21-
componentDidMount() {
22-
this.renderPrettyPrint();
14+
function handleClose() {
15+
props.onCloseSourceCode();
2316
}
2417

25-
handleClose = () => {
26-
this.visiable = false;
27-
this.props.onCloseSourceCode();
28-
}
29-
renderPrettyPrint = () => {
18+
function renderPrettyPrint() {
3019
setTimeout(() => {
31-
if (this.code.current) {
32-
this.code.current.classList.remove('prettyprinted');
20+
if (codeEl.current) {
21+
codeEl.current.classList.remove('prettyprinted');
3322
setTimeout(() => PR.prettyPrint(
34-
() => setTimeout(() =>this.renderHighLight(), 1000)
23+
() => setTimeout(() =>renderHighLight(), 1000)
3524
), 100);
3625
}
37-
}, this.code.current ? 0 : 1000);
26+
}, codeEl.current ? 0 : 1000);
3827
}
3928

40-
renderHighLight = () => {
41-
if (this.mark) {this.mark.unmark()}
42-
this.mark = new Mark(this.code.current);
29+
function renderHighLight() {
30+
if (mark) {mark.unmark()}
31+
mark = new Mark(codeEl.current);
4332
let idx = 0;
44-
this.mark.mark(this.props.sourceCodeVariable.keyword, {each: el => {
33+
mark.mark(props.sourceCodeVariable.keyword, {each: el => {
4534
el.setAttribute('tabindex',idx++);
4635
}});
4736
}
4837

49-
renderDropdownItem() {
50-
if (!this.props.sourceCodeVariable) { return null; }
51-
return this.props.sourceCodeVariable.repoList.map(repo => {
52-
return (
53-
<Dropdown.Item key={Tools.uuid()}>
54-
<Button size='mini' onClick={() => this.props.onRequestSourceCode(repo)}>Codes</Button>
55-
<Button size='mini' as='a' href={repo.repo} target='_blank'>Repo</Button>
56-
<Label size='mini' circular color={Tools.randomLabelColor()}>{repo.language}</Label>
57-
</Dropdown.Item>
58-
)
59-
});
60-
}
61-
62-
render() {
63-
if (!this.props.sourceCodeVariable || !this.props.sourceCodeRepo) { return null; }
64-
const sourceCodeVariable = this.props.sourceCodeVariable;
65-
const dropText = (
66-
<div>All Codes <Label size='mini' circular color={sourceCodeVariable.color}>
67-
{sourceCodeVariable.repoList.length}
68-
</Label></div>
69-
);
38+
if (!props.sourceCodeVariable || !props.sourceCodeRepo) { return null; }
39+
const sourceCodeVariable = props.sourceCodeVariable;
40+
const dropText = (
41+
<div>All Codes <Label size='mini' circular color={sourceCodeVariable.color}>
42+
{sourceCodeVariable.repoList.length}
43+
</Label></div>
44+
);
45+
const dropdownItems = props.sourceCodeVariable && props.sourceCodeVariable.repoList.map(repo => {
7046
return (
71-
<Modal open={this.props.sourceCodeVisible} onClose={this.handleClose}
72-
centered={false} closeIcon className='source-code fix-modal' size='large'>
73-
<Modal.Header>
74-
<Dropdown floating labeled button blurring className='mini icon' style={{padding: '0.35rem 0'}}
75-
text={dropText}>
76-
<Dropdown.Menu>
77-
<Dropdown.Menu scrolling className='fix-dropdown-menu'>
78-
{this.renderDropdownItem()}
79-
</Dropdown.Menu>
80-
</Dropdown.Menu>
81-
</Dropdown>
82-
<Button size='mini' as='a' href={this.props.sourceCodeRepo.repo} target='_blank'>Repo</Button>
83-
</Modal.Header>
84-
<Modal.Content>
85-
{this.props.requestingSourceCode ? <Loading/> : ''}
86-
<pre><code className='prettyprint linenums' ref={this.code}>{this.props.sourceCode}</code></pre>
87-
</Modal.Content>
88-
</Modal>
47+
<Dropdown.Item key={Tools.uuid()}>
48+
<Button size='mini' onClick={() => props.onRequestSourceCode(repo)}>Codes</Button>
49+
<Button size='mini' as='a' href={repo.repo} target='_blank'>Repo</Button>
50+
<Label size='mini' circular color={Tools.randomLabelColor()}>{repo.language}</Label>
51+
</Dropdown.Item>
8952
)
90-
}
53+
});
54+
return (
55+
<Modal open={props.sourceCodeVisible} onClose={handleClose}
56+
centered={false} closeIcon className='source-code fix-modal' size='large'>
57+
<Modal.Header>
58+
<Dropdown floating labeled button blurring className='mini icon' style={{padding: '0.35rem 0'}}
59+
text={dropText}>
60+
<Dropdown.Menu>
61+
<Dropdown.Menu scrolling className='fix-dropdown-menu'>
62+
{dropdownItems}
63+
</Dropdown.Menu>
64+
</Dropdown.Menu>
65+
</Dropdown>
66+
<Button size='mini' as='a' href={props.sourceCodeRepo.repo} target='_blank'>Repo</Button>
67+
</Modal.Header>
68+
<Modal.Content>
69+
{props.sourceCodeRequesting ? <Loading/> : ''}
70+
<pre><code className='prettyprint linenums' ref={codeEl}>{props.sourceCode}</code></pre>
71+
</Modal.Content>
72+
</Modal>
73+
)
9174
}

‎src/components/VariableList.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ let lastPageLen = 0;
99

1010
export default function VariableList(props) {
1111
const variableList = props.variableList;
12+
lastPageLen = variableList.length < variableList ? 0 : variableList;
1213
const list = useMemo(() => { // same as "shouldComponentUpdate", only compute when "variableList" has changed
1314
const pageLen = variableList.length;
1415
let pages = [];

0 commit comments

Comments
 (0)