TypeScript
fix(web): open workspace root links in the file explorer (#12449)
Alex Southwell55ec55b1fb2e
diff --git a/apps/web/src/components/files/FilePreviewPanel.test.ts b/apps/web/src/components/files/FilePreviewPanel.test.tsindex 5ef590847c4..ebebf510994 100644--- a/apps/web/src/components/files/FilePreviewPanel.test.ts+++ b/apps/web/src/components/files/FilePreviewPanel.test.ts@@ -7,6 +7,7 @@ import {} from "./fileCommentAnnotations";import {isMarkdownPreviewFile,+ resolveFilePreviewPath,setMarkdownTaskChecked,shouldShowFileExplorer,} from "./filePreviewMode";@@ -120,3 +121,24 @@ describe("setMarkdownTaskChecked", () => {expect(setMarkdownTaskChecked(markdown, 200, true)).toBe(markdown);});});++describe("resolveFilePreviewPath", () => {+ it.each([+ ["/repo/project", null],+ ["/repo/project/", null],+ [".", null],+ [null, null],+ ["/repo/project/src", "/repo/project/src"],+ ["/repo/project/src/main.ts", "/repo/project/src/main.ts"],+ ["src/main.ts", "src/main.ts"],+ ["/repo/project-other", "/repo/project-other"],+ ])("opens %s in the appropriate workspace surface", (path, expected) => {+ const relativePath = resolveFilePreviewPath(path, "/repo/project");+ expect(relativePath).toBe(expected);+ if (expected === null) {+ expect(+ shouldShowFileExplorer({ relativePath, explorerOpen: false, attachmentOpen: false }),+ ).toBe(true);+ }+ });+});diff --git a/apps/web/src/components/files/FilePreviewPanel.tsx b/apps/web/src/components/files/FilePreviewPanel.tsxindex 9d389ec31bb..e2d3a2b3c8c 100644--- a/apps/web/src/components/files/FilePreviewPanel.tsx+++ b/apps/web/src/components/files/FilePreviewPanel.tsx@@ -81,6 +81,7 @@ import { DiffCommentAnnotation } from "../diffs/DiffCommentAnnotation";import { projectFileCacheKey, projectFileEditorCacheKey } from "./fileContentRevision";import {isMarkdownPreviewFile,+ resolveFilePreviewPath,setMarkdownTaskChecked,shouldShowFileExplorer,} from "./filePreviewMode";@@ -907,7 +908,7 @@ export default function FilePreviewPanel({environmentId,cwd,projectName,- relativePath,+ relativePath: requestedPath,attachment,threadRef,composerDraftTarget,@@ -920,6 +921,8 @@ export default function FilePreviewPanel({selectedFilePending,workspaceMutationId,}: FilePreviewPanelProps) {+ const relativePath =+ attachment === undefined ? resolveFilePreviewPath(requestedPath, cwd) : requestedPath;const { resolvedTheme } = useTheme();const wordWrap = useClientSettings((settings) => settings.wordWrap);const primaryEnvironmentId = usePrimaryEnvironmentId();@@ -945,7 +948,12 @@ export default function FilePreviewPanel({// shown. The read still runs: a folder named `assets.png` is only knowable as a// folder from the read failure, and the server stats before reading, so a folder// costs an open and a stat and returns no body.- const file = useProjectFileQuery(environmentId, cwd, relativePath, attachment === undefined);+ const file = useProjectFileQuery(+ environmentId,+ cwd,+ relativePath,+ attachment === undefined && relativePath !== null,+ );// A chat link cannot tell a folder from a file, so a folder arrives here as// a file surface and the read fails. Keep the breadcrumbs, drop the preview// pane, and let the tree fill the surface with the folder revealed. Mutationdiff --git a/apps/web/src/components/files/filePreviewMode.ts b/apps/web/src/components/files/filePreviewMode.tsindex 9770d36fa2c..12a3eb95a3e 100644--- a/apps/web/src/components/files/filePreviewMode.ts+++ b/apps/web/src/components/files/filePreviewMode.ts@@ -1,5 +1,12 @@+import { workspaceRelativeFilePath } from "@t3tools/client-runtime/markdown-links";import { isAbsolutePath } from "~/terminal-links";+/** Resolve workspace links before choosing between the explorer and a file preview. */+export function resolveFilePreviewPath(path: string | null, cwd: string): string | null {+ if (path === null) return null;+ return path === "." || workspaceRelativeFilePath(path, cwd) === "." ? null : path;+}+export const isMarkdownPreviewFile = (path: string): boolean => /\.(?:md|mdx)$/i.test(path);export function shouldShowFileExplorer(input: {diff --git a/apps/web/src/markdown-links.test.ts b/apps/web/src/markdown-links.test.tsindex 5421c0d8781..fe352ec1a44 100644--- a/apps/web/src/markdown-links.test.ts+++ b/apps/web/src/markdown-links.test.ts@@ -522,3 +522,11 @@ describe("directory paths with a trailing separator", () => {expect(meta?.basename).not.toBe("");});});++it("routes the project-root code link to the workspace explorer", () => {+ const cwd = "/Users/saphid/.t3/worktrees/ov2-standalone-20260918";+ expect(resolveInlineCodeFileLinkMeta(cwd, cwd)).toMatchObject({+ workspaceRelativePath: ".",+ filePath: cwd,+ });+});diff --git a/apps/web/src/rightPanelStore.test.ts b/apps/web/src/rightPanelStore.test.tsindex 151c1699cbf..c73a2a009c1 100644--- a/apps/web/src/rightPanelStore.test.ts+++ b/apps/web/src/rightPanelStore.test.ts@@ -447,6 +447,24 @@ describe("rightPanelStore", () => {});});+ it("opens workspace-root links as the singleton files explorer", () => {+ const store = useRightPanelStore.getState();+ store.openFile(refA, "README.md");+ store.openFile(refA, ".");+ store.openFile(refA, ".");+ const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);+ expect(state.activeSurfaceId).toBe("files");+ expect(state.surfaces.map((surface) => surface.id)).toEqual(["file:README.md", "files"]);+ store.closeSurface(refA, "files");+ expect(+ selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).activeSurfaceId,+ ).toBe("file:README.md");+ store.openFile(refA, ".");+ expect(+ selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).activeSurfaceId,+ ).toBe("files");+ });+it("replaces the standalone explorer with peer file surfaces", () => {useRightPanelStore.getState().open(refA, "files");useRightPanelStore.getState().openFile(refA, "src/index.ts");diff --git a/apps/web/src/rightPanelStore.ts b/apps/web/src/rightPanelStore.tsindex f020f14466b..75f6d2ffe2b 100644--- a/apps/web/src/rightPanelStore.ts+++ b/apps/web/src/rightPanelStore.ts@@ -578,6 +578,9 @@ export const useRightPanelStore = create<RightPanelStoreState>()(openFile: (ref, requestedPath, line) =>set((state) =>userAction(state, scopedThreadKey(ref), (current) => {+ if (requestedPath === ".") {+ return upsertSurface(current, singletonSurface("files"));+ }// Workspace entry paths use '/', including on Windows.const relativePath = /^[A-Za-z]:\/+$/.test(requestedPath)? requestedPathdiff --git a/apps/web/src/workspaceBasenameLookup.test.ts b/apps/web/src/workspaceBasenameLookup.test.tsindex b4a1376e79c..afe159dd402 100644--- a/apps/web/src/workspaceBasenameLookup.test.ts+++ b/apps/web/src/workspaceBasenameLookup.test.ts@@ -15,6 +15,8 @@ describe("needsWorkspaceBasenameLookup", () => {it("leaves anything with a directory alone", () => {expect(needsWorkspaceBasenameLookup("apps/web/src/components/ChatView.tsx")).toBe(false);expect(needsWorkspaceBasenameLookup("apps\\web\\ChatView.tsx")).toBe(false);+ expect(needsWorkspaceBasenameLookup(".")).toBe(false);+ expect(needsWorkspaceBasenameLookup("..")).toBe(false);expect(needsWorkspaceBasenameLookup(" ")).toBe(false);});});diff --git a/apps/web/src/workspaceBasenameLookup.ts b/apps/web/src/workspaceBasenameLookup.tsindex b99d3ba4ded..0efdfcf3a44 100644--- a/apps/web/src/workspaceBasenameLookup.ts+++ b/apps/web/src/workspaceBasenameLookup.ts@@ -25,7 +25,13 @@ function basenameOfPath(path: string): string {export function needsWorkspaceBasenameLookup(relativePath: string): boolean {const trimmed = relativePath.trim();- return trimmed.length > 0 && !trimmed.includes("/") && !trimmed.includes("\\");+ return (+ trimmed !== "." &&+ trimmed !== ".." &&+ trimmed.length > 0 &&+ !trimmed.includes("/") &&+ !trimmed.includes("\\")+ );}export function pickWorkspaceBasenameMatch(diff --git a/packages/client-runtime/src/markdownLinks.test.ts b/packages/client-runtime/src/markdownLinks.test.tsindex 0a12f14dc6b..deea6960478 100644--- a/packages/client-runtime/src/markdownLinks.test.ts+++ b/packages/client-runtime/src/markdownLinks.test.ts@@ -155,6 +155,11 @@ describe("fileBasename", () => {describe("workspaceRelativeFilePath", () => {it.each([+ ["/repo/project", "/repo/project", "."],+ ["/repo/project/", "/repo/project/", "."],+ ["/", "/", "."],+ ["C:/USERS/mike/project", "c:/users/MIKE/project", "."],+ ["C:/", "c:/", "."],["/repo/project/src/main.ts", "/repo/project", "src/main.ts"],["/repo/project/src/main.ts", "/repo/project/", "src/main.ts"],["C:\\Users\\mike\\t3code\\apps\\web\\a.ts", "C:/Users/mike/t3code", "apps/web/a.ts"],diff --git a/packages/client-runtime/src/markdownLinks.ts b/packages/client-runtime/src/markdownLinks.tsindex a7fc6881c69..0a32f506108 100644--- a/packages/client-runtime/src/markdownLinks.ts+++ b/packages/client-runtime/src/markdownLinks.ts@@ -340,6 +340,7 @@ export function workspaceRelativeFilePath(const caseInsensitive = isWindowsAbsolutePath(stripSlashPrefixedWindowsDrive(workspaceRoot));const pathForCompare = caseInsensitive ? normalizedPath.toLowerCase() : normalizedPath;const rootForCompare = caseInsensitive ? normalizedRoot.toLowerCase() : normalizedRoot;+ if (pathForCompare.replace(/\/+$/, "") === rootForCompare) return ".";if (!pathForCompare.startsWith(`${rootForCompare}/`)) return null;return normalizedPath.slice(normalizedRoot.length + 1);}