This repository was archived by the owner on Oct 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 236
/
Copy pathLibraryManager.tsx
209 lines (190 loc) · 8.03 KB
/
LibraryManager.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import * as React from "react";
import { Button, Checkbox, DropdownButton, MenuItem } from "react-bootstrap";
import * as ReactList from "react-list";
import { connect } from "react-redux";
import SearchInput, { createFilter } from "react-search-input";
import * as actions from "../actions";
import { versionCompare } from "../utils/util";
import LibraryItemView from "./LibraryItemView";
interface ILibraryManagerProps extends React.Props<any> {
libraries: any;
types: any[];
categories: any[];
requesting: boolean;
errorMessage: string;
installingLibraryName: string;
uninstallingLibraryName: string;
loadLibraries: (update: boolean) => void;
installLibrary: (libraryName, version, callback) => void;
uninstallLibrary: (libraryName, libraryPath, callback) => void;
}
interface ILibraryManagerState extends React.Props<any> {
searchTerm: string;
type: string;
topic: string;
checked: boolean;
}
const mapStateToProps = (store) => {
return {
libraries: store.libraryManagerStore.libraries,
types: store.libraryManagerStore.types,
categories: store.libraryManagerStore.categories,
requesting: store.libraryManagerStore.requesting,
errorMessage: store.libraryManagerStore.errorMessage,
installingLibraryName: store.libraryManagerStore.installingLibraryName,
uninstallingLibraryName: store.libraryManagerStore.uninstallingLibraryName,
};
};
const mapDispatchToProps = (dispatch) => {
return {
loadLibraries: (update: boolean = false) => actions.getLibraries(dispatch, update),
installLibrary: (libraryName, version, callback) => actions.installLibrary(dispatch, libraryName, version, (error) => {
if (!error) {
// Refresh library manager view
actions.getLibraries(dispatch, false, callback);
} else {
callback();
}
}),
uninstallLibrary: (libraryName, libraryPath, callback) => actions.uninstallLibrary(dispatch, libraryName, libraryPath, (error) => {
if (!error) {
// Refresh library manager view
actions.getLibraries(dispatch, false, callback);
} else {
callback();
}
}),
};
};
class LibraryManager extends React.Component<ILibraryManagerProps, ILibraryManagerState> {
constructor(props) {
super(props);
this.state = {
searchTerm: "",
type: "All",
topic: "All",
checked: false,
};
this.typeUpdate = this.typeUpdate.bind(this);
this.topicUpdate = this.topicUpdate.bind(this);
this.searchUpdate = this.searchUpdate.bind(this);
this.handleCheck = this.handleCheck.bind(this);
}
public componentWillMount() {
this.props.loadLibraries(false);
}
public render() {
function filterType(element, type) {
switch (type) {
case "All":
return true;
case "Updatable":
if (element.version && element.versions && element.versions.length) {
return versionCompare(element.versions[0], element.version) > 0;
}
return false;
case "Installed":
return element.installed;
default:
return element.types && element.types.length && element.types.some((ele) => ele === type);
}
}
function filterTopic(element, topic) {
switch (topic) {
case "All":
return true;
case "Uncategorized":
return !element.category || element.category === topic;
default:
return element.category === topic;
}
}
const SEARCH_KEYS = ["name", "sentence", "paragraph"];
const filterSearch = createFilter(this.state.searchTerm, SEARCH_KEYS);
const filteredLibraries = this.props.libraries.filter((element) => {
const filterSupported = this.state.checked ? element.supported : true;
if (filterSupported && filterType(element, this.state.type) && filterTopic(element, this.state.topic) && filterSearch(element)) {
return true;
} else {
return false;
}
});
const totalCount = filteredLibraries.length;
let totalCountTips = "";
if (this.state.type === "All" && this.state.topic === "All" && !this.state.searchTerm) {
totalCountTips = `Total ${totalCount} Libraries`;
} else {
totalCountTips = `${totalCount} Libraries matched`;
}
const libraryItemProps = {
installLibrary: this.props.installLibrary,
uninstallLibrary: this.props.uninstallLibrary,
};
const isOperating = !!this.props.installingLibraryName || !!this.props.uninstallingLibraryName;
const itemRenderer = (index, key) => {
return (<LibraryItemView key={filteredLibraries[index].name} library={filteredLibraries[index]} {...libraryItemProps}/>);
};
const itemSizeEstimator = (index, cache) => {
return 200;
};
return (<div className={"librarymanager " + (isOperating ? "disabled" : "")}>
{
this.props.requesting && (
<div className="mask theme-bgcolor">Loading...</div>
)
}
<div className="arduinomanager-toolbar theme-bgcolor">
<div className="dropdown-filter">
<span className="dropdown-label">Type</span>
<DropdownButton id="typeselector" title={this.state.type} onSelect={this.typeUpdate}>
{ this.props.types.map((c, index) => {
return (<MenuItem key={index} eventKey={c} active={c === this.state.type}>{c}</MenuItem>);
})}
</DropdownButton>
</div>
<div className="dropdown-filter">
<span className="dropdown-label">Topic</span>
<DropdownButton id="topicselector" title={this.state.topic} onSelect={this.topicUpdate}>
{ this.props.categories.map((c, index) => {
return (<MenuItem key={index} eventKey={c} active={c === this.state.topic}>{c}</MenuItem>);
})}
</DropdownButton>
</div>
<SearchInput className="search-input" placeholder="Filter your search..." onChange={this.searchUpdate} />
<Checkbox className="supported-checkbox" onChange={this.handleCheck}>Only show libraries supported by current board</Checkbox>
<Button className="operation-btn" bsStyle="link" onClick={() => this.props.loadLibraries(true)}>
Refresh Library Index
</Button>
</div>
<div className="arduinomanager-container">
<ReactList itemRenderer={itemRenderer} itemSizeEstimator={itemSizeEstimator} length={filteredLibraries.length} type="variable"/>
</div>
<div className="arduinomanager-footer theme-bgcolor">
<span>{ totalCountTips }</span>
</div>
</div>);
}
private typeUpdate(eventKey: any, event?: React.SyntheticEvent<{}>): void {
this.setState({
type: eventKey,
});
}
private topicUpdate(eventKey: any, event?: React.SyntheticEvent<{}>): void {
this.setState({
topic: eventKey,
});
}
private searchUpdate(term) {
this.setState({
searchTerm: term,
});
}
private handleCheck() {
this.setState({
checked: !this.state.checked,
});
}
}
export default connect(mapStateToProps, mapDispatchToProps)(LibraryManager);