Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: more reliably detect new versions #554

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/public/app/widgets/buttons/global_menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ export default class GlobalMenuWidget extends BasicWidget {

const latestVersion = await this.fetchLatestVersion();
this.updateAvailableWidget.updateVersionStatus(latestVersion);
this.$updateToLatestVersionButton.toggle(latestVersion > glob.triliumVersion);
this.$updateToLatestVersionButton.toggle((Number(latestVersion) > Number(glob.triliumVersion)) ?? false);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typecasting to number doesn't directly work. I believe we have to compare each version component (major, minor, patch) individually.

> Number("1.2.11") > Number("1.2.10")
false

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could implement another library meant for exactly this with:

import * as semver from 'semver';

this.$updateToLatestVersionButton.toggle(semver.gt(latestVersion, glob.triliumVersion));

But I'm not sure if we want to add another lib.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with adding a new library is that we are here at the client level so it would involve a bit of effort to add it. I would personally only extract the version comparison algorithm, since I think it's not that difficult to implement.

  1. Obtain major, minor, patch by splitting the version by "." with a max number of elements of 3.
  2. If the major of $b > a$, then $b$ is newer.
  3. If the minor of $b > a$, then $b$ is newer.
  4. If the patch of $b > a$, then $b$ is newer.
  5. Otherwise, $b$ is older or equal to $a$ (no popup needed).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the function that you're looking for:

function compareVersions(v1, v2) {
  const v1Parts = v1.split('.').map(Number);
  const v2Parts = v2.split('.').map(Number);

  for (let i = 0; i < Math.max(v1Parts.length, v2Parts.length); i++) {
    const part1 = v1Parts[i] || 0;
    const part2 = v2Parts[i] || 0;

    if (part1 > part2) return 1;
    if (part1 < part2) return -1;
  }
  return 0;
}

Let me know if you wanna toss this in @rom1dep, or if you'd prefer if I did it :)

this.$updateToLatestVersionButton.find(".version-text").text(`Version ${latestVersion} is available, click to download.`);
}

Expand Down