feat(web): add copy path button to diff headers (#2403)
Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: maria-rcks <[email protected]>
This commit is contained in:
6 files changed
+92
-36
No files matched your search
@@ -0,0 +1,41 @@
|
||||
import { CheckIcon, CopyIcon } from "lucide-react";
|
||||
import { useRef } from "react";
|
||||
import { useCopyToClipboard } from "../hooks/useCopyToClipboard";
|
||||
import {
|
||||
ANCHORED_COPY_TOAST_TIMEOUT_MS,
|
||||
showAnchoredCopyErrorToast,
|
||||
showAnchoredCopySuccessToast,
|
||||
} from "./ui/anchoredCopyToast";
|
||||
import { Button } from "./ui/button";
|
||||
import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
|
||||
|
||||
export function DiffFilePathCopyButton({ filePath }: { filePath: string }) {
|
||||
const ref = useRef<HTMLButtonElement>(null);
|
||||
const { copyToClipboard, isCopied } = useCopyToClipboard<void>({
|
||||
onCopy: () => showAnchoredCopySuccessToast(ref),
|
||||
onError: (error) => showAnchoredCopyErrorToast(ref, error),
|
||||
timeout: ANCHORED_COPY_TOAST_TIMEOUT_MS,
|
||||
});
|
||||
|
||||
return (
|
||||
<Tooltip>
|
||||
<TooltipTrigger
|
||||
render={
|
||||
<Button
|
||||
ref={ref}
|
||||
size="icon-micro"
|
||||
variant="ghost"
|
||||
className="text-muted-foreground [:hover,[data-pressed]]:bg-transparent"
|
||||
aria-label="Copy file path"
|
||||
onClick={() => copyToClipboard(filePath, undefined)}
|
||||
/>
|
||||
}
|
||||
>
|
||||
{isCopied ? <CheckIcon className="size-3 text-success" /> : <CopyIcon className="size-3" />}
|
||||
</TooltipTrigger>
|
||||
<TooltipPopup>
|
||||
<p>{isCopied ? "Copied" : "Copy path"}</p>
|
||||
</TooltipPopup>
|
||||
</Tooltip>
|
||||
);
|
||||
}
|
||||
@@ -46,6 +46,7 @@ import { useProject, useThread } from "../state/entities";
|
||||
import { resolveThreadRouteRef } from "../threadRoutes";
|
||||
import { useClientSettings } from "../hooks/useSettings";
|
||||
import { formatShortTimestamp } from "../timestampFormat";
|
||||
import { DiffFilePathCopyButton } from "./DiffFilePathCopyButton";
|
||||
import { DiffPanelLoadingState, DiffPanelShell, type DiffPanelMode } from "./DiffPanelShell";
|
||||
import { DiffStatLabel } from "./chat/DiffStatLabel";
|
||||
import { AnnotatableCodeView, type AnnotatableCodeViewHandle } from "./diffs/AnnotatableCodeView";
|
||||
@@ -920,6 +921,9 @@ export default function DiffPanel({
|
||||
sectionId={reviewSectionId}
|
||||
sectionTitle={reviewSectionTitle}
|
||||
composerDraftTarget={composerDraftTarget}
|
||||
renderHeaderFilenameSuffix={(fileDiff) => (
|
||||
<DiffFilePathCopyButton filePath={resolveFileDiffPath(fileDiff)} />
|
||||
)}
|
||||
renderHeaderPrefix={(fileDiff, fileKey, collapsed) => {
|
||||
const filePath = resolveFileDiffPath(fileDiff);
|
||||
return (
|
||||
|
||||
@@ -3,41 +3,13 @@ import { CopyIcon, CheckIcon } from "lucide-react";
|
||||
import { Button } from "../ui/button";
|
||||
import { useCopyToClipboard } from "~/hooks/useCopyToClipboard";
|
||||
import { cn } from "~/lib/utils";
|
||||
import { anchoredToastManager } from "../ui/toast";
|
||||
import {
|
||||
ANCHORED_COPY_TOAST_TIMEOUT_MS,
|
||||
showAnchoredCopyErrorToast,
|
||||
showAnchoredCopySuccessToast,
|
||||
} from "../ui/anchoredCopyToast";
|
||||
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
|
||||
|
||||
const ANCHORED_TOAST_TIMEOUT_MS = 1000;
|
||||
const onCopy = (ref: React.RefObject<HTMLButtonElement | null>) => {
|
||||
if (ref.current) {
|
||||
anchoredToastManager.add({
|
||||
data: {
|
||||
tooltipStyle: true,
|
||||
},
|
||||
positionerProps: {
|
||||
anchor: ref.current,
|
||||
},
|
||||
timeout: ANCHORED_TOAST_TIMEOUT_MS,
|
||||
title: "Copied!",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const onCopyError = (ref: React.RefObject<HTMLButtonElement | null>, error: Error) => {
|
||||
if (ref.current) {
|
||||
anchoredToastManager.add({
|
||||
data: {
|
||||
tooltipStyle: true,
|
||||
},
|
||||
positionerProps: {
|
||||
anchor: ref.current,
|
||||
},
|
||||
timeout: ANCHORED_TOAST_TIMEOUT_MS,
|
||||
title: "Failed to copy",
|
||||
description: error.message,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const MessageCopyButton = memo(function MessageCopyButton({
|
||||
text,
|
||||
size = "xs",
|
||||
@@ -51,9 +23,9 @@ export const MessageCopyButton = memo(function MessageCopyButton({
|
||||
}) {
|
||||
const ref = useRef<HTMLButtonElement>(null);
|
||||
const { copyToClipboard, isCopied } = useCopyToClipboard<void>({
|
||||
onCopy: () => onCopy(ref),
|
||||
onError: (error: Error) => onCopyError(ref, error),
|
||||
timeout: ANCHORED_TOAST_TIMEOUT_MS,
|
||||
onCopy: () => showAnchoredCopySuccessToast(ref),
|
||||
onError: (error: Error) => showAnchoredCopyErrorToast(ref, error),
|
||||
timeout: ANCHORED_COPY_TOAST_TIMEOUT_MS,
|
||||
});
|
||||
|
||||
return (
|
||||
|
||||
@@ -46,6 +46,7 @@ describe("AnnotatableCodeView", () => {
|
||||
composerDraftTarget={"draft-test" as never}
|
||||
options={{}}
|
||||
renderHeaderPrefix={() => null}
|
||||
renderHeaderFilenameSuffix={() => null}
|
||||
/>,
|
||||
);
|
||||
|
||||
|
||||
@@ -86,6 +86,7 @@ interface AnnotatableCodeViewProps {
|
||||
options: StyledDiffCodeViewOptions<DiffCommentAnnotationGroup>;
|
||||
viewerRef?: Ref<AnnotatableCodeViewHandle>;
|
||||
className?: string;
|
||||
renderHeaderFilenameSuffix: (fileDiff: FileDiffMetadata) => ReactNode;
|
||||
renderHeaderPrefix: (
|
||||
fileDiff: FileDiffMetadata,
|
||||
fileKey: string,
|
||||
@@ -106,6 +107,7 @@ export function AnnotatableCodeView({
|
||||
options,
|
||||
viewerRef,
|
||||
className,
|
||||
renderHeaderFilenameSuffix,
|
||||
renderHeaderPrefix,
|
||||
}: AnnotatableCodeViewProps) {
|
||||
const addReviewComment = useComposerDraftStore((store) => store.addReviewComment);
|
||||
@@ -252,6 +254,9 @@ export function AnnotatableCodeView({
|
||||
enableLineSelection: !hasOpenComment,
|
||||
onGutterUtilityClick: beginComment,
|
||||
}}
|
||||
renderHeaderFilenameSuffix={(item) =>
|
||||
item.type === "diff" ? renderHeaderFilenameSuffix(item.fileDiff) : null
|
||||
}
|
||||
renderHeaderPrefix={(item) =>
|
||||
item.type === "diff"
|
||||
? renderHeaderPrefix(item.fileDiff, item.id, item.collapsed === true)
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
import type { RefObject } from "react";
|
||||
import { anchoredToastManager } from "./toast";
|
||||
|
||||
export const ANCHORED_COPY_TOAST_TIMEOUT_MS = 1000;
|
||||
|
||||
export function showAnchoredCopySuccessToast(ref: RefObject<HTMLButtonElement | null>) {
|
||||
if (!ref.current) return;
|
||||
anchoredToastManager.add({
|
||||
data: {
|
||||
tooltipStyle: true,
|
||||
},
|
||||
positionerProps: {
|
||||
anchor: ref.current,
|
||||
},
|
||||
timeout: ANCHORED_COPY_TOAST_TIMEOUT_MS,
|
||||
title: "Copied!",
|
||||
});
|
||||
}
|
||||
|
||||
export function showAnchoredCopyErrorToast(ref: RefObject<HTMLButtonElement | null>, error: Error) {
|
||||
if (!ref.current) return;
|
||||
anchoredToastManager.add({
|
||||
data: {
|
||||
tooltipStyle: true,
|
||||
},
|
||||
positionerProps: {
|
||||
anchor: ref.current,
|
||||
},
|
||||
timeout: ANCHORED_COPY_TOAST_TIMEOUT_MS,
|
||||
title: "Failed to copy",
|
||||
description: error.message,
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user