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

314 lines
13 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { Cpu, HardDrive, MemoryStick, Plus, RefreshCw, ShieldCheck, Trash } from 'lucide-react'
import React, { useEffect, useState } from 'react'
import '../i18n/domains/settings.gen'
import '../i18n/domains/wizard.gen'
import { I18nKey, t } from '../i18n'
import { formatBytes, gpuCompatHint, gpuKey, gpuMetaLine, sameGPU } from '../lib/format'
import { describeHostWHPX } from '../lib/hypervisor'
import { AppState, Locale, StorageBreakdown, Task } from '../types'
import { Button, IconButton } from '../ui/controls'
import { Switch } from '../ui/settings'
import { api } from '../wails'
import { RuntimeOnline } from './RuntimeOnline'
interface Props {
state: AppState
locale: Locale
onRefresh: () => Promise<void>
onNewVM?: () => void
onError: (title: string, e: unknown) => void
onToast?: (title: string) => void
runtimeTask: Task | null
onChannel: (alpha: boolean) => void
onInstallRuntime: (commit: string) => void
onImportRuntime: () => Promise<void>
onCancelTask: (id: string) => void
}
export const HostView: React.FC<Props> = ({ state, locale, onRefresh, onNewVM, onError, onToast, runtimeTask, onChannel, onInstallRuntime, onImportRuntime, onCancelTask }) => {
const [importing, setImporting] = useState(false)
const [enabling, setEnabling] = useState(false)
const [savingASLR, setSavingASLR] = useState(false)
const [confirmDelete, setConfirmDelete] = useState('')
const [removing, setRemoving] = useState('')
const [storage, setStorage] = useState<StorageBreakdown | null>(null)
const { host, config, runtimes } = state
const tr = (k: I18nKey, v?: Record<string, string | number>) => t(locale, k, v)
useEffect(() => {
let live = true
api.GetStorageBreakdown().then((s) => { if (live) setStorage(s) }).catch((e) => console.warn('[host] GetStorageBreakdown failed:', e))
return () => { live = false }
}, [state.config.data_root, state.vms?.length, state.runtimes?.length])
const whpx = describeHostWHPX(locale, host)
const guard = (fn: () => Promise<unknown>) => async () => {
try {
await fn()
await onRefresh()
} catch (e) {
onError(tr('failGeneric'), e)
}
}
const enableWHPX = async () => {
setEnabling(true)
try {
const changed = await api.SetHypervisorBoot(true)
if (!changed) return
onToast?.(tr('toastHypervisorPendingOn'))
onRefresh()
} catch (e) {
onError(tr('failGeneric'), e)
} finally {
setEnabling(false)
}
}
const setQEMUASLR = async (enabled: boolean) => {
setSavingASLR(true)
await guard(() => api.SetQEMUASLR(enabled))()
setSavingASLR(false)
}
const importRuntime = async () => {
setImporting(true)
try {
await onImportRuntime()
} finally {
setImporting(false)
}
}
const removeRuntime = async (commit: string) => {
setRemoving(commit)
try {
await api.RemoveRuntime(commit)
setConfirmDelete('')
onToast?.(tr('runtimeDeleted', { commit }))
onRefresh()
} catch (e) {
onError(tr('failDelete'), e)
} finally {
setRemoving('')
}
}
const gpuKind = (k: string) =>
tr(k === 'discrete' ? 'gpuDiscrete' : k === 'integrated' ? 'gpuIntegrated' : k === 'virtual' ? 'gpuVirtual' : 'gpuUnknown')
const realGPUs = (host.gpus ?? []).filter((g) => g.kind !== 'virtual')
return (
<div className="page">
<header className="page-head">
<div className="page-title"><h1>{tr('sidebarHost')}</h1></div>
<div className="actions">
<IconButton label={tr('refresh')} icon={<RefreshCw size={16} />} onClick={onRefresh} />
</div>
</header>
{(state.vms?.length ?? 0) === 0 && host.whpx === 'ready' && !!state.active_runtime && onNewVM && (
<section className="welcome-card">
<h1>{tr('welcomeTitle')}</h1>
<p>{tr('welcomeSub')}</p>
<Button variant="primary" icon={<Plus size={14} aria-hidden="true" />} onClick={onNewVM}>
{tr('newVM')}
</Button>
</section>
)}
<div className="tiles">
<div className="tile">
<div className="tile-head">
<span className={`tile-icon ${whpx.tone}`} aria-hidden="true"><ShieldCheck size={13} /></span>
{tr('whpxTitle')}
</div>
<div className={`tile-value ${whpx.tone}`}>{whpx.short}</div>
{whpx.canEnable ? (
<Button variant="primary" className="tile-action" busy={enabling} onClick={enableWHPX}>{tr('enableWHPX')}</Button>
) : whpx.hint ? (
<div className="tile-sub" title={whpx.hint}>{whpx.hint}</div>
) : null}
</div>
<div className="tile">
<div className="tile-head">
<span className="tile-icon accent" aria-hidden="true"><Cpu size={13} /></span>
{tr('hostCPU')}
</div>
<div className="tile-value">{tr('hostCoresThreads', { c: host.physical_cores, t: host.logical_cpus })}</div>
{host.p_core_count > 0 && host.e_core_count > 0 && (
<div className="tile-sub">{host.p_core_count}P + {host.e_core_count}E</div>
)}
</div>
<div className="tile">
<div className="tile-head">
<span className="tile-icon accent" aria-hidden="true"><MemoryStick size={13} /></span>
{tr('hostRAM')}
</div>
<div className="tile-value">{tr('hostRAMAvail', { n: host.avail_ram_gb.toFixed(1) })}</div>
<div className="tile-sub">
{tr('hostRAMTotal', { n: host.total_ram_gb.toFixed(1) })}
{host.memory_spec?.speed_mhz
? ` · ${host.memory_spec.speed_mhz} MHz${host.memory_spec.type_name ? ` ${host.memory_spec.type_name}` : ''}`
: ''}
</div>
</div>
<div className="tile">
<div className="tile-head">
<span className="tile-icon" aria-hidden="true"><HardDrive size={13} /></span>
{tr('hostDisk')}
</div>
<div className="tile-value">{host.free_disk_gb.toFixed(0)} GB</div>
</div>
</div>
{storage && storage.total_bytes > 0 && (() => {
const tot = Math.max(storage.total_bytes, 1)
const vmsPct = (storage.vms_bytes / tot) * 100
const instPct = (storage.installers_bytes / tot) * 100
const rtPct = (storage.runtimes_bytes / tot) * 100
return (
<section className="card rg-p-card">
<div className="card-title">
<span>{tr('storageBreakdownTitle')}</span>
<span className="spacer" />
<span className="muted tnum" style={{ fontSize: 'var(--fs-12)', fontWeight: 'var(--fw-medium)' }}>{formatBytes(storage.total_bytes, locale)}</span>
</div>
<div className="storage-bar" aria-hidden="true">
{vmsPct > 0 && <div className="storage-seg-vms" style={{ width: `${vmsPct}%` }} />}
{instPct > 0 && <div className="storage-seg-inst" style={{ width: `${instPct}%` }} />}
{rtPct > 0 && <div className="storage-seg-rt" style={{ width: `${rtPct}%` }} />}
</div>
<div className="storage-legend">
<span className="storage-legend-item">
<span className="storage-dot storage-seg-vms" aria-hidden="true" />
{tr('storageVMs')} · {formatBytes(storage.vms_bytes, locale)}
</span>
<span className="storage-legend-item">
<span className="storage-dot storage-seg-inst" aria-hidden="true" />
{tr('storageInstallers')} · {formatBytes(storage.installers_bytes, locale)}
</span>
<span className="storage-legend-item">
<span className="storage-dot storage-seg-rt" aria-hidden="true" />
{tr('storageRuntimes')} · {formatBytes(storage.runtimes_bytes, locale)}
</span>
</div>
</section>
)
})()}
<section className="section">
<div className="section-title">
{tr('hostRuntime')}
<Button variant="ghost" busy={importing} onClick={importRuntime}>
{tr('importRuntime')}
</Button>
</div>
<div className="list">
<RuntimeOnline
locale={locale}
channel={config.runtime_channel === 'alpha' ? 'alpha' : 'stable'}
installed={runtimes ?? []}
task={runtimeTask}
onChannel={onChannel}
onInstall={onInstallRuntime}
onCancel={onCancelTask}
/>
<div className="row">
<div className="row-main">
<div className="row-title">{tr('qemuASLR')}</div>
<div className="row-sub">{tr('qemuASLRHint')}</div>
</div>
<div className="row-right">
<Switch label={tr('qemuASLR')} checked={!!config.qemu_aslr} disabled={savingASLR} onChange={setQEMUASLR} />
</div>
</div>
{(runtimes ?? []).filter((rt) => !rt.active).map((rt) => (
<div className="row" key={rt.commit}>
<div className="row-main">
<div className="row-title"><span className="mono">{rt.commit}</span></div>
<div className="row-sub">{rt.manifest.qemu_version.replace(/^QEMU emulator version\s*/i, 'QEMU ')}</div>
</div>
<div className="row-right">
{confirmDelete === rt.commit ? (
<>
<Button variant="ghost" disabled={removing === rt.commit} onClick={() => setConfirmDelete('')}>
{tr('cancel')}
</Button>
<Button variant="danger" busy={removing === rt.commit} onClick={() => removeRuntime(rt.commit)}>
{tr('runtimeDeleteConfirm')}
</Button>
</>
) : (
<>
<Button disabled={removing === rt.commit} onClick={guard(() => api.UseRuntime(rt.commit))}>
{tr('activate')}
</Button>
<IconButton
label={tr('runtimeDelete')}
icon={<Trash size={15} />}
disabled={removing !== ''}
onClick={() => setConfirmDelete(rt.commit)}
/>
</>
)}
</div>
</div>
))}
</div>
</section>
<section className="section">
<div className="section-title">{tr('hostGPUs')}</div>
<div className="list">
{realGPUs.length === 0 ? (
<div className="row"><span className="row-sub">—</span></div>
) : (
realGPUs.map((g, idx) => {
const meta = gpuMetaLine(g)
const hint = gpuCompatHint(locale, g)
const isRec = sameGPU(host.recommended_gpu, g)
return (
<div className="row" key={gpuKey(g, idx)}>
<div className="row-main">
<div className="row-title" title={g.vulkan_icd}>
{g.name}
<span className={`badge rg-p-badge ${g.kind === 'discrete' ? 'badge-accent' : ''}`}>{gpuKind(g.kind)}</span>
{g.compat === 'unsupported' && <span className="badge rg-p-badge badge-warn">{tr('gpuCompatUnsupported')}</span>}
{g.compat === 'warn' && <span className="badge rg-p-badge badge-warn">{tr('gpuCompatWarn')}</span>}
{isRec && g.compat !== 'warn' && g.compat !== 'unsupported' && <span className="badge rg-p-badge badge-ok">{tr('gpuRecommended')}</span>}
</div>
{meta && <div className="row-sub">{meta}</div>}
{hint && <div className="row-sub warn-text">{hint}</div>}
</div>
</div>
)
})
)}
</div>
</section>
{(host.displays?.length ?? 0) > 0 && (
<section className="section">
<div className="section-title">{tr('hostDisplays')}</div>
<div className="list">
{(host.displays ?? []).map((d, idx) => (
<div className="row" key={d.name || idx}>
<div className="row-main">
<div className="row-title">
{d.width} × {d.height}
{d.refresh_hz ? ` @ ${d.refresh_hz}Hz` : ''}
<span className="badge rg-p-badge">{d.scale_pct || 100}%</span>
{d.primary && <span className="badge rg-p-badge badge-ok">{tr('hostDisplayPrimary')}</span>}
</div>
</div>
</div>
))}
</div>
</section>
)}
</div>
)
}