Files
Reims-Go/frontend/src/components/RuntimeOnline.tsx
T

127 lines
5.0 KiB
TypeScript

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<Props> = ({ locale, channel, installed, task, onChannel, onInstall, onCancel }) => {
const tr = (k: I18nKey, v?: Record<string, string | number>) => t(locale, k, v)
const [remote, setRemote] = useState<RemoteRuntime[] | null>(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 = (
<div className="row-sub task-inline">
<LoaderCircle size={14} className="spin" aria-hidden="true" />
{task.percent >= 0 && (
<span className="progress rg-p-progress" aria-hidden="true">
<span className="progress-fill" style={{ transform: `scaleX(${Math.max(0, Math.min(1, task.percent / 100))})` }} />
</span>
)}
<span>{task.percent >= 0 ? `${Math.floor(task.percent)}%` : ''} {taskDetail(task)}</span>
</div>
)
} else if (active && !updatable) {
sub = <div className="row-sub">{shortQemu(active.manifest.qemu_version)}</div>
} else if (error) {
const isRateLimit = /rate limit|HTTP 403|HTTP 429/i.test(error)
const msg = isRateLimit ? tr('runtimeLookupRateLimit') : `${tr('runtimeLookupFailed')}: ${error}`
sub = <div className="row-sub" title={error}>{msg}</div>
} else if (!remote) {
sub = <div className="row-sub">…</div>
} else if (!latest) {
sub = <div className="row-sub">{tr('runtimeNoneOnChannel')}</div>
} else {
sub = (
<div className="row-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)}` : ''}
</div>
)
}
return (
<>
<div className="row">
<div className="row-main">
<div className="row-title">
{active ? <>{tr('runtimeCurrent')} <span className="mono">{active.commit}</span></> : tr('runtimeOnline')}
</div>
{sub}
</div>
<div className="row-right">
{running && task?.cancelable ? (
<IconButton label={tr('pauseDownload')} icon={<Pause size={16} />} onClick={() => onCancel(task.id)} />
) : !running && error ? (
<Button icon={<RefreshCw size={14} />} onClick={() => setReload((n) => n + 1)}>
{tr('runtimeRetry')}
</Button>
) : !running && updatable ? (
<Button variant="primary" icon={<Download size={14} />} onClick={() => onInstall(latest!.commit)}>
{tr(active ? 'runtimeUpdate' : 'runtimeInstall')}
</Button>
) : !running && active && remote ? (
<span className="badge rg-p-badge badge-ok">{tr('runtimeUpToDate')}</span>
) : null}
</div>
</div>
<div className="row">
<div className="row-main">
<div className="row-title">{tr('runtimeAlpha')}</div>
<div className="row-sub">{tr('runtimeAlphaHint')}</div>
</div>
<div className="row-right">
<Switch label={tr('runtimeAlpha')} checked={channel === 'alpha'} onChange={onChannel} />
</div>
</div>
</>
)
}