1
+ import semverMaxSatisfying from "semver/ranges/max-satisfying" ;
2
+ import semverCoerce from "semver/functions/coerce" ;
1
3
import { library , icon } from "@fortawesome/fontawesome-svg-core" ;
2
4
import {
3
5
faCircleXmark ,
4
6
faCodePullRequest ,
7
+ faLayerGroup ,
5
8
} from "@fortawesome/free-solid-svg-icons" ;
6
9
import { html , nothing , render , LitElement } from "lit" ;
7
10
@@ -16,6 +19,7 @@ export class NotificationElement extends LitElement {
16
19
static properties = {
17
20
config : { state : true } ,
18
21
urls : { state : true } ,
22
+ highest_version : { state : true } ,
19
23
} ;
20
24
21
25
/** @static @property {Object } - Lit stylesheets to apply to elements */
@@ -30,21 +34,34 @@ export class NotificationElement extends LitElement {
30
34
build : null ,
31
35
external : null ,
32
36
} ;
37
+ this . highest_version = null ;
33
38
}
34
39
35
40
loadConfig ( config ) {
36
41
this . config = config ;
37
42
38
- // TODO: this URL should come from the backend API.
39
- // Doing a simple replacement for now to solve the most common cases.
40
- const vcs_external_url = config . project . repository_url
41
- . replace ( ".git" , "" )
42
- . replace ( "[email protected] :" , "https://github.com/" ) ;
43
+ if (
44
+ config . features . external_version_warning . enabled &&
45
+ config . version . external
46
+ ) {
47
+ // TODO: this URL should come from the backend API.
48
+ // Doing a simple replacement for now to solve the most common cases.
49
+ const vcs_external_url = config . project . repository_url
50
+ . replace ( ".git" , "" )
51
+ . replace ( "[email protected] :" , "https://github.com/" ) ;
52
+
53
+ this . urls = {
54
+ build : `${ window . location . protocol } //${ config . domains . dashboard } /projects/${ config . project . slug } /builds/${ config . build . id } /` ,
55
+ external : `${ vcs_external_url } /pull/${ config . version . slug } ` ,
56
+ } ;
57
+ }
43
58
44
- this . urls = {
45
- build : `${ window . location . protocol } //${ config . domains . dashboard } /projects/${ config . project . slug } /builds/${ config . build . id } /` ,
46
- external : `${ vcs_external_url } /pull/${ config . version . slug } ` ,
47
- } ;
59
+ if (
60
+ config . features . non_latest_version_warning . enabled &&
61
+ ! config . version . external
62
+ ) {
63
+ this . calculateHighestVersion ( ) ;
64
+ }
48
65
}
49
66
50
67
render ( ) {
@@ -54,14 +71,66 @@ export class NotificationElement extends LitElement {
54
71
return nothing ;
55
72
}
56
73
57
- if (
58
- this . config . features . external_version_warning . enabled &&
59
- this . config . version . external
74
+ if ( this . config . version . external ) {
75
+ if ( this . config . features . external_version_warning . enabled ) {
76
+ return this . renderExternalVersionWarning ( ) ;
77
+ }
78
+ } else if (
79
+ this . config . features . non_latest_version_warning . enabled &&
80
+ this . highest_version
60
81
) {
61
- return this . renderExternalVersionWarning ( ) ;
82
+ return this . renderNonLatestVersionWarning ( ) ;
62
83
}
84
+ return nothing ;
85
+ }
63
86
64
- // TODO support the outdated version warning
87
+ calculateHighestVersion ( ) {
88
+ // Convert versions like `v1` into `1.0.0` to be able to compare them
89
+ const versions = this . config . features . non_latest_version_warning . versions ;
90
+ const coercedVersions = versions . map ( ( v ) => semverCoerce ( v ) ) ;
91
+ const coercedHighest = semverMaxSatisfying ( coercedVersions , ">=0.0.0" ) ;
92
+
93
+ // Get back the original `v1` to generate the URLs and display the correct name
94
+ const index = coercedVersions . indexOf ( coercedHighest ) ;
95
+ const highest = versions [ index ] ;
96
+
97
+ if ( highest && highest !== this . config . version . slug ) {
98
+ this . highest_version = {
99
+ name : highest ,
100
+ // TODO: get this URL from the API
101
+ url : `${ window . location . protocol } //${ window . location . hostname } /${ this . config . project . language } /${ highest } /` ,
102
+ } ;
103
+ }
104
+ }
105
+
106
+ renderNonLatestVersionWarning ( ) {
107
+ library . add ( faCircleXmark ) ;
108
+ library . add ( faLayerGroup ) ;
109
+ const xmark = icon ( faCircleXmark , {
110
+ title : "Close notification" ,
111
+ } ) ;
112
+ const iconLayerGroup = icon ( faLayerGroup , {
113
+ title : "This version is not the latest one" ,
114
+ classes : [ "header" , "icon" ] ,
115
+ } ) ;
116
+
117
+ return html `
118
+ < div >
119
+ ${ iconLayerGroup . node [ 0 ] }
120
+ < div class ="title ">
121
+ This is an < span > old version</ span >
122
+ < a href ="# " class ="right " @click =${ this . closeNotification } >
123
+ ${ xmark . node [ 0 ] }
124
+ </ a >
125
+ </ div >
126
+ < div class ="content ">
127
+ You are reading an old version of this documentation. The latest
128
+ stable version is
129
+ < a href ="${ this . highest_version . url } "> ${ this . highest_version . name } </ a
130
+ > .
131
+ </ div >
132
+ </ div >
133
+ ` ;
65
134
}
66
135
67
136
renderExternalVersionWarning ( ) {
@@ -149,11 +218,12 @@ export class NotificationAddon extends AddonBase {
149
218
* @param {Object } config - Addon configuration object
150
219
*/
151
220
static isEnabled ( config ) {
152
- // TODO support the outdated version warning feature here too.
153
221
return (
154
- config . features &&
155
- config . features . external_version_warning . enabled &&
156
- config . version . external
222
+ ( config . features &&
223
+ config . features . external_version_warning . enabled &&
224
+ config . version . external ) ||
225
+ ( config . features . non_latest_version_warning . enabled &&
226
+ ! config . version . external )
157
227
) ;
158
228
}
159
229
}
0 commit comments