/**
* Update Settings Component
* Displays update status and allows manual update checking/installation
*/
import { useEffect, useCallback } from 'react';
import { Download, RefreshCw, Loader2, Rocket, XCircle } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Progress } from '@/components/ui/progress';
import { useUpdateStore } from '@/stores/update';
import { useTranslation } from 'react-i18next';
function formatBytes(bytes: number): string {
if (bytes === 0) return '0 B';
const k = 1024;
const sizes = ['B', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}
function formatUpdateError(error: string | null, fallback: string): string {
if (!error) return fallback;
const firstLine = error.replace(/\\r\\n|\\n|\r\n|\n/g, '\n').split('\n', 1)[0].trim();
const isTechnical =
/(?:^\w*Error\s*:|HttpError|Cannot find channel|node_modules|https?:\/\/|\bnet::ERR_[A-Z0-9_]+\b|\bE(?:ACCES|CONNREFUSED|CONNRESET|EXIST|HOSTUNREACH|ISDIR|NETUNREACH|NOENT|NOTDIR|PERM|PIPE|TIMEDOUT)\b|\b(?:ERR|UPDATER)_[A-Z0-9_]+\b|[A-Z]:\\|\\\\[^\\]|\bat\s+\S+\s*\()/i.test(
firstLine,
) || /^[{[]/.test(firstLine);
const isUserFacingProse = /\p{Script=Han}/u.test(firstLine);
if (!firstLine || firstLine.length > 180 || isTechnical || !isUserFacingProse) return fallback;
return firstLine;
}
export function UpdateSettings() {
const { t } = useTranslation('settings');
const {
status,
currentVersion,
updateInfo,
progress,
error,
isInitialized,
autoInstallCountdown,
init,
checkForUpdates,
downloadUpdate,
installUpdate,
cancelAutoInstall,
clearError,
} = useUpdateStore();
// Initialize on mount
useEffect(() => {
init();
}, [init]);
const handleCheckForUpdates = useCallback(async () => {
clearError();
await checkForUpdates();
}, [checkForUpdates, clearError]);
const renderStatusIcon = () => {
switch (status) {
case 'checking':
case 'downloading':
return
{t('updates.currentVersion')}
v{currentVersion}
{renderStatusText()}
{renderAction()}{Math.round(progress.percent)}% complete
Version {updateInfo.version}
{updateInfo.releaseDate && ({new Date(updateInfo.releaseDate).toLocaleDateString()}
)}{t('updates.whatsNew')}
{updateInfo.releaseNotes}