Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(web): Create new `TableCellWithCopyButton` for `ApiKeyList` (#13057)

Ben Bachemd3d16272ed97
diff --git a/web/src/components/ui/CodeJsonViewer.tsx b/web/src/components/ui/CodeJsonViewer.tsx
index 303430d3fd..5f17319b2f 100644
--- a/web/src/components/ui/CodeJsonViewer.tsx
+++ b/web/src/components/ui/CodeJsonViewer.tsx
@@ -1,4 +1,4 @@
-import { useMemo, useRef, useState } from "react";
+import { useMemo, useState } from "react";
import { Button } from "@/src/components/ui/button";
import {
Check,
@@ -24,6 +24,7 @@ import {
usePromptReferenceProjectId,
} from "@/src/components/ui/PromptReferences";
import { copyTextToClipboard } from "@/src/utils/clipboard";
+import { useCopyToClipboard } from "@/src/hooks/useCopyToClipboard";
export const IO_TABLE_CHAR_LIMIT = 10000;
@@ -221,32 +222,32 @@ export function CodeView(props: {
}) {
const { copiedToClipboardMessage } = props;
- const [isCopied, setIsCopied] = useState(false);
const [isCollapsed, setCollapsed] = useState(props.defaultCollapsed);
- const copySuccessStateDuration = copiedToClipboardMessage ? 3_000 : 1_000;
- const copySuccessTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(
- null,
- );
+ const { copy, isCopied } = useCopyToClipboard({
+ successDuration: copiedToClipboardMessage ? 3_000 : 1_000,
+ });
- const handleCopy = (event: React.MouseEvent<HTMLButtonElement>) => {
+ const handleCopy = async (event: React.MouseEvent<HTMLButtonElement>) => {
event.preventDefault();
- setIsCopied(true);
+ const button = event.currentTarget;
const content =
props.originalContent ??
(typeof props.content === "string"
? props.content
: (props.content?.join("\n") ?? ""));
- void copyTextToClipboard(content);
- if (copySuccessTimeoutRef.current)
- clearTimeout(copySuccessTimeoutRef.current);
- copySuccessTimeoutRef.current = setTimeout(
- () => setIsCopied(false),
- copySuccessStateDuration,
- );
- // Keep focus on the copy button to prevent focus shifting
- event.currentTarget.focus();
+ try {
+ await copy(content);
+ } catch {
+ // Clipboard writes can be rejected when the browser denies permission.
+ }
+
+ if (button) {
+ // Keep focus on the copy button to prevent focus shifting
+ // Note: the original button might no longer be in the DOM if React re-rendered the component after the state update.
+ button.focus();
+ }
};
const handleShowAll = () => setCollapsed(!isCollapsed);
diff --git a/web/src/components/ui/table.tsx b/web/src/components/ui/table.tsx
index 729b9179dc..c79fea2d4e 100644
--- a/web/src/components/ui/table.tsx
+++ b/web/src/components/ui/table.tsx
@@ -1,6 +1,9 @@
import * as React from "react";
+import { Button } from "@/src/components/ui/button";
+import { useCopyToClipboard } from "@/src/hooks/useCopyToClipboard";
import { cn } from "@/src/utils/tailwind";
+import { Check, Copy } from "lucide-react";
type TableDensity = "compact" | "comfortable";
@@ -101,6 +104,59 @@ const TableCell = React.forwardRef<
));
TableCell.displayName = "TableCell";
+type TableCellWithCopyButtonProps =
+ React.TdHTMLAttributes<HTMLTableCellElement> & {
+ text: string;
+ density?: TableDensity;
+ copyButtonLabel?: string;
+ };
+
+const TableCellWithCopyButton = React.forwardRef<
+ HTMLTableCellElement,
+ TableCellWithCopyButtonProps
+>(({ text, copyButtonLabel, className, ...props }, ref) => {
+ const { copy, isCopied } = useCopyToClipboard();
+
+ return (
+ <TableCell
+ ref={ref}
+ className={cn("relative min-w-0 pr-10", className)}
+ title={text}
+ {...props}
+ >
+ {text}
+ <Button
+ variant="ghost"
+ size="icon-xs"
+ className="absolute top-1/2 right-2 -translate-y-1/2"
+ title={copyButtonLabel ?? "Copy to clipboard"}
+ aria-label={copyButtonLabel ?? "Copy to clipboard"}
+ onClick={async (event) => {
+ event.preventDefault();
+ const button = event.currentTarget;
+ try {
+ await copy(text);
+ } catch {
+ // Clipboard writes can be rejected when the browser denies permission.
+ }
+
+ if (button) {
+ // The original button might no longer be in the DOM if React re-rendered the component after the state update.
+ button.focus();
+ }
+ }}
+ >
+ {isCopied ? (
+ <Check className="h-3 w-3" />
+ ) : (
+ <Copy className="h-3 w-3" />
+ )}
+ </Button>
+ </TableCell>
+ );
+});
+TableCellWithCopyButton.displayName = "TableCellWithCopyButton";
+
const TableCaption = React.forwardRef<
HTMLTableCaptionElement,
React.HTMLAttributes<HTMLTableCaptionElement>
@@ -121,5 +177,6 @@ export {
TableHead,
TableRow,
TableCell,
+ TableCellWithCopyButton,
TableCaption,
};
diff --git a/web/src/features/public-api/components/ApiKeyList.tsx b/web/src/features/public-api/components/ApiKeyList.tsx
index 2dcca5c1ae..381eb80663 100644
--- a/web/src/features/public-api/components/ApiKeyList.tsx
+++ b/web/src/features/public-api/components/ApiKeyList.tsx
@@ -15,6 +15,7 @@ import {
Table,
TableBody,
TableCell,
+ TableCellWithCopyButton,
TableHead,
TableHeader,
TableRow,
@@ -144,12 +145,11 @@ export function ApiKeyList(props: { entityId: string; scope: ApiKeyScope }) {
scope={scope}
/>
</TableCell>
- <TableCell density="comfortable" className="font-mono">
- <CodeView
- className="inline-block text-xs"
- content={apiKey.publicKey}
- />
- </TableCell>
+ <TableCellWithCopyButton
+ density="comfortable"
+ text={apiKey.publicKey}
+ className="truncate font-mono"
+ />
<TableCell density="comfortable" className="font-mono">
{apiKey.displaySecretKey}
</TableCell>
diff --git a/web/src/hooks/useCopyToClipboard.clienttest.ts b/web/src/hooks/useCopyToClipboard.clienttest.ts
new file mode 100644
index 0000000000..2b0cde40d1
--- /dev/null
+++ b/web/src/hooks/useCopyToClipboard.clienttest.ts
@@ -0,0 +1,88 @@
+import { act, renderHook } from "@testing-library/react";
+import { useCopyToClipboard } from "@/src/hooks/useCopyToClipboard";
+
+describe("useCopyToClipboard", () => {
+ const originalClipboard = navigator.clipboard;
+
+ beforeEach(() => {
+ jest.useFakeTimers();
+ Object.defineProperty(navigator, "clipboard", {
+ configurable: true,
+ value: {
+ writeText: jest.fn().mockResolvedValue(undefined),
+ },
+ });
+ });
+
+ afterEach(() => {
+ jest.runOnlyPendingTimers();
+ jest.useRealTimers();
+ Object.defineProperty(navigator, "clipboard", {
+ configurable: true,
+ value: originalClipboard,
+ });
+ });
+
+ it("copies the provided text and exposes a temporary success state", async () => {
+ const { result } = renderHook(() =>
+ useCopyToClipboard({ successDuration: 500 }),
+ );
+
+ expect(result.current.isCopied).toBe(false);
+
+ await act(async () => {
+ await result.current.copy("pk-lf-test");
+ });
+
+ expect(result.current.isCopied).toBe(true);
+
+ act(() => {
+ jest.advanceTimersByTime(499);
+ });
+
+ expect(result.current.isCopied).toBe(true);
+
+ act(() => {
+ jest.advanceTimersByTime(1);
+ });
+
+ expect(result.current.isCopied).toBe(false);
+
+ await act(async () => {
+ await result.current.copy("pk-lf-test");
+ });
+
+ expect(result.current.isCopied).toBe(true);
+ });
+
+ it("keeps the active copied state across rerenders and preserves the original timeout", async () => {
+ const { result, rerender } = renderHook(
+ ({ successDuration }) => useCopyToClipboard({ successDuration }),
+ {
+ initialProps: { successDuration: 500 },
+ },
+ );
+
+ await act(async () => {
+ await result.current.copy("first");
+ });
+
+ expect(result.current.isCopied).toBe(true);
+
+ rerender({ successDuration: 2_000 });
+
+ expect(result.current.isCopied).toBe(true);
+
+ act(() => {
+ jest.advanceTimersByTime(499);
+ });
+
+ expect(result.current.isCopied).toBe(true);
+
+ act(() => {
+ jest.advanceTimersByTime(1);
+ });
+
+ expect(result.current.isCopied).toBe(false);
+ });
+});
diff --git a/web/src/hooks/useCopyToClipboard.ts b/web/src/hooks/useCopyToClipboard.ts
new file mode 100644
index 0000000000..ab359544bf
--- /dev/null
+++ b/web/src/hooks/useCopyToClipboard.ts
@@ -0,0 +1,39 @@
+import { useEffect, useRef, useState } from "react";
+import { copyTextToClipboard } from "@/src/utils/clipboard";
+
+/**
+ * Copies text to the clipboard and exposes a temporary success state for UI feedback.
+ */
+export function useCopyToClipboard({
+ successDuration = 1_000,
+}: {
+ successDuration?: number;
+} = {}) {
+ const [isCopied, setIsCopied] = useState(false);
+ const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
+
+ useEffect(() => {
+ return () => {
+ if (timeoutRef.current) {
+ clearTimeout(timeoutRef.current);
+ }
+ };
+ }, []);
+
+ const copy = async (text: string) => {
+ setIsCopied(true);
+ try {
+ await copyTextToClipboard(text);
+ } finally {
+ if (timeoutRef.current) {
+ clearTimeout(timeoutRef.current);
+ }
+
+ timeoutRef.current = setTimeout(() => {
+ setIsCopied(false);
+ }, successDuration);
+ }
+ };
+
+ return { copy, isCopied };
+}