import { FolderOpen, HardDrive } from 'lucide-react' import React, { useEffect, useState } from 'react' import '../i18n/domains/wizard.gen' import '../i18n/domains/settings.gen' import { getLocaleOptions, I18nKey, joinNotices, locText, t } from '../i18n' import { AppState, DataRootCheck, DataRootChoice, Locale, MarketCatalogItem, ResolvedLocalePack, Task } from '../types' import { Button } from '../ui/controls' import { PopupSelect } from '../ui/settings' import { gpuCompatHint, recommendedMemoryGB, shortGPUName } from '../lib/format' import { describeHostWHPX } from '../lib/hypervisor' import { hasStambhaRuntime } from '../lib/endpoints' import { api } from '../wails' import { PluginAppIcon } from './PluginAppIcon' import { RuntimeOnline } from './RuntimeOnline' interface Props { locale: Locale locales?: ResolvedLocalePack[] state: AppState stage: 'folder' | 'runtime' runtimeTask: Task | null onLocale: (l: Locale) => void /** A plugin was installed or enabled; the app reloads plugin contributions. */ onPluginsChanged: () => void onFolderDone: () => void onFinish: () => void onError: (title: string, e: unknown) => void onChannel: (alpha: boolean) => void onInstallRuntime: (commit: string) => void onCancelTask: (id: string) => void onImportRuntime: () => void } const LOW_SPACE_GB = 60 /** First launch: choose the data folder, then (optionally) install the runtime. */ export const Setup: React.FC = (p) => { const tr = (k: I18nKey, v?: Record) => t(p.locale, k, v) return (
{p.stage === 'folder' ? : }
) } type StepProps = Props & { tr: (k: I18nKey, v?: Record) => string } const FolderStep: React.FC = ({ locale, locales, state, tr, onLocale, onPluginsChanged, onFolderDone, onError }) => { const [choices, setChoices] = useState([]) const [custom, setCustom] = useState('') const [customMode, setCustomMode] = useState(false) const [selected, setSelected] = useState('') const [check, setCheck] = useState(null) const [busy, setBusy] = useState(false) // A market language pack for the OS language, looked up once on first launch. const [langPack, setLangPack] = useState(null) const [langPackOn, setLangPackOn] = useState(true) useEffect(() => { api.SuggestDataRoots().then((list) => { const l = list ?? [] setChoices(l) const rec = l.find((c) => c.recommended) ?? l[0] if (rec) setSelected(rec.path) }).catch((e) => onError(tr('failGeneric'), e)) api.SuggestLanguagePack().then((p) => { if (p?.entry) setLangPack(p) }).catch((e) => console.warn('[setup] SuggestLanguagePack failed:', e)) }, []) useEffect(() => { if (!selected) { setCheck(null); return } let live = true api.CheckDataRoot(selected) .then((c) => { if (live) setCheck(c) }) .catch((e) => { if (live) setCheck(null) console.warn('[setup] CheckDataRoot failed:', e) }) return () => { live = false } }, [selected]) const browse = async () => { try { const dir = await api.PickDirectory(tr('setupTitle')) if (dir) { setCustom(dir); setCustomMode(true); setSelected(dir) } } catch (e) { console.warn('[setup] PickDirectory failed:', e) onError(tr('failGeneric'), e) } } const next = async () => { setBusy(true) try { await api.CompleteSetup(selected) } catch (e) { onError(tr('failGeneric'), e) setBusy(false) return } // Plugins live in the data folder, so the pack installs once it exists. // With the language left on "auto", enabling the pack is enough for the // UI to switch to the OS language. if (langPack && langPackOn) { try { const info = await api.InstallMarketPlugin(langPack.entry.id, false) if (info && !info.enabled && !info.needs_approval) await api.EnablePlugin(info.id, false) onPluginsChanged() } catch (e) { onError(tr('setupLangPackFailed'), e) } } setBusy(false) onFolderDone() } const osTag = state.detected_locale || locale const option = (path: string, meta: React.ReactNode) => ( ) const notices: string[] = [] if (check?.error) notices.push(check.error) else if (check) { if (check.system) notices.push(tr('setupWarnSystem')) if (check.external) notices.push(tr('setupWarnExternal')) if (check.free_gb > 0 && check.free_gb < LOW_SPACE_GB) notices.push(tr('setupWarnSpace')) } return ( <>

{tr('setupTitle')}

{tr('setupDesc')}

{choices.map((c) => option(c.path, ( <> {tr('setupFree', { n: c.free_gb.toFixed(0) })} {c.recommended && {tr('setupRecommended')}} {c.system && {tr('setupSystem')}} {c.external && {tr('setupExternal')}} )))} {customMode ? (
custom && setSelected(custom)} onChange={(e) => { setCustom(e.target.value); setSelected(e.target.value.trim()) }} />
) : ( )}
{notices.length > 0 && (
{joinNotices(locale, notices)}
)} {!check?.error && check?.has_config &&
{tr('setupReuse')}
} {customMode && check && check.path === custom && !check.error && (
{tr('setupFree', { n: check.free_gb.toFixed(0) })}
)} {langPack && ( )}
) } const RuntimeStep: React.FC = ({ locale, tr, state, runtimeTask, onChannel, onInstallRuntime, onCancelTask, onImportRuntime, onFolderDone, onFinish, onError }) => { const ready = !!state.active_runtime || hasStambhaRuntime(state.runtimes) const { host } = state const [enabling, setEnabling] = useState(false) const [activating, setActivating] = useState('') const activate = async (commit: string) => { setActivating(commit) try { await api.UseRuntime(commit) onFolderDone() } catch (e) { onError(tr('runtimeActivationFailed'), e) } finally { setActivating('') } } const enableWHPX = async () => { setEnabling(true) try { const changed = await api.SetHypervisorBoot(true) if (changed) onFolderDone() } catch (e) { onError(tr('failGeneric'), e) } finally { setEnabling(false) } } const whpx = describeHostWHPX(locale, host) const whpxOK = whpx.tone === 'ok' const gpu = host.recommended_gpu ?? (host.gpus ?? []).find((g) => g.kind !== 'virtual') const gpuHint = gpuCompatHint(locale, gpu) const gpuOK = !host.vulkan_probe_error && !!gpu && gpu.compat !== 'warn' && gpu.compat !== 'unsupported' const recMem = recommendedMemoryGB(host) return ( <>

{tr('setupRuntimeTitle')}

{tr('setupRuntimeDesc')}

{tr('setupCheck')}
{tr('hostWHPX')}
{whpx.hint && (
{whpx.hint}
)}
{whpx.canEnable ? ( ) : ( {whpx.short} )}
{tr('setupGPU')}
{gpu &&
{shortGPUName(gpu.name)}
} {host.vulkan_probe_error ? (
{tr('vulkanProbeFailed', { detail: host.vulkan_probe_error })}
) : !gpu &&
{tr('setupNoGPU')}
} {gpuHint &&
{gpuHint}
}
{tr( gpuOK ? 'whpxReady' : gpu?.compat === 'unsupported' ? 'gpuCompatUnsupported' : gpu?.compat === 'warn' ? 'gpuCompatWarn' : 'hostNeedsAction', )}
{tr('hostRAM')}
{host.commit_avail_gb > 0 ? tr('memCommitHint', { avail: host.commit_avail_gb.toFixed(1), rec: recMem }) : tr('hostRAMTotal', { n: host.total_ram_gb.toFixed(1) })}
= 8 ? 'badge-ok' : 'badge-warn'}`}>{tr('setupRecMem', { n: recMem })}
{(state.runtimes ?? []).filter((rt) => !rt.active).map((rt) => (
{rt.commit}
))}
) }