This repository has been archived by the owner on Jan 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: convert Author component to hooks
- Loading branch information
1 parent
f8a1f2c
commit 0ba12a1
Showing
4 changed files
with
57 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,44 @@ | ||
import React, { Component, ReactNode, ReactElement } from 'react'; | ||
import React, { FC, useContext } from 'react'; | ||
|
||
import Avatar from '@material-ui/core/Avatar'; | ||
import List from '@material-ui/core/List'; | ||
|
||
import { DetailContextConsumer } from '../../pages/Version'; | ||
import { DetailContext } from '../../pages/Version'; | ||
import { Heading, AuthorListItem, AuthorListItemText } from './styles'; | ||
import { isEmail } from '../../utils/url'; | ||
|
||
class Authors extends Component { | ||
public render(): ReactElement<HTMLElement> { | ||
return ( | ||
<DetailContextConsumer> | ||
{context => { | ||
const { packageMeta } = context; | ||
|
||
if (!packageMeta) { | ||
return null; | ||
} | ||
|
||
return this.renderAuthor(packageMeta); | ||
}} | ||
</DetailContextConsumer> | ||
); | ||
const Authors: FC = () => { | ||
const { packageMeta } = useContext(DetailContext); | ||
|
||
if (!packageMeta) { | ||
return null; | ||
} | ||
|
||
public renderLinkForMail(email: string, avatarComponent: ReactNode, packageName: string, version: string): ReactElement<HTMLElement> | ReactNode { | ||
if (!email || isEmail(email) === false) { | ||
return avatarComponent; | ||
} | ||
const { author, name: packageName, version } = packageMeta.latest; | ||
|
||
return ( | ||
<a href={`mailto:${email}?subject=${packageName}@${version}`} target={'_top'}> | ||
{avatarComponent} | ||
</a> | ||
); | ||
if (!author) { | ||
return null; | ||
} | ||
|
||
public renderAuthor = ({ latest }) => { | ||
const { author, name: packageName, version } = latest; | ||
|
||
if (!author) { | ||
return null; | ||
} | ||
|
||
const avatarComponent = <Avatar alt={author.name} src={author.avatar} />; | ||
return ( | ||
<List subheader={<Heading variant={'subtitle1'}>{'Author'}</Heading>}> | ||
<AuthorListItem button={true}> | ||
{this.renderLinkForMail(author.email, avatarComponent, packageName, version)} | ||
<AuthorListItemText primary={author.name} /> | ||
</AuthorListItem> | ||
</List> | ||
); | ||
}; | ||
} | ||
const { email, name } = author; | ||
|
||
const avatarComponent = <Avatar alt={author.name} src={author.avatar} />; | ||
|
||
return ( | ||
<List subheader={<Heading variant={'subtitle1'}>{'Author'}</Heading>}> | ||
<AuthorListItem button={true}> | ||
{!email || !isEmail(email) ? ( | ||
<>{avatarComponent}</> | ||
) : ( | ||
<a href={`mailto:${email}?subject=${packageName}@${version}`} target={'_top'}> | ||
{avatarComponent} | ||
</a> | ||
)} | ||
|
||
<AuthorListItemText primary={name} /> | ||
</AuthorListItem> | ||
</List> | ||
); | ||
}; | ||
|
||
export default Authors; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters