π OpenHands: AI-Driven Development
feat: show workspace path in Files view (#16362)
Hurairabalochd636ae6893a8
diff --git a/__tests__/components/features/files-tab/workspace-path.test.tsx b/__tests__/components/features/files-tab/workspace-path.test.tsxnew file mode 100644index 0000000000..43af240b38--- /dev/null+++ b/__tests__/components/features/files-tab/workspace-path.test.tsx@@ -0,0 +1,76 @@+import { render, screen, within } from "@testing-library/react";+import userEvent from "@testing-library/user-event";+import { afterEach, describe, expect, it, vi } from "vitest";++import { WorkspacePath } from "#/components/features/files-tab/workspace-path";++const originalClipboard = navigator.clipboard;++describe("WorkspacePath", () => {+ afterEach(() => {+ vi.restoreAllMocks();+ Object.defineProperty(navigator, "clipboard", {+ configurable: true,+ value: originalClipboard,+ });+ });++ it("shows the effective workspace path", () => {+ const path = "/Users/alice/workspace/project/abc123";++ render(<WorkspacePath path={path} />);++ expect(screen.getByTestId("files-tab-workspace-path")).toHaveTextContent(+ "WORKSPACE$TITLE:",+ );+ expect(+ screen.getByTestId("files-tab-workspace-path-value"),+ ).toHaveTextContent(path);+ });++ it("keeps the complete path available when the text is truncated", () => {+ const path =+ "/Users/alice/a-very-long-workspace-name/project/with/nested/directories";++ render(<WorkspacePath path={path} />);++ const value = screen.getByTestId("files-tab-workspace-path-value");+ expect(value).toHaveClass("truncate");+ expect(value).toHaveAttribute("title", path);+ });++ it("copies the complete path and confirms the action", async () => {+ const path = "C:\\Users\\alice\\workspace\\project";+ const writeText = vi.fn().mockResolvedValue(undefined);+ const user = userEvent.setup();+ Object.defineProperty(navigator, "clipboard", {+ configurable: true,+ value: { writeText },+ });++ render(<WorkspacePath path={path} />);++ const workspacePath = screen.getByTestId("files-tab-workspace-path");+ await user.click(+ within(workspacePath).getByRole("button", { name: "BUTTON$COPY" }),+ );++ expect(writeText).toHaveBeenCalledWith(path);+ expect(+ within(workspacePath).getByRole("button", { name: "BUTTON$COPIED" }),+ ).toBeDisabled();+ });++ it("does not render without a workspace path", () => {+ const { rerender } = render(<WorkspacePath path={null} />);++ expect(+ screen.queryByTestId("files-tab-workspace-path"),+ ).not.toBeInTheDocument();++ rerender(<WorkspacePath path=" " />);+ expect(+ screen.queryByTestId("files-tab-workspace-path"),+ ).not.toBeInTheDocument();+ });+});diff --git a/__tests__/routes/files-tab.test.tsx b/__tests__/routes/files-tab.test.tsxindex c3a7dfc380..0cbaed3638 100644--- a/__tests__/routes/files-tab.test.tsx+++ b/__tests__/routes/files-tab.test.tsx@@ -15,6 +15,7 @@ const useHasGitCommitsMock = vi.fn();const useUnifiedGitCommitsMock = vi.fn();const useWorkspaceFilesMock = vi.fn();const useWorkspaceFileContentMock = vi.fn();+const useActiveConversationMock = vi.fn();const refetchGitChangesMock = vi.fn();vi.mock("#/hooks/use-has-attached-source", () => ({@@ -39,6 +40,10 @@ vi.mock("#/hooks/query/use-workspace-file-content", () => ({useWorkspaceFileContentMock(path),}));+vi.mock("#/hooks/query/use-active-conversation", () => ({+ useActiveConversation: () => useActiveConversationMock(),+}));+vi.mock("#/hooks/query/use-unified-get-git-changes", () => ({useUnifiedGetGitChanges: () => ({refetch: refetchGitChangesMock,@@ -94,6 +99,7 @@ describe("FilesTab", () => {useUnifiedGitCommitsMock.mockReset();useWorkspaceFilesMock.mockReset();useWorkspaceFileContentMock.mockReset();+ useActiveConversationMock.mockReset();refetchGitChangesMock.mockReset();// Default: pretend the probe has already resolved with at least one// commit. Individual tests can override this for "empty repo" cases.@@ -129,6 +135,11 @@ describe("FilesTab", () => {isLoading: false,isError: false,});+ useActiveConversationMock.mockReturnValue({+ data: {+ workspace: { working_dir: "/workspace/project" },+ },+ });});it("defaults to diff view when the user attached a source (repo or workspace)", () => {@@ -213,6 +224,24 @@ describe("FilesTab", () => {).toBeInTheDocument();});+ it("shows the active conversation workspace path in files view", () => {+ useHasAttachedSourceMock.mockReturnValue({+ hasAttachedSource: false,+ isLoading: false,+ });+ useActiveConversationMock.mockReturnValue({+ data: {+ workspace: { working_dir: "/workspace/project/worktree-123" },+ },+ });++ renderTab();++ expect(+ screen.getByTestId("files-tab-workspace-path-value"),+ ).toHaveTextContent("/workspace/project/worktree-123");+ });+it("lets users toggle diff view off even when a source is attached", async () => {useHasAttachedSourceMock.mockReturnValue({hasAttachedSource: true,diff --git a/src/components/features/files-tab/workspace-path.tsx b/src/components/features/files-tab/workspace-path.tsxnew file mode 100644index 0000000000..6f6508e60d--- /dev/null+++ b/src/components/features/files-tab/workspace-path.tsx@@ -0,0 +1,53 @@+import React from "react";+import { useTranslation } from "react-i18next";++import { CopyToClipboardButton } from "#/components/shared/buttons/copy-to-clipboard-button";+import { I18nKey } from "#/i18n/declaration";++interface WorkspacePathProps {+ path?: string | null;+}++export function WorkspacePath({ path }: WorkspacePathProps) {+ const { t } = useTranslation("openhands");+ const [isCopied, setIsCopied] = React.useState(false);+ const workspacePath = path?.trim();++ React.useEffect(() => {+ if (!isCopied) return undefined;++ const timeout = window.setTimeout(() => setIsCopied(false), 2000);+ return () => window.clearTimeout(timeout);+ }, [isCopied]);++ if (!workspacePath) return null;++ const handleCopy = async () => {+ await navigator.clipboard.writeText(workspacePath);+ setIsCopied(true);+ };++ return (+ <div+ className="flex min-w-0 items-center gap-2 border-b border-[var(--oh-border)] px-3 py-1.5 text-xs"+ data-testid="files-tab-workspace-path"+ >+ <span className="shrink-0 text-[var(--oh-muted)]">+ {t(I18nKey.WORKSPACE$TITLE)}:+ </span>+ <span+ className="min-w-0 flex-1 truncate font-mono"+ data-testid="files-tab-workspace-path-value"+ title={workspacePath}+ >+ {workspacePath}+ </span>+ <CopyToClipboardButton+ isHidden={false}+ isDisabled={isCopied}+ onClick={handleCopy}+ mode={isCopied ? "copied" : "copy"}+ />+ </div>+ );+}diff --git a/src/routes/files-tab.tsx b/src/routes/files-tab.tsxindex 3dcb428fe8..845ee7b4e7 100644--- a/src/routes/files-tab.tsx+++ b/src/routes/files-tab.tsx@@ -22,9 +22,11 @@ import { FileQuickRow } from "#/components/features/files-tab/file-quick-row";import { FileTreeView } from "#/components/features/files-tab/file-tree-view";import { FileContentViewer } from "#/components/features/files-tab/file-content-viewer";import { SegmentedToggle } from "#/components/features/files-tab/segmented-toggle";+import { WorkspacePath } from "#/components/features/files-tab/workspace-path";import type { ViewMode } from "#/components/features/files-tab/view-mode";import RefreshIcon from "#/icons/u-refresh.svg?react";import LinkExternalIcon from "#/icons/link-external.svg?react";+import { useActiveConversation } from "#/hooks/query/use-active-conversation";import { useUnifiedGitCommits } from "#/hooks/query/use-unified-git-commits";import GitChanges from "./changes-tab";import GitCommits from "./commits-tab";@@ -35,6 +37,9 @@ function FilesTab() {// Keep the list / content / diff caches fresh as the agent writes files.useAutoRefreshFilesOnEdit();+ const { data: activeConversation } = useActiveConversation();+ const workspacePath = activeConversation?.workspace?.working_dir;+const { hasAttachedSource, isLoading: isAttachedSourceLoading } =useHasAttachedSource();// A workspace with zero commits has no diff base to compare against, so@@ -237,6 +242,7 @@ function FilesTab() {)}{activeView === "off" && (<div className="flex flex-1 flex-col min-h-0">+ <WorkspacePath path={workspacePath} />{filesQuery.isLoading ? (<div className="flex flex-1 items-center justify-center text-sm text-[var(--oh-muted)]">{t(I18nKey.FILES$LOADING_FILES)}