(input: {
state: WslServersState | undefined
view: WslAddServerView
selectedDistro: string | null
catalogSearch: string
catalogTarget: string | null
adding: boolean
probingAddable: boolean
})
| 66 | } |
| 67 | |
| 68 | export function addServerViewModel(input: { |
| 69 | state: WslServersState | undefined |
| 70 | view: WslAddServerView |
| 71 | selectedDistro: string | null |
| 72 | catalogSearch: string |
| 73 | catalogTarget: string | null |
| 74 | adding: boolean |
| 75 | probingAddable: boolean |
| 76 | }) { |
| 77 | const state = input.state |
| 78 | const visibleInstalledDistros = (state?.installed ?? []).filter((item) => !isHiddenDistro(item.name)) |
| 79 | const visibleOnlineDistros = (state?.online ?? []).filter((item) => !isHiddenDistro(item.name)) |
| 80 | const existingServerDistros = new Set((state?.servers ?? []).map((item) => item.config.distro)) |
| 81 | const addableInstalledDistros = visibleInstalledDistros.filter((item) => !existingServerDistros.has(item.name)) |
| 82 | const selectedDistro = addServerSelectedDistro(input.selectedDistro, visibleInstalledDistros, addableInstalledDistros) |
| 83 | const opencodeCheck = selectedDistro ? (state?.opencodeChecks[selectedDistro] ?? null) : null |
| 84 | const installableDistros = addServerInstallableDistros(visibleInstalledDistros, visibleOnlineDistros) |
| 85 | const filteredInstallableDistros = addServerFilteredInstallableDistros(installableDistros, input.catalogSearch) |
| 86 | const catalogTarget = addServerCatalogTarget(input.catalogTarget, filteredInstallableDistros) |
| 87 | const busy = !!state?.job || input.adding || input.probingAddable |
| 88 | |
| 89 | return { |
| 90 | busy, |
| 91 | runtimeState: addServerRuntimeState(state), |
| 92 | visibleInstalledDistros, |
| 93 | visibleOnlineDistros, |
| 94 | addableInstalledDistros, |
| 95 | selectedDistro, |
| 96 | opencodeCheck, |
| 97 | wslReady: !!state?.runtime?.available && !state?.pendingRestart, |
| 98 | distroStatuses: Object.fromEntries( |
| 99 | addableInstalledDistros.flatMap((item) => { |
| 100 | const status = addServerDistroStatus({ state, name: item.name, probingAddable: input.probingAddable }) |
| 101 | if (!status) return [] |
| 102 | return [[item.name, status]] |
| 103 | }), |
| 104 | ) as Record<string, DistroStatus | undefined>, |
| 105 | primaryButton: addServerPrimaryButton({ |
| 106 | state, |
| 107 | selectedDistro, |
| 108 | opencodeCheck, |
| 109 | adding: input.adding, |
| 110 | probingAddable: input.probingAddable, |
| 111 | }), |
| 112 | installableDistros, |
| 113 | filteredInstallableDistros, |
| 114 | catalogTarget, |
| 115 | installingCatalogDistro: state?.job?.kind === "install-distro", |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | function addServerSelectedDistro( |
| 120 | selected: string | null, |
no test coverage detected