2019-10-11 20:02:53 +07:00
|
|
|
import React, { useContext } from 'react';
|
|
|
|
import { useHistory } from 'react-router-dom';
|
2019-06-20 19:37:28 +07:00
|
|
|
import CardContent from '@material-ui/core/CardContent';
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-10-11 20:02:53 +07:00
|
|
|
import { PackageDependencies } from '../../../types/packageMeta';
|
|
|
|
import { DetailContext } from '../../pages/Version';
|
2019-10-08 03:19:18 +07:00
|
|
|
import NoItems from '../NoItems';
|
2019-03-28 05:39:06 +07:00
|
|
|
|
|
|
|
import { CardWrap, Heading, Tags, Tag } from './styles';
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-10-11 20:02:53 +07:00
|
|
|
interface DependencyBlockProps {
|
|
|
|
title: string;
|
|
|
|
dependencies: PackageDependencies;
|
2019-06-25 05:54:32 +07:00
|
|
|
}
|
|
|
|
|
2019-10-11 20:02:53 +07:00
|
|
|
const DependencyBlock: React.FC<DependencyBlockProps> = ({ title, dependencies }) => {
|
|
|
|
const { enableLoading } = useContext(DetailContext);
|
|
|
|
const history = useHistory();
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-10-11 20:02:53 +07:00
|
|
|
const deps = Object.entries(dependencies);
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-10-11 20:02:53 +07:00
|
|
|
function handleClick(name: string): void {
|
|
|
|
enableLoading && enableLoading();
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-03-28 05:39:06 +07:00
|
|
|
history.push(`/-/web/detail/${name}`);
|
2019-10-11 20:02:53 +07:00
|
|
|
}
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-10-11 20:02:53 +07:00
|
|
|
return (
|
|
|
|
<CardWrap>
|
|
|
|
<CardContent>
|
|
|
|
<Heading variant="subtitle1">{`${title} (${deps.length})`}</Heading>
|
|
|
|
<Tags>
|
|
|
|
{deps.map(([name, version]) => (
|
|
|
|
// eslint-disable-next-line
|
|
|
|
<Tag className={'dep-tag'} clickable={true} key={name} label={`${name}@${version}`} onClick={() => handleClick(name)} />
|
|
|
|
))}
|
|
|
|
</Tags>
|
|
|
|
</CardContent>
|
|
|
|
</CardWrap>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
function hasKeys(object?: { [key: string]: any }): boolean {
|
|
|
|
return !!object && Object.keys(object).length > 0;
|
|
|
|
}
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-10-11 20:02:53 +07:00
|
|
|
const Dependencies: React.FC<{}> = () => {
|
|
|
|
const { packageMeta } = useContext(DetailContext);
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-10-11 20:02:53 +07:00
|
|
|
if (!packageMeta) {
|
|
|
|
throw new Error('packageMeta is required at DetailContext');
|
2019-02-03 17:23:33 +07:00
|
|
|
}
|
2019-03-28 05:39:06 +07:00
|
|
|
|
2019-10-11 20:02:53 +07:00
|
|
|
const { latest } = packageMeta;
|
|
|
|
// FIXME: add dependencies to package meta type
|
|
|
|
// @ts-ignore
|
|
|
|
const { dependencies, devDependencies, peerDependencies, name } = latest;
|
|
|
|
const dependencyMap = { dependencies, devDependencies, peerDependencies };
|
|
|
|
const hasDependencies = hasKeys(dependencies) || hasKeys(devDependencies) || hasKeys(peerDependencies);
|
2019-03-28 05:39:06 +07:00
|
|
|
|
2019-10-11 20:02:53 +07:00
|
|
|
if (hasDependencies) {
|
2019-02-03 17:23:33 +07:00
|
|
|
return (
|
2019-10-11 20:02:53 +07:00
|
|
|
<>
|
|
|
|
{Object.entries(dependencyMap).map(([dependencyType, dependencies]) => {
|
|
|
|
if (!dependencies || Object.keys(dependencies).length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return <DependencyBlock dependencies={dependencies} key={dependencyType} title={dependencyType} />;
|
|
|
|
})}
|
|
|
|
</>
|
2019-02-03 17:23:33 +07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-10-11 20:02:53 +07:00
|
|
|
return <NoItems className="no-dependencies" text={`${name} has no dependencies.`} />;
|
|
|
|
};
|
2019-02-03 17:23:33 +07:00
|
|
|
|
|
|
|
export default Dependencies;
|