1
1
import { HtmlRenderer , Parser } from 'commonmark' ;
2
2
import { Icon } from 'office-ui-fabric-react/lib/Icon' ;
3
3
import * as React from 'react' ;
4
+ import { Link } from 'react-router-dom' ;
4
5
import timeago from 'timeago.js' ;
5
6
import Dependencies from './Dependencies' ;
6
7
import InstallationInfo from './InstallationInfo' ;
@@ -14,6 +15,7 @@ interface IDisplayPackageProps {
14
15
match : {
15
16
params : {
16
17
id : string ;
18
+ version ?: string ;
17
19
}
18
20
}
19
21
}
@@ -30,9 +32,10 @@ interface IPackage {
30
32
repositoryUrl : string ;
31
33
repositoryType : string ;
32
34
totalDownloads : number ;
33
- latestDownloads : number ;
35
+ downloads : number ;
34
36
authors : string ;
35
37
tags : string [ ] ;
38
+ version : string ;
36
39
versions : IPackageVersion [ ] ;
37
40
dependencyGroups : Registration . IDependencyGroup [ ] ;
38
41
}
@@ -50,6 +53,7 @@ interface IDisplayPackageState {
50
53
class DisplayPackage extends React . Component < IDisplayPackageProps , IDisplayPackageState > {
51
54
52
55
private id : string ;
56
+ private version ?: string ;
53
57
private parser : Parser ;
54
58
private htmlRenderer : HtmlRenderer ;
55
59
@@ -61,10 +65,12 @@ class DisplayPackage extends React.Component<IDisplayPackageProps, IDisplayPacka
61
65
62
66
this . parser = new Parser ( ) ;
63
67
this . htmlRenderer = new HtmlRenderer ( ) ;
68
+
64
69
this . registrationController = new AbortController ( ) ;
65
70
this . readmeController = new AbortController ( ) ;
66
71
67
72
this . id = props . match . params . id ;
73
+ this . version = props . match . params . version ;
68
74
this . state = { package : undefined } ;
69
75
}
70
76
@@ -73,6 +79,23 @@ class DisplayPackage extends React.Component<IDisplayPackageProps, IDisplayPacka
73
79
this . readmeController . abort ( ) ;
74
80
}
75
81
82
+ public componentDidUpdate ( previous : IDisplayPackageProps ) {
83
+ // This is used to switch between versions of the same package.
84
+ if ( previous . match . params . id !== this . props . match . params . id ||
85
+ previous . match . params . version !== this . props . match . params . version ) {
86
+ this . registrationController . abort ( ) ;
87
+ this . readmeController . abort ( ) ;
88
+
89
+ this . registrationController = new AbortController ( ) ;
90
+ this . readmeController = new AbortController ( ) ;
91
+
92
+ this . id = this . props . match . params . id ;
93
+ this . version = this . props . match . params . version ;
94
+ this . setState ( { package : undefined } ) ;
95
+ this . componentDidMount ( ) ;
96
+ }
97
+ }
98
+
76
99
public componentDidMount ( ) {
77
100
const url = `/v3/registration/${ this . id } /index.json` ;
78
101
@@ -82,7 +105,8 @@ class DisplayPackage extends React.Component<IDisplayPackageProps, IDisplayPacka
82
105
const results = json as Registration . IRegistrationIndex ;
83
106
84
107
const latestVersion = results . items [ 0 ] . upper ;
85
- let latestItem : Registration . IRegistrationPageItem | undefined ;
108
+ let currentItem : Registration . IRegistrationPageItem | undefined ;
109
+ let lastUpdate : Date | undefined ;
86
110
87
111
const versions : IPackageVersion [ ] = [ ] ;
88
112
@@ -93,40 +117,37 @@ class DisplayPackage extends React.Component<IDisplayPackageProps, IDisplayPacka
93
117
version : entry . catalogEntry . version ,
94
118
} ) ;
95
119
96
- if ( entry . catalogEntry . version === latestVersion ) {
97
- latestItem = entry ;
120
+ if ( ( ! currentItem && entry . catalogEntry . version === latestVersion ) ||
121
+ ( this . version && entry . catalogEntry . version === this . version ) ) {
122
+ currentItem = entry ;
123
+ }
124
+
125
+ const published = new Date ( entry . catalogEntry . published ) ;
126
+ if ( ! lastUpdate || lastUpdate < published ) {
127
+ lastUpdate = published ;
98
128
}
99
129
}
100
130
101
- if ( latestItem ) {
131
+ if ( currentItem && lastUpdate ) {
102
132
let readme = "" ;
103
- if ( ! latestItem . catalogEntry . hasReadme ) {
104
- readme = latestItem . catalogEntry . description ;
133
+ if ( ! currentItem . catalogEntry . hasReadme ) {
134
+ readme = currentItem . catalogEntry . description ;
105
135
}
106
136
107
137
this . setState ( {
108
138
package : {
109
- authors : latestItem . catalogEntry . authors ,
110
- dependencyGroups : latestItem . catalogEntry . dependencyGroups ,
111
- downloadUrl : latestItem . packageContent ,
112
- iconUrl : latestItem . catalogEntry . iconUrl ,
113
- id : latestItem . catalogEntry . id ,
114
- lastUpdate : new Date ( latestItem . catalogEntry . published ) ,
115
- latestDownloads : latestItem . catalogEntry . downloads ,
139
+ ...currentItem . catalogEntry ,
140
+ downloadUrl : currentItem . packageContent ,
141
+ lastUpdate,
116
142
latestVersion,
117
- licenseUrl : latestItem . catalogEntry . licenseUrl ,
118
- projectUrl : latestItem . catalogEntry . projectUrl ,
119
143
readme,
120
- repositoryType : latestItem . catalogEntry . repositoryType ,
121
- repositoryUrl : latestItem . catalogEntry . repositoryUrl ,
122
- tags : latestItem . catalogEntry . tags ,
123
144
totalDownloads : results . totalDownloads ,
124
145
versions
125
146
}
126
147
} ) ;
127
148
128
- if ( latestItem . catalogEntry . hasReadme ) {
129
- const readmeUrl = `/v3/package/${ this . id } /${ latestVersion } /readme` ;
149
+ if ( currentItem . catalogEntry . hasReadme ) {
150
+ const readmeUrl = `/v3/package/${ this . id } /${ currentItem . catalogEntry . version } /readme` ;
130
151
131
152
fetch ( readmeUrl , { signal : this . readmeController . signal } ) . then ( response => {
132
153
return response . text ( ) ;
@@ -161,12 +182,12 @@ class DisplayPackage extends React.Component<IDisplayPackageProps, IDisplayPacka
161
182
< div className = "package-title" >
162
183
< h1 >
163
184
{ this . state . package . id }
164
- < small className = "text-nowrap" > { this . state . package . latestVersion } </ small >
185
+ < small className = "text-nowrap" > { this . state . package . version } </ small >
165
186
</ h1 >
166
187
167
188
</ div >
168
189
169
- < InstallationInfo id = { this . state . package . id } version = { this . state . package . latestVersion } />
190
+ < InstallationInfo id = { this . state . package . id } version = { this . state . package . version } />
170
191
171
192
{ /* TODO: Fix this */ }
172
193
< div dangerouslySetInnerHTML = { { __html : this . state . package . readme } } />
@@ -207,7 +228,7 @@ class DisplayPackage extends React.Component<IDisplayPackageProps, IDisplayPacka
207
228
</ li >
208
229
< li >
209
230
< Icon iconName = "GiftBox" className = "ms-Icon" />
210
- { this . state . package . latestDownloads . toLocaleString ( ) } downloads of latest version
231
+ { this . state . package . downloads . toLocaleString ( ) } downloads of latest version
211
232
</ li >
212
233
</ ul >
213
234
</ div >
@@ -217,7 +238,7 @@ class DisplayPackage extends React.Component<IDisplayPackageProps, IDisplayPacka
217
238
218
239
{ this . state . package . versions . map ( value => (
219
240
< div key = { value . version } >
220
- < span > v { value . version } : </ span >
241
+ < span > < Link to = { `/packages/ ${ this . state . package ! . id } / ${ value . version } ` } > { value . version } </ Link > : </ span >
221
242
< span > { this . dateToString ( value . date ) } </ span >
222
243
</ div >
223
244
) ) }
0 commit comments