Skip to content

Commit 9013f22

Browse files
committed
apply react hooks
1 parent 5d67f91 commit 9013f22

10 files changed

Lines changed: 155 additions & 282 deletions

File tree

‎src/components/Bookmark.js‎

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +0,0 @@
1-
import React from 'react';
2-
import {Modal, Button, Dropdown, Label} from 'semantic-ui-react';
3-
import * as Tools from '../utils/Tools';
4-
import Loading from "./Loading";
5-
6-
export default class Bookmark extends React.Component {
7-
code = React.createRef();
8-
mark = null;
9-
visible = false;
10-
constructor(props) {
11-
super(props);
12-
}
13-
14-
componentDidUpdate(prevProps, prevState, snapshot) {
15-
if (!this.visiable) {
16-
this.visible = true;
17-
}
18-
}
19-
20-
componentDidMount() {
21-
}
22-
23-
handleClose = () => {
24-
this.visiable = false;
25-
this.props.onCloseBookmark();
26-
}
27-
28-
29-
renderDropdownItem() {
30-
if (!this.props.sourceCodeVariable) { return null; }
31-
return this.props.sourceCodeVariable.repoList.map(repo => {
32-
return (
33-
<Dropdown.Item key={Tools.uuid()}>
34-
<Button size='mini' onClick={() => this.props.onRequestSourceCode(repo)}>Codes</Button>
35-
<Button size='mini' as='a' href={repo.repo} target='_blank'>Repo</Button>
36-
<Label size='mini' circular color={Tools.randomLabelColor()}>{repo.language}</Label>
37-
</Dropdown.Item>
38-
)
39-
});
40-
}
41-
42-
render() {
43-
const dropText = (
44-
<div>All Codes <Label size='mini' circular>
45-
</Label></div>
46-
);
47-
return (
48-
<Modal open={this.props.sourceCodeVisible} onClose={this.handleClose}
49-
centered={false} closeIcon className='source-code fix-modal' size='large'>
50-
<Modal.Header>
51-
<Dropdown floating labeled button blurring className='mini icon' style={{padding: '0.35rem 0'}}
52-
text={dropText}>
53-
<Dropdown.Menu>
54-
<Dropdown.Menu scrolling className='fix-dropdown-menu'>
55-
{this.renderDropdownItem()}
56-
</Dropdown.Menu>
57-
</Dropdown.Menu>
58-
</Dropdown>
59-
<Button size='mini' as='a' href={this.props.sourceCodeRepo.repo} target='_blank'>Repo</Button>
60-
</Modal.Header>
61-
<Modal.Content>
62-
{this.props.requestingSourceCode ? <Loading/> : ''}
63-
<pre><code className='prettyprint linenums' ref={this.code}>{this.props.sourceCode}</code></pre>
64-
</Modal.Content>
65-
</Modal>
66-
)
67-
}
68-
}

‎src/components/Donate.js‎

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,21 @@
1-
import React from 'react';
2-
3-
export default class Donate extends React.Component {
4-
5-
constructor(props) {
6-
super(props);
1+
export default function Donate (props) {
2+
let text = <h4 className='lang'>Buy <a href='https://twitter.com/unbug' rel='noopener noreferrer' target='_blank'>@unbug</a> a drink</h4>;
3+
if (props.isZH) {
4+
text = <h4 className='lang cn'>给作者 <a href='https://weibo.com/unbug/' rel='noopener noreferrer' target='_blank'>@听奏</a> 打赏个小红包吧</h4>;
75
}
8-
9-
render() {
10-
let text = <h4 className='lang'>Buy <a href='https://twitter.com/unbug' rel='noopener noreferrer' target='_blank'>@unbug</a> a drink</h4>;
11-
if (this.props.isZH) {
12-
text = <h4 className='lang cn'>赞助 <a href='https://weibo.com/unbug/' rel='noopener noreferrer' target='_blank'>@听奏</a> 这个月的房租</h4>;
13-
}
14-
return (
15-
<div className='donate'>
16-
<div className='hd'>{text}</div>
17-
<div className='bd'>
18-
<img src='images/wechatpay.jpg' className='wechatpay' title='微信付款'/>
19-
<img src='images/zhifubao.png' className='zhifubao' title='支付宝转账'/>
20-
<form action='https://www.paypal.com/cgi-bin/webscr' method='post' target='_top'>
21-
<input type='hidden' name='cmd' value='_s-xclick'/>
22-
<input type='hidden' name='hosted_button_id' value='43H7K8PWR4VV4'/>
23-
<input type='image' className='paypal' src='images/paypal.png' border='0' name='submit'
24-
title='Paypal'/>
25-
</form>
26-
</div>
6+
return (
7+
<div className='donate'>
8+
<div className='hd'>{text}</div>
9+
<div className='bd'>
10+
<img src='images/wechatpay.jpg' className='wechatpay' title='微信付款'/>
11+
<img src='images/zhifubao.png' className='zhifubao' title='支付宝转账'/>
12+
<form action='https://www.paypal.com/cgi-bin/webscr' method='post' target='_top'>
13+
<input type='hidden' name='cmd' value='_s-xclick'/>
14+
<input type='hidden' name='hosted_button_id' value='43H7K8PWR4VV4'/>
15+
<input type='image' className='paypal' src='images/paypal.png' border='0' name='submit'
16+
title='Paypal'/>
17+
</form>
2718
</div>
28-
)
29-
}
19+
</div>
20+
)
3021
}

‎src/components/Loading.js‎

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
1-
import React from 'react';
2-
3-
export default class Loading extends React.Component {
4-
constructor(props) {
5-
super(props);
6-
}
7-
8-
render() {
9-
return (
10-
<div className='loading'>
11-
<div className='logo'><img className='spinner' src='images/codelf_logo.png'/></div>
12-
</div>
13-
)
14-
}
1+
export default function Loading() {
2+
return (
3+
<div className='loading'>
4+
<div className='logo'><img className='spinner' src='images/codelf_logo.png'/></div>
5+
</div>
6+
)
157
}

‎src/components/NavBar.js‎

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,31 @@
1-
import React from 'react';
21
import {Icon, Popup} from 'semantic-ui-react';
32

4-
export default class NavBar extends React.Component {
5-
6-
constructor(props) {
7-
super(props);
8-
}
9-
10-
render() {
11-
return (
12-
<div className='nav-bar'>
13-
<Popup
14-
hoverable={true}
15-
trigger={
16-
<div className='copybook-btn animated fadeInDown' onClick={this.props.onOpenCopybook}>
17-
<Icon name='clipboard'/>
18-
</div>
19-
}>
20-
<Icon name='thumbs up outline'/> Daily algorithm copybook, learn algorithm easily!
21-
</Popup>
22-
<Popup
23-
hoverable={true}
24-
trigger={
25-
<div className='bookmark-btn animated fadeInDown'>
26-
<Icon name='bookmark'/>
27-
</div>
28-
}>
29-
Sorry, GitHub stars organize tool currently is not available, <a href="https://github.com/unbug/codelf/projects/2" target='_blank' rel='noopener noreferrer'>new version</a> is coming soon :)
30-
</Popup>
31-
<a href='https://github.com/unbug/codelf' className='github-corner animated fadeInDown'
32-
title='Star me on GitHub'
33-
target='_blank' rel='noopener noreferrer'>
34-
<Icon name='github square'/>
35-
</a>
36-
</div>
37-
)
38-
}
3+
export default function NavBar (props) {
4+
return (
5+
<div className='nav-bar'>
6+
<Popup
7+
hoverable={true}
8+
trigger={
9+
<div className='copybook-btn animated fadeInDown' onClick={props.onOpenCopybook}>
10+
<Icon name='clipboard'/>
11+
</div>
12+
}>
13+
<Icon name='thumbs up outline'/> Daily algorithm copybook, learn algorithm easily!
14+
</Popup>
15+
<Popup
16+
hoverable={true}
17+
trigger={
18+
<div className='bookmark-btn animated fadeInDown'>
19+
<Icon name='bookmark'/>
20+
</div>
21+
}>
22+
Sorry, GitHub stars organize tool currently is not available, <a href="https://github.com/unbug/codelf/projects/2" target='_blank' rel='noopener noreferrer'>new version</a> is coming soon :)
23+
</Popup>
24+
<a href='https://github.com/unbug/codelf' className='github-corner animated fadeInDown'
25+
title='Star me on GitHub'
26+
target='_blank' rel='noopener noreferrer'>
27+
<Icon name='github square'/>
28+
</a>
29+
</div>
30+
)
3931
}

‎src/components/NoticeLinks.js‎

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,52 @@
1-
import React from 'react';
1+
import { useEffect, useRef } from 'react';
22
import {Icon} from 'semantic-ui-react';
33

4-
export default class NoticeLinks extends React.Component {
4+
export default function NoticeLinks() {
5+
const listEl = useRef(null);
6+
useSliderEffect(listEl);
57

6-
list = React.createRef();
7-
active = 1;
8-
constructor(props) {
9-
super(props);
10-
}
8+
return (
9+
<div className='notice-links' ref={listEl}>
10+
<a className='animated fadeIn show' target='_blank' rel='noopener noreferrer'
11+
href='https://github.com/unbug/snts'>
12+
<Icon name='heartbeat'/> SAY NO TO SUICIDE PUBLIC LICENSE
13+
</a>
14+
<a className='animated fadeIn' target='_blank' rel='noopener noreferrer' href='//mihtool.com/'>
15+
<Icon name='code'/> [MIHTool] iOS 上调试和优化页面的工具
16+
</a>
17+
<a className='animated fadeIn' target='_blank' rel='noopener noreferrer' href='https://www.wasmrocks.com/'>
18+
<Icon name='hand rock'/> WebAssembly Rocks
19+
</a>
20+
<a className='animated fadeIn' target='_blank' rel='noopener noreferrer'
21+
href='https://github.com/unbug/react-native-train/blob/master/README.md'>
22+
<Icon name='video'/> [开源] React Native 开发培训资料和视频
23+
</a>
24+
<a className='animated fadeIn' target='_blank' rel='noopener noreferrer'
25+
href='https://www.zhihu.com/question/27097399/answer/78619944'>
26+
[知乎] 编程的时候命名方法或变量词穷了怎么办?
27+
</a>
28+
</div>
29+
)
30+
}
1131

12-
componentDidMount() {
13-
setInterval(() => this.renderItem(this.list.current.children), 10000);
14-
}
32+
function useSliderEffect(el) {
33+
useEffect(() => {
34+
const timer = setInterval(() => renderItem(el.current.children), 10000);
35+
return () => clearInterval(timer);
36+
}, []); // pass [] to run an effect and clean it up only once
1537

16-
renderItem(list) {
38+
let active = 1;
39+
function renderItem(list) {
1740
Array.prototype.forEach.call(list, (item, i) => {
18-
if (i === this.active) {
41+
if (i === active) {
1942
item.classList.add('show');
2043
} else {
2144
item.classList.remove('show');
2245
}
2346
});
24-
this.active++;
25-
if (this.active >= list.length) {
26-
this.active = 0;
47+
active++;
48+
if (active >= list.length) {
49+
active = 0;
2750
}
2851
}
29-
30-
render() {
31-
return (
32-
<div className='notice-links' ref={this.list}>
33-
<a className='animated fadeIn show' target='_blank' rel='noopener noreferrer'
34-
href='https://github.com/unbug/snts'>
35-
<Icon name='heartbeat'/> SAY NO TO SUICIDE PUBLIC LICENSE
36-
</a>
37-
<a className='animated fadeIn' target='_blank' rel='noopener noreferrer' href='//mihtool.com/'>
38-
<Icon name='code'/> [MIHTool] iOS 上调试和优化页面的工具
39-
</a>
40-
<a className='animated fadeIn' target='_blank' rel='noopener noreferrer' href='https://www.wasmrocks.com/'>
41-
<Icon name='hand rock'/> WebAssembly Rocks
42-
</a>
43-
<a className='animated fadeIn' target='_blank' rel='noopener noreferrer'
44-
href='https://github.com/unbug/react-native-train/blob/master/README.md'>
45-
<Icon name='video'/> [开源] React Native 开发培训资料和视频
46-
</a>
47-
<a className='animated fadeIn' target='_blank' rel='noopener noreferrer'
48-
href='https://www.zhihu.com/question/27097399/answer/78619944'>
49-
[知乎] 编程的时候命名方法或变量词穷了怎么办?
50-
</a>
51-
</div>
52-
)
53-
}
5452
}

‎src/components/SearchError.js‎

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
1-
import React from 'react';
21
import {Label} from 'semantic-ui-react';
32

4-
export default class SearchError extends React.Component {
5-
constructor(props) {
6-
super(props);
7-
}
8-
9-
render() {
10-
return (
11-
<div className='search-error'>
12-
<div>Nothing found, please try <Label color='grey' size='mini'>Quick Search</Label> or come back later :)</div>
13-
<div>You can also get help from <a href='https://github.com/unbug/codelf/issues'
14-
target='_blank'
15-
rel="noopener noreferrer" >https://github.com/unbug/codelf/issues</a>.
16-
</div>
3+
export default function SearchError() {
4+
return (
5+
<div className='search-error'>
6+
<div>Nothing found, please try <Label color='grey' size='mini'>Quick Search</Label> or come back later :)</div>
7+
<div>You can also get help from <a href='https://github.com/unbug/codelf/issues'
8+
target='_blank'
9+
rel="noopener noreferrer" >https://github.com/unbug/codelf/issues</a>.
1710
</div>
18-
);
19-
}
11+
</div>
12+
);
2013
}

‎src/components/Suggestion.js‎

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
import React from 'react';
22
import {Label} from 'semantic-ui-react';
33

4-
export default class Suggestion extends React.Component {
5-
6-
constructor(props) {
7-
super(props);
8-
}
9-
10-
render() {
11-
if (!this.props.suggestion || !this.props.suggestion.length) { return null; }
12-
const list = this.props.suggestion.map((item, i) => {
13-
return <Label key={i} circular size='mini' color='grey' as='a' href={`#${item}`}>{item}</Label>
14-
});
15-
return (
16-
<div className='suggestion'>
17-
<Label color='grey' size='mini'>Quick Search:</Label> {list}
18-
</div>
19-
)
20-
}
4+
export default function Suggestion(props) {
5+
if (!props.suggestion || !props.suggestion.length) { return null; }
6+
const list = props.suggestion.map((item, i) => {
7+
return <Label key={i} circular size='mini' color='grey' as='a' href={`#${item}`}>{item}</Label>
8+
});
9+
return (
10+
<div className='suggestion'>
11+
<Label color='grey' size='mini'>Quick Search:</Label> {list}
12+
</div>
13+
)
2114
}

‎src/components/TitleLogo.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default function Title () {
1+
export default function TitleLogo () {
22
return (
33
<header className='title animated'>
44
<h1><a href="./"><span>C</span><span>O</span><span>D</span><span>E</span><span>L</span><span>F</span></a></h1>

0 commit comments

Comments
 (0)