Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix(home): remember local workspace mode selection (#16881)

Tim O'Farrell9e6e27aa7b41
diff --git a/__tests__/components/features/home/home-chat-launcher.test.tsx b/__tests__/components/features/home/home-chat-launcher.test.tsx
index 58b252ef19..097c03503f 100644
--- a/__tests__/components/features/home/home-chat-launcher.test.tsx
+++ b/__tests__/components/features/home/home-chat-launcher.test.tsx
@@ -7,6 +7,10 @@ import toast from "react-hot-toast";
import { HomeChatLauncher } from "#/components/features/home/home-chat-launcher";
import AgentServerConversationService from "#/api/conversation-service/agent-server-conversation-service.api";
import WorkspacesService from "#/api/workspaces-service/workspaces-service.api";
+import {
+ LAST_LOCAL_WORKSPACE_MODE_STORAGE_KEY,
+ writeStoredLocalWorkspaceMode,
+} from "#/utils/workspace-mode";
const mockNavigate = vi.fn();
const mockUseActiveBackend = vi.fn();
@@ -194,6 +198,13 @@ vi.mock("#/components/features/home/home-git-control-bar-preview", () => ({
>
New Worktree
</button>
+ <button
+ type="button"
+ data-testid="stub-workspace-mode-local-repo"
+ onClick={() => onWorkspaceModeChange("local_repo")}
+ >
+ Local Repo
+ </button>
</div>
),
}));
@@ -201,18 +212,21 @@ vi.mock("#/components/features/home/home-git-control-bar-preview", () => ({
// Stub the picker modal: pressing it selects one plugin then closes, mirroring
// the real modal's `onChange` + `onClose` contract. The picker catalog itself
// is covered by plugin-picker.test.tsx.
-vi.mock("#/components/features/automations/recommended-automations-launcher", () => ({
- RecommendedAutomationsLauncher: ({
- variant,
- className,
- }: {
- variant?: string;
- className?: string;
- }) =>
- variant === "rail" ? (
- <div data-testid="recommended-automations-rail" className={className} />
- ) : null,
-}));
+vi.mock(
+ "#/components/features/automations/recommended-automations-launcher",
+ () => ({
+ RecommendedAutomationsLauncher: ({
+ variant,
+ className,
+ }: {
+ variant?: string;
+ className?: string;
+ }) =>
+ variant === "rail" ? (
+ <div data-testid="recommended-automations-rail" className={className} />
+ ) : null,
+ }),
+);
vi.mock("#/components/features/plugins/plugin-picker-modal", () => ({
PluginPickerModal: ({
@@ -311,6 +325,7 @@ describe("HomeChatLauncher", () => {
fileUrls: [],
timestamp: "2020-01-01T00:00:00.000Z",
});
+ window.localStorage.removeItem(LAST_LOCAL_WORKSPACE_MODE_STORAGE_KEY);
vi.spyOn(WorkspacesService, "listWorkspaces").mockResolvedValue({
workspaces: [],
workspaceParents: [],
@@ -319,6 +334,7 @@ describe("HomeChatLauncher", () => {
afterEach(() => {
toast.remove();
+ window.localStorage.removeItem(LAST_LOCAL_WORKSPACE_MODE_STORAGE_KEY);
});
it("creates a conversation with just the typed query and navigates when no workspace is selected", async () => {
@@ -421,6 +437,38 @@ describe("HomeChatLauncher", () => {
);
});
+ it("restores and updates the last selected local workspace mode", async () => {
+ writeStoredLocalWorkspaceMode("new_worktree");
+ const { unmount } = renderLauncher();
+ const user = userEvent.setup();
+
+ await user.click(screen.getByTestId("open-workspace-button"));
+ await user.click(
+ await screen.findByTestId("stub-workspace-dialog-confirm"),
+ );
+
+ expect(screen.getByTestId("stub-workspace-mode")).toHaveTextContent(
+ "local:new_worktree",
+ );
+
+ await user.click(screen.getByTestId("stub-workspace-mode-local-repo"));
+ expect(screen.getByTestId("stub-workspace-mode")).toHaveTextContent(
+ "local:local_repo",
+ );
+
+ unmount();
+ renderLauncher();
+
+ await user.click(screen.getByTestId("open-workspace-button"));
+ await user.click(
+ await screen.findByTestId("stub-workspace-dialog-confirm"),
+ );
+
+ expect(screen.getByTestId("stub-workspace-mode")).toHaveTextContent(
+ "local:local_repo",
+ );
+ });
+
it("disables the local workspace launcher when the agent server is too old", async () => {
vi.spyOn(WorkspacesService, "listWorkspaces").mockRejectedValue({
code: "AGENT_SERVER_VERSION_TOO_OLD",
diff --git a/src/components/features/home/home-chat-launcher.tsx b/src/components/features/home/home-chat-launcher.tsx
index 2fb04b4fb2..1c7c5643df 100644
--- a/src/components/features/home/home-chat-launcher.tsx
+++ b/src/components/features/home/home-chat-launcher.tsx
@@ -25,6 +25,10 @@ import {
TOAST_OPTIONS,
} from "#/utils/custom-toast-handlers";
import { getWorkspacesUnsupportedMessage } from "#/utils/workspaces-compatibility";
+import {
+ readStoredLocalWorkspaceMode,
+ writeStoredLocalWorkspaceMode,
+} from "#/utils/workspace-mode";
import type { PluginSpec } from "#/api/conversation-service/agent-server-conversation-service.types";
import { PluginPickerModal } from "#/components/features/plugins/plugin-picker-modal";
import { PluginPickerTrigger } from "#/components/features/plugins/plugin-picker-trigger";
@@ -50,8 +54,9 @@ export function HomeChatLauncher() {
useState<GitRepository | null>(null);
const [pendingBranch, setPendingBranch] = useState<Branch | null>(null);
const [pendingProvider, setPendingProvider] = useState<Provider | null>(null);
- const [workspaceMode, setWorkspaceMode] =
- useState<WorkspaceMode>("local_repo");
+ const [workspaceMode, setWorkspaceModeState] = useState<WorkspaceMode>(() =>
+ readStoredLocalWorkspaceMode(),
+ );
const [selectedPlugins, setSelectedPlugins] = useState<PluginSpec[]>([]);
const [isPluginPickerOpen, setIsPluginPickerOpen] = useState(false);
@@ -72,6 +77,11 @@ export function HomeChatLauncher() {
? getWorkspacesUnsupportedMessage(workspacesError, t)
: null;
+ const setWorkspaceMode = (mode: WorkspaceMode) => {
+ setWorkspaceModeState(mode);
+ if (isLocal) writeStoredLocalWorkspaceMode(mode);
+ };
+
const hasSelection = isLocal
? !!pendingWorkspace
: !!pendingRepository && !!pendingBranch;
@@ -278,7 +288,6 @@ export function HomeChatLauncher() {
setPendingRepository(null);
setPendingBranch(null);
setPendingProvider(null);
- setWorkspaceMode("local_repo");
}}
/>
) : (
@@ -290,7 +299,7 @@ export function HomeChatLauncher() {
setPendingBranch(branch);
setPendingProvider(provider ?? repository.git_provider);
setPendingWorkspace(null);
- setWorkspaceMode("local_repo");
+ setWorkspaceModeState("local_repo");
}}
/>
)}
diff --git a/src/utils/workspace-mode.ts b/src/utils/workspace-mode.ts
index 6375aaad5c..b97d7c73a4 100644
--- a/src/utils/workspace-mode.ts
+++ b/src/utils/workspace-mode.ts
@@ -2,6 +2,38 @@ import type { BackendKind } from "#/api/backend-registry/types";
import type { WorkspaceMode } from "#/api/conversation-metadata-store";
import { I18nKey } from "#/i18n/declaration";
+export const DEFAULT_WORKSPACE_MODE: WorkspaceMode = "local_repo";
+
+export const LAST_LOCAL_WORKSPACE_MODE_STORAGE_KEY =
+ "openhands-last-local-workspace-mode";
+
+function isWorkspaceMode(value: string | null): value is WorkspaceMode {
+ return value === "local_repo" || value === "new_worktree";
+}
+
+export function readStoredLocalWorkspaceMode(): WorkspaceMode {
+ if (typeof window === "undefined") return DEFAULT_WORKSPACE_MODE;
+
+ try {
+ const stored = window.localStorage.getItem(
+ LAST_LOCAL_WORKSPACE_MODE_STORAGE_KEY,
+ );
+ return isWorkspaceMode(stored) ? stored : DEFAULT_WORKSPACE_MODE;
+ } catch {
+ return DEFAULT_WORKSPACE_MODE;
+ }
+}
+
+export function writeStoredLocalWorkspaceMode(mode: WorkspaceMode): void {
+ if (typeof window === "undefined") return;
+
+ try {
+ window.localStorage.setItem(LAST_LOCAL_WORKSPACE_MODE_STORAGE_KEY, mode);
+ } catch {
+ // Ignore storage failures; the selector still works for this render.
+ }
+}
+
export function getWorkspaceModeI18nKey(
mode: WorkspaceMode,
backendKind: BackendKind,