Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion apps/web/src/components/DiffFilePathCopyButton.browser.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { AnchoredToastProvider } from "./ui/toast";
it("explains a denied clipboard write and lets the same path be copied again", async () => {
await using screen = await render(
<AnchoredToastProvider timeout={0}>
<DiffFilePathCopyButton filePath="apps/web/src/index.ts" />
<DiffFilePathCopyButton filePath="apps/web/src/index.ts" workspaceRoot="/repo" />
</AnchoredToastProvider>,
);
using write = vi
Expand All @@ -28,3 +28,16 @@ it("explains a denied clipboard write and lets the same path be copied again", a
expect(write).toHaveBeenCalledTimes(2);
expect(write).toHaveBeenLastCalledWith("apps/web/src/index.ts");
});

it("copies a Windows workspace's file path with Windows separators", async () => {
await using screen = await render(
<AnchoredToastProvider timeout={0}>
<DiffFilePathCopyButton filePath="apps/web/src/index.ts" workspaceRoot="C:\repo" />
</AnchoredToastProvider>,
);
using write = vi.spyOn(navigator.clipboard, "writeText").mockResolvedValue(undefined);

await screen.getByRole("button", { name: "Copy file path" }).click();

await vi.waitFor(() => expect(write).toHaveBeenCalledWith("apps\\web\\src\\index.ts"));
});
13 changes: 11 additions & 2 deletions apps/web/src/components/DiffFilePathCopyButton.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { formatFilePathForClipboard } from "@t3tools/client-runtime/media-actions";
import { CheckIcon, CopyIcon } from "lucide-react";
import { useRef } from "react";
import { useCopyToClipboard } from "../hooks/useCopyToClipboard";
import { ANCHORED_COPY_TOAST_TIMEOUT_MS, showAnchoredCopyErrorToast } from "./ui/anchoredCopyToast";
import { Button } from "./ui/button";
import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";

export function DiffFilePathCopyButton({ filePath }: { filePath: string }) {
export function DiffFilePathCopyButton({
filePath,
workspaceRoot,
}: {
filePath: string;
workspaceRoot: string | undefined;
}) {
const ref = useRef<HTMLButtonElement>(null);
const { copyToClipboard, isCopied } = useCopyToClipboard<void>({
onError: (error) => showAnchoredCopyErrorToast(ref, error),
Expand All @@ -21,7 +28,9 @@ export function DiffFilePathCopyButton({ filePath }: { filePath: string }) {
size="icon-micro"
variant="ghost-muted"
aria-label="Copy file path"
onClick={() => copyToClipboard(filePath, undefined)}
onClick={() =>
copyToClipboard(formatFilePathForClipboard(filePath, workspaceRoot), undefined)
}
/>
}
>
Expand Down
5 changes: 4 additions & 1 deletion apps/web/src/components/DiffPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,14 @@ function DiffFileCollapseToggle({

function DiffFileHeaderSuffix({
filePath,
workspaceRoot,
hasStat,
error,
truncated,
onRetry,
}: {
filePath: string;
workspaceRoot: string | undefined;
hasStat: boolean;
error: boolean;
truncated: boolean;
Expand All @@ -191,7 +193,7 @@ function DiffFileHeaderSuffix({
return (
<>
<span data-diff-copy-path className="inline-flex">
<DiffFilePathCopyButton filePath={filePath} />
<DiffFilePathCopyButton filePath={filePath} workspaceRoot={workspaceRoot} />
</span>
{hasStat ? (
<DiffFileStatus error={error} truncated={truncated} retry={() => onRetry(filePath)} />
Expand Down Expand Up @@ -1436,6 +1438,7 @@ export default function DiffPanel({
return (
<DiffFileHeaderSuffix
filePath={path}
workspaceRoot={activeCwd}
hasStat={fileStats.has(path)}
error={state?.error ?? false}
truncated={state?.truncated ?? false}
Expand Down
Loading