import React, { useState, useEffect } from 'react'; import { motion } from 'framer-motion'; import { useSettings } from '~/lib/hooks/useSettings'; import { logStore } from '~/lib/stores/logs'; import { classNames } from '~/utils/classNames'; interface GitHubCommitResponse { sha: string; } interface UpdateInfo { currentVersion: string; latestVersion: string; branch: string; hasUpdate: boolean; } const GITHUB_URLS = { commitJson: async (branch: string): Promise => { try { const response = await fetch(`https://api.github.com/repos/stackblitz-labs/bolt.diy/commits/${branch}`); const data = (await response.json()) as GitHubCommitResponse; const currentCommitHash = __COMMIT_HASH; const remoteCommitHash = data.sha.slice(0, 7); return { currentVersion: currentCommitHash, latestVersion: remoteCommitHash, branch, hasUpdate: remoteCommitHash !== currentCommitHash, }; } catch (error) { console.error('Failed to fetch commit info:', error); throw new Error('Failed to fetch commit info'); } }, }; const UpdateTab = () => { const { isLatestBranch } = useSettings(); const [updateInfo, setUpdateInfo] = useState(null); const [isChecking, setIsChecking] = useState(false); const [error, setError] = useState(null); const checkForUpdates = async () => { setIsChecking(true); setError(null); try { const branchToCheck = isLatestBranch ? 'main' : 'stable'; const info = await GITHUB_URLS.commitJson(branchToCheck); setUpdateInfo(info); if (info.hasUpdate) { // Add update notification only if it doesn't already exist const existingLogs = Object.values(logStore.logs.get()); const hasUpdateNotification = existingLogs.some( (log) => log.level === 'warning' && log.details?.type === 'update' && log.details.latestVersion === info.latestVersion, ); if (!hasUpdateNotification) { logStore.logWarning('Update Available', { currentVersion: info.currentVersion, latestVersion: info.latestVersion, branch: branchToCheck, type: 'update', message: `A new version is available on the ${branchToCheck} branch`, updateUrl: `https://github.com/stackblitz-labs/bolt.diy/compare/${info.currentVersion}...${info.latestVersion}`, }); } } } catch (err) { setError('Failed to check for updates. Please try again later.'); console.error('Update check failed:', err); } finally { setIsChecking(false); } }; useEffect(() => { checkForUpdates(); }, [isLatestBranch]); const handleViewChanges = () => { if (updateInfo) { window.open( `https://github.com/stackblitz-labs/bolt.diy/compare/${updateInfo.currentVersion}...${updateInfo.latestVersion}`, '_blank', ); } }; return (

Updates

Check for and manage application updates

Currently on {isLatestBranch ? 'main' : 'stable'} branch {updateInfo && ( Version: {updateInfo.currentVersion} )}
{error && (
{error}
)} {updateInfo && (

{updateInfo.hasUpdate ? 'Update Available' : 'Up to Date'}

{updateInfo.hasUpdate ? `A new version is available on the ${updateInfo.branch} branch` : 'You are running the latest version'}

{updateInfo.hasUpdate && (

Current Version: {updateInfo.currentVersion}

Latest Version: {updateInfo.latestVersion}

Branch: {updateInfo.branch}

)}
{updateInfo.hasUpdate && ( )}
)}
); }; export default UpdateTab;