Skip to content

Commit f1e0b95

Browse files
committed
translate with youdao API
1 parent 38b47bf commit f1e0b95

10 files changed

Lines changed: 310 additions & 61 deletions

File tree

‎src/components/Loading.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}
15+
}

‎src/components/SearchBar.js‎

Lines changed: 70 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
11
import React from 'react';
2-
import {Dropdown, Icon, Input} from 'semantic-ui-react';
2+
import {Dropdown, Icon, Input, Checkbox} from 'semantic-ui-react';
3+
4+
// http://githut.info/
5+
const topProgramLan = [
6+
{id: '22,106', language: 'JavaScript, CoffeeScript'},
7+
{id: '133,135', language: 'CSS'},
8+
{id: '3,39', language: 'HTML'},
9+
{id: 137, language: 'Swift'},
10+
{id: 35, language: 'Objective-C'},
11+
{id: 23, language: 'Java'},
12+
{id: 19, language: 'Python'},
13+
{id: 24, language: 'PHP'},
14+
{id: 32, language: 'Ruby'},
15+
{id: 28, language: 'C'},
16+
{id: 16, language: 'C++'},
17+
{id: 6, language: 'C#'},
18+
{id: 55, language: 'Go'},
19+
{id: 51, language: 'Perl'},
20+
{id: '104,109', language: 'Clojure, ClojureScript'},
21+
{id: 40, language: 'Haskell'},
22+
{id: 54, language: 'Lua'},
23+
{id: 20, language: 'Matlab'},
24+
{id: 144, language: 'R'},
25+
{id: 47, language: 'Scala'},
26+
{id: '69,78,146', language: 'Shell'},
27+
{id: 29, language: 'Lisp'},
28+
{id: 42, language: 'ActionScript'}
29+
];
30+
31+
332

433
export default class SearchBar extends React.Component {
534
input = React.createRef();
@@ -8,7 +37,7 @@ export default class SearchBar extends React.Component {
837
constructor(props) {
938
super(props);
1039
this.state = {
11-
lang: null,
40+
lang: [],
1241
prevProps: props,
1342
inputSize: 'huge'
1443
}
@@ -17,7 +46,9 @@ export default class SearchBar extends React.Component {
1746

1847
static getDerivedStateFromProps(nextProps, prevState) {
1948
// avoid calculating expensive derived data
20-
if (prevState.prevProps === nextProps) { return null}
49+
if (prevState.prevProps === nextProps) {
50+
return null
51+
}
2152
let newState = {
2253
prevProps: nextProps // prevProps memoization
2354
}
@@ -40,7 +71,34 @@ export default class SearchBar extends React.Component {
4071
this.props.onSearch(this.input.current.inputRef.value, this.state.lang);
4172
}
4273

43-
handleTypeChange = (e, { value }) => this.setState({ type: value });
74+
handleRestLang = () => {
75+
this.setState({lang: []})
76+
}
77+
78+
handleSelectLang = id => {
79+
this.setState({lang: this.state.lang.concat(id)})
80+
}
81+
82+
handleDeselectLang = id => {
83+
let lang = this.state.lang;
84+
lang.splice(this.state.lang.indexOf(id), 1);
85+
this.setState({lang: lang})
86+
}
87+
88+
handleToggleSelectLang = id => {
89+
this.state.lang.indexOf(id) === -1 ? this.handleSelectLang(id) : this.handleDeselectLang(id);
90+
}
91+
92+
renderItems() {
93+
return topProgramLan.map(key => {
94+
const active = this.state.lang.indexOf(key.id) !== -1;
95+
return <Dropdown.Item key={key.id}
96+
active={active}
97+
onClick={() => this.handleToggleSelectLang(key.id)}>
98+
<Icon name={active ? 'check circle outline' : 'circle outline'}/>{key.language}
99+
</Dropdown.Item>;
100+
})
101+
}
44102

45103
render() {
46104
return (
@@ -49,30 +107,22 @@ export default class SearchBar extends React.Component {
49107
Search over GitHub, Bitbucket, GitLab to find real-world usage variable names
50108
</div>
51109
<Input ref={this.input}
52-
className={`search-bar__input${this.props.variableList.length ? ' search-bar__input--dark' :''}`}
110+
className={`search-bar__input${this.props.variableList.length ? ' search-bar__input--dark' : ''}`}
53111
icon fluid placeholder={this.props.placeholder} size={this.state.inputSize}>
54112
<Dropdown text='' icon='filter' className='search-bar__dropdown'>
55113
<Dropdown.Menu>
56-
<Dropdown.Item icon='undo' text='All 90 Languages (Reset)' />
114+
<Dropdown.Item icon='undo' text='All 90 Languages (Reset)' onClick={this.handleRestLang}/>
57115
<Dropdown.Menu scrolling>
58-
<Dropdown.Item text='Open...' description='ctrl + o' />
59-
<Dropdown.Item text='Save as...' description='ctrl + s' />
60-
<Dropdown.Item text='Rename' description='ctrl + r' />
61-
<Dropdown.Item text='Make a copy' />
62-
<Dropdown.Item icon='folder' text='Move to folder' />
63-
<Dropdown.Item icon='trash' text='Move to trash' />
64-
<Dropdown.Item icon='trash' text='Move to trash' />
65-
<Dropdown.Item icon='trash' text='Move to trash' />
66-
<Dropdown.Item icon='trash' text='Move to trash' />
67-
<Dropdown.Item icon='trash' text='Move to trash' />
68-
<Dropdown.Item icon='trash' text='Move to trash' />
69-
<Dropdown.Item icon='trash' text='Move to trash' />
116+
{this.renderItems()}
70117
</Dropdown.Menu>
71118
</Dropdown.Menu>
72119
</Dropdown>
73120
<input defaultValue={this.props.searchValue}
74-
onKeyPress={e => {e.key === 'Enter' && this.handleSearch()}}/>
75-
<Icon name='search' link onClick={() => this.handleSearch()}/>
121+
onKeyPress={e => {
122+
e.key === 'Enter' && this.handleSearch()
123+
}}/>
124+
<Icon name={this.props.variableList.length ? 'search plus' : 'search'}
125+
link onClick={() => this.handleSearch()}/>
76126
</Input>
77127
<div className='search-bar__helper'>
78128
</div>

‎src/components/SearchError.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React from 'react';
2+
import {Label} from 'semantic-ui-react';
3+
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 come back later, or try <Label>Suggestions</Label> :)</div>
13+
</div>
14+
);
15+
}
16+
}

‎src/components/VariableList.js‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import {Popup, Label, Button} from 'semantic-ui-react';
2+
import {Button, Label, Popup} from 'semantic-ui-react';
33
import * as Tools from '../utils/Tools';
44

55
const formatRepobUrl = url => {
@@ -27,9 +27,9 @@ class Variable extends React.Component {
2727
<Popup style={{padding: '0'}}
2828
position='top center'
2929
trigger={
30-
<Label circular color={this.props.color}>
31-
{this.props.keyword}
32-
</Label>}
30+
<Label circular color={this.props.color} className={this.props.className} style={this.props.style}>
31+
{this.props.keyword}
32+
</Label>}
3333
onMount={this.handlePopOnMount}
3434
onUnmount={this.handlePopUnmount}
3535
hoverable={true}>
@@ -46,20 +46,32 @@ class Variable extends React.Component {
4646
}
4747
}
4848

49-
5049
export default class VariableList extends React.Component {
5150

51+
lastPageLen = 0;
52+
5253
constructor(props) {
5354
super(props);
54-
this.state = {
55-
}
55+
this.state = {}
5656
}
5757

5858
renderPage() {
5959
let pages = [];
60-
this.props.variableList.forEach(list => {
60+
const pageLen = this.props.variableList.length;
61+
this.props.variableList.forEach((list, i) => {
62+
const isLast = i === pageLen - 1 && this.lastPageLen != pageLen;
6163
const variables = list.map(variable => {
62-
return <Variable key={Tools.uuid()} {...variable} {...this.props}/>
64+
let style = {}, className = '';
65+
if (isLast) {
66+
className = 'animated';
67+
style = {
68+
opacity: 0,
69+
animationName: 'fadeIn',
70+
animationDelay: 0.1 + Math.random() + 's',
71+
animationDuration: 0.35 + Math.random() + 's'
72+
};
73+
}
74+
return <Variable key={Tools.uuid()} {...variable} {...this.props} style={style} className={className}/>
6375
});
6476
if (variables && variables.length) {
6577
if (pages.length) {
@@ -68,6 +80,7 @@ export default class VariableList extends React.Component {
6880
Array.prototype.unshift.apply(pages, variables)
6981
}
7082
});
83+
this.lastPageLen = pageLen;
7184
return pages;
7285
}
7386

‎src/containers/MainContainer.js‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ import Title from '../components/Title';
55
import SearchcodeModel from '../models/SearchcodeModel';
66
import HashHandler from '../utils/HashHandler';
77
import VariableList from '../components/VariableList';
8+
import SearchError from '../components/SearchError';
9+
import Loading from '../components/Loading';
810

911
export default class MainContainer extends React.Component {
1012
state = {
13+
isError: false,
1114
isSearchingVariable: false,
1215
searchValue: SearchcodeModel.searchValue,
1316
searchLang: SearchcodeModel.searchLang,
@@ -37,6 +40,7 @@ export default class MainContainer extends React.Component {
3740

3841
handleSearchcodeModelUpdate = (curr, prev, mutation) => {
3942
this.setState({
43+
isError: this.checkError(curr),
4044
isSearchingVariable: !mutation.variableList,
4145
searchValue: SearchcodeModel.searchValue,
4246
searchLang: SearchcodeModel.searchLang,
@@ -51,27 +55,44 @@ export default class MainContainer extends React.Component {
5155
val = val.trim().replace(/\s+/ig, ' '); // filter spaces
5256
if (val.length < 1) { return; }
5357
if (val == this.state.searchValue) {
54-
this.requestVariable(val);
58+
this.requestVariable(val, lang);
5559
} else {
5660
HashHandler.set(val); // update window.location.hash
5761
}
5862
}
5963

60-
requestVariable = val => {
64+
checkError(data) {
65+
if (this.state.isSearchingVariable) {
66+
// no search result
67+
if (data.variableList.length < 1 || data.variableList[data.variableList.length - 1].length < 1) {
68+
return true;
69+
}
70+
}
71+
return false;
72+
}
73+
74+
requestVariable = (val, lang) => {
6175
val = decodeURIComponent(val);
6276
let page = this.state.page;
6377
if (val === this.state.searchValue) {
6478
page += 1;
6579
}
6680
this.setState({searchValue: val, isSearchingVariable: true});
67-
SearchcodeModel.requestVariable(val, page, this.state.searchLang);
81+
SearchcodeModel.requestVariable(val, page, lang || this.state.searchLang);
82+
}
83+
84+
renderDefault() {
85+
if (this.state.page > 0 || this.state.variableList.length) { return ''; }
86+
return <div className='default'><img src="images/twohardtings.jpg"/></div>;
6887
}
6988

7089
render() {
7190
return (
7291
<Container className='main'>
7392
<Title {...this.state}/>
7493
<SearchBar placeholder='AI 人工智能' {...this.state} onSearch={this.handleSearch}/>
94+
{this.state.isSearchingVariable ? <Loading/> : (this.state.isError ? <SearchError/> : '')}
95+
{this.renderDefault()}
7596
<VariableList {...this.state}/>
7697
</Container>
7798
)

0 commit comments

Comments
 (0)