2019-06-20 19:37:28 +07:00
|
|
|
import { DetailContextConsumer } from '../../pages/version/Version';
|
2019-02-03 17:23:33 +07:00
|
|
|
import { formatDateDistance } from '../../utils/package';
|
|
|
|
import { Heading, Spacer, ListItemText } from './styles';
|
2019-06-20 19:37:28 +07:00
|
|
|
import List from '@material-ui/core/List';
|
|
|
|
import ListItem from '@material-ui/core/ListItem';
|
|
|
|
import React, { ReactElement } from 'react';
|
2019-02-03 17:23:33 +07:00
|
|
|
import { DIST_TAGS } from '../../../lib/constants';
|
|
|
|
|
2019-04-28 18:15:45 +07:00
|
|
|
const NOT_AVAILABLE = 'Not available';
|
|
|
|
|
2019-06-25 05:54:32 +07:00
|
|
|
class Versions extends React.PureComponent {
|
2019-06-20 19:37:28 +07:00
|
|
|
public render(): ReactElement<HTMLDivElement> {
|
2019-02-03 17:23:33 +07:00
|
|
|
return (
|
|
|
|
<DetailContextConsumer>
|
2019-06-20 19:37:28 +07:00
|
|
|
{context => {
|
|
|
|
return context && context.packageMeta && this.renderContent(context.packageMeta);
|
2019-02-03 17:23:33 +07:00
|
|
|
}}
|
|
|
|
</DetailContextConsumer>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-26 06:10:15 +07:00
|
|
|
public renderPackageList = (packages: {}, isVersion: boolean = false, timeMap: Record<string, {}> = {}): ReactElement<HTMLDivElement> => {
|
2019-03-28 05:39:06 +07:00
|
|
|
return (
|
|
|
|
<List>
|
|
|
|
{Object.keys(packages)
|
|
|
|
.reverse()
|
|
|
|
.map(version => (
|
2019-06-20 19:37:28 +07:00
|
|
|
<ListItem className="version-item" key={version}>
|
2019-03-28 05:39:06 +07:00
|
|
|
<ListItemText>{version}</ListItemText>
|
|
|
|
<Spacer />
|
2019-04-28 18:15:45 +07:00
|
|
|
{isVersion && <ListItemText>{timeMap[version] ? `${formatDateDistance(timeMap[version])} ago` : NOT_AVAILABLE}</ListItemText>}
|
|
|
|
{isVersion === false && <ListItemText>{packages[version]}</ListItemText>}
|
2019-03-28 05:39:06 +07:00
|
|
|
</ListItem>
|
|
|
|
))}
|
|
|
|
</List>
|
|
|
|
);
|
|
|
|
};
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
public renderContent(packageMeta): ReactElement<HTMLDivElement> {
|
2019-03-28 05:39:06 +07:00
|
|
|
const { versions = {}, time: timeMap = {}, [DIST_TAGS]: distTags = {} } = packageMeta;
|
|
|
|
|
2019-02-03 17:23:33 +07:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{distTags && (
|
|
|
|
<>
|
2019-06-20 19:37:28 +07:00
|
|
|
<Heading variant="subheading">{'Current Tags'}</Heading>
|
2019-03-28 05:39:06 +07:00
|
|
|
{this.renderPackageList(distTags, false, timeMap)}
|
2019-02-03 17:23:33 +07:00
|
|
|
</>
|
|
|
|
)}
|
|
|
|
{versions && (
|
|
|
|
<>
|
2019-06-20 19:37:28 +07:00
|
|
|
<Heading variant="subheading">{'Version History'}</Heading>
|
2019-03-28 05:39:06 +07:00
|
|
|
{this.renderPackageList(versions, true, timeMap)}
|
2019-02-03 17:23:33 +07:00
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Versions;
|