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 } export function RestoreImageSource({ locale, host, tasks, initialImage, disabled, selectedPath, onPathChange, onCancelTask }: Props) { const [source, setSource] = useState('apple') const [localPath, setLocalPath] = useState('') const [cached, setCached] = useState(() => initialImage ? [initialImage] : []) const [latest, setLatest] = useState() 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[1], vars?: Record) => 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, fn: () => Promise) => { setPending(action) setActionErr('') try { await fn() } catch (error) { setActionErr(parseAppError(error).summary) } finally { setPending('') } } return ( <>
{(['apple', 'local'] as const).map((mode) => { const apple = mode === 'apple' const Icon = apple ? CloudDownload : Disc3 return })}
{actionErr &&
{actionErr}
} {local ? setLocalPath(e.target.value)} /> } /> : {choices.length > 0 && ({ value: choice.key, label: choice.label }))} /> } />} {active ? = 0 ? active.percent : undefined} /> {active.cancelable && } } /> : complete ? : currentRelease && } busy={pending === 'download'} disabled={!!pending || lowSpace} onClick={() => doAction('download', async () => { await api.DownloadInstaller(currentRelease.version) })}>{tr(partial ? 'installersResume' : 'installersDownload')} } />} {(releaseErr || cacheErr) &&
{releaseErr || cacheErr}
} {!releaseErr && latest === undefined && !complete &&

{tr('offlineLoading')}

} {!releaseErr && latest === null && choices.length === 0 &&

{tr('srcIPSWNone')}

}
} ) }