Visualize Your Ideas With Code
fix(ui): version comparison issue (#520)
Ashish Baravaliya93b5e088b4a4
diff --git a/packages/ui/src/components/footer/Versions.tsx b/packages/ui/src/components/footer/Versions.tsxindex 197833b1..f4958500 100644--- a/packages/ui/src/components/footer/Versions.tsx+++ b/packages/ui/src/components/footer/Versions.tsx@@ -3,11 +3,12 @@ import {useApplication} from '../../contexts';import clsx from 'clsx';import {useEffect} from 'preact/hooks';import {useState} from 'react';+import {compareVersions} from '../../utils';export function Versions() {const {project} = useApplication();const [newVersion, setNewVersion] = useState(project.versions.core);- const isOld = project.versions.core.localeCompare(newVersion) < 0;+ const isOld = compareVersions(project.versions.core, newVersion) < 0;useEffect(() => {const abort = new AbortController();diff --git a/packages/ui/src/utils/compareVersions.tsx b/packages/ui/src/utils/compareVersions.tsxnew file mode 100644index 00000000..077525a6--- /dev/null+++ b/packages/ui/src/utils/compareVersions.tsx@@ -0,0 +1,40 @@+export function compareVersions(v1: string, v2: string): number {+ const v1Parts = v1.split('.');+ const v2Parts = v2.split('.');+ const len = Math.max(v1Parts.length, v2Parts.length);++ for (let i = 0; i < len; i++) {+ const v1Part = getNumericPart(v1Parts[i]);+ const v2Part = getNumericPart(v2Parts[i]);++ if (v1Part === v2Part) {+ const v1Pre = getPreReleasePart(v1Parts[i]);+ const v2Pre = getPreReleasePart(v2Parts[i]);++ if (v1Pre && v2Pre && v1Pre !== v2Pre) {+ return v1Pre.localeCompare(v2Pre);+ } else if (v1Pre && !v2Pre) {+ return -1;+ } else if (!v1Pre && v2Pre) {+ return 1;+ }+ } else {+ return v1Part > v2Part ? 1 : -1;+ }+ }++ return 0;+}++function getNumericPart(part: string): string | number {+ const numPart = parseInt(part, 10);+ return isNaN(numPart) ? part : numPart;+}++function getPreReleasePart(part: string): string {+ if (!part) {+ return null;+ }+ const preReleaseMatch = part.match(/-([0-9A-Za-z-.]+)/);+ return preReleaseMatch ? preReleaseMatch[1] : null;+}diff --git a/packages/ui/src/utils/index.ts b/packages/ui/src/utils/index.tsindex 0f647618..a0cc00a2 100644--- a/packages/ui/src/utils/index.ts+++ b/packages/ui/src/utils/index.ts@@ -2,3 +2,4 @@ export * from './clamp';export * from './localStorage';export * from './sourceMaps';export * from './withLoader';+export * from './compareVersions';