TypeScript
refactor(web): file tree drag mentions use React drag handlers (#25913)
Julius Marminge6dc484252321
diff --git a/apps/web/src/components/files/FileBrowserPanel.browser.test.tsx b/apps/web/src/components/files/FileBrowserPanel.browser.test.tsxnew file mode 100644index 00000000000..75e55cc46f4--- /dev/null+++ b/apps/web/src/components/files/FileBrowserPanel.browser.test.tsx@@ -0,0 +1,79 @@+import { EnvironmentId, type ProjectEntry } from "@t3tools/contracts";+import { describe, expect, it, vi } from "vite-plus/test";++import { COMPOSER_MENTION_DRAG_TYPE } from "~/components/chat/composerMentionDrag";+import { render } from "../../../test/render";+import FileBrowserPanel from "./FileBrowserPanel";++const entries: readonly ProjectEntry[] = [+ { path: "src", kind: "directory" },+ { path: "README.md", kind: "file" },+];+const loadingDirectories: ReadonlySet<string> = new Set();+const directory = {+ entries,+ load: () => Promise.resolve(),+ refresh: () => {},+ ready: true,+ error: null,+ isPending: false,+ loadingDirectories,+};+const pathSearch = {+ entries: [],+ isPending: false,+ error: null,+ truncated: false,+ refresh: () => {},+};++vi.mock("./useDirectoryEntries", () => ({ useDirectoryEntries: () => directory }));+vi.mock("~/state/queries", () => ({ useProjectPathSearch: () => pathSearch }));+vi.mock("~/fileContextMenu", () => ({+ useFileContextMenu: () => ({ buildItems: () => [], activate: () => Promise.resolve() }),+}));+vi.mock("~/hooks/useWorkspaceMutationRefresh", () => ({ useWorkspaceMutationRefresh: () => {} }));++function findTreeRow(path: string): HTMLElement | null {+ for (const host of document.querySelectorAll("*")) {+ const row = host.shadowRoot?.querySelector<HTMLElement>(`[data-item-path="${path}"]`);+ if (row) return row;+ }+ return null;+}++describe("FileBrowserPanel", () => {+ it("dragging a file row carries a composer mention without opening it, and dragend releases it", async () => {+ const onOpenFile = vi.fn();+ await using _screen = await render(+ <FileBrowserPanel+ environmentId={EnvironmentId.make("env-files")}+ cwd="/repo"+ projectName="repo"+ selectedPath={null}+ selectedPathRevealId={0}+ onOpenFile={onOpenFile}+ workspaceMutationId={null}+ />,+ );+ await vi.waitFor(() => expect(findTreeRow("README.md")).not.toBeNull());+ const row = findTreeRow("README.md")!;++ const dataTransfer = new DataTransfer();+ row.dispatchEvent(+ new DragEvent("dragstart", { bubbles: true, cancelable: true, composed: true, dataTransfer }),+ );+ // The tree selects the row the drag picked up; mid-drag that must not open it.+ row.click();+ await vi.waitFor(() => expect(row.getAttribute("aria-selected")).toBe("true"));++ expect(dataTransfer.getData(COMPOSER_MENTION_DRAG_TYPE)).toContain("README.md");+ expect(onOpenFile).not.toHaveBeenCalled();++ row.dispatchEvent(new DragEvent("dragend", { bubbles: true, composed: true, dataTransfer }));+ await vi.waitFor(() => expect(row.getAttribute("aria-selected")).not.toBe("true"));++ row.click();+ await vi.waitFor(() => expect(onOpenFile).toHaveBeenCalledTimes(1));+ });+});diff --git a/apps/web/src/components/files/FileBrowserPanel.tsx b/apps/web/src/components/files/FileBrowserPanel.tsxindex 3d9913d3e73..faed4a113b8 100644--- a/apps/web/src/components/files/FileBrowserPanel.tsx+++ b/apps/web/src/components/files/FileBrowserPanel.tsx@@ -463,33 +463,17 @@ export default function FileBrowserPanel({});}, [entryKinds, model, selectedPath, selectedPathRevealId]);- // Capture dragstart before the tree selects the row so selection sees the drag flag.- const panelRef = useRef<HTMLDivElement | null>(null);- useEffect(() => {- const panel = panelRef.current;- if (panel === null) {- return;- }- const handleDragStart = (event: DragEvent) => dragMention.handleDragStart(event);- const handleDragEnd = () =>- dragMention.handleDragEnd((path) => model.getItem(path)?.deselect());- panel.addEventListener("dragstart", handleDragStart, true);- panel.addEventListener("dragend", handleDragEnd);- return () => {- panel.removeEventListener("dragstart", handleDragStart, true);- panel.removeEventListener("dragend", handleDragEnd);- };- }, [dragMention, model]);-return (<div- ref={panelRef}className="flex min-h-0 flex-1 flex-col bg-background"data-file-browser-panel={`${environmentId}:${cwd}`}// Capture runs before the tree's shadow-internal row handler opens the menu.onContextMenuCapture={(event) => {contextMenuPointerRef.current = { x: event.clientX, y: event.clientY, at: event.timeStamp };}}+ // Capture dragstart before the tree selects the row so selection sees the drag flag.+ onDragStartCapture={(event) => dragMention.handleDragStart(event.nativeEvent)}+ onDragEnd={() => dragMention.handleDragEnd((path) => model.getItem(path)?.deselect())}><divclassName="flex h-10 min-h-10 shrink-0 items-center gap-1 border-b border-border/60 bg-background px-2 in-data-[preview-panel-mode=inline]:mb-1 in-data-[preview-panel-mode=inline]:h-9 in-data-[preview-panel-mode=inline]:min-h-9 in-data-[preview-panel-mode=inline]:border-b-transparent"