2019-08-25 19:34:27 +07:00
|
|
|
import React, { ReactElement } from 'react';
|
2019-06-20 19:37:28 +07:00
|
|
|
import List from '@material-ui/core/List';
|
2019-09-01 18:09:23 +07:00
|
|
|
import { Link as RouterLink } from 'react-router-dom';
|
|
|
|
import Link from '@material-ui/core/Link';
|
2019-06-20 19:37:28 +07:00
|
|
|
import ListItem from '@material-ui/core/ListItem';
|
2019-08-25 19:34:27 +07:00
|
|
|
|
|
|
|
import { DetailContextConsumer } from '../../pages/Version';
|
|
|
|
import { formatDateDistance } from '../../utils/package';
|
2019-02-03 17:23:33 +07:00
|
|
|
import { DIST_TAGS } from '../../../lib/constants';
|
2019-09-01 18:09:23 +07:00
|
|
|
|
2019-08-25 19:34:27 +07:00
|
|
|
import { Heading, Spacer, ListItemText } from './styles';
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-09-01 18:09:23 +07:00
|
|
|
export const NOT_AVAILABLE = 'Not available';
|
|
|
|
export const LABEL_CURRENT_TAGS = 'Current Tags';
|
|
|
|
export const LABEL_VERSION_HISTORY = 'Version History';
|
2019-04-28 18:15:45 +07:00
|
|
|
|
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 => {
|
2019-09-01 18:09:23 +07:00
|
|
|
const { packageMeta, packageName } = context;
|
|
|
|
|
|
|
|
if (!packageMeta) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.renderContent(packageMeta, packageName);
|
2019-02-03 17:23:33 +07:00
|
|
|
}}
|
|
|
|
</DetailContextConsumer>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-09-01 18:09:23 +07:00
|
|
|
public renderPackageList = (packages: {}, timeMap: Record<string, {}>, packageName): ReactElement<HTMLDivElement> => {
|
2019-03-28 05:39:06 +07:00
|
|
|
return (
|
2019-09-01 18:09:23 +07:00
|
|
|
<List dense={true}>
|
2019-03-28 05:39:06 +07:00
|
|
|
{Object.keys(packages)
|
|
|
|
.reverse()
|
|
|
|
.map(version => (
|
2019-06-20 19:37:28 +07:00
|
|
|
<ListItem className="version-item" key={version}>
|
2019-09-01 18:09:23 +07:00
|
|
|
<Link component={RouterLink} to={`/-/web/detail/${packageName}/v/${version}`}>
|
|
|
|
<ListItemText>{version}</ListItemText>
|
|
|
|
</Link>
|
|
|
|
<Spacer />
|
|
|
|
<ListItemText>{timeMap[version] ? `${formatDateDistance(timeMap[version])} ago` : NOT_AVAILABLE}</ListItemText>
|
|
|
|
</ListItem>
|
|
|
|
))}
|
|
|
|
</List>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
public renderTagList = (packages: {}): ReactElement<HTMLDivElement> => {
|
|
|
|
return (
|
|
|
|
<List dense={true}>
|
|
|
|
{Object.keys(packages)
|
|
|
|
.reverse()
|
|
|
|
.map(tag => (
|
|
|
|
<ListItem className="version-item" key={tag}>
|
|
|
|
<ListItemText>{tag}</ListItemText>
|
2019-03-28 05:39:06 +07:00
|
|
|
<Spacer />
|
2019-09-01 18:09:23 +07:00
|
|
|
<ListItemText>{packages[tag]}</ListItemText>
|
2019-03-28 05:39:06 +07:00
|
|
|
</ListItem>
|
|
|
|
))}
|
|
|
|
</List>
|
|
|
|
);
|
|
|
|
};
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-09-01 18:09:23 +07:00
|
|
|
public renderContent(packageMeta, packageName): 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-09-01 18:09:23 +07:00
|
|
|
<Heading variant="subtitle1">{LABEL_CURRENT_TAGS}</Heading>
|
|
|
|
{this.renderTagList(distTags)}
|
2019-02-03 17:23:33 +07:00
|
|
|
</>
|
|
|
|
)}
|
|
|
|
{versions && (
|
|
|
|
<>
|
2019-09-01 18:09:23 +07:00
|
|
|
<Heading variant="subtitle1">{LABEL_VERSION_HISTORY}</Heading>
|
|
|
|
{this.renderPackageList(versions, timeMap, packageName)}
|
2019-02-03 17:23:33 +07:00
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Versions;
|