From 6fdd6b4ed9bac66c0f345453e3daf075ce7eda03 Mon Sep 17 00:00:00 2001 From: Tristan Yang Date: Fri, 30 Dec 2022 14:39:48 +0800 Subject: [PATCH] feat: server version checker --- src/common/component/ServerVersionChecker.tsx | 20 +++++++ src/common/utils.tsx | 57 ++++++++++++++++++- src/routes/setting/navs.tsx | 3 +- 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 src/common/component/ServerVersionChecker.tsx diff --git a/src/common/component/ServerVersionChecker.tsx b/src/common/component/ServerVersionChecker.tsx new file mode 100644 index 00000000..e18169f9 --- /dev/null +++ b/src/common/component/ServerVersionChecker.tsx @@ -0,0 +1,20 @@ +import React, { PropsWithChildren } from 'react'; +import { useGetServerVersionQuery } from '../../app/services/server'; +import { compareVersion } from '../utils'; + +type Props = { + version: string, +} + +const ServerVersionChecker = ({ version, children }: PropsWithChildren) => { + const { data: currentVersion, isSuccess } = useGetServerVersionQuery(); + if (!isSuccess) return null; + const res = compareVersion(currentVersion, version); + if (res < 0) return
+ 🚨 Server version:{version} needed at least, your current version:{currentVersion}, please upgrade the Server! + How to Update VoceChat Server +
; + return children; +}; + +export default ServerVersionChecker; \ No newline at end of file diff --git a/src/common/utils.tsx b/src/common/utils.tsx index 6b587f91..0b876801 100644 --- a/src/common/utils.tsx +++ b/src/common/utils.tsx @@ -8,7 +8,6 @@ import IconCode from "../assets/icons/file.code.svg"; import IconImage from "../assets/icons/file.image.svg"; import { Archive, ArchiveMessage } from "../types/resource"; import { MessagePayload } from "../app/slices/message"; -import { PinnedMessage } from "../types/channel"; export const getLocalAuthData = () => { return { @@ -269,3 +268,59 @@ export const normalizeArchiveData = ( } ); }; +// https://github.com/gabe0x02/version_compare/blob/20d79649da39febc883350f441ee0bd6f1a6b1e6/version_compare.js +export const compareVersion = (v1: string, v2: string, options?: { lexicographical: boolean, zeroExtend: boolean }) => { + //remove anything after - 1.1.2-3-a4agbr-dirty + function cropDash(s: string) { + let idx = s.indexOf('-'); + if (idx !== -1) { + s = s.substring(0, idx); + } + return s; + } + v1 = cropDash(v1); + v2 = cropDash(v2); + let lexicographical = options && options.lexicographical, + zeroExtend = options && options.zeroExtend, + v1parts = v1.split('.'), + v2parts = v2.split('.'); + function isValidPart(x) { + return (lexicographical ? /^\d+[A-Za-z]*$/ : /^\d+$/).test(x); + } + + if (!v1parts.every(isValidPart) || !v2parts.every(isValidPart)) { + return NaN; + } + + if (zeroExtend) { + while (v1parts.length < v2parts.length) v1parts.push("0"); + while (v2parts.length < v1parts.length) v2parts.push("0"); + } + + if (!lexicographical) { + v1parts = v1parts.map(Number); + v2parts = v2parts.map(Number); + } + + for (var i = 0; i < v1parts.length; ++i) { + if (v2parts.length == i) { + return 1; + } + + if (v1parts[i] == v2parts[i]) { + continue; + } + else if (v1parts[i] > v2parts[i]) { + return 1; + } + else { + return -1; + } + } + + if (v1parts.length != v2parts.length) { + return -1; + } + + return 0; +}; \ No newline at end of file diff --git a/src/routes/setting/navs.tsx b/src/routes/setting/navs.tsx index 97d691f2..7f1062e8 100644 --- a/src/routes/setting/navs.tsx +++ b/src/routes/setting/navs.tsx @@ -13,6 +13,7 @@ import Version from "../../common/component/Version"; // import ConfigAgora from "./config/Agora"; import { useAppSelector } from "../../app/store"; import { useTranslation } from "react-i18next"; +import ServerVersionChecker from "../../common/component/ServerVersionChecker"; const navs = [ { @@ -38,7 +39,7 @@ const navs = [ items: [ { name: "bot", - component: , + component: , admin: true }, {