Skip to content

Commit 82a2efb

Browse files
committed
minor update & cache data in session storage
1 parent 5e97f2c commit 82a2efb

9 files changed

Lines changed: 87 additions & 19 deletions

File tree

‎src/components/SearchBar.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export default class SearchBar extends React.Component {
6666
}
6767

6868
handleSearch = () => {
69+
document.body.focus();
6970
this.props.onSearch(this.input.current.inputRef.value, this.state.lang);
7071
}
7172

@@ -105,7 +106,7 @@ export default class SearchBar extends React.Component {
105106
Search over GitHub, Bitbucket, GitLab to find real-world usage variable names
106107
</div>
107108
<Input ref={this.input}
108-
className={`search-bar__input${this.props.variableList.length ? ' search-bar__input--dark' : ''}`}
109+
className='search-bar__input'
109110
icon fluid placeholder={this.props.placeholder} size={this.state.inputSize}>
110111
<Dropdown floating text='' icon='filter' className='search-bar__dropdown'>
111112
<Dropdown.Menu>
@@ -123,7 +124,7 @@ export default class SearchBar extends React.Component {
123124
link onClick={() => this.handleSearch()}/>
124125
</Input>
125126
<div className='search-bar__plugins'>
126-
<Icon name='plug'/>For:&nbsp;
127+
Extensions:&nbsp;
127128
<a href='https://github.com/unbug/codelf#codelf-for-vs-code'
128129
target='_blank' rel='noopener noreferrer'>VS Code</a>,&nbsp;
129130
<a className='text-muted' href='https://atom.io/packages/codelf'

‎src/components/SourceCode.js‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export default class SourceCode extends React.Component {
4747
}
4848

4949
renderDropdownItem() {
50+
if (!this.props.sourceCodeVariable) { return null; }
5051
return this.props.sourceCodeVariable.repoList.map(repo => {
5152
return (
5253
<Dropdown.Item key={Tools.uuid()}>
@@ -59,14 +60,19 @@ export default class SourceCode extends React.Component {
5960
}
6061

6162
render() {
62-
if (!this.props.sourceCodeVariable) { return null; }
63+
if (!this.props.sourceCodeVariable || !this.props.sourceCodeRepo) { return null; }
6364
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+
);
6470
return (
6571
<Modal open={this.props.sourceCodeVisible} onClose={this.handleClose}
6672
centered={false} closeIcon className='source-code fix-modal'>
6773
<Modal.Header>
68-
<Dropdown floating labeled button blurring className='mini icon'
69-
text={`Matched results (${sourceCodeVariable.repoList.length})`}>
74+
<Dropdown floating labeled button blurring className='mini icon' style={{padding: '0.35rem 0'}}
75+
text={dropText}>
7076
<Dropdown.Menu>
7177
<Dropdown.Menu scrolling className='fix-dropdown-menu'>
7278
{this.renderDropdownItem()}

‎src/components/VariableList.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Variable extends React.Component {
3535
<Button compact as='a' href={variable.repoLink} target='_blank'>Repo</Button>
3636
<Button compact data-clipboard-text={variable.keyword} id={this.clipboardId}>Copy</Button>
3737
<Button compact onClick={() => this.props.onOpenSourceCode(variable)}>
38-
Codes <Label size='mini' circular color={variable.color}>{variable.repoList.length}</Label>
38+
[{variable.repoLang}] Codes <Label size='mini' circular color={variable.color}>{variable.repoList.length}</Label>
3939
</Button>
4040
</Button.Group>
4141
</Popup>

‎src/containers/MainContainer.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@ export default class MainContainer extends React.Component {
7272
DDMSModel.postKeyWords(val);
7373
}
7474

75-
requestSourceCode(repo) {
75+
requestSourceCode = repo => {
7676
this.setState({
7777
sourceCodeVisible: true,
7878
requestingSourceCode: true,
7979
sourceCodeRepo: repo
8080
});
8181
SearchCodeModel.requestSourceCode(repo.id);
82-
AppModel.analytics('vc&q=' + this.sourceCodeVariable.keyword);
82+
AppModel.analytics('vc&q=' + this.state.sourceCodeVariable.keyword);
8383
}
8484

8585
handleLocationHashChanged = e => {
@@ -123,7 +123,7 @@ export default class MainContainer extends React.Component {
123123

124124
handleOpenSourceCode = variable => {
125125
this.setState({sourceCodeVariable: variable});
126-
this.requestSourceCode(variable.repoList[0]);
126+
setTimeout(() => this.requestSourceCode(variable.repoList[0]), 0);
127127
}
128128

129129
handleCloseSourceCode = () => {

‎src/models/AppModel.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ class AppModel extends BaseModel {
2828
}, param ? 500 : 1000);
2929
}
3030

31+
genPersistenceKey(key) {
32+
if (key !== undefined && key !== null) {
33+
return `${this._data.appName}_${key}`;
34+
}
35+
return null;
36+
}
37+
3138
get debug() {
3239
return this._data.debug;
3340
}

‎src/models/SearchCodeModel.js‎

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import * as Tools from '../utils/Tools';
33
import YoudaoTranslateData from './metadata/YoudaoTranslateData';
44
import JSONP from '../utils/JSONP';
55
import Store from './Store';
6+
import AppModel from './AppModel';
7+
import md5 from 'md5';
68

79
class SearchCodeModel extends BaseModel {
810
constructor() {
@@ -17,7 +19,14 @@ class SearchCodeModel extends BaseModel {
1719
sourceCode: null
1820
};
1921
this._variableRepoMapping = {};
20-
this._sourceCodeStore = new Store(Infinity);
22+
this._sourceCodeStore = new Store(Infinity, {
23+
persistence: 'session',
24+
persistenceKey: AppModel.genPersistenceKey('source_code_key')
25+
});
26+
this._variableListStore = new Store(Infinity, {
27+
persistence: 'session',
28+
persistenceKey: AppModel.genPersistenceKey('variable_list_key')
29+
});
2130
}
2231

2332
//search code by query
@@ -34,24 +43,42 @@ class SearchCodeModel extends BaseModel {
3443
if (isZH) {
3544
// translate by youdao
3645
const translate = await YoudaoTranslateData.request(val);
37-
q = translate.translation;
38-
suggestion = this._parseSuggestion(translate.suggestion, suggestion);
39-
suggestion = this._parseSuggestion(q.split(' '), suggestion);
46+
if (translate) {
47+
q = translate.translation;
48+
suggestion = this._parseSuggestion(translate.suggestion, suggestion);
49+
suggestion = this._parseSuggestion(q.split(' '), suggestion);
50+
} else {
51+
this.update({
52+
searchValue: val,
53+
page: page,
54+
variableList: [...this.variableList, []],
55+
searchLang: lang,
56+
suggestion: suggestion,
57+
isZH: isZH || this.isZH
58+
});
59+
}
60+
}
61+
const cacheId = md5(q + page);
62+
const cache = this._variableListStore.get(cacheId);
63+
if (cache) {
64+
this.update(cache);
65+
return;
4066
}
4167
// multiple val separate with '+'
4268
// const url = `//searchcode.com/api/codesearch_I/?q=${q.replace(' ', '+')}&p=${page}&per_page=42${lang.length ? ('&lan=' + lang.join(',')) : ''}`;
4369
const url = `//searchcode.com/api/jsonp_codesearch_I/?callback=?&q=${q.replace(' ', '+')}&p=${page}&per_page=42${lang.length ? ('&lan=' + lang.join(',')) : ''}`;
4470
val && JSONP(url)
45-
// .then(res => res.json())
4671
.then(data => {
47-
this.update({
72+
const cdata = {
4873
searchValue: val,
4974
page: page,
5075
variableList: [...this._data.variableList, this._parseVariableList(data.results, q)],
5176
searchLang: lang,
5277
suggestion: suggestion,
5378
isZH: isZH || this.isZH
54-
});
79+
};
80+
this.update(cdata);
81+
this._variableListStore.save(cacheId, cdata);
5582
}).catch(err => {
5683
this.update({
5784
searchValue: val,
@@ -120,6 +147,7 @@ class SearchCodeModel extends BaseModel {
120147
variables.push({
121148
keyword: val,
122149
repoLink: res.repo,
150+
repoLang: res.language,
123151
color: Tools.randomLabelColor()
124152
});
125153
}

‎src/models/Store.js‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import LocalStorage, {SessionStorage} from '../utils/LocalStorage';
2+
import md5 from 'md5';
13
/**
24
* Store data in memory cache.
35
*/
@@ -7,9 +9,19 @@ export default class Store {
79
*
810
* @param expire expire time in seconds for each record, Infinity value will never expire. default (60 * 60 * 1000)s.
911
*/
10-
constructor(expire) {
12+
constructor(expire, options) {
1113
this._expire = expire || (60 * 60 * 1000);
1214
this._cache = {};
15+
this._options = options || {};
16+
if (this._options.persistence === 'local') {
17+
this._local = LocalStorage;
18+
}
19+
if (this._options.persistence === 'session') {
20+
this._local = SessionStorage;
21+
}
22+
if (this._local && this._options.persistenceKey) {
23+
this._cache = this._local.getItem(this._options.persistenceKey) || {};
24+
}
1325
}
1426

1527
/**
@@ -21,6 +33,7 @@ export default class Store {
2133
*/
2234
get(id) {
2335
if (id !== undefined || id != null) {
36+
id = md5(id);
2437
let record = this._cache[id];
2538
if (record) {
2639
// delete record when it is expired
@@ -44,11 +57,13 @@ export default class Store {
4457
*/
4558
save(id, data) {
4659
if (id !== undefined || id != null) {
60+
id = md5(id);
4761
this._cache[id] = {
4862
id: id,
4963
data: data,
5064
created: Date.now()
5165
};
66+
this._persist();
5267
}
5368
}
5469

@@ -57,5 +72,12 @@ export default class Store {
5772
*/
5873
clear() {
5974
this._cache = {};
75+
this._persist();
76+
}
77+
78+
_persist() {
79+
if (this._local && this._options.persistenceKey) {
80+
this._local.setItem(this._options.persistenceKey, this._cache)
81+
}
6082
}
6183
}

‎src/models/metadata/YoudaoTranslateData.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import Store from '../Store';
22
import JSONP from '../../utils/JSONP';
3+
import AppModel from '../AppModel';
34

45
class YoudaoTranslateData {
56
constructor() {
6-
this._store = new Store(Infinity);
7+
this._store = new Store(Infinity, {
8+
persistence: 'session',
9+
persistenceKey: AppModel.genPersistenceKey('youdao_translate_key')
10+
});
711
}
812

913
async request(val) {

‎src/sw.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
// cache, then increment the CACHE_VERSION value. It will kick off the service worker update
2525
// flow and the old cache(s) will be purged as part of the activate event handler when the
2626
// updated service worker is activated.
27-
var CACHE_VERSION = _BUILD_VERSION_;
27+
var CACHE_VERSION = '_BUILD_VERSION_';
2828
var CURRENT_CACHES = {
2929
prefetch: 'prefetch-cache-v' + CACHE_VERSION
3030
};

0 commit comments

Comments
 (0)