fix(web): open workspace root links in the file explorer (#12449)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
916ec94f93
commit
55ec55b1fb
10 files changed
+83
-3
No files matched your search
@@ -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);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -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. Mutation
|
||||
|
||||
@@ -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: {
|
||||
|
||||
@@ -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,
|
||||
});
|
||||
});
|
||||
@@ -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");
|
||||
|
||||
@@ -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)
|
||||
? requestedPath
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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"],
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
Reference in new issue
Block a user