import React, { useEffect, useState } from 'react' import { Quote, X } from 'lucide-react' import './UpdateDialog.scss' interface UpdateInfo { version?: string releaseNotes?: string } interface UpdateDialogProps { open: boolean updateInfo: UpdateInfo | null onClose: () => void onUpdate: () => void onIgnore?: () => void isDownloading: boolean progress: number | { percent: number bytesPerSecond?: number transferred?: number total?: number remaining?: number // seconds } } const UpdateDialog: React.FC = ({ open, updateInfo, onClose, onUpdate, onIgnore, isDownloading, progress }) => { if (!open || !updateInfo) return null // Safe normalize progress const safeProgress = typeof progress === 'number' ? { percent: progress } : (progress || { percent: 0 }) const percent = safeProgress.percent || 0 const bytesPerSecond = safeProgress.bytesPerSecond const total = safeProgress.total const transferred = safeProgress.transferred const remaining = safeProgress.remaining // Format bytes const formatBytes = (bytes: number) => { if (!Number.isFinite(bytes) || bytes === 0) return '0 B' const k = 1024 const sizes = ['B', 'KB', 'MB', 'GB', 'TB'] const i = Math.floor(Math.log(bytes) / Math.log(k)) const unitIndex = Math.max(0, Math.min(i, sizes.length - 1)) return parseFloat((bytes / Math.pow(k, unitIndex)).toFixed(1)) + ' ' + sizes[unitIndex] } // Format speed const formatSpeed = (bytesPerSecond: number) => { return `${formatBytes(bytesPerSecond)}/s` } // Format time const formatTime = (seconds: number) => { if (!Number.isFinite(seconds)) return '计算中...' if (seconds < 60) return `${Math.ceil(seconds)} 秒` const minutes = Math.floor(seconds / 60) const remainingSeconds = Math.ceil(seconds % 60) return `${minutes} 分 ${remainingSeconds} 秒` } return (
{!isDownloading && ( )}
新版本 {updateInfo.version}

欢迎体验全新的 WeFlow

我们带来了一些改进

优化

{updateInfo.releaseNotes ? (
) : (

修复了一些已知问题,提升了稳定性。

)}
{isDownloading ? (
{bytesPerSecond ? formatSpeed(bytesPerSecond) : '下载中...'} {total ? `${formatBytes(transferred || 0)} / ${formatBytes(total)}` : `${percent.toFixed(1)}%`} {remaining !== undefined && 剩余 {formatTime(remaining)}}
{/* Fallback status text if detailed info is missing */} {(!bytesPerSecond && !total) && (
{percent.toFixed(0)}% 已下载
)}
) : (
{onIgnore && ( )}
)}
) } export default UpdateDialog