-
-
Notifications
You must be signed in to change notification settings - Fork 270
Expand file tree
/
Copy pathScanResults.tsx
More file actions
378 lines (332 loc) · 10.4 KB
/
Copy pathScanResults.tsx
File metadata and controls
378 lines (332 loc) · 10.4 KB
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
import Link from 'next/link'
import Router, { withRouter, type NextRouter } from 'next/router'
import React, { Component } from 'react'
import FlipMove from 'react-flip-move'
import cx from 'classnames'
import PQueue from 'p-queue'
import { stringify } from 'query-string'
import Analytics from '../../client/analytics'
import API, { type PackageBuildInfo } from '../../client/api'
import Stat from '../../client/components/Stat'
import ResultLayout from '../../client/components/ResultLayout'
import { parsePackageString, sanitizeErrorHTML } from '../../utils/common.utils'
import { getTimeFromSize } from '../../utils'
type PromiseState = 'pending' | 'fulfilled' | 'rejected'
type SortMode = 'alphabetic' | 'size'
type PackageBuildError = {
code: string
message: string
}
type ParsedPackage = ReturnType<typeof parsePackageString>
type ScanPackage = ParsedPackage & {
promiseState: PromiseState
packageString: string
result?: PackageBuildInfo
error?: PackageBuildError
}
type ResultCardProps = {
pack: ScanPackage
index: number
}
type ScanResultsProps = {
router: NextRouter
}
type ScanResultsState = {
packages: ScanPackage[]
sortMode: SortMode
}
function getQueryValue(value: string | string[] | undefined) {
return Array.isArray(value) ? value[0] : value
}
function getPackagesFromRouter(router: NextRouter): ScanPackage[] {
const packageStrings = getQueryValue(router.query.packages)
if (!packageStrings) {
return []
}
return packageStrings
.split(',')
.map(str => str.trim())
.filter(Boolean)
.map(str => ({
promiseState: 'pending' as const,
packageString: str,
...parsePackageString(str),
}))
}
function getSortModeFromRouter(router: NextRouter): SortMode {
const sortMode = getQueryValue(router.query.sortMode)
return sortMode === 'size' ? 'size' : 'alphabetic'
}
class ResultCard extends Component<ResultCardProps> {
render() {
const { pack, index } = this.props
let content: React.ReactNode = null
switch (pack.promiseState) {
case 'pending':
content = (
<div className="scan-results__loading-text">Calculating …</div>
)
break
case 'fulfilled':
if (!pack.result) {
break
}
content = (
<div className="scan-results__stat-container">
<Stat
className="scan-results__stat-item"
value={pack.result.size}
type={Stat.type.SIZE}
label="Min"
compact
/>
<Stat
className="scan-results__stat-item"
value={pack.result.gzip}
type={Stat.type.SIZE}
label="Min + GZIP"
compact
/>
<Stat
className="scan-results__stat-item"
value={getTimeFromSize(pack.result.gzip).threeG}
type={Stat.type.TIME}
label="Slow 3G"
compact
/>
<Stat
className="scan-results__stat-item"
value={getTimeFromSize(pack.result.gzip).fourG}
type={Stat.type.TIME}
label="Emerging 4G"
compact
/>
</div>
)
break
case 'rejected':
if (!pack.error) {
break
}
content = (
<details className="scan-results__error-text">
<summary> {pack.error.code}</summary>
<p
dangerouslySetInnerHTML={{
__html: sanitizeErrorHTML(pack.error.message),
}}
/>
</details>
)
break
}
return (
<li
className={cx('scan-results__item', {
'scan-results__item--loading': pack.promiseState === 'pending',
'scan-results__item--error': pack.promiseState === 'rejected',
})}
>
<div className="scan-results__index">
<span> {index + 1}. </span>
</div>
<div className="scan-results__name" data-name={pack.name}>
<Link href={`/package/${pack.packageString}`}>
<span className="scan-results__package-name"> {pack.name}</span>
<div>
{pack.version && (
<span className="scan-results__package-version">
v{pack.version}
</span>
)}
</div>
</Link>
</div>
{content}
</li>
)
}
}
class ScanResults extends Component<ScanResultsProps, ScanResultsState> {
state: ScanResultsState = {
packages: getPackagesFromRouter(this.props.router),
sortMode: getSortModeFromRouter(this.props.router),
}
// Disables Next.js's Automatic Static Optimization
// which causes query params to be empty
// see https://nextjs.org/docs/routing/dynamic-routes#caveats
static async getInitialProps() {
return {}
}
componentDidMount() {
const { packages } = this.state
const queue = new PQueue({ concurrency: 3 })
const startTime = Date.now()
Analytics.pageView('scan results')
packages.forEach(pack => {
const packageStartTime = Date.now()
queue.add(() =>
API.getInfo(pack.packageString)
.then(result => {
this.updatePackageState(pack.packageString, {
promiseState: 'fulfilled',
version: result.version,
result,
})
Analytics.searchSuccess({
packageName: pack.packageString,
timeTaken: Date.now() - packageStartTime,
})
})
.catch(({ error }: { error: PackageBuildError }) => {
console.error(error)
this.updatePackageState(pack.packageString, {
promiseState: 'rejected',
error,
})
Analytics.searchFailure({
packageName: pack.packageString,
timeTaken: Date.now() - packageStartTime,
})
})
)
})
queue.onIdle().then(() => {
this.setState(currentState => {
const successfulBuildCount = currentState.packages.reduce(
(curSum, nextPack) =>
nextPack.promiseState === 'fulfilled' ? curSum + 1 : curSum,
0
)
Analytics.scanCompleted({
successRatio:
currentState.packages.length === 0
? 0
: successfulBuildCount / currentState.packages.length,
timeTaken: Date.now() - startTime,
})
return null
})
})
}
updatePackageState(packageString: string, state: Partial<ScanPackage>) {
this.setState(currentState => ({
packages: currentState.packages.map(pack =>
pack.packageString === packageString ? { ...pack, ...state } : pack
),
}))
}
setParamsAndState = (sortMode: SortMode) => {
const updatedQuery = { ...this.props.router.query, sortMode }
Router.replace(
`/scan-results?${stringify(updatedQuery, { encode: false })}`
)
this.setState({ sortMode })
}
handleSortAlphabetic = () => {
this.setParamsAndState('alphabetic')
}
handleSortSize = () => {
this.setParamsAndState('size')
}
sortPackages = () => {
const { packages, sortMode } = this.state
const packagesCopy = [...packages]
if (sortMode === 'size') {
return packagesCopy.sort((packA, packB) => {
const packASize = packA.result ? packA.result.gzip : 0
const packBSize = packB.result ? packB.result.gzip : 0
return packBSize - packASize
})
}
return packagesCopy.sort((packA, packB) =>
packA.name.localeCompare(packB.name)
)
}
render() {
const { sortMode } = this.state
const packages = this.sortPackages()
const totalMinSize = packages.reduce(
(curTotal, pack) => curTotal + (pack.result ? pack.result.size : 0),
0
)
const totalGZIPSize = packages.reduce(
(curTotal, pack) => curTotal + (pack.result ? pack.result.gzip : 0),
0
)
return (
<ResultLayout className="scan-results">
<h1> Results</h1>
<div className="scan-results__sort-panel">
<label> Sort By: </label>
<button
className={cx({
'scan-results__sort--selected': sortMode === 'alphabetic',
})}
onClick={this.handleSortAlphabetic}
>
Name: A → Z
</button>
<button
className={cx({
'scan-results__sort--selected': sortMode === 'size',
})}
onClick={this.handleSortSize}
>
Size: High → Low
</button>
</div>
<ul className="scan-results__container">
<FlipMove
duration={350}
easing="cubic-bezier(0.175, 0.885, 0.325, 1.040)"
>
{packages.map((pack, index) => (
<ResultCard pack={pack} index={index} key={pack.packageString} />
))}
</FlipMove>
<li className="scan-results__item scan-results__item--total">
<div className="scan-results__name">Total</div>
<div className="scan-results__stat-container">
<Stat
className="scan-results__stat-item"
value={totalMinSize}
type={Stat.type.SIZE}
label="Min"
compact
/>
<Stat
className="scan-results__stat-item"
value={totalGZIPSize}
type={Stat.type.SIZE}
label="Min + GZIP"
compact
/>
<Stat
className="scan-results__stat-item"
value={totalGZIPSize / 1024 / 30}
type={Stat.type.TIME}
label="2G Edge"
compact
/>
<Stat
className="scan-results__stat-item"
value={totalGZIPSize / 1024 / 50}
type={Stat.type.TIME}
label="Slow 3G"
compact
/>
</div>
</li>
</ul>
<div className="scan-results__note">
<b>NOTE:</b> Sizes shown are when importing the complete package.
Actual sizes might be smaller if only parts of the package are used or
if packages share common dependencies. This is not a substitute for
bundle size.
</div>
</ResultLayout>
)
}
}
export default withRouter(ScanResults)