-
Notifications
You must be signed in to change notification settings - Fork 87
/
AnalyzerFileTree.jsx
306 lines (281 loc) · 12.2 KB
/
AnalyzerFileTree.jsx
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import * as React from "react";
import AceEditor from "react-ace";
import { withRouter } from "react-router-dom";
import { getFileFormat, rootPath, Utilities } from "../../utilities/utilities";
import sortBy from "lodash/sortBy";
import find from "lodash/find";
import has from "lodash/has";
import queryString from "query-string";
import ReactTooltip from "react-tooltip"
import Loader from "../shared/Loader";
import FileTree from "../shared/FileTree";
import "../../scss/components/troubleshoot/FileTree.scss";
import "brace/mode/json";
import "brace/mode/text";
import "brace/mode/yaml";
import "brace/theme/chrome";
class AnalyzerFileTree extends React.Component {
constructor(props) {
super();
this.state = {
files: [],
selectedFile: "/" + props.location.pathname.split("/").slice(8, props.location.pathname.length).join("/"),
fileContents: [],
fileLoading: false,
fileLoadErr: false,
fileLoadErrMessage: "",
activeMarkers: [],
analysisError: false,
currentViewIndex: 0
};
}
hasContentAlready = (path) => {
const { fileContents } = this.state;
let i;
for (i = 0; i < fileContents.length; i++) {
if (fileContents[i].key === path) { return true; }
}
return false;
}
buildFileContent = (data) => {
const nextFiles = this.state.fileContents;
const keys = Object.keys(data);
if (!keys.length) {
return;
}
let newObj = {};
newObj.content = new Buffer(data[keys[0]], "base64").toString();
newObj.key = keys[0];
nextFiles.push(newObj);
this.setState({ fileContents: nextFiles });
}
async setSelectedFile(path) {
const { watchSlug } = this.props;
const newPath = rootPath(path);
this.props.history.replace(`/app/${watchSlug}/troubleshoot/analyze/${this.props.match.params.bundleSlug}/contents${newPath}`);
this.setState({ selectedFile: newPath, activeMarkers: [] });
if (this.hasContentAlready(newPath)) { return; } // Don't go fetch it if we already have that content in our state
this.fetchFiles(this.state.bundleId, newPath)
}
fetchFiles = (bundleId, path) => {
if (path === "/") {
return;
}
this.setState({
fileLoading: true,
fileLoadErr: false,
});
fetch(`${window.env.API_ENDPOINT}/troubleshoot/supportbundle/${bundleId}/files?filename=${encodeURIComponent(path)}`, {
method: "GET",
headers: {
"Authorization": Utilities.getToken(),
"Content-Type": "application/json",
},
})
.then(async (result) => {
const data = await result.json();
this.buildFileContent(data.files);
this.setState({ fileLoading: false });
})
.catch(err => {
this.setState({
fileLoading: false,
fileLoadErr: true,
fileLoadErrMessage: err,
})
});
}
setFileTree = () => {
if (!this.state.fileTree) { return; }
const parsedTree = JSON.parse(this.state.fileTree);
let sortedTree = sortBy(parsedTree, (dir) => {
return dir.children ? dir.children.length : [];
});
this.setState({ files: sortedTree });
}
setRedactorMarkersFromHash = () => {
const { hash, search } = this.props.location;
let redactorFileName = "";
if (search) {
redactorFileName = queryString.parse(search);
}
let newMarkers = [];
const lines = hash.substring(1).split(",");
lines.forEach(line => {
newMarkers.push({
startRow: parseInt(line) - 1,
endRow: parseInt(line),
className: "active-highlight",
type: "background"
})
});
this.setState({ activeMarkers: newMarkers, redactionMarkersSet: true, redactorFileName: redactorFileName.file }, () => {
// Clear hash from URL to prevent highlighting again on a refresh
const splitLocation = this.props.location.pathname.split("#");
this.props.history.replace(splitLocation[0]);
})
}
scrollToRedactions = (index) => {
this.setState({ currentViewIndex: index });
const editor = this.aceEditor.editor;
editor.scrollToLine(this.state.activeMarkers[index].endRow, true, true);
editor.gotoLine(this.state.activeMarkers[index].endRow, 1, true);
}
componentDidUpdate(lastProps, lastState) {
const { bundle } = this.props;
if (this.state.fileTree !== lastState.fileTree && this.state.fileTree) {
this.setFileTree();
}
if (bundle !== lastProps.bundle && bundle) {
this.setState({
bundleId: bundle.id,
fileTree: bundle.treeIndex
});
if (this.props.location !== lastProps.location && this.props.location) {
this.setState({ selectedFile: "/" + this.props.location.pathname.split("/").slice(7, this.props.location.pathname.length).join("/") })
this.fetchFiles(bundle.id, "/" + this.props.location.pathname.split("/").slice(7, this.props.location.pathname.length).join("/"))
if (this.props.location.hash) {
this.setRedactorMarkersFromHash();
}
}
}
if (this.aceEditor && this.state.redactionMarkersSet) {
this.setState({ redactionMarkersSet: false });
this.aceEditor.editor.resize(true);
this.scrollToRedactions(0);
}
}
componentDidMount() {
const { bundle } = this.props;
if (this.state.fileTree) {
this.setFileTree();
}
if (bundle) {
this.setState({
bundleId: bundle.id,
fileTree: bundle.treeIndex
})
if (this.props.location) {
this.setState({ selectedFile: "/" + this.props.location.pathname.split("/").slice(7, this.props.location.pathname.length).join("/") })
this.fetchFiles(bundle.id, "/" + this.props.location.pathname.split("/").slice(7, this.props.location.pathname.length).join("/"))
if (this.props.location.hash) {
this.setRedactorMarkersFromHash();
}
}
}
}
onSelectionChange = () => {
const column = this.refAceEditor?.editor?.selection?.anchor.column;
const row = this.refAceEditor?.editor?.selection?.anchor.row;
if (column === 0) {
let newMarker = [];
newMarker.push({
startRow: row - 1,
endRow: row,
className: "active-highlight",
type: "background"
})
this.setState({ activeMarkers: newMarker });
this.props.history.replace(`${this.props.location.pathname}#L${row}`);
}
}
handleDownload = () => {
const { downloadBundle } = this.props;
if (downloadBundle && typeof downloadBundle == "function") {
downloadBundle();
}
}
render() {
const { files, fileContents, selectedFile, fileLoadErr, fileLoadErrMessage, fileLoading } = this.state;
const fileToView = find(fileContents, ["key", selectedFile]);
const format = getFileFormat(selectedFile);
const isOld = files && has(files[0], "size");
const isFirstRedaction = this.state.currentViewIndex === 0;
const isLastRedaction = this.state.currentViewIndex + 1 === this.state.activeMarkers.length;
return (
<div className="flex-column flex1 AnalyzerFileTree--wrapper">
{!files || !files.length || isOld ?
<div className="flex-column flex1 justifyContent--center alignItems--center u-textAlign--center">
<p className="u-textColor--primary u-fontSize--normal u-fontWeight--bold">We were unable to detect files from this Support Bundle</p>
<p className="u-fontSize--small u-textColor--bodyCopy u-fontWeight--medium u-marginTop--10">It's possible that this feature didn't exists when you uploaded the bundle, try re-analyzing it to have files detected.</p>
</div>
:
<div className="flex flex1">
<div className="flex1 dirtree-wrapper flex-column u-overflow-hidden">
<div className="u-overflow--auto dirtree">
<FileTree
files={files}
isRoot={true}
handleFileSelect={(path) => this.setSelectedFile(path)}
selectedFile={this.state.selectedFile}
/>
</div>
</div>
<div className="AceEditor flex1 flex-column file-contents-wrapper u-position--relative">
{this.state.activeMarkers.length > 0 ?
<div className="redactor-pager flex alignItems--center">
<div className={`arrow-wrapper prev ${isFirstRedaction ? "": "can-scroll"}`} onClick={isFirstRedaction ? undefined : () => this.scrollToRedactions(this.state.currentViewIndex - 1)}>
<span className={`icon u-iconFullArrow${isFirstRedaction ? "Gray" : "Blue clickable"} previous`} />
</div>
<div className="flex alignItems--center">
<span>Redaction {this.state.currentViewIndex + 1} of {this.state.activeMarkers.length}</span>
<span className="icon grayOutlineQuestionMark--icon u-marginLeft--10" data-tip data-for="current-redator-filename" />
</div>
<div className={`arrow-wrapper next ${isLastRedaction ? "": "can-scroll"}`} onClick={isLastRedaction ? undefined : () => this.scrollToRedactions(this.state.currentViewIndex + 1)}>
<span className={`icon u-iconFullArrow${isLastRedaction ? "Gray" : "Blue clickable"}`} />
</div>
</div>
: null}
{selectedFile === "" || selectedFile === "/" ?
<div className="flex-column flex1 alignItems--center justifyContent--center">
<p className="u-textColor--bodyCopy u-fontSize--normal u-fontWeight--medium">Select a file from the directory tree to view it here.</p>
</div>
: fileLoadErr ?
<div className="flex-column flex1 alignItems--center justifyContent--center">
<p className="u-textColor--error u-fontSize--normal u-fontWeight--medium">Oops, we ran into a probelm getting that file, <span className="u-fontWeight--bold">{fileLoadErrMessage}</span></p>
<p className="u-marginTop--10 u-fontSize--small u-fontWeight--medium u-textColor--bodyCopy">Don't worry, you can download the bundle and have access to all of the files</p>
<div className="u-marginTop--20">
<button className="btn secondary" onClick={this.handleDownload}>Download bundle</button>
</div>
</div>
: fileLoading || !fileToView ?
<div className="flex-column flex1 alignItems--center justifyContent--center">
<Loader size="50" />
</div>
: fileToView.content === "" ?
<div className="flex-column flex1 alignItems--center justifyContent--center">
<p className="u-textColor--secondary u-fontSize--normal u-fontWeight--medium">This file was collected and analyzed but it contains no data.</p>
</div>
:
<AceEditor
ref={el => (this.aceEditor = el)}
mode={format}
theme="chrome"
className="flex1 flex"
readOnly={true}
value={fileToView.content}
height="100%"
width="100%"
markers={this.state.activeMarkers}
editorProps={{
$blockScrolling: Infinity,
useSoftTabs: true,
tabSize: 2,
}}
onLoad={(editor) => editor.gotoLine(this.props.location.hash !== "" && parseInt(this.props.location.hash.substring(2)))}
onSelectionChange={this.onSelectionChange}
setOptions={{
scrollPastEnd: false,
showGutter: true,
}}
/>
}
<ReactTooltip id="current-redator-filename" type="light" effect="solid" borderColor="#C4C4C4" textColor="#4A4A4A" border={true} className="u-textColor--secondary">Viewing redactions from <span className="u-fontWeight--bold">{this.state.redactorFileName}</span></ReactTooltip>
</div>
</div>
}
</div>
);
}
}
export default withRouter(AnalyzerFileTree);