1
0
Fork 1
mirror of https://github.com/SomboChea/ui synced 2024-06-02 17:45:05 +07:00
verdaccio-ui/src/components/Versions/Versions.tsx
2019-06-26 00:10:15 +01:00

62 lines
2.0 KiB
TypeScript

import { DetailContextConsumer } from '../../pages/version/Version';
import { formatDateDistance } from '../../utils/package';
import { Heading, Spacer, ListItemText } from './styles';
import List from '@material-ui/core/List';
import ListItem from '@material-ui/core/ListItem';
import React, { ReactElement } from 'react';
import { DIST_TAGS } from '../../../lib/constants';
const NOT_AVAILABLE = 'Not available';
class Versions extends React.PureComponent {
public render(): ReactElement<HTMLDivElement> {
return (
<DetailContextConsumer>
{context => {
return context && context.packageMeta && this.renderContent(context.packageMeta);
}}
</DetailContextConsumer>
);
}
public renderPackageList = (packages: {}, isVersion: boolean = false, timeMap: Record<string, {}> = {}): ReactElement<HTMLDivElement> => {
return (
<List>
{Object.keys(packages)
.reverse()
.map(version => (
<ListItem className="version-item" key={version}>
<ListItemText>{version}</ListItemText>
<Spacer />
{isVersion && <ListItemText>{timeMap[version] ? `${formatDateDistance(timeMap[version])} ago` : NOT_AVAILABLE}</ListItemText>}
{isVersion === false && <ListItemText>{packages[version]}</ListItemText>}
</ListItem>
))}
</List>
);
};
public renderContent(packageMeta): ReactElement<HTMLDivElement> {
const { versions = {}, time: timeMap = {}, [DIST_TAGS]: distTags = {} } = packageMeta;
return (
<>
{distTags && (
<>
<Heading variant="subheading">{'Current Tags'}</Heading>
{this.renderPackageList(distTags, false, timeMap)}
</>
)}
{versions && (
<>
<Heading variant="subheading">{'Version History'}</Heading>
{this.renderPackageList(versions, true, timeMap)}
</>
)}
</>
);
}
}
export default Versions;