Skip to content

Commit 5fa03a3

Browse files
authored
Notification: show "not reading latest version" (#47)
It re-uses the code from `NotificationElement`. However, I'm not happy with the implementation. It seems that they are pretty much different to make sense to re-use the class. It seems that what we want here is create a `NotificationBaseElement` where `NotificationExternalElement` and `NotificationNonLatestElement` can extend. ### Example ![Screenshot_2023-04-12_16-40-16](https://user-images.githubusercontent.com/244656/231492831-04a9d617-9430-48d4-b921-321ef06aa2b0.png)
1 parent f884be3 commit 5fa03a3

File tree

2 files changed

+89
-18
lines changed

2 files changed

+89
-18
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
*.svg
2+
*.orig

src/notification.js

Lines changed: 88 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import semverMaxSatisfying from "semver/ranges/max-satisfying";
2+
import semverCoerce from "semver/functions/coerce";
13
import { library, icon } from "@fortawesome/fontawesome-svg-core";
24
import {
35
faCircleXmark,
46
faCodePullRequest,
7+
faLayerGroup,
58
} from "@fortawesome/free-solid-svg-icons";
69
import { html, nothing, render, LitElement } from "lit";
710

@@ -16,6 +19,7 @@ export class NotificationElement extends LitElement {
1619
static properties = {
1720
config: { state: true },
1821
urls: { state: true },
22+
highest_version: { state: true },
1923
};
2024

2125
/** @static @property {Object} - Lit stylesheets to apply to elements */
@@ -30,21 +34,34 @@ export class NotificationElement extends LitElement {
3034
build: null,
3135
external: null,
3236
};
37+
this.highest_version = null;
3338
}
3439

3540
loadConfig(config) {
3641
this.config = config;
3742

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+
}
4358

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+
}
4865
}
4966

5067
render() {
@@ -54,14 +71,66 @@ export class NotificationElement extends LitElement {
5471
return nothing;
5572
}
5673

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
6081
) {
61-
return this.renderExternalVersionWarning();
82+
return this.renderNonLatestVersionWarning();
6283
}
84+
return nothing;
85+
}
6386

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+
`;
65134
}
66135

67136
renderExternalVersionWarning() {
@@ -149,11 +218,12 @@ export class NotificationAddon extends AddonBase {
149218
* @param {Object} config - Addon configuration object
150219
*/
151220
static isEnabled(config) {
152-
// TODO support the outdated version warning feature here too.
153221
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)
157227
);
158228
}
159229
}

0 commit comments

Comments
 (0)