diff --git a/src/public/app/widgets/buttons/global_menu.js b/src/public/app/widgets/buttons/global_menu.js index 7bcf23f9e..93f7c8678 100644 --- a/src/public/app/widgets/buttons/global_menu.js +++ b/src/public/app/widgets/buttons/global_menu.js @@ -1,7 +1,6 @@ import BasicWidget from "../basic_widget.js"; import utils from "../../services/utils.js"; import UpdateAvailableWidget from "./update_available.js"; -const axios = require("axios"); const TPL = ` `; -const RELEASES_API_URL = "https://api.github.com/repos/zadam/trilium/releases/latest"; -const CURRENT_VERSION = process.env.npm_package_version; export default class GlobalMenuWidget extends BasicWidget { - static getVersionChange(oldVersion, newVersion) { - const [oldMajor, oldMinor, oldPatch] = oldVersion.split(".").map(Number); - const [newMajor, newMinor, newPatch] = newVersion.split(".").map(Number); + constructor() { + super(); - if (newMajor !== oldMajor) { - return "major"; - } else if (newMinor !== oldMinor) { - return "minor"; - } else if (newPatch !== oldPatch) { - return "patch"; - } + this.updateAvailableWidget = new UpdateAvailableWidget(); } doRender() { @@ -154,28 +145,36 @@ export default class GlobalMenuWidget extends BasicWidget { this.$widget.on('click', '.dropdown-item', () => this.$widget.find("[data-toggle='dropdown']").dropdown('toggle')); - this.loadUpdateAvailable(); - } - - async loadUpdateAvailable() { - const newVersion = await this.fetchNewVersion(); - - if (!newVersion) { - return; - } - - const versionChange = "major"; - this.$widget.find(".global-menu-button-update-available").append( - new UpdateAvailableWidget() - .withVersionChange(versionChange) - .render() - ) + this.updateAvailableWidget.render() + ); + + this.$updateToLatestVersionButton = this.$widget.find(".update-to-latest-version-button"); + + this.updateVersionStatus(); + + setInterval(() => this.updateVersionStatus(), 8 * 60 * 60 * 1000); } - async fetchNewVersion() { - const {data} = await axios.get(RELEASES_API_URL); + async updateVersionStatus() { + const latestVersion = await this.fetchLatestVersion(); + + this.updateAvailableWidget.updateVersionStatus(latestVersion); + + this.$updateToLatestVersionButton.toggle(latestVersion > glob.triliumVersion); + this.$updateToLatestVersionButton.find(".version-text").text(`Version ${latestVersion} is available, click to download.`); + } + + async fetchLatestVersion() { + const RELEASES_API_URL = "https://api.github.com/repos/zadam/trilium/releases/latest"; + + const resp = await fetch(RELEASES_API_URL); + const data = await resp.json(); return data.tag_name.substring(1); } + + downloadLatestVersionCommand() { + window.open("https://github.com/zadam/trilium/releases/latest"); + } } diff --git a/src/public/app/widgets/buttons/update_available.js b/src/public/app/widgets/buttons/update_available.js index 32ac5f31c..e59519940 100644 --- a/src/public/app/widgets/buttons/update_available.js +++ b/src/public/app/widgets/buttons/update_available.js @@ -1,7 +1,7 @@ import BasicWidget from "../basic_widget.js"; const TPL = ` - +
-` -const VERSION_CHANGE_COLOR_MAP = { - patch: "#666666", - minor: "#5bc625", - major: "#ec2f2f" -} -const VERSION_CHANGE_BACKGROUND_COLOR_MAP = Object.fromEntries( - Object.entries( - VERSION_CHANGE_COLOR_MAP).map(([key, value]) => [ - key, - `${value}40` - ] - ) -) + + +
+`; export default class UpdateAvailableWidget extends BasicWidget { - versionChange = undefined - doRender() { this.$widget = $(TPL); - - this.setButton(); } - setButton() { - switch (this.versionChange) { - case "major": - case "minor": - case "patch": - this.$widget.show(); - this.$widget.css({ - color: VERSION_CHANGE_COLOR_MAP[this.versionChange], - backgroundColor: VERSION_CHANGE_BACKGROUND_COLOR_MAP[this.versionChange] - }); - break; - default: - this.$widget.hide(); - } - } - - withVersionChange(versionChange) { - this.versionChange = versionChange; - - return this; + updateVersionStatus(latestVersion) { + this.$widget.toggle(latestVersion > glob.triliumVersion); } } diff --git a/src/routes/index.js b/src/routes/index.js index 0265278bd..37ba9fb46 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -36,7 +36,8 @@ function index(req, res) { isMainWindow: !req.query.extra, extraHoistedNoteId: req.query.extraHoistedNoteId, isProtectedSessionAvailable: protectedSessionService.isProtectedSessionAvailable(), - maxContentWidth: parseInt(options.maxContentWidth) + maxContentWidth: parseInt(options.maxContentWidth), + triliumVersion: process.env.npm_package_version }); } diff --git a/src/views/desktop.ejs b/src/views/desktop.ejs index cbbd92538..ca45a1beb 100644 --- a/src/views/desktop.ejs +++ b/src/views/desktop.ejs @@ -55,6 +55,7 @@ isMainWindow: <%= isMainWindow %>, extraHoistedNoteId: '<%= extraHoistedNoteId %>', isProtectedSessionAvailable: <%= isProtectedSessionAvailable %>, + triliumVersion: "<%= triliumVersion %>" };