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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
refactor(ui): move usage-card meter and sidebar button onto shared primitives
Yuneng Jiang1c6d9faf7a5a
diff --git a/ui/litellm-dashboard/src/components/SidebarUsageCard.test.tsx b/ui/litellm-dashboard/src/components/SidebarUsageCard.test.tsxindex 927ea657f46..3244906ed8e 100644--- a/ui/litellm-dashboard/src/components/SidebarUsageCard.test.tsx+++ b/ui/litellm-dashboard/src/components/SidebarUsageCard.test.tsx@@ -4,6 +4,7 @@ import { render, screen, waitFor } from "@testing-library/react";import userEvent from "@testing-library/user-event";import { QueryClient, QueryClientProvider } from "@tanstack/react-query";import SidebarUsageCard from "./SidebarUsageCard";+import type { LicenseInfo } from "./networking";vi.mock("./networking", () => ({ getRemainingUsers: vi.fn() }));@@ -12,12 +13,23 @@ vi.mock("@/app/(dashboard)/hooks/useDisableUsageIndicator", () => ({}));vi.mock("@/app/(dashboard)/hooks/license/useLicenseInfo", () => ({- useLicenseInfo: vi.fn(() => ({ data: null })),+ useLicenseInfo: vi.fn(),}));import { getRemainingUsers } from "./networking";+import { useLicenseInfo } from "@/app/(dashboard)/hooks/license/useLicenseInfo";const mockGetRemainingUsers = vi.mocked(getRemainingUsers);+const mockUseLicenseInfo = vi.mocked(useLicenseInfo);++const licenseResult = (data: LicenseInfo | null) => ({ data }) as unknown as ReturnType<typeof useLicenseInfo>;+const ACTIVE_LICENSE: LicenseInfo = {+ has_license: true,+ license_type: null,+ expiration_date: null,+ allowed_features: [],+ limits: { max_users: null, max_teams: null },+};const renderWithClient = (ui: React.ReactElement) => {const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });@@ -55,6 +67,7 @@ describe("SidebarUsageCard", () => {beforeEach(() => {vi.clearAllMocks();mockGetRemainingUsers.mockResolvedValue(SEATS_DATA);+ mockUseLicenseInfo.mockReturnValue(licenseResult(ACTIVE_LICENSE));});it("renders an expanded seat meter reporting value and range when data loads", async () => {@@ -103,6 +116,18 @@ describe("SidebarUsageCard", () => {await waitFor(() => expect(screen.queryByText("Enterprise usage")).not.toBeInTheDocument());});+ it("renders nothing without an enterprise license even when seat limits exist", async () => {+ mockUseLicenseInfo.mockReturnValue(licenseResult(null));++ const { container } = renderWithClient(+ <SidebarUsageCard accessToken="token" collapsed={false} onExpandRail={() => {}} />,+ );++ await waitFor(() => expect(mockGetRemainingUsers).toHaveBeenCalled());+ expect(screen.queryByText("Enterprise usage")).not.toBeInTheDocument();+ expect(container.querySelector('[data-slot="meter"]')).toBeNull();+ });+it("shows a collapsed rail button that expands the sidebar", async () => {const onExpandRail = vi.fn();const user = userEvent.setup();diff --git a/ui/litellm-dashboard/src/components/SidebarUsageCard.tsx b/ui/litellm-dashboard/src/components/SidebarUsageCard.tsxindex 7ab50001ae9..4fb0e1dac21 100644--- a/ui/litellm-dashboard/src/components/SidebarUsageCard.tsx+++ b/ui/litellm-dashboard/src/components/SidebarUsageCard.tsx@@ -1,6 +1,7 @@import { useDisableUsageIndicator } from "@/app/(dashboard)/hooks/useDisableUsageIndicator";import { useLicenseInfo } from "@/app/(dashboard)/hooks/license/useLicenseInfo";import { getDaysUntilExpiration } from "@/utils/licenseUtils";+import { Button } from "@/components/ui/button";import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@/components/ui/collapsible";import { Meter, MeterIndicator, MeterLabel, MeterTrack } from "@/components/ui/meter";import { useQuery } from "@tanstack/react-query";@@ -29,10 +30,10 @@ const formatExpiration = (daysRemaining: number | null): string => {return `${Math.floor(daysRemaining / 30)} months remaining`;};-const meterBarClass = (pct: number): string => {- if (pct > 100) return "bg-destructive";- if (pct >= 90) return "bg-amber-500";- return "bg-sidebar-primary";+const meterTone = (pct: number): "default" | "warning" | "over" => {+ if (pct > 100) return "over";+ if (pct >= 80) return "warning";+ return "default";};const UsageMeter = ({ label, used, total }: MeterData) => {@@ -47,7 +48,7 @@ const UsageMeter = ({ label, used, total }: MeterData) => {</span></div><MeterTrack>- <MeterIndicator className={meterBarClass(pct)} />+ <MeterIndicator tone={meterTone(pct)} /></MeterTrack></Meter>);@@ -85,20 +86,21 @@ export default function SidebarUsageCard({ accessToken, collapsed, onExpandRailconst hasData = data !== null && (data.total_users !== null || data.total_teams !== null);const noUsableData = !isLoading && !hasData;- if (disableUsageIndicator || !accessToken || noUsableData) {+ const noLicensedUsage = !licenseInfo?.has_license || noUsableData;+ if (disableUsageIndicator || !accessToken || noLicensedUsage) {return null;}if (collapsed) {return (- <button- type="button"+ <Button+ variant="outline"onClick={onExpandRail}title="Enterprise usage"- className="flex h-9 w-full items-center justify-center rounded-lg border border-sidebar-border bg-sidebar text-sidebar-primary transition-colors hover:bg-sidebar-accent"+ className="h-9 w-full rounded-lg border-sidebar-border bg-sidebar text-sidebar-primary shadow-none hover:bg-sidebar-accent hover:text-sidebar-primary"><Award className="size-[18px]" strokeWidth={1.75} />- </button>+ </Button>);}diff --git a/ui/litellm-dashboard/src/components/ui/meter.tsx b/ui/litellm-dashboard/src/components/ui/meter.tsxindex f87fa601de5..7454f95f507 100644--- a/ui/litellm-dashboard/src/components/ui/meter.tsx+++ b/ui/litellm-dashboard/src/components/ui/meter.tsx@@ -1,9 +1,22 @@"use client";import { Meter as MeterPrimitive } from "@base-ui/react/meter";+import { type VariantProps } from "cva";import * as React from "react";-import { cn } from "@/lib/cva.config";+import { cn, cva } from "@/lib/cva.config";++const meterIndicatorVariants = cva({+ base: "h-full rounded-full transition-[width] duration-300",+ variants: {+ tone: {+ default: "bg-primary",+ warning: "bg-amber-500",+ over: "bg-destructive",+ },+ },+ defaultVariants: { tone: "default" },+});const Meter = React.forwardRef<React.ComponentRef<typeof MeterPrimitive.Root>, MeterPrimitive.Root.Props>(({ className, ...props }, ref) => (@@ -55,12 +68,12 @@ MeterTrack.displayName = "MeterTrack";const MeterIndicator = React.forwardRef<React.ComponentRef<typeof MeterPrimitive.Indicator>,- MeterPrimitive.Indicator.Props->(({ className, ...props }, ref) => (+ MeterPrimitive.Indicator.Props & VariantProps<typeof meterIndicatorVariants>+>(({ className, tone, ...props }, ref) => (<MeterPrimitive.Indicatorref={ref}data-slot="meter-indicator"- className={cn("h-full rounded-full bg-primary transition-[width] duration-300", className)}+ className={cn(meterIndicatorVariants({ tone, className }))}{...props}/>));diff --git a/ui/litellm-dashboard/src/components/ui/sidebar.tsx b/ui/litellm-dashboard/src/components/ui/sidebar.tsxindex 5ac82788d87..a0c94ab6fee 100644--- a/ui/litellm-dashboard/src/components/ui/sidebar.tsx+++ b/ui/litellm-dashboard/src/components/ui/sidebar.tsx@@ -1,6 +1,7 @@"use client";import * as React from "react";+import { Button as ButtonPrimitive } from "@base-ui/react/button";import { type VariantProps } from "cva";import { cn, cva } from "@/lib/cva.config";@@ -158,13 +159,12 @@ const sidebarMenuButtonVariants = cva({defaultVariants: { isActive: false, size: "default" },});-type SidebarMenuButtonProps = React.ComponentPropsWithoutRef<"button"> & VariantProps<typeof sidebarMenuButtonVariants>;+type SidebarMenuButtonProps = ButtonPrimitive.Props & VariantProps<typeof sidebarMenuButtonVariants>;const SidebarMenuButton = React.forwardRef<HTMLButtonElement, SidebarMenuButtonProps>(({ className, isActive, size, ...props }, ref) => (- <button+ <ButtonPrimitiveref={ref}- type="button"data-slot="sidebar-menu-button"data-active={isActive || undefined}className={cn(sidebarMenuButtonVariants({ isActive, size, className }))}