Skip to content

Commit

Permalink
feat: Add basic package infos and resource links to sidebar.
Browse files Browse the repository at this point in the history
Shows homepage, repository url and license if available in meta data.
If no data is available the module isn't shown.
  • Loading branch information
Peter Kaske committed Jun 13, 2018
1 parent 9159e20 commit 7bd3a4f
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/webui/src/components/PackageSidebar/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
import LastSync from './modules/LastSync';
import Maintainers from './modules/Maintainers';
import Dependencies from './modules/Dependencies';
import Infos from './modules/Infos';

import API from '../../../utils/api';

Expand Down Expand Up @@ -53,6 +54,7 @@ export default class PackageSidebar extends React.Component {
return packageMeta ?
(<aside className="sidebar-info">
<LastSync packageMeta={packageMeta} />
<Infos packageMeta={packageMeta} />
<Maintainers packageMeta={packageMeta} />
<Dependencies packageMeta={packageMeta} />
{/* Package management module? Help us implement it! */}
Expand Down
61 changes: 61 additions & 0 deletions src/webui/src/components/PackageSidebar/modules/Infos/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import React from 'react';
import PropTypes from 'prop-types';
import get from 'lodash/get';
import Module from '../../Module';

import classes from './style.scss';

export default class Infos extends React.Component {
static propTypes = {
packageMeta: PropTypes.object.isRequired
};

get infos() {
const homepage = this.normalizeInfo(get(this, 'props.packageMeta.latest.homepage', null));
const repo = this.normalizeInfo(get(this, 'props.packageMeta.latest.repository', null));
const license = get(this, 'props.packageMeta.latest.license', 'N/A');

return {homepage, repo, license};
}

normalizeInfo(infoObj) {
if (typeof infoObj === 'string') {
return {url: infoObj};
} else if (infoObj === null) {
return {url: ''};
}

infoObj.url = infoObj.url.replace(/^git\+/, '');

return infoObj;
}

render() {
const infos = this.infos;

if (!infos.homepage.url && !infos.repo.url && infos.license === 'N/A') {
return '';
}

return (
<Module
title="Infos"
className={classes.infosModule}
>
<ul>
{infos.homepage.url &&
<li><span>Homepage</span><a href={infos.homepage.url} target="_blank">{infos.homepage.url}</a></li>
}

{infos.repo.url &&
<li><span>Repository</span><a href={infos.repo.url} target="_blank">{infos.repo.url}</a></li>
}

{infos.license &&
<li><span>License</span><span>{infos.license}</span></li>
}
</ul>
</Module>
);
}
}
22 changes: 22 additions & 0 deletions src/webui/src/components/PackageSidebar/modules/Infos/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@import '../../../../styles/variable';

.infosModule {
li {
display: flex;
font-size: 14px;
line-height: 2;

a {
color: inherit;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
max-width: 150px;
}

a:last-child,
span:last-child {
margin-left: auto;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`<PackageSidebar /> : <Infos /> should load the Info component and match snapshot 1`] = `"<div class=\\"module infosModule\\"><h2 class=\\"moduleTitle\\">Infos</h2><div><ul><li><span>Homepage</span><a href=\\"https://github.com/verdaccio/verdaccio#readme\\" target=\\"_blank\\">https://github.com/verdaccio/verdaccio#readme</a></li><li><span>Repository</span><a href=\\"git://github.com/verdaccio/verdaccio.git\\" target=\\"_blank\\">git://github.com/verdaccio/verdaccio.git</a></li><li><span>License</span><span>WTFPL</span></li></ul></div></div>"`;
29 changes: 29 additions & 0 deletions test/webui/components/PackageSidebar/infos.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* LastSync component
*/

import React from 'react';
import { mount, shallow } from 'enzyme';
import Infos from '../../../../src/webui/src/components/PackageSidebar/modules/Infos';
import { packageMeta } from '../store/packageMeta';

console.error = jest.fn();

describe('<PackageSidebar /> : <Infos />', () => {
it('should load the component and check getter: info with package data', () => {
const wrapper = mount(<Infos packageMeta={packageMeta} />);
const instance = wrapper.instance();
const result = {
repo: { type: 'git', url: 'git://github.com/verdaccio/verdaccio.git' },
homepage: { url: 'https://github.com/verdaccio/verdaccio#readme' },
license: 'WTFPL'
};

expect(instance.infos).toEqual(result);
});

it('should load the Info component and match snapshot', () => {
const wrapper = shallow(<Infos packageMeta={packageMeta} />);
expect(wrapper.html()).toMatchSnapshot();
});
});

0 comments on commit 7bd3a4f

Please sign in to comment.