mirror of
https://github.com/siteboon/claudecodeui.git
synced 2026-05-16 09:13:36 +00:00
refactor: useVersionCheck.js to typescript
This commit is contained in:
76
src/hooks/useVersionCheck.ts
Normal file
76
src/hooks/useVersionCheck.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { version } from '../../package.json';
|
||||
|
||||
/**
|
||||
* Compare two semantic version strings
|
||||
* Works only with numeric versions separated by dots (e.g. "1.2.3")
|
||||
* @param {string} v1
|
||||
* @param {string} v2
|
||||
* @returns positive if v1 > v2, negative if v1 < v2, 0 if equal
|
||||
*/
|
||||
const compareVersions = (v1: string, v2: string) => {
|
||||
const parts1 = v1.split('.').map(Number);
|
||||
const parts2 = v2.split('.').map(Number);
|
||||
|
||||
for (let i = 0; i < Math.max(parts1.length, parts2.length); i++) {
|
||||
const p1 = parts1[i] || 0;
|
||||
const p2 = parts2[i] || 0;
|
||||
if (p1 !== p2) return p1 - p2;
|
||||
}
|
||||
return 0;
|
||||
};
|
||||
|
||||
type ReleaseInfo = {
|
||||
title: string;
|
||||
body: string;
|
||||
htmlUrl: string;
|
||||
publishedAt: string;
|
||||
};
|
||||
|
||||
export const useVersionCheck = (owner: string, repo: string) => {
|
||||
const [updateAvailable, setUpdateAvailable] = useState(false);
|
||||
const [latestVersion, setLatestVersion] = useState<string | null>(null);
|
||||
const [releaseInfo, setReleaseInfo] = useState<ReleaseInfo | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
const checkVersion = async () => {
|
||||
try {
|
||||
const response = await fetch(`https://api.github.com/repos/${owner}/${repo}/releases/latest`);
|
||||
const data = await response.json();
|
||||
|
||||
// Handle the case where there might not be any releases
|
||||
if (data.tag_name) {
|
||||
const latest = data.tag_name.replace(/^v/, '');
|
||||
setLatestVersion(latest);
|
||||
// Only show update if latest version is actually newer
|
||||
setUpdateAvailable(compareVersions(latest, version) > 0);
|
||||
|
||||
// Store release information
|
||||
setReleaseInfo({
|
||||
title: data.name || data.tag_name,
|
||||
body: data.body || '',
|
||||
htmlUrl: data.html_url || `https://github.com/${owner}/${repo}/releases/latest`,
|
||||
publishedAt: data.published_at
|
||||
});
|
||||
} else {
|
||||
// No releases found, don't show update notification
|
||||
setUpdateAvailable(false);
|
||||
setLatestVersion(null);
|
||||
setReleaseInfo(null);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Version check failed:', error);
|
||||
// On error, don't show update notification
|
||||
setUpdateAvailable(false);
|
||||
setLatestVersion(null);
|
||||
setReleaseInfo(null);
|
||||
}
|
||||
};
|
||||
|
||||
checkVersion();
|
||||
const interval = setInterval(checkVersion, 5 * 60 * 1000); // Check every 5 minutes
|
||||
return () => clearInterval(interval);
|
||||
}, [owner, repo]);
|
||||
|
||||
return { updateAvailable, latestVersion, currentVersion: version, releaseInfo };
|
||||
};
|
||||
Reference in New Issue
Block a user