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]

Change to useAuthorized hook

yuneng-jiang52cb54968a7d
diff --git a/ui/litellm-dashboard/src/app/(dashboard)/usage/page.tsx b/ui/litellm-dashboard/src/app/(dashboard)/usage/page.tsx
index 2fecfb6a0b2..d55939c7a6e 100644
--- a/ui/litellm-dashboard/src/app/(dashboard)/usage/page.tsx
+++ b/ui/litellm-dashboard/src/app/(dashboard)/usage/page.tsx
@@ -8,16 +8,7 @@ const UsagePage = () => {
const { accessToken, userRole, userId, premiumUser } = useAuthorized();
const { teams } = useTeams();
- return (
- <UsagePageView
- accessToken={accessToken}
- userRole={userRole}
- userID={userId}
- teams={teams ?? []}
- premiumUser={premiumUser}
- organizations={[]}
- />
- );
+ return <UsagePageView teams={teams ?? []} organizations={[]} />;
};
export default UsagePage;
diff --git a/ui/litellm-dashboard/src/app/page.tsx b/ui/litellm-dashboard/src/app/page.tsx
index b767c81e58b..b7157eed5ed 100644
--- a/ui/litellm-dashboard/src/app/page.tsx
+++ b/ui/litellm-dashboard/src/app/page.tsx
@@ -446,12 +446,8 @@ export default function CreateKeyPage() {
<VectorStoreManagement accessToken={accessToken} userRole={userRole} userID={userID} />
) : page == "new_usage" ? (
<NewUsagePage
- userID={userID}
- userRole={userRole}
- accessToken={accessToken}
teams={(teams as Team[]) ?? []}
organizations={(organizations as Organization[]) ?? []}
- premiumUser={premiumUser}
/>
) : (
<Usage
diff --git a/ui/litellm-dashboard/src/components/usage.tsx b/ui/litellm-dashboard/src/components/usage.tsx
index bb76480d0bb..624e9da8311 100644
--- a/ui/litellm-dashboard/src/components/usage.tsx
+++ b/ui/litellm-dashboard/src/components/usage.tsx
@@ -589,14 +589,7 @@ const UsagePage: React.FC<UsagePageProps> = ({ accessToken, token, userRole, use
Project Spend {new Date().toLocaleString("default", { month: "long" })} 1 -{" "}
{new Date(new Date().getFullYear(), new Date().getMonth() + 1, 0).getDate()}
</Text>
- <ViewUserSpend
- userID={userID}
- userRole={userRole}
- accessToken={accessToken}
- userSpend={totalMonthlySpend}
- selectedTeam={null}
- userMaxBudget={null}
- />
+ <ViewUserSpend userSpend={totalMonthlySpend} selectedTeam={null} userMaxBudget={null} />
</Col>
<Col numColSpan={2}>
<Card>
@@ -616,14 +609,7 @@ const UsagePage: React.FC<UsagePageProps> = ({ accessToken, token, userRole, use
<Col numColSpan={1}>
<Card className="h-full">
<Title>Top Virtual Keys</Title>
- <TopKeyView
- topKeys={topKeys}
- accessToken={accessToken}
- userID={userID}
- userRole={userRole}
- teams={null}
- premiumUser={premiumUser}
- />
+ <TopKeyView topKeys={topKeys} teams={null} />
</Card>
</Col>
<Col numColSpan={1}>
diff --git a/ui/litellm-dashboard/src/components/usage/components/EntityUsage/EntityUsage.tsx b/ui/litellm-dashboard/src/components/usage/components/EntityUsage/EntityUsage.tsx
index aa4eddd1bf3..3179ce25f80 100644
--- a/ui/litellm-dashboard/src/components/usage/components/EntityUsage/EntityUsage.tsx
+++ b/ui/litellm-dashboard/src/components/usage/components/EntityUsage/EntityUsage.tsx
@@ -576,15 +576,7 @@ const EntityUsage: React.FC<EntityUsageProps> = ({
<Col numColSpan={1}>
<Card>
<Title>Top Virtual Keys</Title>
- <TopKeyView
- topKeys={getTopAPIKeys()}
- accessToken={accessToken}
- userID={userID}
- userRole={userRole}
- teams={null}
- premiumUser={premiumUser}
- showTags={entityType === "tag"}
- />
+ <TopKeyView topKeys={getTopAPIKeys()} teams={null} showTags={entityType === "tag"} />
</Card>
</Col>
diff --git a/ui/litellm-dashboard/src/components/usage/components/EntityUsage/TopKeyView.test.tsx b/ui/litellm-dashboard/src/components/usage/components/EntityUsage/TopKeyView.test.tsx
index 273b85d6d20..6c074623139 100644
--- a/ui/litellm-dashboard/src/components/usage/components/EntityUsage/TopKeyView.test.tsx
+++ b/ui/litellm-dashboard/src/components/usage/components/EntityUsage/TopKeyView.test.tsx
@@ -1,87 +1,64 @@
-import { render } from "@testing-library/react";
-import { describe, expect, it } from "vitest";
+import { render, screen } from "@testing-library/react";
+import { describe, expect, it, vi, beforeEach } from "vitest";
import TopKeyView from "./TopKeyView";
+import useAuthorized from "@/app/(dashboard)/hooks/useAuthorized";
+
+vi.mock("@/app/(dashboard)/hooks/useAuthorized", () => ({
+ __esModule: true,
+ default: vi.fn(),
+}));
describe("TopKeyView", () => {
+ const mockUseAuthorized = vi.mocked(useAuthorized);
+ const mockAuth = {
+ token: "mock-token",
+ accessToken: "test-token",
+ userId: "user-1",
+ userEmail: "[email protected]",
+ userRole: "admin",
+ premiumUser: true,
+ disabledPersonalKeyCreation: false,
+ showSSOBanner: false,
+ };
+ const baseProps = {
+ topKeys: [],
+ teams: null,
+ showTags: false,
+ };
+
+ beforeEach(() => {
+ mockUseAuthorized.mockReturnValue(mockAuth);
+ });
+
it("should render", () => {
- const { container } = render(
- <TopKeyView
- topKeys={[]}
- accessToken={null}
- userID={null}
- userRole={null}
- teams={null}
- premiumUser={false}
- showTags={false}
- />,
- );
- expect(container).toBeTruthy();
+ render(<TopKeyView {...baseProps} />);
+ expect(screen.getByText("Table View")).toBeInTheDocument();
});
it("should have a table view button", () => {
- const { getByText } = render(
- <TopKeyView
- topKeys={[]}
- accessToken={null}
- userID={null}
- userRole={null}
- teams={null}
- premiumUser={false}
- showTags={false}
- />,
- );
- expect(getByText("Table View")).toBeInTheDocument();
+ render(<TopKeyView {...baseProps} />);
+ expect(screen.getByText("Table View")).toBeInTheDocument();
});
it("should have a chart view", () => {
- const { getByText } = render(
- <TopKeyView
- topKeys={[]}
- accessToken={null}
- userID={null}
- userRole={null}
- teams={null}
- premiumUser={false}
- showTags={false}
- />,
- );
- expect(getByText("Chart View")).toBeInTheDocument();
+ render(<TopKeyView {...baseProps} />);
+ expect(screen.getByText("Chart View")).toBeInTheDocument();
});
["Key ID", "Key Alias", "Spend (USD)"].forEach((header) => {
it(`should have a ${header} column`, () => {
- const { getByText } = render(
- <TopKeyView
- topKeys={[]}
- accessToken={null}
- userID={null}
- userRole={null}
- teams={null}
- premiumUser={false}
- showTags={false}
- />,
- );
- expect(getByText(header)).toBeInTheDocument();
+ render(<TopKeyView {...baseProps} />);
+ expect(screen.getByText(header)).toBeInTheDocument();
});
});
it("should have a Tags column when showTags is true", () => {
- const { getByText } = render(
- <TopKeyView
- topKeys={[]}
- accessToken={null}
- userID={null}
- userRole={null}
- teams={null}
- premiumUser={false}
- showTags={true}
- />,
- );
- expect(getByText("Tags")).toBeInTheDocument();
+ render(<TopKeyView {...baseProps} showTags={true} />);
+ expect(screen.getByText("Tags")).toBeInTheDocument();
});
it("should show the key's information on the table", () => {
- const { getByText } = render(
+ render(
<TopKeyView
topKeys={[
{
@@ -94,17 +71,13 @@ describe("TopKeyView", () => {
],
},
]}
- accessToken="test-token"
- userID={null}
- userRole={null}
teams={null}
- premiumUser={false}
showTags={true}
/>,
);
- expect(getByText("Test Key")).toBeInTheDocument();
- expect(getByText(/tag-1/)).toBeInTheDocument();
- expect(getByText(/tag-2/)).toBeInTheDocument();
- expect(getByText("$100.00")).toBeInTheDocument();
+ expect(screen.getByText("Test Key")).toBeInTheDocument();
+ expect(screen.getByText(/tag-1/)).toBeInTheDocument();
+ expect(screen.getByText(/tag-2/)).toBeInTheDocument();
+ expect(screen.getByText("$100.00")).toBeInTheDocument();
});
});
diff --git a/ui/litellm-dashboard/src/components/usage/components/EntityUsage/TopKeyView.tsx b/ui/litellm-dashboard/src/components/usage/components/EntityUsage/TopKeyView.tsx
index 43dc3244b4d..8f6bc411630 100644
--- a/ui/litellm-dashboard/src/components/usage/components/EntityUsage/TopKeyView.tsx
+++ b/ui/litellm-dashboard/src/components/usage/components/EntityUsage/TopKeyView.tsx
@@ -9,26 +9,16 @@ import { Button } from "@tremor/react";
import { formatNumberWithCommas } from "../../../../utils/dataUtils";
import { TagUsage } from "../../types";
import { ChevronDownIcon, ChevronUpIcon } from "@heroicons/react/outline";
+import useAuthorized from "@/app/(dashboard)/hooks/useAuthorized";
interface TopKeyViewProps {
topKeys: any[];
- accessToken: string | null;
- userID: string | null;
- userRole: string | null;
teams: any[] | null;
- premiumUser: boolean;
showTags?: boolean;
}
-const TopKeyView: React.FC<TopKeyViewProps> = ({
- topKeys,
- accessToken,
- userID,
- userRole,
- teams,
- premiumUser,
- showTags = false,
-}) => {
+const TopKeyView: React.FC<TopKeyViewProps> = ({ topKeys, teams, showTags = false }) => {
+ const { accessToken, userRole, userId: userID, premiumUser } = useAuthorized();
const [isModalOpen, setIsModalOpen] = useState<boolean>(false);
const [selectedKey, setSelectedKey] = useState<string | null>(null);
const [keyData, setKeyData] = useState<any | undefined>(undefined);
diff --git a/ui/litellm-dashboard/src/components/usage/components/UsagePageView.test.tsx b/ui/litellm-dashboard/src/components/usage/components/UsagePageView.test.tsx
index da12660b15d..265103b5ccc 100644
--- a/ui/litellm-dashboard/src/components/usage/components/UsagePageView.test.tsx
+++ b/ui/litellm-dashboard/src/components/usage/components/UsagePageView.test.tsx
@@ -5,6 +5,7 @@ import type { Organization } from "../../networking";
import * as networking from "../../networking";
import { useCustomers } from "@/app/(dashboard)/hooks/customers/useCustomers";
import { useAgents } from "@/app/(dashboard)/hooks/agents/useAgents";
+import useAuthorized from "@/app/(dashboard)/hooks/useAuthorized";
// Polyfill ResizeObserver for test environment
beforeAll(() => {
@@ -63,11 +64,17 @@ vi.mock("@/app/(dashboard)/hooks/agents/useAgents", () => ({
useAgents: vi.fn(),
}));
+vi.mock("@/app/(dashboard)/hooks/useAuthorized", () => ({
+ __esModule: true,
+ default: vi.fn(),
+}));
+
describe("NewUsage", () => {
const mockUserDailyActivityAggregatedCall = vi.mocked(networking.userDailyActivityAggregatedCall);
const mockTagListCall = vi.mocked(networking.tagListCall);
const mockUseCustomers = vi.mocked(useCustomers);
const mockUseAgents = vi.mocked(useAgents);
+ const mockUseAuthorized = vi.mocked(useAuthorized);
const mockSpendData = {
results: [
@@ -233,6 +240,16 @@ describe("NewUsage", () => {
};
beforeEach(() => {
+ mockUseAuthorized.mockReturnValue({
+ token: "mock-token",
+ accessToken: defaultProps.accessToken,
+ userId: defaultProps.userID,
+ userEmail: "[email protected]",
+ userRole: defaultProps.userRole,
+ premiumUser: defaultProps.premiumUser,
+ disabledPersonalKeyCreation: false,
+ showSSOBanner: false,
+ });
mockUserDailyActivityAggregatedCall.mockClear();
mockTagListCall.mockClear();
mockUserDailyActivityAggregatedCall.mockResolvedValue(mockSpendData);
diff --git a/ui/litellm-dashboard/src/components/usage/components/UsagePageView.tsx b/ui/litellm-dashboard/src/components/usage/components/UsagePageView.tsx
index b8728b85605..d52e9d7a94b 100644
--- a/ui/litellm-dashboard/src/components/usage/components/UsagePageView.tsx
+++ b/ui/litellm-dashboard/src/components/usage/components/UsagePageView.tsx
@@ -50,17 +50,15 @@ import { valueFormatterSpend } from "../utils/value_formatters";
import UserAgentActivity from "../../user_agent_activity";
import ViewUserSpend from "../../view_user_spend";
import { useAgents } from "@/app/(dashboard)/hooks/agents/useAgents";
+import useAuthorized from "@/app/(dashboard)/hooks/useAuthorized";
interface UsagePageProps {
- accessToken: string | null;
- userRole: string | null;
- userID: string | null;
teams: Team[];
organizations: Organization[];
- premiumUser: boolean;
}
-const UsagePage: React.FC<UsagePageProps> = ({ accessToken, userRole, userID, teams, organizations, premiumUser }) => {
+const UsagePage: React.FC<UsagePageProps> = ({ teams, organizations }) => {
+ const { accessToken, userRole, userId: userID, premiumUser } = useAuthorized();
const [userSpendData, setUserSpendData] = useState<{
results: DailyData[];
metadata: any;
@@ -488,14 +486,7 @@ const UsagePage: React.FC<UsagePageProps> = ({ accessToken, userRole, userID, te
)}
</Text>
- <ViewUserSpend
- userID={userID}
- userRole={userRole}
- accessToken={accessToken}
- userSpend={totalSpend}
- selectedTeam={null}
- userMaxBudget={null}
- />
+ <ViewUserSpend userSpend={totalSpend} selectedTeam={null} userMaxBudget={null} />
</Col>
<Col numColSpan={2}>
@@ -581,14 +572,7 @@ const UsagePage: React.FC<UsagePageProps> = ({ accessToken, userRole, userID, te
<Col numColSpan={1}>
<Card className="h-full">
<Title>Top Virtual Keys</Title>
- <TopKeyView
- topKeys={getTopKeys()}
- accessToken={accessToken}
- userID={userID}
- userRole={userRole}
- teams={null}
- premiumUser={premiumUser}
- />
+ <TopKeyView topKeys={getTopKeys()} teams={null} />
</Card>
</Col>
diff --git a/ui/litellm-dashboard/src/components/view_user_spend.tsx b/ui/litellm-dashboard/src/components/view_user_spend.tsx
index 611b308e500..51e87f8bc0d 100644
--- a/ui/litellm-dashboard/src/components/view_user_spend.tsx
+++ b/ui/litellm-dashboard/src/components/view_user_spend.tsx
@@ -2,6 +2,7 @@
import React, { useEffect, useState } from "react";
import { modelAvailableCall } from "./networking";
import { formatNumberWithCommas } from "@/utils/dataUtils";
+import useAuthorized from "@/app/(dashboard)/hooks/useAuthorized";
// Define the props type
interface UserSpendData {
@@ -10,21 +11,12 @@ interface UserSpendData {
// Add other properties if needed
}
interface ViewUserSpendProps {
- userID: string | null;
- userRole: string | null;
- accessToken: string | null;
userSpend: number | null;
userMaxBudget: number | null;
selectedTeam: any | null;
}
-const ViewUserSpend: React.FC<ViewUserSpendProps> = ({
- userID,
- userRole,
- accessToken,
- userSpend,
- userMaxBudget,
- selectedTeam,
-}) => {
+const ViewUserSpend: React.FC<ViewUserSpendProps> = ({ userSpend, userMaxBudget, selectedTeam }) => {
+ const { accessToken, userRole, userId: userID } = useAuthorized();
console.log(`userSpend: ${userSpend}`);
let [spend, setSpend] = useState(userSpend !== null ? userSpend : 0.0);
const [maxBudget, setMaxBudget] = useState(
diff --git a/ui/litellm-dashboard/tests/top_key_view.test.tsx b/ui/litellm-dashboard/tests/top_key_view.test.tsx
index 08a762f0900..3cdb44cabb3 100644
--- a/ui/litellm-dashboard/tests/top_key_view.test.tsx
+++ b/ui/litellm-dashboard/tests/top_key_view.test.tsx
@@ -2,6 +2,7 @@ import { describe, it, expect, vi, beforeEach } from "vitest";
import { renderWithProviders, screen, fireEvent } from "./test-utils";
import TopKeyView from "../src/components/Usage/components/EntityUsage/TopKeyView";
import { TagUsage } from "../src/components/Usage/types";
+import useAuthorized from "../src/app/(dashboard)/hooks/useAuthorized";
// Mock the networking module
vi.mock("../src/components/networking", () => ({
@@ -13,7 +14,12 @@ vi.mock("../src/components/key_team_helpers/transform_key_info", () => ({
transformKeyInfo: vi.fn((data) => data),
}));
+vi.mock("../src/app/(dashboard)/hooks/useAuthorized", () => ({
+ default: vi.fn(),
+}));
+
describe("TopKeyView", () => {
+ const mockUseAuthorized = vi.mocked(useAuthorized);
const mockProps = {
topKeys: [],
accessToken: "test-token",
@@ -58,6 +64,16 @@ describe("TopKeyView", () => {
beforeEach(() => {
vi.clearAllMocks();
+ mockUseAuthorized.mockReturnValue({
+ token: "mock-token",
+ accessToken: mockProps.accessToken,
+ userId: mockProps.userID,
+ userEmail: "[email protected]",
+ userRole: mockProps.userRole,
+ premiumUser: mockProps.premiumUser,
+ disabledPersonalKeyCreation: false,
+ showSSOBanner: false,
+ });
});
describe("Tags Column Visibility", () => {