-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.jsx
424 lines (364 loc) · 14.4 KB
/
App.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// TODO https://github.com/mduclehcm/solid-router
// TODO https://github.com/rturnq/solid-router
import { createSignal, createState, onMount } from "solid-js";
import { glob as globalStyle } from "solid-styled-components";
//import logo from "./logo.svg";
import styles from "./App.module.css";
import 'highlight.js/styles/github.css';
//import hljs from 'highlight.js'; // all languages (dynamic include on demand?)
import hljs from 'highlight.js/lib/core';
import hljsLangNix from 'highlight.js/lib/languages/nix';
import hljsLangBash from 'highlight.js/lib/languages/bash';
import hljsLangMakefile from 'highlight.js/lib/languages/makefile';
import hljsLangDiff from 'highlight.js/lib/languages/diff';
hljs.registerLanguage('nix', hljsLangNix);
hljs.registerLanguage('bash', hljsLangBash);
hljs.registerLanguage('makefile', hljsLangMakefile);
hljs.registerLanguage('diff', hljsLangDiff);
const highlight = (src, path) => hljs.highlight(src, { language: path.split(".").slice(-1)[0] }).value;
/*
// FIXME hide/show parent dirs
// note. prop.root is not passed to recursion
const getList = (list, root) => {
if (root) {
for (const idx in root) {
list = list[idx];
}
}
return list;
};
// <For each={getList(props.list, props.root)} fallback={<li>list is empty</li>}>
// <For each={props.list} fallback={<li>list is empty</li>}>
*/
// ul.tree-view.root { height: 100%; /* fit to container */; overflow: auto; /* scroll on demand */ }
globalStyle(`
ul.tree-view.root { margin-left: 1px; margin-right: 1px; }
ul.tree-view { text-align: left; }
ul.tree-view, ul.tree-view ul { list-style: none; padding: 0; }
ul.tree-view li.branch > span { color: blue; font-family: monospace; }
ul.tree-view li.branch > ul { display: none; /* default collapsed */ }
ul.tree-view li.branch.expanded { outline: solid 1px grey; }
ul.tree-view li.branch.expanded > ul { display: block; }
ul.tree-view li.empty { font-style: italic; }
ul.tree-view span.link-source { color: green; font-family: monospace; }
ul.tree-view span.file { font-family: monospace; }
ul.tree-view span.prefix { opacity: 0.6; }
body { padding: 0.5em; }
`);
function TreeView(props) {
return (
<ul class={(() => (props.prefix ? 'tree-view' : 'tree-view root'))()}>
<For each={props.filter ? props.data.filter(props.filter) : props.data} fallback={
<li class="empty">{props.get.emptyLabel(props.prefix)}</li>
}>
{(node, idx) => (
<Show when={props.get.isLeaf(node)} fallback={
<li class="branch">
<span onClick={event => {
props.load(node, props.prefix, props.get);
// go up to nearest <li class="branch">
let li = event.target;
while (li && li.localName != 'li') li = li.parentNode;
if (!li) throw { error: 'li not found', event };
li.classList.toggle('expanded');
}}>
{props.get.branchLabel(node, props.prefix)}
</span>
<TreeView
data={props.get.childNodes(node)}
get={props.get}
prefix={props.get.path(node, props.prefix)}
load={props.load}
/>
</li>
}>
<li class="leaf">{props.get.leafLabel(node, props.prefix)}</li>
</Show>
)}
</For>
</ul>
);
}
function JobView(props) {
return (
<div class={(() => (props.prefix ? 'job-view' : 'job-view root'))()}>
<Show when={props.job} fallback={
<div>( no job )</div>
}>
<Show when={props.job.readyTime} fallback={
<div>( job {props.job.jobId} is loading ... )</div>
}>
<div>
<div>job {props.job.jobId}</div>
<div>
download and diff took {' '}
{roundFloat((props.job.readyTime - props.job.startTime), 2)}
{' '} seconds
</div>
<div>{props.job.filesCount} files</div>
</div>
<For each={props.job.files} fallback={
<div>( no job files )</div>
}>
{(file, fileId) => {
// TODO use solidjs styled components
globalStyle(`pre.file { white-space: pre-wrap; text-align: left; font-family: monospace; }`);
const fileExt = file.filename.split(".").slice(-1)[0];
//const fileExt = path => path.split(".").slice(-1)[0]; // TODO need this?
const fileLower = () => highlight(file.lowerText, fileExt);
const fileUpper = () => highlight(file.upperText, fileExt);
const fileText = () => highlight(file.fileText, fileExt);
const fileDiff = () => highlight(file.patchText, 'diff');
return (
<details>
<summary>{file.filename}</summary>
<div>filename = {file.filename}</div>
<div>raw_url = <a href={file.raw_url}>{file.raw_url}</a></div>
<div>sha = {file.sha}</div>
<div>localPath = {file.localPath}</div>
<details>
<summary>github vs patched</summary>
<pre class="file github-diff" innerHTML={fileDiff()} />
</details>
<details>
<summary>patched</summary>
<pre class="file lower" innerHTML={fileText()} />
</details>
<details>
<summary>lower vs patched</summary>
<pre class="file lower-diff" innerHTML={file.lowerDiff} />
</details>
<details>
<summary>lower</summary>
<pre class="file lower" innerHTML={fileLower()} />
</details>
<Show when={file.upperDiff}>
<details>
<summary>upper vs patched</summary>
<pre class="file upper-diff" innerHTML={file.upperDiff} />
</details>
<details>
<summary>upper</summary>
<pre class="file upper" innerHTML={fileUpper()} />
</details>
</Show>
</details>
);
}}
</For>
</Show>
</Show>
</div>
);
}
const roundFloat = (number, precision) => parseFloat(number.toFixed(precision));
function postOptions(data) {
return {
method: 'POST',
body: JSON.stringify(data),
headers: { 'Content-Type': 'application/json' }
};
}
function parseQuery(queryStr) {
return Object.fromEntries(
queryStr.split('&').map(keyval => {
const cut = keyval.indexOf('=');
if (cut == -1) return [keyval, true]; // key only
const key = keyval.slice(0, cut);
const val = keyval.slice(cut+1);
return [key, val];
})
);
}
function App() {
let fileInput;
const [state, setState] = createState({
fileList: [],
fileSelected: '',
patchedFiles: [],
job: null,
diffData: null,
});
onMount(async () => {
const queryStr = window.location.hash.slice(1);
const query = parseQuery(queryStr);
console.log('query', query);
if (query.install) {
//console.log(`install. url = ${query.install}`);
const dataObject = { url: query.install };
const response = await fetch(`/backend/install`, postOptions(dataObject));
if (!response.ok) { console.log(`http request error ${response.status}`); return; }
const responseData = await response.json();
setState('job', { jobId: responseData.jobId, url: responseData.url }); // show 'job x is loading ...'
//console.log("install response:"); console.dir(responseData);
// start polling the backend for the job status ...
setTimeout(() => pollInstallJobStatus(responseData.jobId), 100);
}
else if (query.pulls) {
console.log(`call loadPulls`)
loadPulls();
}
else {
loadFiles(); // ... for TreeView
}
})
async function pollInstallJobStatus(jobId, step = 0) {
//console.log(`poll install job STATUS. job ${jobId} + step ${step}`);
var dataObject = { jobId };
var response = await fetch(`/backend/jobstatus`, postOptions(dataObject));
if (!response.ok) { console.log(`http request error ${response.status}`); return; }
var jobStatus = await response.json();
//console.log("jobstatus response:"); console.dir(jobStatus);
// repeat poll ...
// latency for a small patch: 2 second
if (jobStatus.readyTime == undefined) {
// not ready
if (step < 30) // wait 15 seconds
return setTimeout(() => pollInstallJobStatus(jobId, step + 1), 500); // repeat
else
return console.log(`stop polling job ${jobId}`); // give up. TODO better
}
// done polling. job data is ready :)
// visualize the patch auditing
//console.log(`poll install job DATA. job ${jobId}`);
var dataObject = { jobId };
var response = await fetch(`/backend/jobdata`, postOptions(dataObject));
if (!response.ok) { console.log(`http request error ${response.status}`); return; }
const jobData = await response.json();
//console.log("jobdata response:"); console.dir(jobData);
if (jobData.job.files.length == 0) { console.log('error: no files in job data'); return; }
setState('job', jobData.job);
}
async function loadPulls(step = 0) {
const dataObject = {};
const response = await fetch(`/backend/pulls`, postOptions(dataObject));
if (!response.ok) { console.log(`http request error ${response.status}`); return; }
const responseData = await response.json();
console.log("pulls response:"); console.dir(responseData);
setState('pulls', responseData);
if (responseData.status == 'loading' || responseData.data?.status == 'loading') {
console.log(`pulls loading ... (step ${step})`);
setTimeout(() => loadPulls(step + 1), 1000);
}
else if (responseData.status == 'ready') {
console.log("pulls ready");
}
}
async function loadFiles(node = null, prefix = '', get = null) {
const path = (node && get) ? get.path(node, prefix) : '';
//console.log(`loadFiles. path = ${path}`);
const dataObject = { path };
const response = await fetch(`/backend/list`, postOptions(dataObject));
if (!response.ok) { console.log(`http request error ${response.status}`); return; }
const responseData = await response.json();
//console.dir(responseData.files);
if (!state.fileList || state.fileList.length == 0)
setState('fileList', responseData.files); // init
else {
//console.log(`add files for path ${path}`)
const keyPath = ['fileList'];
const childNodesIdx = 3;
let parentDir = state.fileList;
path.split('/').filter(Boolean).forEach((d, di) => {
const i = parentDir.findIndex(([ depth, type, file, arg ]) => (type == 'd' && file == d));
keyPath.push(i); parentDir = parentDir[i];
keyPath.push(childNodesIdx); parentDir = parentDir[childNodesIdx];
})
setState(...keyPath, responseData.files);
}
}
function handleUpload(event) {
event.preventDefault();
//console.log('submit');
var fileReader = new FileReader();
fileReader.onload = async event => {
const bText = event.target.result;
const bExt = fileInput.files[0].name.split(".").slice(-1)[0];
bContainer.innerHTML = highlight(bText, bExt);
const dataObject = {
a: state.fileSelected,
b: fileInput.files[0].name,
bTime: fileInput.files[0].lastModified,
bSize: fileInput.files[0].size,
bText,
};
const response = await fetch(`/backend/diff`, postOptions(dataObject));
if (!response.ok) { console.log(`http request error ${response.status}`); return; }
const diffData = await response.json();
//console.dir({ diffData });
setState('diffData', diffData);
//diffContainer.innerHTML = diffData.diffHtml;
//aContainer.innerHTML = highlight(diffData.aText, 'nix');
// TODO render state.diffData
};
fileReader.readAsText(fileInput.files[0], "UTF-8");
}
function fileListGetters() {
const get = {};
get.isLeaf = node => (node[1] != 'd');
get.name = node => node[2];
get.path = (node, prefix) => prefix ? `${prefix}/${get.name(node)}` : get.name(node);
get.childNodes = node => node[3];
const fancyPath = (node, prefix) => (
prefix ? <>
<span class="prefix">{(() => prefix)()}/</span>
<span class="name">{get.name(node)}</span>
</> : get.name(node)
);
get.branchLabel = fancyPath;
get.emptyLabel = (prefix) => '( empty )';
const isLink = node => (node[1] == 'l');
const linkTarget = node => node[3];
get.leafLabel = (node, prefix) => {
if (isLink(node))
return <>
<span class="link-source">{fancyPath(node, prefix)}</span>{" -> "}
<span class="link-target">{linkTarget(node)}</span>
</>;
return <span class="file" onClick={() => setState('fileSelected', get.path(node, prefix))}>{fancyPath(node, prefix)}</span>;
};
return get;
}
function fileListFilter() {
return node => (node[2][0] != '.'); // hide dotfiles
}
return (
<div class={styles.App}>
<div>file to patch: {state.fileSelected || '( none. please select file )'}</div>
<form onSubmit={handleUpload}>
<input type="file" name="bFile" ref={fileInput} />
<input type="submit" value="Upload and Compare" />
</form>
<h4>install job</h4>
<JobView job={state.job} />
<h4>file tree</h4>
<TreeView data={state.fileList} get={fileListGetters()} filter={fileListFilter()} load={loadFiles} />
</div>
);
}
const Loader = () => {
const [state, setState] = createState({ count: 0 }),
interval = setInterval(() => setState("count", (c) => c + 1), 1000);
onCleanup(() => clearInterval(interval));
return <div>Loading... {state.count}</div>;
};
function createDelay() {
return new Promise((resolve) =>
setTimeout(() => resolve(Date.now()), Math.random() * 8000)
);
}
const AsyncChild = ({ start }) => {
const [time] = createResource("time", createDelay);
return <div>Async loaded after {time() - start}ms</div>;
};
function Result() {
return (
<Suspense fallback={<Loader />}>
<AsyncChild start={startTime} />
<AsyncChild start={startTime} />
<AsyncChild start={startTime} />
<AsyncChild start={startTime} />
</Suspense>
);
}
export default App;