2019-02-03 17:23:33 +07:00
|
|
|
import React from 'react';
|
2019-03-28 05:39:06 +07:00
|
|
|
import BugReport from '@material-ui/icons/BugReport';
|
2019-11-08 00:11:08 +07:00
|
|
|
import DownloadIcon from '@material-ui/icons/CloudDownload';
|
2019-03-28 05:39:06 +07:00
|
|
|
import HomeIcon from '@material-ui/icons/Home';
|
2019-10-08 03:19:18 +07:00
|
|
|
|
2019-10-15 17:08:16 +07:00
|
|
|
import { PackageMetaInterface, Author as PackageAuthor } from '../../../types/packageMeta';
|
2019-02-03 17:23:33 +07:00
|
|
|
import Tag from '../Tag';
|
2019-03-28 05:39:06 +07:00
|
|
|
import fileSizeSI from '../../utils/file-size';
|
2019-02-03 17:23:33 +07:00
|
|
|
import { formatDate, formatDateDistance } from '../../utils/package';
|
2019-10-03 23:17:04 +07:00
|
|
|
import Tooltip from '../../muiComponents/Tooltip';
|
2019-10-08 03:19:18 +07:00
|
|
|
import { isURL } from '../../utils/url';
|
2019-11-08 00:11:08 +07:00
|
|
|
import { downloadHandler } from '../ActionBar/ActionBar';
|
2019-10-13 02:11:23 +07:00
|
|
|
import ListItem from '../../muiComponents/ListItem';
|
2019-10-31 14:12:18 +07:00
|
|
|
import Grid from '../../muiComponents/Grid';
|
2019-10-08 03:19:18 +07:00
|
|
|
|
2019-07-06 16:50:09 +07:00
|
|
|
import {
|
|
|
|
Author,
|
|
|
|
Avatar,
|
|
|
|
Description,
|
|
|
|
Details,
|
|
|
|
GridRightAligned,
|
|
|
|
Icon,
|
|
|
|
IconButton,
|
|
|
|
OverviewItem,
|
|
|
|
PackageList,
|
|
|
|
PackageListItemText,
|
|
|
|
PackageTitle,
|
|
|
|
Published,
|
|
|
|
TagContainer,
|
|
|
|
Text,
|
|
|
|
WrapperLink,
|
|
|
|
} from './styles';
|
2019-06-20 19:37:28 +07:00
|
|
|
|
|
|
|
interface Bugs {
|
|
|
|
url: string;
|
|
|
|
}
|
|
|
|
interface Dist {
|
|
|
|
unpackedSize: number;
|
2019-11-08 00:11:08 +07:00
|
|
|
tarball: string;
|
2019-06-20 19:37:28 +07:00
|
|
|
}
|
|
|
|
|
2019-06-22 16:43:59 +07:00
|
|
|
export interface PackageInterface {
|
2019-06-20 19:37:28 +07:00
|
|
|
name: string;
|
|
|
|
version: string;
|
2019-06-22 16:43:59 +07:00
|
|
|
time?: number | string;
|
2019-10-06 20:44:48 +07:00
|
|
|
author: PackageAuthor;
|
2019-06-20 19:37:28 +07:00
|
|
|
description?: string;
|
|
|
|
keywords?: string[];
|
2019-07-06 16:50:09 +07:00
|
|
|
license?: PackageMetaInterface['latest']['license'];
|
2019-06-20 19:37:28 +07:00
|
|
|
homepage?: string;
|
|
|
|
bugs?: Bugs;
|
|
|
|
dist?: Dist;
|
|
|
|
}
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-06-22 16:43:59 +07:00
|
|
|
const Package: React.FC<PackageInterface> = ({
|
2019-03-28 05:39:06 +07:00
|
|
|
author: { name: authorName, avatar: authorAvatar },
|
2019-06-20 19:37:28 +07:00
|
|
|
bugs,
|
2019-03-28 05:39:06 +07:00
|
|
|
description,
|
2019-06-20 19:37:28 +07:00
|
|
|
dist,
|
2019-03-28 05:39:06 +07:00
|
|
|
homepage,
|
|
|
|
keywords = [],
|
|
|
|
license,
|
|
|
|
name: packageName,
|
|
|
|
time,
|
|
|
|
version,
|
2019-06-20 19:37:28 +07:00
|
|
|
}) => {
|
|
|
|
const renderVersionInfo = (): React.ReactNode =>
|
2019-03-28 05:39:06 +07:00
|
|
|
version && (
|
|
|
|
<OverviewItem>
|
|
|
|
<Icon name={'version'} />
|
|
|
|
{`v${version}`}
|
|
|
|
</OverviewItem>
|
|
|
|
);
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
const renderAuthorInfo = (): React.ReactNode =>
|
2019-03-28 05:39:06 +07:00
|
|
|
authorName && (
|
|
|
|
<Author>
|
|
|
|
<Avatar alt={authorName} src={authorAvatar} />
|
|
|
|
<Details>
|
|
|
|
<Text text={authorName} />
|
|
|
|
</Details>
|
|
|
|
</Author>
|
|
|
|
);
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
const renderFileSize = (): React.ReactNode =>
|
|
|
|
dist &&
|
|
|
|
dist.unpackedSize && (
|
2019-03-28 05:39:06 +07:00
|
|
|
<OverviewItem>
|
|
|
|
<Icon name={'filebinary'} />
|
2019-06-20 19:37:28 +07:00
|
|
|
{fileSizeSI(dist.unpackedSize)}
|
2019-03-28 05:39:06 +07:00
|
|
|
</OverviewItem>
|
|
|
|
);
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
const renderLicenseInfo = (): React.ReactNode =>
|
2019-02-03 17:23:33 +07:00
|
|
|
license && (
|
|
|
|
<OverviewItem>
|
2019-06-20 19:37:28 +07:00
|
|
|
<Icon name="law" />
|
2019-02-03 17:23:33 +07:00
|
|
|
{license}
|
|
|
|
</OverviewItem>
|
|
|
|
);
|
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
const renderPublishedInfo = (): React.ReactNode =>
|
2019-03-28 05:39:06 +07:00
|
|
|
time && (
|
|
|
|
<OverviewItem>
|
2019-06-20 19:37:28 +07:00
|
|
|
<Icon name="time" />
|
2019-03-28 05:39:06 +07:00
|
|
|
<Published>{`Published on ${formatDate(time)} •`}</Published>
|
|
|
|
{`${formatDateDistance(time)} ago`}
|
|
|
|
</OverviewItem>
|
|
|
|
);
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
const renderHomePageLink = (): React.ReactNode =>
|
2019-05-16 21:25:40 +07:00
|
|
|
homepage &&
|
|
|
|
isURL(homepage) && (
|
2019-03-28 05:39:06 +07:00
|
|
|
<a href={homepage} target={'_blank'}>
|
|
|
|
<Tooltip aria-label={'Homepage'} title={'Visit homepage'}>
|
|
|
|
<IconButton aria-label={'Homepage'}>
|
|
|
|
{/* eslint-disable-next-line react/jsx-max-depth */}
|
|
|
|
<HomeIcon />
|
|
|
|
</IconButton>
|
|
|
|
</Tooltip>
|
|
|
|
</a>
|
2019-02-03 17:23:33 +07:00
|
|
|
);
|
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
const renderBugsLink = (): React.ReactNode =>
|
|
|
|
bugs &&
|
|
|
|
bugs.url &&
|
|
|
|
isURL(bugs.url) && (
|
|
|
|
<a href={bugs.url} target={'_blank'}>
|
2019-03-28 05:39:06 +07:00
|
|
|
<Tooltip aria-label={'Bugs'} title={'Open an issue'}>
|
|
|
|
<IconButton aria-label={'Bugs'}>
|
|
|
|
{/* eslint-disable-next-line react/jsx-max-depth */}
|
|
|
|
<BugReport />
|
|
|
|
</IconButton>
|
|
|
|
</Tooltip>
|
|
|
|
</a>
|
|
|
|
);
|
|
|
|
|
2019-11-08 00:11:08 +07:00
|
|
|
const renderDownloadLink = (): React.ReactNode =>
|
|
|
|
dist &&
|
|
|
|
dist.tarball &&
|
|
|
|
isURL(dist.tarball) && (
|
|
|
|
// eslint-disable-next-line
|
|
|
|
<a onClick={() => downloadHandler(dist.tarball.replace(`https://registry.npmjs.org/`, window.location.href))} target={'_blank'}>
|
|
|
|
<Tooltip aria-label={'Download the tar file'} title={'Download tarball'}>
|
|
|
|
<IconButton aria-label={'Download'}>
|
|
|
|
{/* eslint-disable-next-line react/jsx-max-depth */}
|
|
|
|
<DownloadIcon />
|
|
|
|
</IconButton>
|
|
|
|
</Tooltip>
|
|
|
|
</a>
|
|
|
|
);
|
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
const renderPrimaryComponent = (): React.ReactNode => {
|
2019-03-28 05:39:06 +07:00
|
|
|
return (
|
|
|
|
<Grid container={true} item={true} xs={12}>
|
|
|
|
<Grid item={true} xs={true}>
|
|
|
|
<WrapperLink to={`/-/web/detail/${packageName}`}>
|
|
|
|
{/* eslint-disable-next-line react/jsx-max-depth */}
|
2019-11-08 12:23:22 +07:00
|
|
|
<PackageTitle className="package-title">{packageName}</PackageTitle>
|
2019-03-28 05:39:06 +07:00
|
|
|
</WrapperLink>
|
|
|
|
</Grid>
|
|
|
|
<GridRightAligned item={true} xs={true}>
|
|
|
|
{renderHomePageLink()}
|
|
|
|
{renderBugsLink()}
|
2019-11-08 00:11:08 +07:00
|
|
|
{renderDownloadLink()}
|
2019-03-28 05:39:06 +07:00
|
|
|
</GridRightAligned>
|
|
|
|
</Grid>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
const renderSecondaryComponent = (): React.ReactNode => {
|
2019-03-28 05:39:06 +07:00
|
|
|
const tags = keywords.sort().map((keyword, index) => <Tag key={index}>{keyword}</Tag>);
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Description component={'span'}>{description}</Description>
|
|
|
|
{tags.length > 0 && <TagContainer>{tags}</TagContainer>}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
const renderPackageListItemText = (): React.ReactNode => (
|
2019-10-27 21:49:30 +07:00
|
|
|
<PackageListItemText
|
|
|
|
className="package-link"
|
|
|
|
// @ts-ignore
|
|
|
|
component="div"
|
|
|
|
primary={renderPrimaryComponent()}
|
|
|
|
secondary={renderSecondaryComponent()}
|
|
|
|
/>
|
2019-06-20 19:37:28 +07:00
|
|
|
);
|
|
|
|
|
2019-02-03 17:23:33 +07:00
|
|
|
return (
|
2019-03-28 05:39:06 +07:00
|
|
|
<PackageList className={'package'}>
|
2019-06-20 19:37:28 +07:00
|
|
|
<ListItem alignItems={'flex-start'}>{renderPackageListItemText()}</ListItem>
|
2019-09-29 21:44:10 +07:00
|
|
|
<ListItem alignItems={'flex-start'}>
|
2019-03-28 05:39:06 +07:00
|
|
|
{renderAuthorInfo()}
|
|
|
|
{renderVersionInfo()}
|
|
|
|
{renderPublishedInfo()}
|
|
|
|
{renderFileSize()}
|
|
|
|
{renderLicenseInfo()}
|
2019-09-29 21:44:10 +07:00
|
|
|
</ListItem>
|
2019-03-28 05:39:06 +07:00
|
|
|
</PackageList>
|
2019-02-03 17:23:33 +07:00
|
|
|
);
|
|
|
|
};
|
2019-03-28 05:39:06 +07:00
|
|
|
|
2019-02-03 17:23:33 +07:00
|
|
|
export default Package;
|