import { Download, LoaderCircle, Pause, RefreshCw } from 'lucide-react' import React, { useEffect, useState } from 'react' import '../i18n/domains/wizard.gen' import { I18nKey, t } from '../i18n' import { taskDetail } from '../lib/tasks' import { InstalledRuntime, Locale, RemoteRuntime, Task } from '../types' import { Button, IconButton } from '../ui/controls' import { Switch } from '../ui/settings' import { api } from '../wails' interface Props { locale: Locale channel: 'stable' | 'alpha' installed: InstalledRuntime[] task: Task | null onChannel: (alpha: boolean) => void onInstall: (commit: string) => void onCancel: (taskId: string) => void } const gb = (b: number) => (b >= 1e9 ? `${(b / 1e9).toFixed(1)} GB` : `${Math.ceil(b / 1e6)} MB`) /** "QEMU emulator version 11.1.90" → "QEMU 11.1.90". */ const shortQemu = (v: string) => v.replace(/^QEMU emulator version\s*/i, 'QEMU ') /** * The runtime rows: the active runtime and whether it is current (or, with * none installed, the newest release to install), then the alpha switch. */ export const RuntimeOnline: React.FC = ({ locale, channel, installed, task, onChannel, onInstall, onCancel }) => { const tr = (k: I18nKey, v?: Record) => t(locale, k, v) const [remote, setRemote] = useState(null) const [error, setError] = useState('') const [reload, setReload] = useState(0) useEffect(() => { let live = true setRemote(null) setError('') api.ListRemoteRuntimes() .then((list) => { if (live) setRemote(list ?? []) }) .catch((e) => { if (live) setError(String(e).replace(/^Error:\s*/, '')) }) return () => { live = false } }, [channel, task?.status === 'done', reload]) const active = installed.find((i) => i.active) ?? installed[0] const latest = remote?.[0] const sameRuntimeId = (a: string, b: string) => { const [baseA, revA = ''] = a.split('-', 2) const [baseB, revB = ''] = b.split('-', 2) return revA === revB && (baseA.startsWith(baseB) || baseB.startsWith(baseA)) } const isInstalled = (c: string) => installed.some((i) => sameRuntimeId(i.commit, c)) const running = task?.status === 'running' const updatable = !!latest && !isInstalled(latest.commit) let sub: React.ReactNode if (running && task) { sub = (
) } else if (active && !updatable) { sub =
{shortQemu(active.manifest.qemu_version)}
} else if (error) { const isRateLimit = /rate limit|HTTP 403|HTTP 429/i.test(error) const msg = isRateLimit ? tr('runtimeLookupRateLimit') : `${tr('runtimeLookupFailed')}: ${error}` sub =
{msg}
} else if (!remote) { sub =
…
} else if (!latest) { sub =
{tr('runtimeNoneOnChannel')}
} else { sub = (
{active ? tr('runtimeNewer', { v: latest.commit }) : tr('runtimeLatest', { v: latest.commit })} {' · '}{tr(`runtimeChannel_${latest.channel}` as I18nKey)} {latest.archive_size > 0 ? ` · ${gb(latest.archive_size)}` : ''}
) } return ( <>
{active ? <>{tr('runtimeCurrent')} {active.commit} : tr('runtimeOnline')}
{sub}
{running && task?.cancelable ? ( } onClick={() => onCancel(task.id)} /> ) : !running && error ? ( ) : !running && updatable ? ( ) : !running && active && remote ? ( {tr('runtimeUpToDate')} ) : null}
{tr('runtimeAlpha')}
{tr('runtimeAlphaHint')}
) }