|
1 | | -import React, { Component } from 'react'; |
| 1 | +import React, { FC, useContext } from 'react'; |
2 | 2 |
|
3 | 3 | import List from '@material-ui/core/List'; |
4 | 4 |
|
5 | | -import { VersionPageConsumerProps, DetailContextConsumer } from '../../pages/Version'; |
| 5 | +import { DetailContext } from '../../pages/Version'; |
6 | 6 | import { Heading, DistListItem, DistChips } from './styles'; |
7 | 7 | import fileSizeSI from '../../utils/file-size'; |
8 | | -import { PackageMetaInterface } from 'types/packageMeta'; |
9 | 8 | import { formatLicense } from '../../utils/package'; |
10 | 9 |
|
11 | | -class Dist extends Component { |
12 | | - public render(): JSX.Element { |
13 | | - return ( |
14 | | - <DetailContextConsumer> |
15 | | - {(context: Partial<VersionPageConsumerProps>) => { |
16 | | - return context && context.packageMeta && this.renderDist(context.packageMeta); |
17 | | - }} |
18 | | - </DetailContextConsumer> |
19 | | - ); |
20 | | - } |
21 | | - |
22 | | - private renderChips(dist, license: PackageMetaInterface['latest']['license']): (JSX.Element | undefined)[] { |
23 | | - const distDict = { |
24 | | - 'file-count': dist.fileCount, |
25 | | - size: dist.unpackedSize && fileSizeSI(dist.unpackedSize), |
26 | | - license, |
27 | | - }; |
28 | | - |
29 | | - const chipsList = Object.keys(distDict).map((dist, key) => { |
30 | | - if (!distDict[dist]) return; |
31 | | - |
32 | | - const value = dist === 'license' ? formatLicense(distDict[dist]) : distDict[dist]; |
33 | | - const label = ( |
| 10 | +const DistChip: FC<{ name: string }> = ({ name, children }) => |
| 11 | + children ? ( |
| 12 | + <DistChips |
| 13 | + // lint rule conflicting with prettier |
| 14 | + /* eslint-disable react/jsx-wrap-multilines */ |
| 15 | + label={ |
34 | 16 | <> |
35 | | - {/* eslint-disable-next-line */} |
36 | | - <b>{dist.replace('-', ' ')}</b>: {value} |
| 17 | + <b>{name}</b> |
| 18 | + {': '} |
| 19 | + {children} |
37 | 20 | </> |
38 | | - ); |
39 | | - return <DistChips key={key} label={label} />; |
40 | | - }); |
| 21 | + } |
| 22 | + /* eslint-enable */ |
| 23 | + /> |
| 24 | + ) : null; |
41 | 25 |
|
42 | | - return chipsList; |
43 | | - } |
| 26 | +const Dist: FC = () => { |
| 27 | + const { packageMeta } = useContext(DetailContext); |
44 | 28 |
|
45 | | - private renderDist = (packageMeta: PackageMetaInterface) => { |
46 | | - const { dist, license } = packageMeta && packageMeta.latest; |
| 29 | + if (!packageMeta) { |
| 30 | + return null; |
| 31 | + } |
47 | 32 |
|
48 | | - return ( |
49 | | - <List subheader={<Heading variant="subtitle1">{'Latest Distribution'}</Heading>}> |
50 | | - <DistListItem button={true}>{this.renderChips(dist, license)}</DistListItem> |
51 | | - </List> |
52 | | - ); |
53 | | - }; |
54 | | -} |
| 33 | + const { dist, license } = packageMeta && packageMeta.latest; |
| 34 | + |
| 35 | + return ( |
| 36 | + <List subheader={<Heading variant="subtitle1">{'Latest Distribution'}</Heading>}> |
| 37 | + <DistListItem button={true}> |
| 38 | + <DistChip name="file count">{dist.fileCount}</DistChip> |
| 39 | + <DistChip name="size">{dist.unpackedSize && fileSizeSI(dist.unpackedSize)}</DistChip> |
| 40 | + <DistChip name="license">{formatLicense(license)}</DistChip> |
| 41 | + </DistListItem> |
| 42 | + </List> |
| 43 | + ); |
| 44 | +}; |
55 | 45 |
|
56 | 46 | export default Dist; |
0 commit comments