Files
Reims-Go/frontend/src/components/RestoreImageSource.tsx
T
千川白浪 e0ff794b0e feat: add experimental macOS desktop support on Apple Silicon (#207)
* feat: add experimental Apple Silicon support with Nivfold

Integrate the standalone Nivfold protocol 1 runner; tested against Nivfold f436fb6. The Windows Reims vGPU runtime remains independent.

* fix: open QQ invites and VM SSH on macOS

* fix: move VM, View and Help to the macOS menu bar

* feat(macos): add resumable Apple restore image downloads

Expose the compatible restore image in the VM wizard and Images page, validate downloads before caching, and fix macOS memory reporting and plugin SVG rendering.

* fix(macos): localize the native application menu

* fix(macos): address native desktop review findings
2026-12-06 00:47:07 +08:00

138 lines
7.8 KiB
TypeScript

import { CloudDownload, Disc3, Download, Pause, X } from 'lucide-react'
import { useEffect, useLayoutEffect, useState } from 'react'
import '../i18n/domains/settings.gen'
import '../i18n/domains/wizard.gen'
import { parseAppError, t } from '../i18n'
import { cachedRestoreImage, matchesRestoreImage, restoreImageChoiceKey, restoreImageChoices } from '../lib/restoreImages'
import { findActiveInstallerTask, taskDetail, taskInstallerKind, taskInstallerPhase, taskStopAction } from '../lib/tasks'
import type { CachedInstaller, HostReport, Locale, Release, Task } from '../types'
import { Button } from '../ui/controls'
import { PopupSelect, SetGroup, SetRow } from '../ui/settings'
import { api } from '../wails'
type Source = 'apple' | 'local'
interface Props {
locale: Locale
host: HostReport
tasks: Task[]
initialImage?: CachedInstaller
disabled: boolean
selectedPath: string
onPathChange: (path: string) => void
onCancelTask: (id: string) => Promise<void>
}
export function RestoreImageSource({ locale, host, tasks, initialImage, disabled, selectedPath, onPathChange, onCancelTask }: Props) {
const [source, setSource] = useState<Source>('apple')
const [localPath, setLocalPath] = useState('')
const [cached, setCached] = useState<CachedInstaller[]>(() => initialImage ? [initialImage] : [])
const [latest, setLatest] = useState<Release | null>()
const [selection, setSelection] = useState(() => initialImage?.version ? restoreImageChoiceKey(initialImage.version, initialImage.build ?? '') : '')
const [releaseErr, setReleaseErr] = useState('')
const [cacheErr, setCacheErr] = useState('')
const [actionErr, setActionErr] = useState('')
const [catalogRequest, setCatalogRequest] = useState(0)
const [pending, setPending] = useState<'download' | 'stop' | 'file' | ''>('')
const tr = (key: Parameters<typeof t>[1], vars?: Record<string, string | number>) => t(locale, key, vars)
const tasksSig = tasks.filter((task) => taskInstallerKind(task) === 'ipsw')
.map((task) => `${task.id}:${task.status}:${task.installer_phase}`).join('|')
useEffect(() => {
if (source !== 'apple') return
let live = true
api.ListInstallers().then((items) => {
if (!live) return
setCacheErr('')
setCached(items ?? [])
}).catch((error) => { if (live) setCacheErr(parseAppError(error).summary) })
return () => { live = false }
}, [source, tasksSig, catalogRequest])
useEffect(() => {
if (source !== 'apple') return
let live = true
setReleaseErr('')
setLatest(undefined)
api.ListOfflineReleases().then((items) => {
if (live) setLatest(items?.[0] ?? null)
}).catch((error) => {
if (!live) return
setLatest(null)
setReleaseErr(parseAppError(error).summary)
})
return () => { live = false }
}, [source, catalogRequest])
const choices = restoreImageChoices(latest ?? undefined, cached)
const selected = choices.find((choice) => choice.key === selection) ?? choices[0]
const complete = selected && cachedRestoreImage(cached, selected.version, selected.build)
const currentRelease = selected && latest?.version === selected.version && latest.build === selected.build ? latest : undefined
const active = currentRelease && findActiveInstallerTask(tasks, 'ipsw', currentRelease.version)?.primary
const partial = currentRelease && cached.find((item) => item.partial && matchesRestoreImage(item, currentRelease.version, currentRelease.build))
const needGB = currentRelease?.size ? Math.max(0, currentRelease.size - (partial?.bytes ?? 0)) / (2 ** 30) : 0
const lowSpace = needGB > 0 && !complete && !active && host.free_disk_gb > 0 && host.free_disk_gb < needGB
const local = source === 'local'
const path = local ? localPath : complete?.path ?? ''
// Update the parent submit control before the new selection is painted.
useLayoutEffect(() => { if (selectedPath !== path) onPathChange(path) }, [path, selectedPath, onPathChange])
const doAction = async (action: Exclude<typeof pending, ''>, fn: () => Promise<void>) => {
setPending(action)
setActionErr('')
try {
await fn()
} catch (error) {
setActionErr(parseAppError(error).summary)
} finally {
setPending('')
}
}
return (
<>
<div className="source-cards" role="radiogroup" aria-label={tr('step1')}>
{(['apple', 'local'] as const).map((mode) => {
const apple = mode === 'apple'
const Icon = apple ? CloudDownload : Disc3
return <label key={mode} className="source-card" data-selected={source === mode || undefined}>
<input type="radio" name="restore-source" checked={source === mode} disabled={disabled} onChange={() => { setActionErr(''); setSource(mode) }} />
<span className={`source-icon source-icon-${apple ? 'apple' : 'installer'}`} aria-hidden="true"><Icon size={20} /></span>
<span className="source-title">{tr(apple ? 'srcIPSWDownloadTitle' : 'srcIPSWLocalTitle')}</span>
<span className="source-sub">{tr(apple ? 'srcIPSWDownloadSub' : 'srcIPSWLocalSub')}</span>
</label>
})}
</div>
{actionErr && <div className="notice notice-error" role="alert">{actionErr}</div>}
{local ? <SetGroup>
<SetRow title={tr('srcIPSWLocalTitle')} disabled={disabled} control={
<div className="input-group">
<input className="input rg-p-input mono" value={localPath} aria-label={tr('srcIPSWLocalTitle')} onChange={(e) => setLocalPath(e.target.value)} />
<Button busy={pending === 'file'} disabled={!!pending} onClick={() => doAction('file', async () => {
const file = await api.PickFile(tr('srcIPSWLocalTitle'), '*.ipsw')
if (file) setLocalPath(file)
})}>{tr('wizChooseFile')}</Button>
</div>
} />
</SetGroup> : <SetGroup>
{choices.length > 0 && <SetRow title={tr('macosVersion')} disabled={disabled} control={
<PopupSelect label={tr('macosVersion')} value={selected?.key ?? ''} onChange={setSelection} options={choices.map((choice) => ({ value: choice.key, label: choice.label }))} />
} />}
{active ? <SetRow title={tr(taskInstallerPhase(active) === 'queued' ? 'offlineQueued' : 'offlineDownloading')} desc={taskDetail(active)} disabled={disabled} control={
<div className="hstack" role="status">
<progress aria-label={tr('offlineDownloading')} max={100} value={active.percent >= 0 ? active.percent : undefined} />
{active.cancelable && <Button icon={taskStopAction(locale, active).kind === 'pause' ? <Pause size={14} /> : <X size={14} />} busy={pending === 'stop'} disabled={!!pending} onClick={() => doAction('stop', () => onCancelTask(active.id))}>{taskStopAction(locale, active).label}</Button>}
</div>
} /> : complete ? <SetRow title={tr('offlineCached')} desc={selected?.label} /> : currentRelease && <SetRow title={tr('srcIPSWDownloadTitle')} disabled={disabled} desc={lowSpace ? tr('offlineSpaceWarn', { free: host.free_disk_gb.toFixed(0), need: Math.ceil(needGB) }) : undefined} control={
<Button icon={<Download size={14} />} busy={pending === 'download'} disabled={!!pending || lowSpace} onClick={() => doAction('download', async () => { await api.DownloadInstaller(currentRelease.version) })}>{tr(partial ? 'installersResume' : 'installersDownload')}</Button>
} />}
{(releaseErr || cacheErr) && <div className="notice notice-error hstack" role="alert">
<span>{releaseErr || cacheErr}</span>
<Button onClick={() => setCatalogRequest((id) => id + 1)}>{tr('installersRetry')}</Button>
</div>}
{!releaseErr && latest === undefined && !complete && <p className="hint" role="status">{tr('offlineLoading')}</p>}
{!releaseErr && latest === null && choices.length === 0 && <p className="hint">{tr('srcIPSWNone')}</p>}
</SetGroup>}
</>
)
}