fix(ui): detect VS Code from bootstrap config in shared runtime helpers
d2efa707 fixed projects-store detection via __VSCODE_CONFIG__, but
lib/desktop.isVSCodeRuntime (used by useDirectoryStore) still required
RuntimeAPIs. At webview startup that left directory init on stale
localStorage paths from other windows (#2359).
Share bootstrap detection in lib/vscodeBootstrap and use it from both
desktop runtime checks and the projects-store helper.
Co-authored-by: Serhii Dziupin <makeittech@users.noreply.github.com>
This commit is contained in:
co-authored by
Serhii Dziupin
parent
8801d69c66
commit
ccb74d8366
@@ -2,6 +2,7 @@ import { create } from 'zustand';
|
||||
import { devtools } from 'zustand/middleware';
|
||||
import { opencodeClient } from '@/lib/opencode/client';
|
||||
import { getDesktopHomeDirectory, isVSCodeRuntime } from '@/lib/desktop';
|
||||
import { getVSCodeBootstrapConfig } from '@/lib/vscodeBootstrap';
|
||||
import { subscribeRuntimeEndpointChanged } from '@/lib/runtime-switch';
|
||||
import { updateDesktopSettings } from '@/lib/persistence';
|
||||
import { useFileSearchStore } from '@/stores/useFileSearchStore';
|
||||
@@ -227,7 +228,7 @@ const getVsCodeWorkspaceFolder = (): string | null => {
|
||||
if (!isVSCodeRuntime()) {
|
||||
return null;
|
||||
}
|
||||
const workspaceFolder = (window as unknown as { __VSCODE_CONFIG__?: { workspaceFolder?: unknown } }).__VSCODE_CONFIG__?.workspaceFolder;
|
||||
const workspaceFolder = getVSCodeBootstrapConfig()?.workspaceFolder;
|
||||
if (typeof workspaceFolder !== 'string' || workspaceFolder.trim().length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { describe, expect, test } from 'bun:test';
|
||||
import type { RuntimeAPIs } from '@/lib/api/types';
|
||||
import { isVSCodeRuntime } from './vscodeRuntime';
|
||||
|
||||
describe('VS Code runtime detection', () => {
|
||||
@@ -9,6 +10,13 @@ describe('VS Code runtime detection', () => {
|
||||
})).toBe(true);
|
||||
});
|
||||
|
||||
test('uses registered runtime APIs when bootstrap is absent', () => {
|
||||
const runtimeApis = {
|
||||
runtime: { platform: 'vscode', isDesktop: false, isVSCode: true },
|
||||
} as RuntimeAPIs;
|
||||
expect(isVSCodeRuntime(runtimeApis, null)).toBe(true);
|
||||
});
|
||||
|
||||
test('does not classify an unregistered web runtime as VS Code', () => {
|
||||
expect(isVSCodeRuntime(null, null)).toBe(false);
|
||||
});
|
||||
|
||||
@@ -1,18 +1,14 @@
|
||||
import type { RuntimeAPIs } from '@/lib/api/types';
|
||||
import {
|
||||
getVSCodeBootstrapConfig,
|
||||
isVSCodeBootstrapPresent,
|
||||
type VSCodeBootstrapConfig,
|
||||
} from '@/lib/vscodeBootstrap';
|
||||
|
||||
export interface VSCodeBootstrapConfig {
|
||||
workspaceFolder?: unknown;
|
||||
workspaceFolders?: unknown;
|
||||
}
|
||||
|
||||
export const getVSCodeBootstrapConfig = (): VSCodeBootstrapConfig | null => {
|
||||
if (typeof window === 'undefined') {
|
||||
return null;
|
||||
}
|
||||
return (window as unknown as { __VSCODE_CONFIG__?: VSCodeBootstrapConfig }).__VSCODE_CONFIG__ ?? null;
|
||||
};
|
||||
export type { VSCodeBootstrapConfig };
|
||||
export { getVSCodeBootstrapConfig };
|
||||
|
||||
export const isVSCodeRuntime = (
|
||||
runtimeApis: RuntimeAPIs | null,
|
||||
bootstrapConfig = getVSCodeBootstrapConfig(),
|
||||
): boolean => Boolean(bootstrapConfig || runtimeApis?.runtime?.isVSCode);
|
||||
bootstrapConfig: VSCodeBootstrapConfig | null = getVSCodeBootstrapConfig(),
|
||||
): boolean => Boolean(isVSCodeBootstrapPresent(bootstrapConfig) || runtimeApis?.runtime?.isVSCode);
|
||||
|
||||
Reference in New Issue
Block a user