2019-06-22 16:43:59 +07:00
|
|
|
import React, { Component, ReactNode, ReactElement } from 'react';
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
import Avatar from '@material-ui/core/Avatar';
|
|
|
|
import List from '@material-ui/core/List';
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-08-25 19:34:27 +07:00
|
|
|
import { DetailContextConsumer } from '../../pages/Version';
|
2019-08-31 16:02:46 +07:00
|
|
|
import { Heading, AuthorListItem, AuthorListItemText } from './styles';
|
2019-05-16 21:25:40 +07:00
|
|
|
import { isEmail } from '../../utils/url';
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-06-22 16:43:59 +07:00
|
|
|
class Authors extends Component {
|
|
|
|
public render(): ReactElement<HTMLElement> {
|
2019-02-03 17:23:33 +07:00
|
|
|
return (
|
|
|
|
<DetailContextConsumer>
|
2019-06-22 16:43:59 +07:00
|
|
|
{context => {
|
2019-08-31 16:02:46 +07:00
|
|
|
const { packageMeta } = context;
|
|
|
|
|
|
|
|
if (!packageMeta) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.renderAuthor(packageMeta);
|
2019-02-03 17:23:33 +07:00
|
|
|
}}
|
|
|
|
</DetailContextConsumer>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-22 16:43:59 +07:00
|
|
|
public renderLinkForMail(email: string, avatarComponent: ReactNode, packageName: string, version: string): ReactElement<HTMLElement> | ReactNode {
|
2019-05-16 21:25:40 +07:00
|
|
|
if (!email || isEmail(email) === false) {
|
2019-03-28 05:39:06 +07:00
|
|
|
return avatarComponent;
|
|
|
|
}
|
2019-05-16 21:25:40 +07:00
|
|
|
|
2019-03-28 05:39:06 +07:00
|
|
|
return (
|
|
|
|
<a href={`mailto:${email}?subject=${packageName}@${version}`} target={'_top'}>
|
|
|
|
{avatarComponent}
|
|
|
|
</a>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-08-31 16:02:46 +07:00
|
|
|
public renderAuthor = ({ latest }) => {
|
|
|
|
const { author, name: packageName, version } = latest;
|
2019-02-03 17:23:33 +07:00
|
|
|
|
|
|
|
if (!author) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-03-28 05:39:06 +07:00
|
|
|
const avatarComponent = <Avatar alt={author.name} src={author.avatar} />;
|
2019-02-03 17:23:33 +07:00
|
|
|
return (
|
2019-08-31 16:02:46 +07:00
|
|
|
<List subheader={<Heading variant={'subtitle1'}>{'Author'}</Heading>}>
|
|
|
|
<AuthorListItem button={true}>
|
2019-03-28 05:39:06 +07:00
|
|
|
{this.renderLinkForMail(author.email, avatarComponent, packageName, version)}
|
2019-08-31 16:02:46 +07:00
|
|
|
<AuthorListItemText primary={author.name} />
|
2019-03-28 05:39:06 +07:00
|
|
|
</AuthorListItem>
|
|
|
|
</List>
|
2019-02-03 17:23:33 +07:00
|
|
|
);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Authors;
|