Skip to content

Commit bc6c1ff

Browse files
Improve the pdependents UI (loic-sharma#572)
Addresses loic-sharma#570
1 parent 7c514bf commit bc6c1ff

File tree

8 files changed

+110
-14
lines changed

8 files changed

+110
-14
lines changed

src/BaGet.Azure/Search/AzureSearchService.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,12 @@ public async Task<DependentsResponse> FindDependentsAsync(
163163

164164
var response = await _searchClient.Documents.SearchAsync<PackageDocument>(query, parameters, cancellationToken: cancellationToken);
165165
var results = response.Results
166-
.Select(r => r.Document.Id)
166+
.Select(r => new DependentResult
167+
{
168+
Id = r.Document.Id,
169+
Description = r.Document.Description,
170+
TotalDownloads = r.Document.TotalDownloads
171+
})
167172
.ToList()
168173
.AsReadOnly();
169174

src/BaGet.Azure/Table/TableSearchService.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class TableSearchService : ISearchService
2121
Task.FromResult(new DependentsResponse
2222
{
2323
TotalHits = 0,
24-
Data = EmptyStringList
24+
Data = new List<DependentResult>()
2525
});
2626

2727
private readonly CloudTable _table;

src/BaGet.Core/Search/DatabaseSearchService.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,12 @@ public async Task<DependentsResponse> FindDependentsAsync(string packageId, Canc
125125
.OrderByDescending(p => p.Downloads)
126126
.Where(p => p.Dependencies.Any(d => d.Id == packageId))
127127
.Take(20)
128-
.Select(p => p.Id)
128+
.Select(r => new DependentResult
129+
{
130+
Id = r.Id,
131+
Description = r.Description,
132+
TotalDownloads = r.Downloads
133+
})
129134
.Distinct()
130135
.ToListAsync(cancellationToken);
131136

src/BaGet.Core/Search/DependentsResponse.cs

+25-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,30 @@ public class DependentsResponse
1919
/// The package IDs matched by the dependent query.
2020
/// </summary>
2121
[JsonPropertyName("data")]
22-
public IReadOnlyList<string> Data { get; set; }
22+
public IReadOnlyList<DependentResult> Data { get; set; }
23+
}
24+
25+
/// <summary>
26+
/// A package that depends on the queried package.
27+
/// </summary>
28+
public class DependentResult
29+
{
30+
/// <summary>
31+
/// The dependent package id.
32+
/// </summary>
33+
[JsonPropertyName("id")]
34+
public string Id { get; set; }
35+
36+
/// <summary>
37+
/// The description of the dependent package.
38+
/// </summary>
39+
[JsonPropertyName("description")]
40+
public string Description { get; set; }
41+
42+
/// <summary>
43+
/// The total downloads for the dependent package.
44+
/// </summary>
45+
[JsonPropertyName("totalDownloads")]
46+
public long TotalDownloads { get; set; }
2347
}
2448
}

src/BaGet.Core/Search/NullSearchService.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class NullSearchService : ISearchService
2424
Task.FromResult(new DependentsResponse
2525
{
2626
TotalHits = 0,
27-
Data = EmptyStringList
27+
Data = new List<DependentResult>()
2828
});
2929

3030
private static readonly Task<SearchResponse> EmptySearchResponseTask =
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
td, th {
2+
padding: 8px 8px 8px 15px;
3+
}
4+
5+
thead tr {
6+
border-bottom: 2px solid #d3d3d3;
7+
}
8+
9+
tbody tr {
10+
border-bottom: 1px solid #d3d3d3;
11+
}
12+
13+
td div {
14+
margin-top: 4px;
15+
}
16+
17+
td span {
18+
margin-left: 3px;
19+
}

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

+51-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
1+
import { Icon } from 'office-ui-fabric-react/lib/Icon';
2+
import { Link } from 'office-ui-fabric-react';
13
import { config } from '../config';
24
import * as React from 'react';
35

6+
import './Dependents.css'
7+
48
interface IDependentsProps {
59
packageId: string;
610
}
711

812
interface IDependentsState {
913
totalHits: number | undefined;
10-
data: string[] | undefined;
14+
data: IDependentState[] | undefined;
15+
}
16+
17+
interface IDependentState {
18+
id: string | undefined;
19+
description: string | undefined;
20+
totalDownloads: number | undefined;
1121
}
1222

1323
class Dependents extends React.Component<IDependentsProps, IDependentsState> {
@@ -40,6 +50,21 @@ class Dependents extends React.Component<IDependentsProps, IDependentsState> {
4050
}).catch((e) => console.log("Failed to load dependents.", e));
4151
}
4252

53+
private getDependentsMessage() {
54+
const hits = this.state.totalHits ?? -1;
55+
const packageId = this.props.packageId;
56+
57+
if (hits < 0) {
58+
return ""
59+
}
60+
61+
if (hits === 0) {
62+
return `No packages depend on ${packageId}.`;
63+
}
64+
65+
return `Showing the top 20 packages that depend on ${packageId}.`;
66+
}
67+
4368
public render() {
4469
if (!this.state.data) {
4570
return (
@@ -49,19 +74,37 @@ class Dependents extends React.Component<IDependentsProps, IDependentsState> {
4974

5075
if (this.state.totalHits === 0) {
5176
return (
52-
<div>No packages depend on {this.props.packageId}.</div>
77+
<div>{this.getDependentsMessage()}</div>
5378
);
5479
}
5580

5681
return (
5782
<div>
58-
<p>{this.state.totalHits} {this.state.totalHits === 1 ? 'package depends' : 'packages depend' } on {this.props.packageId}:</p>
83+
<p>{this.getDependentsMessage()}</p>
5984
<div>
60-
<ul>
61-
{this.state.data.map(dependent => (
62-
<li key={dependent}>{dependent}</li>
63-
))}
64-
</ul>
85+
<table>
86+
<thead>
87+
<tr>
88+
<th className="col-sm-10">Package</th>
89+
<th className="col-sm-2">Downloads</th>
90+
</tr>
91+
</thead>
92+
<tbody>
93+
{this.state.data.map(dependent => (
94+
<tr key={dependent.id}>
95+
<td>
96+
<Link to={`/packages/${dependent.id}`}>{dependent.id}</Link>
97+
<div>{dependent.description}</div>
98+
</td>
99+
<td>
100+
<Icon iconName="Download" className="ms-Icon" />
101+
<span>{dependent.totalDownloads?.toLocaleString()}</span>
102+
</td>
103+
</tr>
104+
105+
))}
106+
</tbody>
107+
</table>
65108
</div>
66109
</div>
67110
);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ class DisplayPackage extends React.Component<IDisplayPackageProps, IDisplayPacka
251251
}
252252
})()}
253253

254-
<ExpandableSection title="Dependents" expanded={false}>
254+
<ExpandableSection title="Used By" expanded={false}>
255255
<Dependents packageId={this.state.package.id} />
256256
</ExpandableSection>
257257

0 commit comments

Comments
 (0)