()
| 1298 | } |
| 1299 | |
| 1300 | async function closeLocalVaultForWindow(): Promise<VaultInfo | null> { |
| 1301 | const win = currentIpcWindow() ?? mainWindow |
| 1302 | if (win && !win.isDestroyed() && windowVaults.isRemoteWindow(win.id)) return null |
| 1303 | if ((!win || win.isDestroyed()) && currentWorkspaceMode === 'remote') return null |
| 1304 | const vault = win && !win.isDestroyed() ? windowVaults.vaultForWindow(win.id) : currentVault |
| 1305 | if (!vault) return null |
| 1306 | |
| 1307 | const cfg = await loadConfig() |
| 1308 | const candidates = new Map<string, { root: string; name: string }>() |
| 1309 | const remainingLocalVaults = forgetLocalVault(cfg.localVaults, vault.root) |
| 1310 | for (const entry of remainingLocalVaults) { |
| 1311 | candidates.set(path.resolve(entry.root), entry) |
| 1312 | } |
| 1313 | for (const entry of windowVaults.localVaultsExcept(vault.root)) { |
| 1314 | const root = path.resolve(entry.root) |
| 1315 | if (!candidates.has(root)) candidates.set(root, entry) |
| 1316 | } |
| 1317 | const nextLocalVault = candidates.values().next().value ?? null |
| 1318 | const nextVault = |
| 1319 | nextLocalVault && win && !win.isDestroyed() |
| 1320 | ? await setVaultForWindow(win, nextLocalVault.root, { persist: false }) |
| 1321 | : nextLocalVault |
| 1322 | ? await setVault(nextLocalVault.root) |
| 1323 | : null |
| 1324 | |
| 1325 | if (!nextVault) { |
| 1326 | if (win && !win.isDestroyed()) { |
| 1327 | windowVaults.clearWindow(win.id) |
| 1328 | } |
| 1329 | if (currentVault && path.resolve(currentVault.root) === path.resolve(vault.root)) { |
| 1330 | currentVault = null |
| 1331 | } |
| 1332 | currentWorkspaceMode = 'local' |
| 1333 | } |
| 1334 | |
| 1335 | await updateConfig((cfg) => ({ |
| 1336 | ...cfg, |
| 1337 | workspaceMode: 'local', |
| 1338 | vaultRoot: nextVault ? nextVault.root : null, |
| 1339 | localVaults: forgetLocalVault(cfg.localVaults, vault.root), |
| 1340 | remoteWorkspaceProfileId: null |
| 1341 | })) |
| 1342 | |
| 1343 | return nextVault |
| 1344 | } |
| 1345 | |
| 1346 | async function listLocalVaults(): Promise<LocalVaultEntry[]> { |
| 1347 | const cfg = await loadConfig() |
no test coverage detected