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.
feat: version Component - Replaced classes by func. comp (#129)
* refactor: replaced classes by func comp * fix: fixed space margin * refactor: changed display logic * fix: fixed types * fix: fixed Version test * fix: fixed version style
- Loading branch information
1 parent
1a74c08
commit 1d705da
Showing
7 changed files
with
146 additions
and
111 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,90 +1,46 @@ | ||
import React, { ReactElement } from 'react'; | ||
import List from '@material-ui/core/List'; | ||
import { Link as RouterLink } from 'react-router-dom'; | ||
import Link from '@material-ui/core/Link'; | ||
import ListItem from '@material-ui/core/ListItem'; | ||
import React, { useContext } from 'react'; | ||
|
||
import { DetailContextConsumer } from '../../pages/Version'; | ||
import { formatDateDistance } from '../../utils/package'; | ||
import { DIST_TAGS } from '../../../lib/constants'; | ||
import { DetailContext } from '../../pages/Version'; | ||
|
||
import { Heading } from './styles'; | ||
|
||
import { Heading, Spacer, ListItemText } from './styles'; | ||
import VersionsTagList from './VersionsTagList'; | ||
import VersionsHistoryList from './VersionsHistoryList'; | ||
|
||
import { DIST_TAGS } from '../../../lib/constants'; | ||
|
||
export const NOT_AVAILABLE = 'Not available'; | ||
export const LABEL_CURRENT_TAGS = 'Current Tags'; | ||
export const LABEL_VERSION_HISTORY = 'Version History'; | ||
|
||
class Versions extends React.PureComponent { | ||
public render(): ReactElement<HTMLDivElement> { | ||
return ( | ||
<DetailContextConsumer> | ||
{context => { | ||
const { packageMeta, packageName } = context; | ||
const Versions: React.FC = () => { | ||
const detailContext = useContext(DetailContext); | ||
|
||
if (!packageMeta) { | ||
return null; | ||
} | ||
const { packageMeta, packageName } = detailContext; | ||
|
||
return this.renderContent(packageMeta, packageName); | ||
}} | ||
</DetailContextConsumer> | ||
); | ||
if (!packageMeta) { | ||
return null; | ||
} | ||
|
||
public renderPackageList = (packages: {}, timeMap: Record<string, {}>, packageName): ReactElement<HTMLDivElement> => { | ||
return ( | ||
<List dense={true}> | ||
{Object.keys(packages) | ||
.reverse() | ||
.map(version => ( | ||
<ListItem className="version-item" key={version}> | ||
<Link component={RouterLink} to={`/-/web/detail/${packageName}/v/${version}`}> | ||
<ListItemText>{version}</ListItemText> | ||
</Link> | ||
<Spacer /> | ||
<ListItemText>{timeMap[version] ? `${formatDateDistance(timeMap[version])} ago` : NOT_AVAILABLE}</ListItemText> | ||
</ListItem> | ||
))} | ||
</List> | ||
); | ||
}; | ||
|
||
public renderTagList = (packages: {}): ReactElement<HTMLDivElement> => { | ||
return ( | ||
<List dense={true}> | ||
{Object.keys(packages) | ||
.reverse() | ||
.map(tag => ( | ||
<ListItem className="version-item" key={tag}> | ||
<ListItemText>{tag}</ListItemText> | ||
<Spacer /> | ||
<ListItemText>{packages[tag]}</ListItemText> | ||
</ListItem> | ||
))} | ||
</List> | ||
); | ||
}; | ||
|
||
public renderContent(packageMeta, packageName): ReactElement<HTMLDivElement> { | ||
const { versions = {}, time: timeMap = {}, [DIST_TAGS]: distTags = {} } = packageMeta; | ||
|
||
return ( | ||
<> | ||
{distTags && ( | ||
<> | ||
<Heading variant="subtitle1">{LABEL_CURRENT_TAGS}</Heading> | ||
{this.renderTagList(distTags)} | ||
</> | ||
)} | ||
{versions && ( | ||
<> | ||
<Heading variant="subtitle1">{LABEL_VERSION_HISTORY}</Heading> | ||
{this.renderPackageList(versions, timeMap, packageName)} | ||
</> | ||
)} | ||
</> | ||
); | ||
} | ||
} | ||
// @ts-ignore - Property 'dist-tags' does not exist on type 'PackageMetaInterface' | ||
const { versions = {}, time = {}, [DIST_TAGS]: distTags = {} } = packageMeta; | ||
|
||
return ( | ||
<> | ||
{distTags && Object.keys(distTags).length > 0 && ( | ||
<> | ||
<Heading variant="subtitle1">{LABEL_CURRENT_TAGS}</Heading> | ||
<VersionsTagList tags={distTags} /> | ||
</> | ||
)} | ||
{versions && Object.keys(versions).length > 0 && packageName && ( | ||
<> | ||
<Heading variant="subtitle1">{LABEL_VERSION_HISTORY}</Heading> | ||
<VersionsHistoryList packageName={packageName} time={time} versions={versions} /> | ||
</> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
export default Versions; |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React from 'react'; | ||
import List from '@material-ui/core/List'; | ||
import Link from '@material-ui/core/Link'; | ||
import ListItem from '@material-ui/core/ListItem'; | ||
import { Link as RouterLink } from 'react-router-dom'; | ||
import { Spacer, ListItemText } from './styles'; | ||
|
||
import { Versions, Time } from '../../../types/packageMeta'; | ||
import { formatDateDistance } from '../../utils/package'; | ||
|
||
export const NOT_AVAILABLE = 'Not available'; | ||
|
||
interface Props { | ||
versions: Versions; | ||
packageName: string; | ||
time: Time; | ||
} | ||
|
||
const VersionsHistoryList: React.FC<Props> = ({ versions, packageName, time }) => ( | ||
<List dense={true}> | ||
{Object.keys(versions) | ||
.reverse() | ||
.map(version => ( | ||
<ListItem className="version-item" key={version}> | ||
<Link component={RouterLink} to={`/-/web/detail/${packageName}/v/${version}`}> | ||
<ListItemText>{version}</ListItemText> | ||
</Link> | ||
<Spacer /> | ||
<ListItemText>{time[version] ? `${formatDateDistance(time[version])} ago` : NOT_AVAILABLE}</ListItemText> | ||
</ListItem> | ||
))} | ||
</List> | ||
); | ||
|
||
export default VersionsHistoryList; |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React from 'react'; | ||
import List from '@material-ui/core/List'; | ||
import ListItem from '@material-ui/core/ListItem'; | ||
import { Spacer, ListItemText } from './styles'; | ||
|
||
import { DistTags } from '../../../types/packageMeta'; | ||
|
||
interface Props { | ||
tags: DistTags; | ||
} | ||
|
||
const VersionsTagList: React.FC<Props> = ({ tags }) => ( | ||
<List dense={true}> | ||
{Object.keys(tags) | ||
.reverse() | ||
.map(tag => ( | ||
<ListItem className="version-item" key={tag}> | ||
<ListItemText>{tag}</ListItemText> | ||
<Spacer /> | ||
<ListItemText>{tags[tag]}</ListItemText> | ||
</ListItem> | ||
))} | ||
</List> | ||
); | ||
|
||
export default VersionsTagList; |
Large diffs are not rendered by default.
Oops, something went wrong.
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