Skip to content

Commit

Permalink
chore: compare version check
Browse files Browse the repository at this point in the history
  • Loading branch information
lgou2w committed Jun 9, 2023
1 parent 84db8de commit 7b97d36
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/components/common/VersionChecker.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import { dialog, process } from '@tauri-apps/api'
import { useVersion, useLatestVersion } from '@/components/common/useVersion'
import { useVersion, useLatestVersion, CurrentVersion, LatestVersion } from '@/components/common/useVersion'
import Button from '@mui/material/Button'
import Typography from '@mui/material/Typography'
import DownloadIcon from '@mui/icons-material/Download'
Expand Down Expand Up @@ -53,8 +53,7 @@ export default function VersionChecker () {
if (latestVersion.isLoading) return <Typography component="span" variant="body2">加载中...</Typography>
if (latestVersion.isError) return <Typography component="span" variant="body2" color="error">检查最新版本失败</Typography>

// TODO: semver
const needUpdate = dayjs(version.data.date).isBefore(latestVersion.data.created_at)
const needUpdate = isNeedUpdate(version.data, latestVersion.data)
if (!needUpdate) return <Typography component="span" variant="body2" color="error">已是最新版本</Typography>

return (
Expand All @@ -67,3 +66,21 @@ export default function VersionChecker () {
</Button>
)
}

function isNeedUpdate (
version: CurrentVersion,
latestVersion: LatestVersion | null
): boolean {
if (!latestVersion) return false

// TODO: Strictly compare semver
const current = version.version
const latest = latestVersion.tag_name
const currentSemver = current.split('.').map(v => parseInt(v))
const latestSemver = latest.split('.').map(v => parseInt(v))
for (let i = 0; i < 3; i++) {
if (currentSemver[i] < latestSemver[i]) return true
if (currentSemver[i] > latestSemver[i]) return false
}
return false
}

0 comments on commit 7b97d36

Please sign in to comment.