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';
|
|
|
|
import ListItemText from '@material-ui/core/ListItemText';
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
import { DetailContextConsumer } from '../../pages/version/Version';
|
2019-03-28 05:39:06 +07:00
|
|
|
import { Heading, AuthorListItem } 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-06-20 19:37:28 +07:00
|
|
|
return context && context.packageMeta && this.renderAuthor(context.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-06-22 16:43:59 +07:00
|
|
|
public renderAuthor = packageMeta => {
|
2019-03-28 05:39:06 +07:00
|
|
|
const { author, name: packageName, version } = packageMeta.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-03-28 05:39:06 +07:00
|
|
|
<List subheader={<Heading variant={'subheading'}>{'Author'}</Heading>}>
|
|
|
|
<AuthorListItem>
|
|
|
|
{this.renderLinkForMail(author.email, avatarComponent, packageName, version)}
|
|
|
|
<ListItemText primary={author.name} />
|
|
|
|
</AuthorListItem>
|
|
|
|
</List>
|
2019-02-03 17:23:33 +07:00
|
|
|
);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Authors;
|