verdaccio-ui/src/components/Dependencies/Dependencies.tsx

79 lines
2.4 KiB
TypeScript
Raw Normal View History

import React, { useContext } from 'react';
import { useHistory } from 'react-router-dom';
2019-02-03 17:23:33 +07:00
import CardContent from '../../muiComponents/CardContent';
import { PackageDependencies } from '../../../types/packageMeta';
import { DetailContext } from '../../pages/Version';
import NoItems from '../NoItems';
2019-03-28 05:39:06 +07:00
import { CardWrap, StyledText, Tags, Tag } from './styles';
2019-02-03 17:23:33 +07:00
interface DependencyBlockProps {
title: string;
dependencies: PackageDependencies;
}
const DependencyBlock: React.FC<DependencyBlockProps> = ({ title, dependencies }) => {
const { enableLoading } = useContext(DetailContext);
const history = useHistory();
2019-02-03 17:23:33 +07:00
const deps = Object.entries(dependencies);
2019-02-03 17:23:33 +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-02-03 17:23:33 +07:00
return (
<CardWrap>
<CardContent>
<StyledText variant="subtitle1">{`${title} (${deps.length})`}</StyledText>
<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
const Dependencies: React.FC<{}> = () => {
const { packageMeta } = useContext(DetailContext);
2019-02-03 17:23:33 +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
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
if (hasDependencies) {
2019-02-03 17:23:33 +07:00
return (
<>
{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
);
}
return <NoItems className="no-dependencies" text={`${name} has no dependencies.`} />;
};
2019-02-03 17:23:33 +07:00
export default Dependencies;