Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

refactor(ui): extract a shared CopyButton and fix the sidebar copy confirmation (#32945)

yuneng-jiangf2fb6b8e73e0
diff --git a/ui/litellm-dashboard/src/components/SidebarAccountMenu/SidebarAccountMenu.test.tsx b/ui/litellm-dashboard/src/components/SidebarAccountMenu/SidebarAccountMenu.test.tsx
index 716a2ebe6e2..2d122246ce0 100644
--- a/ui/litellm-dashboard/src/components/SidebarAccountMenu/SidebarAccountMenu.test.tsx
+++ b/ui/litellm-dashboard/src/components/SidebarAccountMenu/SidebarAccountMenu.test.tsx
@@ -179,7 +179,7 @@ describe("SidebarAccountMenu", () => {
expect(screen.queryByTitle("Thanks for using LiteLLM!")).not.toBeInTheDocument();
});
- it("should copy the email to the clipboard", async () => {
+ it("wires the email row to the shared copy button", async () => {
const user = userEvent.setup();
const writeText = vi.fn().mockResolvedValue(undefined);
Object.defineProperty(navigator, "clipboard", { value: { writeText }, configurable: true });
@@ -187,9 +187,11 @@ describe("SidebarAccountMenu", () => {
renderWithProviders(<SidebarAccountMenu onLogout={mockOnLogout} />);
await openMenu(user);
- await user.click(screen.getByRole("button", { name: "Copy email" }));
+ const copyButton = screen.getByRole("button", { name: "Copy email" });
+ await user.click(copyButton);
expect(writeText).toHaveBeenCalledWith("[email protected]");
+ await waitFor(() => expect(copyButton.querySelector(".lucide-check")).toBeInTheDocument());
});
it("should call onLogout when logout is clicked", async () => {
diff --git a/ui/litellm-dashboard/src/components/SidebarAccountMenu/SidebarAccountMenu.tsx b/ui/litellm-dashboard/src/components/SidebarAccountMenu/SidebarAccountMenu.tsx
index 46d7271c9df..4842f25f36b 100644
--- a/ui/litellm-dashboard/src/components/SidebarAccountMenu/SidebarAccountMenu.tsx
+++ b/ui/litellm-dashboard/src/components/SidebarAccountMenu/SidebarAccountMenu.tsx
@@ -7,6 +7,7 @@ import { useDisableShowPrompts } from "@/app/(dashboard)/hooks/useDisableShowPro
import { useDisableUsageIndicator } from "@/app/(dashboard)/hooks/useDisableUsageIndicator";
import { emitLocalStorageChange, removeLocalStorageItem, setLocalStorageItem } from "@/utils/localStorageUtils";
import { navAccountDisplayName } from "@/components/Navbar/navDisplayName";
+import CopyButton from "@/components/shared/CopyButton";
import { Avatar, AvatarFallback } from "@/components/ui/avatar";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
@@ -14,8 +15,8 @@ import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover
import { Separator } from "@/components/ui/separator";
import { Switch } from "@/components/ui/switch";
import { cn } from "@/lib/cva.config";
-import { Check, ChevronsUpDown, Copy, Crown, IdCard, LogOut, Mail, ShieldCheck } from "lucide-react";
-import React, { useEffect, useState } from "react";
+import { ChevronsUpDown, Crown, IdCard, LogOut, Mail, ShieldCheck } from "lucide-react";
+import React from "react";
const RELEASE_NOTES_URL = "https://docs.litellm.ai/release_notes";
@@ -52,39 +53,6 @@ function initialsFromIdentity(email: string | null, userId: string | null): stri
return "?";
}
-const CopyButton: React.FC<{ value: string | null; label: string }> = ({ value, label }) => {
- const [copied, setCopied] = useState(false);
-
- useEffect(() => {
- if (!copied) return;
- const timer = setTimeout(() => setCopied(false), 1200);
- return () => clearTimeout(timer);
- }, [copied]);
-
- if (!value) return null;
-
- return (
- <Button
- type="button"
- variant="ghost"
- size="icon-xs"
- onClick={() => {
- try {
- navigator.clipboard?.writeText(value);
- setCopied(true);
- } catch {
- setCopied(false);
- }
- }}
- aria-label={label}
- title={label}
- className="text-muted-foreground hover:text-primary"
- >
- {copied ? <Check className="size-[15px]" /> : <Copy className="size-[15px]" />}
- </Button>
- );
-};
-
const InfoRow: React.FC<{ icon: React.ReactNode; label: string; children: React.ReactNode }> = ({
icon,
label,
@@ -218,7 +186,7 @@ const SidebarAccountMenu: React.FC<SidebarAccountMenuProps> = ({ onLogout, colla
title="Thanks for using LiteLLM!"
aria-hidden
>
- 🌑
+ 🌴
</span>
)}
<span className="flex-1" />
diff --git a/ui/litellm-dashboard/src/components/shared/CopyButton.test.tsx b/ui/litellm-dashboard/src/components/shared/CopyButton.test.tsx
new file mode 100644
index 00000000000..07e115d7a39
--- /dev/null
+++ b/ui/litellm-dashboard/src/components/shared/CopyButton.test.tsx
@@ -0,0 +1,55 @@
+import userEvent from "@testing-library/user-event";
+import { describe, expect, it, vi } from "vitest";
+import { renderWithProviders, screen, waitFor } from "../../../tests/test-utils";
+import CopyButton from "./CopyButton";
+
+describe("CopyButton", () => {
+ it("renders nothing when there is no value", () => {
+ const { container } = renderWithProviders(<CopyButton value={null} label="Copy value" />);
+ expect(container.querySelector("button")).toBeNull();
+ });
+
+ it("shows the confirmation checkmark only after a successful write", async () => {
+ const user = userEvent.setup();
+ const writeText = vi.fn().mockResolvedValue(undefined);
+ Object.defineProperty(navigator, "clipboard", { value: { writeText }, configurable: true });
+
+ renderWithProviders(<CopyButton value="secret-value" label="Copy value" />);
+
+ const button = screen.getByRole("button", { name: "Copy value" });
+ expect(button.querySelector(".lucide-copy")).toBeInTheDocument();
+
+ await user.click(button);
+
+ expect(writeText).toHaveBeenCalledWith("secret-value");
+ await waitFor(() => expect(button.querySelector(".lucide-check")).toBeInTheDocument());
+ });
+
+ it("does not show the checkmark when the clipboard write is rejected", async () => {
+ const user = userEvent.setup();
+ const writeText = vi.fn().mockRejectedValue(new Error("permission denied"));
+ Object.defineProperty(navigator, "clipboard", { value: { writeText }, configurable: true });
+
+ renderWithProviders(<CopyButton value="secret-value" label="Copy value" />);
+
+ const button = screen.getByRole("button", { name: "Copy value" });
+ await user.click(button);
+
+ await waitFor(() => expect(writeText).toHaveBeenCalledWith("secret-value"));
+ expect(button.querySelector(".lucide-check")).not.toBeInTheDocument();
+ expect(button.querySelector(".lucide-copy")).toBeInTheDocument();
+ });
+
+ it("does not show the checkmark when the clipboard API is unavailable", async () => {
+ const user = userEvent.setup();
+ Object.defineProperty(navigator, "clipboard", { value: undefined, configurable: true });
+
+ renderWithProviders(<CopyButton value="secret-value" label="Copy value" />);
+
+ const button = screen.getByRole("button", { name: "Copy value" });
+ await user.click(button);
+
+ expect(button.querySelector(".lucide-check")).not.toBeInTheDocument();
+ expect(button.querySelector(".lucide-copy")).toBeInTheDocument();
+ });
+});
diff --git a/ui/litellm-dashboard/src/components/shared/CopyButton.tsx b/ui/litellm-dashboard/src/components/shared/CopyButton.tsx
new file mode 100644
index 00000000000..052d03df319
--- /dev/null
+++ b/ui/litellm-dashboard/src/components/shared/CopyButton.tsx
@@ -0,0 +1,49 @@
+import { Button } from "@/components/ui/button";
+import { cn } from "@/lib/cva.config";
+import { Check, Copy } from "lucide-react";
+import React, { useEffect, useState } from "react";
+
+interface CopyButtonProps {
+ value: string | null | undefined;
+ label: string;
+ className?: string;
+ iconClassName?: string;
+}
+
+const CopyButton: React.FC<CopyButtonProps> = ({ value, label, className, iconClassName = "size-[15px]" }) => {
+ const [copied, setCopied] = useState(false);
+
+ useEffect(() => {
+ if (!copied) return;
+ const timer = setTimeout(() => setCopied(false), 1200);
+ return () => clearTimeout(timer);
+ }, [copied]);
+
+ if (!value) return null;
+
+ const handleCopy = async () => {
+ if (!navigator.clipboard) return;
+ try {
+ await navigator.clipboard.writeText(value);
+ setCopied(true);
+ } catch {
+ setCopied(false);
+ }
+ };
+
+ return (
+ <Button
+ type="button"
+ variant="ghost"
+ size="icon-xs"
+ onClick={handleCopy}
+ aria-label={label}
+ title={label}
+ className={cn("text-muted-foreground hover:text-primary", className)}
+ >
+ {copied ? <Check className={iconClassName} /> : <Copy className={iconClassName} />}
+ </Button>
+ );
+};
+
+export default CopyButton;