Skip to content

Commit 6af08d3

Browse files
authored
[UI] Prettify dependencies (loic-sharma#375)
Addresses loic-sharma#369
1 parent 73e244f commit 6af08d3

File tree

4 files changed

+101
-16
lines changed

4 files changed

+101
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.dependency-groups h4 {
2+
line-height: 1.4;
3+
}
4+
5+
.dependency-groups h4 > span {
6+
border-bottom: 1px solid #d8d8d8;
7+
margin-bottom: 3px;
8+
}
9+
10+
.dependency-group li span {
11+
color: #666;
12+
}

src/BaGet.UI/src/DisplayPackage/Dependencies.tsx

+63-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import { Link } from 'react-router-dom';
12
import * as React from 'react';
23
import * as Registration from './Registration';
34

5+
import './Dependencies.css';
6+
47
interface IDependenciesProps {
58
dependencyGroups: Registration.IDependencyGroup[];
69
}
@@ -11,14 +14,60 @@ interface IPackageDependenciesProps {
1114

1215
class Dependencies extends React.Component<IDependenciesProps> {
1316

14-
constructor(props: IDependenciesProps) {
15-
props.dependencyGroups.forEach(group => {
16-
if (!group.dependencies) {
17-
group.dependencies = [];
18-
}
19-
});
17+
static readonly netFrameworkRegex : RegExp = /net[0-9]{2,3}$/;
18+
static readonly netCoreRegex : RegExp = /netcoreapp[0-9].[0-9]$/;
19+
static readonly netStandardRegex : RegExp = /netstandard[0-9].[0-9]$/;
20+
static readonly versionRangeRegex : RegExp = /\[[0-9](.[0-9])*, \)$/;
2021

22+
constructor(props: IDependenciesProps) {
2123
super(props);
24+
25+
props.dependencyGroups.forEach(Dependencies.prettifyDependencyGroup);
26+
}
27+
28+
private static prettifyDependencyGroup(group: Registration.IDependencyGroup) {
29+
if (!group.dependencies) {
30+
group.dependencies = [];
31+
}
32+
33+
Dependencies.prettifyTargetFramework(group);
34+
35+
if (group.dependencies !== undefined) {
36+
group.dependencies.forEach(Dependencies.prettifyDepency);
37+
}
38+
}
39+
40+
private static prettifyTargetFramework(group: Registration.IDependencyGroup) {
41+
// This uses heuristics and may produce incorrect results.
42+
// This ignores portable class libraries.
43+
if (Dependencies.netFrameworkRegex.test(group.targetFramework)) {
44+
const version = group.targetFramework.substring("net".length);
45+
const prettyVersion = version.length == 2
46+
? `${version[0]}.${version[1]}`
47+
: `${version[0]}.${version[1]}.${version[2]}`;
48+
49+
group.targetFramework = `.NET Framework ${prettyVersion}`;
50+
return;
51+
}
52+
53+
if (Dependencies.netCoreRegex.test(group.targetFramework)) {
54+
const version = group.targetFramework.substring("netcoreapp".length);
55+
group.targetFramework = `.NET Core ${version}`;
56+
return;
57+
}
58+
59+
if (Dependencies.netStandardRegex.test(group.targetFramework)) {
60+
const version = group.targetFramework.substring("netstandard".length);
61+
group.targetFramework = `.NET Standard ${version}`;
62+
return;
63+
}
64+
}
65+
66+
private static prettifyDepency(dependency: Registration.IDependency) {
67+
// This uses heuristics and may produce incorrect results.
68+
if (Dependencies.versionRangeRegex.test(dependency.range)) {
69+
dependency.range = `(>= ${dependency.range.slice(1, -3)})`;
70+
}
2271
}
2372

2473
public render() {
@@ -36,10 +85,12 @@ class Dependencies extends React.Component<IDependenciesProps> {
3685
<div>
3786
<h3>Dependencies</h3>
3887

39-
<div>
88+
<div className="dependency-groups">
4089
{this.props.dependencyGroups.map(group => (
4190
<div key={group.targetFramework}>
42-
<h4>{group.targetFramework}</h4>
91+
<h4>
92+
<span>{group.targetFramework}</span>
93+
</h4>
4394

4495
<PackageDependencies dependencies={group.dependencies} />
4596
</div>
@@ -59,10 +110,12 @@ class PackageDependencies extends React.Component<IPackageDependenciesProps> {
59110
}
60111

61112
return (
62-
<ul>
113+
<ul className="list-unstyled dependency-group">
63114
{this.props.dependencies.map(dependency => (
64115
<li key={dependency.id}>
65-
{dependency.id} {dependency.range}
116+
<Link to={`/packages/${dependency.id}`}>{dependency.id}</Link>
117+
118+
<span> {dependency.range}</span>
66119
</li>
67120
))}
68121
</ul>

src/BaGet.UI/src/DisplayPackage/DisplayPackage.tsx

+20-6
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ interface IPackageVersion {
5454

5555
interface IDisplayPackageState {
5656
package?: IPackage;
57+
loading: boolean;
5758
}
5859

5960
class DisplayPackage extends React.Component<IDisplayPackageProps, IDisplayPackageState> {
@@ -79,7 +80,7 @@ class DisplayPackage extends React.Component<IDisplayPackageProps, IDisplayPacka
7980

8081
this.id = props.match.params.id.toLowerCase();
8182
this.version = props.match.params.version;
82-
this.state = {package: undefined};
83+
this.state = {package: undefined, loading: true};
8384
}
8485

8586
public componentWillUnmount() {
@@ -108,8 +109,16 @@ class DisplayPackage extends React.Component<IDisplayPackageProps, IDisplayPacka
108109
const url = `${config.apiUrl}/v3/registration/${this.id}/index.json`;
109110

110111
fetch(url, {signal: this.registrationController.signal}).then(response => {
111-
return response.json();
112+
return (response.ok) ? response.json() : null;
112113
}).then(json => {
114+
if (!json) {
115+
this.setState(prevState => {
116+
return { ...prevState, loading: false };
117+
});
118+
119+
return;
120+
}
121+
113122
const results = json as Registration.IRegistrationIndex;
114123

115124
const latestVersion = results.items[0].upper;
@@ -143,6 +152,7 @@ class DisplayPackage extends React.Component<IDisplayPackageProps, IDisplayPacka
143152
currentItem.catalogEntry.packageTypes.indexOf("DotnetTool") !== -1);
144153

145154
this.setState({
155+
loading: false,
146156
package: {
147157
...currentItem.catalogEntry,
148158
downloadUrl: currentItem.packageContent,
@@ -178,10 +188,14 @@ class DisplayPackage extends React.Component<IDisplayPackageProps, IDisplayPacka
178188
}
179189

180190
public render() {
181-
if (!this.state.package) {
182-
return (
183-
<div>...</div>
184-
);
191+
if (this.state.loading) {
192+
return (
193+
<div>...</div>
194+
);
195+
} else if (!this.state.package) {
196+
return (
197+
<div>Could not find package '{this.id}'.</div>
198+
);
185199
} else {
186200
return (
187201
<div className="row display-package">

src/BaGet.UI/src/index.css

+6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ h2, h3 {
2424
margin-top: 40px;
2525
}
2626

27+
h4 {
28+
margin-top: 11px;
29+
margin-bottom: 11px;
30+
font-size: 20px;
31+
}
32+
2733
.img-responsive {
2834
display: block;
2935
max-width: 100%;

0 commit comments

Comments
 (0)