2019-06-20 19:37:28 +07:00
|
|
|
import React, { Component, Fragment, ReactElement } from 'react';
|
2019-06-29 15:03:41 +07:00
|
|
|
import { withRouter, RouteComponentProps } 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-06-20 19:37:28 +07:00
|
|
|
import { DetailContextConsumer, VersionPageConsumerProps } from '../../pages/version/Version';
|
2019-03-28 05:39:06 +07:00
|
|
|
|
|
|
|
import { CardWrap, Heading, Tags, Tag } from './styles';
|
|
|
|
import NoItems from '../NoItems';
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-06-29 15:03:41 +07:00
|
|
|
type DepDetailProps = {
|
2019-06-25 05:54:32 +07:00
|
|
|
name: string;
|
|
|
|
version: string;
|
2019-06-29 15:03:41 +07:00
|
|
|
onLoading?: () => void;
|
|
|
|
} & RouteComponentProps;
|
|
|
|
|
2019-06-25 05:54:32 +07:00
|
|
|
interface DepDetailState {
|
|
|
|
name: string;
|
|
|
|
version: string;
|
|
|
|
}
|
|
|
|
|
2019-06-29 15:03:41 +07:00
|
|
|
class DepDetail extends Component<DepDetailProps, DepDetailState> {
|
2019-06-25 05:54:32 +07:00
|
|
|
constructor(props: DepDetailProps) {
|
2019-02-03 17:23:33 +07:00
|
|
|
super(props);
|
|
|
|
const { name, version } = this.props;
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
name,
|
|
|
|
version,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
public render(): ReactElement<HTMLElement> {
|
2019-02-03 17:23:33 +07:00
|
|
|
const { name, version } = this.state;
|
|
|
|
const tagText = `${name}@${version}`;
|
2019-03-28 05:39:06 +07:00
|
|
|
return <Tag className={'dep-tag'} clickable={true} component={'div'} label={tagText} onClick={this.handleOnClick} />;
|
2019-02-03 17:23:33 +07:00
|
|
|
}
|
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
private handleOnClick = () => {
|
2019-02-03 17:23:33 +07:00
|
|
|
const { name } = this.state;
|
|
|
|
const { onLoading, history } = this.props;
|
|
|
|
|
2019-06-29 15:03:41 +07:00
|
|
|
onLoading && onLoading();
|
2019-03-28 05:39:06 +07:00
|
|
|
history.push(`/-/web/detail/${name}`);
|
2019-02-03 17:23:33 +07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-06-29 15:03:41 +07:00
|
|
|
const WrapperDependencyDetail = withRouter(DepDetail);
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-06-25 05:54:32 +07:00
|
|
|
class DependencyBlock extends Component<{ title: string; dependencies: [] }> {
|
2019-06-20 19:37:28 +07:00
|
|
|
public render(): ReactElement<HTMLElement> {
|
2019-02-03 17:23:33 +07:00
|
|
|
const { dependencies, title } = this.props;
|
2019-06-25 05:54:32 +07:00
|
|
|
const deps = Object.entries(dependencies) as [];
|
2019-02-03 17:23:33 +07:00
|
|
|
|
|
|
|
return (
|
|
|
|
<DetailContextConsumer>
|
2019-06-25 05:54:32 +07:00
|
|
|
{({ enableLoading }) => {
|
2019-02-03 17:23:33 +07:00
|
|
|
return (
|
|
|
|
<CardWrap>
|
|
|
|
<CardContent>
|
2019-06-20 19:37:28 +07:00
|
|
|
<Heading variant="subheading">{`${title} (${deps.length})`}</Heading>
|
2019-02-03 17:23:33 +07:00
|
|
|
<Tags>{this.renderTags(deps, enableLoading)}</Tags>
|
|
|
|
</CardContent>
|
|
|
|
</CardWrap>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</DetailContextConsumer>
|
|
|
|
);
|
|
|
|
}
|
2019-03-28 05:39:06 +07:00
|
|
|
|
2019-06-25 05:54:32 +07:00
|
|
|
private renderTags = (deps: [], enableLoading?: () => void) =>
|
2019-03-28 05:39:06 +07:00
|
|
|
deps.map(dep => {
|
2019-06-25 05:54:32 +07:00
|
|
|
const [name, version] = dep as [string, string];
|
2019-03-28 05:39:06 +07:00
|
|
|
|
2019-04-09 03:29:20 +07:00
|
|
|
return <WrapperDependencyDetail key={name} name={name} onLoading={enableLoading} version={version} />;
|
2019-03-28 05:39:06 +07:00
|
|
|
});
|
2019-02-03 17:23:33 +07:00
|
|
|
}
|
|
|
|
|
2019-06-25 05:54:32 +07:00
|
|
|
class Dependencies extends Component {
|
2019-06-20 19:37:28 +07:00
|
|
|
public state = {
|
2019-02-03 17:23:33 +07:00
|
|
|
tabPosition: 0,
|
|
|
|
};
|
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
public render(): ReactElement<HTMLElement> {
|
2019-02-03 17:23:33 +07:00
|
|
|
return (
|
|
|
|
<DetailContextConsumer>
|
|
|
|
{packageMeta => {
|
2019-06-20 19:37:28 +07:00
|
|
|
return this.renderDependencies(packageMeta as VersionPageConsumerProps);
|
2019-02-03 17:23:33 +07:00
|
|
|
}}
|
|
|
|
</DetailContextConsumer>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-25 05:54:32 +07:00
|
|
|
private checkDependencyLength<T>(dependency: Record<string, T> = {}): boolean {
|
2019-03-28 05:39:06 +07:00
|
|
|
return Object.keys(dependency).length > 0;
|
|
|
|
}
|
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
private renderDependencies({ packageMeta }): ReactElement<HTMLElement> {
|
2019-02-03 17:23:33 +07:00
|
|
|
const { latest } = packageMeta;
|
2019-03-28 05:39:06 +07:00
|
|
|
const { dependencies, devDependencies, peerDependencies, name } = latest;
|
|
|
|
|
|
|
|
const dependencyMap = { dependencies, devDependencies, peerDependencies };
|
|
|
|
|
|
|
|
const dependencyList = Object.keys(dependencyMap).reduce((result, value, key) => {
|
|
|
|
const selectedDepndency = dependencyMap[value];
|
|
|
|
if (selectedDepndency && this.checkDependencyLength(selectedDepndency)) {
|
2019-06-20 19:37:28 +07:00
|
|
|
// @ts-ignore
|
|
|
|
result.push(<DependencyBlock className="dependency-block" dependencies={selectedDepndency} key={key} title={value} />);
|
2019-03-28 05:39:06 +07:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
if (dependencyList.length) {
|
|
|
|
return <Fragment>{dependencyList}</Fragment>;
|
|
|
|
}
|
2019-06-20 19:37:28 +07:00
|
|
|
return <NoItems className="no-dependencies" text={`${name} has no dependencies.`} />;
|
2019-02-03 17:23:33 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Dependencies;
|