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 pathBoardManager.tsx
163 lines (146 loc) · 6.44 KB
/
BoardManager.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import * as React from "react";
import { Button, DropdownButton, MenuItem } from "react-bootstrap";
import { connect } from "react-redux";
import SearchInput, { createFilter } from "react-search-input";
import * as actions from "../actions";
import * as API from "../actions/api";
import { versionCompare } from "../utils/util";
import BoardItemView from "./BoardItemView";
interface IBoardManagerProps extends React.Props<any> {
platforms: any;
categories: any;
requesting: boolean;
errorMessage: string;
installingBoardName: string;
installErrorMessage: string;
uninstallingBoardName: string;
uninstallErrorMessage: string;
loadBoardPackages: (update: boolean) => void;
installBoard: (boardName, packageName, arch, version) => void;
uninstallBoard: (boardName, packagePath) => void;
}
interface IBoardManagerState extends React.Props<any> {
searchTerm: string;
category: string;
}
const mapStateToProps = (state) => {
return {
platforms: state.boardManagerStore.platforms,
categories: state.boardManagerStore.categories,
requesting: state.boardManagerStore.requesting,
errorMessage: state.boardManagerStore.errorMessage,
installingBoardName: state.boardManagerStore.installingBoardName,
installErrorMessage: state.boardManagerStore.installErrorMessage,
uninstallingBoardName: state.boardManagerStore.uninstallingBoardName,
uninstallErrorMessage: state.boardManagerStore.uninstallErrorMessage,
};
};
const mapDispatchToProps = (dispatch) => {
return {
loadBoardPackages: (update: boolean = false) => actions.getBoardPackages(dispatch, update),
installBoard: (boardName, packageName, arch, version) => actions.installBoard(dispatch, boardName, packageName, arch, version, () => {
actions.getBoardPackages(dispatch, false);
}),
uninstallBoard: (boardName, packagePath) => actions.uninstallBoard(dispatch, boardName, packagePath, () => {
actions.getBoardPackages(dispatch, false);
}),
};
};
class BoardManager extends React.Component<IBoardManagerProps, IBoardManagerState> {
constructor(props) {
super(props);
this.state = {
searchTerm: "",
category: "All",
};
this.searchUpdate = this.searchUpdate.bind(this);
this.typeUpdate = this.typeUpdate.bind(this);
}
public componentWillMount() {
this.props.loadBoardPackages(false);
}
public render() {
function filterType(element, type) {
switch (type) {
case "All":
return true;
case "Updatable":
if (element.installedVersion && element.versions && element.versions.length) {
return versionCompare(element.versions[0], element.installedVersion) > 0;
}
return false;
case "Installed":
return !!element.installedVersion;
default:
return element.category === type;
}
}
const SEARCH_KEYS = ["name", "boards.name"];
const filterSearch = createFilter(this.state.searchTerm, SEARCH_KEYS);
const filteredPlatforms = this.props.platforms.filter((element) => {
return filterType(element, this.state.category) && filterSearch(element);
});
let totalCountTips = "";
if (this.state.category === "All" && !this.state.searchTerm) {
totalCountTips = `Total ${filteredPlatforms.length} Boards`;
} else {
totalCountTips = `${filteredPlatforms.length} Boards matched`;
}
const boardProps = {
installingBoardName: this.props.installingBoardName,
installErrorMessage: this.props.installErrorMessage,
uninstallingBoardName: this.props.uninstallingBoardName,
uninstallErrorMessage: this.props.uninstallErrorMessage,
installBoard: this.props.installBoard,
uninstallBoard: this.props.uninstallBoard,
};
const isOperating = !!this.props.installingBoardName || !!this.props.uninstallingBoardName;
return (
<div className={"boardmanager " + (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.category} onSelect={this.typeUpdate}>
{this.props.categories.map((c, index) => {
return (<MenuItem key={index} eventKey={c} active={c === this.state.category}>{c}</MenuItem>);
})}
</DropdownButton>
</div>
<SearchInput className="search-input" placeholder="Filter your search..." onChange={this.searchUpdate} />
<Button className="operation-btn" bsStyle="link" onClick={() => this.props.loadBoardPackages(true)}>
Refresh Package Indexes
</Button>
</div>
<div className="arduinomanager-container">
{
filteredPlatforms.map((p, index) => {
return (<BoardItemView key={p.name} platform={p} {...boardProps} />);
})
}
</div>
<div className="arduinomanager-footer theme-bgcolor">
<span>{totalCountTips}</span>
<a className="help-link right-side" title="Configure Additional Boards Manager URLs"
onClick={() => API.openSettings()}>Additional URLs</a>
</div>
</div>);
}
private typeUpdate(eventKey: any, event?: React.SyntheticEvent<{}>): void {
this.setState({
category: eventKey,
});
}
private searchUpdate(term) {
this.setState({
searchTerm: term,
});
}
}
export default connect(mapStateToProps, mapDispatchToProps)(BoardManager);