diff --git a/ui/litellm-dashboard/eslint-suppressions.json b/ui/litellm-dashboard/eslint-suppressions.json index 51a7a196d0a..07dbf01881d 100644 --- a/ui/litellm-dashboard/eslint-suppressions.json +++ b/ui/litellm-dashboard/eslint-suppressions.json @@ -1066,7 +1066,7 @@ "count": 1 }, "prefer-const": { - "count": 2 + "count": 1 } }, "src/app/(dashboard)/search-tools/_components/SearchTools.tsx": { diff --git a/ui/litellm-dashboard/src/app/(dashboard)/access-groups/_components/AccessGroupsDetailsPage.test.tsx b/ui/litellm-dashboard/src/app/(dashboard)/access-groups/_components/AccessGroupsDetailsPage.test.tsx index aad63e979ac..0bae9b5bc38 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/access-groups/_components/AccessGroupsDetailsPage.test.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/access-groups/_components/AccessGroupsDetailsPage.test.tsx @@ -2,6 +2,7 @@ import { useAccessGroupDetails } from "@/app/(dashboard)/hooks/accessGroups/useA import { AccessGroupResponse } from "@/app/(dashboard)/hooks/accessGroups/useAccessGroups"; import { screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; +import type { OnUrlUpdateFunction } from "nuqs/adapters/testing"; import { beforeEach, describe, expect, it, vi } from "vitest"; import { renderWithProviders } from "../../../../../tests/test-utils"; import { AccessGroupDetail } from "./AccessGroupsDetailsPage"; @@ -368,6 +369,45 @@ describe("AccessGroupDetail", () => { }); }); + describe("detail_tab URL state", () => { + it("opens the tab named in detail_tab", () => { + renderWithProviders(, { + searchParams: "?detail_tab=agents", + }); + + expect(screen.getByRole("tab", { name: /Agents/i })).toHaveAttribute("aria-selected", "true"); + expect(screen.getByText("Support Agent")).toBeInTheDocument(); + expect(screen.queryByText("model-1")).not.toBeInTheDocument(); + }); + + it("falls back to Models for an unknown detail_tab", () => { + renderWithProviders(, { + searchParams: "?detail_tab=keys", + }); + + expect(screen.getByRole("tab", { name: /Models/i })).toHaveAttribute("aria-selected", "true"); + expect(screen.getByText("model-1")).toBeInTheDocument(); + }); + + it("writes the clicked tab to detail_tab and drops it for Models", async () => { + const user = userEvent.setup(); + const onUrlUpdate = vi.fn(); + renderWithProviders(, { + searchParams: "?group=ag-1", + onUrlUpdate, + }); + + await user.click(screen.getByRole("tab", { name: /MCP Servers/i })); + expect(onUrlUpdate.mock.calls.at(-1)?.[0].searchParams.get("detail_tab")).toBe("mcp"); + expect(onUrlUpdate.mock.calls.at(-1)?.[0].searchParams.get("group")).toBe("ag-1"); + expect(onUrlUpdate.mock.calls.at(-1)?.[0].searchParams.has("tab")).toBe(false); + + await user.click(screen.getByRole("tab", { name: /Models/i })); + expect(onUrlUpdate.mock.calls.at(-1)?.[0].searchParams.has("detail_tab")).toBe(false); + expect(screen.getByText("model-1")).toBeInTheDocument(); + }); + }); + it("should show empty state in Models tab when no models assigned", () => { renderWith({ access_model_names: [] }); diff --git a/ui/litellm-dashboard/src/app/(dashboard)/access-groups/_components/AccessGroupsDetailsPage.tsx b/ui/litellm-dashboard/src/app/(dashboard)/access-groups/_components/AccessGroupsDetailsPage.tsx index 118a8655d5a..ee2ad5d5aa1 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/access-groups/_components/AccessGroupsDetailsPage.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/access-groups/_components/AccessGroupsDetailsPage.tsx @@ -10,6 +10,7 @@ import { Card, CardAction, CardContent, CardHeader, CardTitle } from "@/componen import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { SimpleTooltip } from "@/components/ui/tooltip"; import { UiLoadingSpinner } from "@/components/ui/ui-loading-spinner"; +import { useUrlTab } from "@/hooks/useUrlTab"; import type { components } from "@/lib/http/schema"; import { keyDetailHref, teamDetailHref } from "@/utils/entityLinks"; import { AccessGroupEditModal } from "./AccessGroupsModal/AccessGroupEditModal"; @@ -23,6 +24,9 @@ interface AccessGroupDetailProps { const MAX_PREVIEW = 5; +export const ACCESS_GROUP_DETAIL_TAB_KEY = "detail_tab"; +const DETAIL_TABS = ["models", "mcp", "agents"] as const; + const shortId = (id: string) => (id.length > 20 ? `${id.slice(0, 10)}...${id.slice(-6)}` : id); function ResourceList({ items, emptyMessage }: { items: readonly AccessGroupResource[]; emptyMessage: string }) { @@ -70,6 +74,7 @@ export function AccessGroupDetail({ accessGroupId, onBack }: AccessGroupDetailPr const [isEditModalVisible, setIsEditModalVisible] = useState(false); const [showAllKeys, setShowAllKeys] = useState(false); const [showAllTeams, setShowAllTeams] = useState(false); + const [activeTab, setActiveTab] = useUrlTab(DETAIL_TABS, "models", ACCESS_GROUP_DETAIL_TAB_KEY); if (isLoading) { return ( @@ -214,7 +219,7 @@ export function AccessGroupDetail({ accessGroupId, onBack }: AccessGroupDetailPr - + diff --git a/ui/litellm-dashboard/src/app/(dashboard)/access-groups/_components/AccessGroupsPage.test.tsx b/ui/litellm-dashboard/src/app/(dashboard)/access-groups/_components/AccessGroupsPage.test.tsx index 12d3d773c1f..8691ac9152e 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/access-groups/_components/AccessGroupsPage.test.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/access-groups/_components/AccessGroupsPage.test.tsx @@ -1,6 +1,7 @@ import { fireEvent, renderWithProviders, screen, within } from "@/../tests/test-utils"; import { waitFor } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; +import type { OnUrlUpdateFunction } from "nuqs/adapters/testing"; import { beforeEach, describe, expect, it, vi } from "vitest"; import { AccessGroupsPage } from "./AccessGroupsPage"; import type { AccessGroupResponse } from "@/app/(dashboard)/hooks/accessGroups/useAccessGroups"; @@ -62,6 +63,7 @@ vi.mock("@/app/(dashboard)/hooks/useAuthorized", () => ({ })); vi.mock("./AccessGroupsDetailsPage", () => ({ + ACCESS_GROUP_DETAIL_TAB_KEY: "detail_tab", AccessGroupDetail: ({ accessGroupId, onBack }: { accessGroupId: string; onBack: () => void }) => (
Detail for {accessGroupId} @@ -90,6 +92,16 @@ const makeGroups = (count: number): AccessGroupResponse[] => }; }); +const SEARCH_PLACEHOLDER = "Search groups by name, ID, or description..."; + +const lastUrl = (onUrlUpdate: ReturnType>) => onUrlUpdate.mock.calls.at(-1)?.[0]; + +const renderedGroupIds = () => + screen + .getAllByRole("row") + .slice(1) + .map((row) => within(row).getAllByRole("cell")[0].textContent); + const openRowMenu = async (user: ReturnType, groupId: string) => { await user.click(screen.getByTestId(`access-group-actions-${groupId}`)); return screen.findByTestId("access-group-action-delete"); @@ -276,4 +288,102 @@ describe("AccessGroupsPage", () => { // The read-only view still lists the groups. expect(screen.getByText("Admin Group")).toBeInTheDocument(); }); + + describe("URL state", () => { + it("opens the detail view for the group named in the URL", () => { + renderWithProviders(, { searchParams: "?group=ag-2" }); + expect(screen.getByText("Detail for ag-2")).toBeInTheDocument(); + expect(screen.queryByPlaceholderText(SEARCH_PLACEHOLDER)).not.toBeInTheDocument(); + }); + + it("pushes the clicked group into the URL and resets the detail tab", async () => { + const user = userEvent.setup(); + const onUrlUpdate = vi.fn(); + renderWithProviders(, { searchParams: "?detail_tab=agents", onUrlUpdate }); + await user.click(screen.getByText("ag-1")); + expect(lastUrl(onUrlUpdate)?.searchParams.get("group")).toBe("ag-1"); + expect(lastUrl(onUrlUpdate)?.searchParams.has("detail_tab")).toBe(false); + expect(lastUrl(onUrlUpdate)?.options.history).toBe("push"); + }); + + it("clears the group and its detail tab on Back but keeps the list search", async () => { + const user = userEvent.setup(); + const onUrlUpdate = vi.fn(); + renderWithProviders(, { + searchParams: "?group=ag-1&detail_tab=mcp&group_search=Read", + onUrlUpdate, + }); + await user.click(screen.getByRole("button", { name: "Back" })); + const url = lastUrl(onUrlUpdate); + expect(url?.searchParams.has("group")).toBe(false); + expect(url?.searchParams.has("detail_tab")).toBe(false); + expect(url?.searchParams.get("group_search")).toBe("Read"); + expect(url?.options.history).toBe("push"); + expect(screen.getByPlaceholderText(SEARCH_PLACEHOLDER)).toHaveValue("Read"); + expect(screen.getByText("Read Only")).toBeInTheDocument(); + expect(screen.queryByText("Admin Group")).not.toBeInTheDocument(); + }); + + it("filters by the search in the URL", () => { + renderWithProviders(, { searchParams: "?group_search=admin" }); + expect(screen.getByPlaceholderText(SEARCH_PLACEHOLDER)).toHaveValue("admin"); + expect(screen.getByText("Admin Group")).toBeInTheDocument(); + expect(screen.queryByText("Read Only")).not.toBeInTheDocument(); + }); + + it("writes the search to group_search, resets the page, and removes it when cleared", async () => { + const user = userEvent.setup(); + const onUrlUpdate = vi.fn(); + mockUseAccessGroups.mockReturnValue({ data: makeGroups(25), isLoading: false }); + renderWithProviders(, { searchParams: "?page=2", onUrlUpdate }); + fireEvent.change(screen.getByPlaceholderText(SEARCH_PLACEHOLDER), { target: { value: "Group 0" } }); + await waitFor(() => expect(lastUrl(onUrlUpdate)?.searchParams.get("group_search")).toBe("Group 0")); + expect(lastUrl(onUrlUpdate)?.searchParams.has("search")).toBe(false); + expect(lastUrl(onUrlUpdate)?.searchParams.has("page")).toBe(false); + await user.click(screen.getByRole("button", { name: "Clear search" })); + await waitFor(() => expect(lastUrl(onUrlUpdate)?.searchParams.has("group_search")).toBe(false)); + expect(screen.getByPlaceholderText(SEARCH_PLACEHOLDER)).toHaveValue(""); + }); + + it("opens the page named in the URL and writes page changes back", async () => { + const user = userEvent.setup(); + const onUrlUpdate = vi.fn(); + mockUseAccessGroups.mockReturnValue({ data: makeGroups(25), isLoading: false }); + renderWithProviders(, { searchParams: "?page=3", onUrlUpdate }); + expect(renderedGroupIds()).toEqual(["ag-21", "ag-22", "ag-23", "ag-24", "ag-25"]); + await user.click(screen.getByTestId("pagination-prev")); + expect(lastUrl(onUrlUpdate)?.searchParams.get("page")).toBe("2"); + expect(screen.getByText("ag-11")).toBeInTheDocument(); + }); + + it("sorts by the column in the URL and writes header sort changes back", async () => { + const user = userEvent.setup(); + const onUrlUpdate = vi.fn(); + mockUseAccessGroups.mockReturnValue({ data: [mockAccessGroups[1], mockAccessGroups[0]], isLoading: false }); + renderWithProviders(, { searchParams: "?sort_by=name&sort_order=asc", onUrlUpdate }); + expect(renderedGroupIds()).toEqual(["ag-1", "ag-2"]); + await user.click(screen.getByTestId("sort-header-name")); + expect(lastUrl(onUrlUpdate)?.searchParams.get("sort_by")).toBe("name"); + expect(lastUrl(onUrlUpdate)?.searchParams.has("sort_order")).toBe(false); + expect(renderedGroupIds()).toEqual(["ag-2", "ag-1"]); + }); + + it("sorts oldest first when the URL asks for createdAt ascending", () => { + mockUseAccessGroups.mockReturnValue({ data: [mockAccessGroups[0], mockAccessGroups[1]], isLoading: false }); + renderWithProviders(, { searchParams: "?sort_by=createdAt&sort_order=asc" }); + expect(renderedGroupIds()).toEqual(["ag-2", "ag-1"]); + }); + + it("sorts by name in the default descending order when only sort_by is in the URL", () => { + mockUseAccessGroups.mockReturnValue({ data: [mockAccessGroups[0], mockAccessGroups[1]], isLoading: false }); + renderWithProviders(, { searchParams: "?sort_by=name" }); + expect(renderedGroupIds()).toEqual(["ag-2", "ag-1"]); + }); + + it("falls back to newest first when the URL names an unsortable column", () => { + mockUseAccessGroups.mockReturnValue({ data: [mockAccessGroups[1], mockAccessGroups[0]], isLoading: false }); + renderWithProviders(, { searchParams: "?sort_by=bogus" }); + expect(renderedGroupIds()).toEqual(["ag-1", "ag-2"]); + }); + }); }); diff --git a/ui/litellm-dashboard/src/app/(dashboard)/access-groups/_components/AccessGroupsPage.tsx b/ui/litellm-dashboard/src/app/(dashboard)/access-groups/_components/AccessGroupsPage.tsx index 2e82fe3c418..5e19fab65f1 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/access-groups/_components/AccessGroupsPage.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/access-groups/_components/AccessGroupsPage.tsx @@ -1,18 +1,25 @@ import { AccessGroupResponse, useAccessGroups } from "@/app/(dashboard)/hooks/accessGroups/useAccessGroups"; import { useDeleteAccessGroup } from "@/app/(dashboard)/hooks/accessGroups/useDeleteAccessGroup"; import { Boxes, Plus, SearchIcon, X } from "lucide-react"; -import { useMemo, useState } from "react"; +import { parseAsString, useQueryStates } from "nuqs"; +import { useCallback, useMemo, useState } from "react"; import DeleteResourceModal from "@/components/common_components/DeleteResourceModal"; +import { useUrlTableState } from "@/components/shared/DataTable"; import { PageHeader } from "@/components/shared/PageHeader"; import { Button } from "@/components/ui/button"; import { InputGroup, InputGroupAddon, InputGroupButton, InputGroupInput } from "@/components/ui/input-group"; -import { AccessGroupDetail } from "./AccessGroupsDetailsPage"; +import { ACCESS_GROUP_DETAIL_TAB_KEY, AccessGroupDetail } from "./AccessGroupsDetailsPage"; import { AccessGroupCreateDialog } from "./access-group-create/AccessGroupCreateDialog"; -import { AccessGroupsTable } from "./AccessGroupsTable"; +import { ACCESS_GROUPS_TABLE_STATE, AccessGroupsTable } from "./AccessGroupsTable"; import { AccessGroup } from "./types"; import useAuthorized from "@/app/(dashboard)/hooks/useAuthorized"; import { isProxyAdminRole } from "@/utils/roles"; +const DETAIL_URL_STATE = { + group: parseAsString, + [ACCESS_GROUP_DETAIL_TAB_KEY]: parseAsString, +}; + function mapResponseToAccessGroup(r: AccessGroupResponse): AccessGroup { return { id: r.access_group_id, @@ -37,9 +44,9 @@ export function AccessGroupsPage() { const { data: groupsData, isLoading } = useAccessGroups(); const groups = useMemo(() => (groupsData ?? []).map(mapResponseToAccessGroup), [groupsData]); - const [selectedGroupId, setSelectedGroupId] = useState(null); + const [{ group: selectedGroupId }, setDetail] = useQueryStates(DETAIL_URL_STATE, { history: "push" }); const [isCreateModalVisible, setIsCreateModalVisible] = useState(false); - const [searchText, setSearchText] = useState(""); + const { search: searchText, setSearch: setSearchText } = useUrlTableState(ACCESS_GROUPS_TABLE_STATE); const [groupToDelete, setGroupToDelete] = useState(null); const deleteMutation = useDeleteAccessGroup(); @@ -54,8 +61,13 @@ export function AccessGroupsPage() { ); }, [groups, searchText]); + const openGroup = useCallback( + (id: string) => void setDetail({ group: id, [ACCESS_GROUP_DETAIL_TAB_KEY]: null }), + [setDetail], + ); + if (selectedGroupId) { - return setSelectedGroupId(null)} />; + return void setDetail(null)} />; } return ( @@ -99,7 +111,7 @@ export function AccessGroupsPage() { isLoading={isLoading} isFiltered={searchText.trim().length > 0} canModify={canModify} - onGroupClick={setSelectedGroupId} + onGroupClick={openGroup} onDeleteClick={setGroupToDelete} /> diff --git a/ui/litellm-dashboard/src/app/(dashboard)/access-groups/_components/AccessGroupsTable.tsx b/ui/litellm-dashboard/src/app/(dashboard)/access-groups/_components/AccessGroupsTable.tsx index 10d1735d3e7..7ed99586364 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/access-groups/_components/AccessGroupsTable.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/access-groups/_components/AccessGroupsTable.tsx @@ -1,10 +1,9 @@ "use client"; -import { SortingState } from "@tanstack/react-table"; import { Layers } from "lucide-react"; -import { useMemo, useState } from "react"; +import { useMemo } from "react"; -import { DataTable } from "@/components/shared/DataTable"; +import { DataTable, useUrlTableState, type UrlTableStateOptions } from "@/components/shared/DataTable"; import { getAccessGroupsTableColumns } from "./AccessGroupsTableColumns"; import { AccessGroup } from "./types"; @@ -20,6 +19,15 @@ interface AccessGroupsTableProps { const PAGE_SIZE_OPTIONS = [10, 25, 50]; +export const ACCESS_GROUPS_TABLE_STATE: UrlTableStateOptions = { + sortFields: ["name", "createdAt"], + defaultSort: { id: "createdAt", desc: true }, + defaultPageSize: PAGE_SIZE_OPTIONS[0], + maxPageSize: PAGE_SIZE_OPTIONS[PAGE_SIZE_OPTIONS.length - 1], + filterColumns: [], + urlKeys: { search: "group_search" }, +}; + function EmptyState({ isFiltered }: { isFiltered: boolean }) { return (
@@ -46,7 +54,7 @@ export function AccessGroupsTable({ onGroupClick, onDeleteClick, }: AccessGroupsTableProps) { - const [sorting, setSorting] = useState([]); + const { sorting, onSortingChange, pagination, onPaginationChange } = useUrlTableState(ACCESS_GROUPS_TABLE_STATE); const columns = useMemo(() => { const deps = { canModify, onGroupClick, onDeleteClick }; @@ -60,8 +68,10 @@ export function AccessGroupsTable({ getRowId={(group, index) => group.id || String(index)} sortingMode="client" sorting={sorting} - onSortingChange={setSorting} + onSortingChange={onSortingChange} paginationMode="client" + pagination={pagination} + onPaginationChange={onPaginationChange} pageSizeOptions={PAGE_SIZE_OPTIONS} isLoading={isLoading} loadingMessage="Loading access groups…" diff --git a/ui/litellm-dashboard/src/app/(dashboard)/admin-panel/_components/AdminPanel.test.tsx b/ui/litellm-dashboard/src/app/(dashboard)/admin-panel/_components/AdminPanel.test.tsx index ee55c568bac..0fc642b675a 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/admin-panel/_components/AdminPanel.test.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/admin-panel/_components/AdminPanel.test.tsx @@ -1,6 +1,7 @@ -import { fireEvent, render, screen, waitFor, within } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; +import type { OnUrlUpdateFunction } from "nuqs/adapters/testing"; import { beforeEach, describe, expect, it, vi } from "vitest"; +import { fireEvent, renderWithProviders, screen, waitFor, within } from "../../../../../tests/test-utils"; import AdminPanel from "./AdminPanel"; const mockGetSSOSettings = vi.fn(); @@ -63,14 +64,14 @@ describe("AdminPanel", () => { }); it("should render the admin panel", () => { - render(); + renderWithProviders(); expect(screen.getByRole("heading", { name: /admin access/i })).toBeInTheDocument(); expect(screen.getByText(/go to 'internal users' page to add other admins/i)).toBeInTheDocument(); }); describe("Tabs", () => { it("should render all tabs", () => { - render(); + renderWithProviders(); expect(screen.getByRole("tab", { name: /sso settings/i })).toBeInTheDocument(); expect(screen.getByRole("tab", { name: /security settings/i })).toBeInTheDocument(); expect(screen.getByRole("tab", { name: /scim/i })).toBeInTheDocument(); @@ -79,7 +80,7 @@ describe("AdminPanel", () => { it("should display Security Settings content when Security Settings tab is clicked", async () => { const user = userEvent.setup(); - render(); + renderWithProviders(); const securityTab = screen.getByRole("tab", { name: /security settings/i }); await user.click(securityTab); expect(screen.getByRole("heading", { name: /security settings/i })).toBeInTheDocument(); @@ -88,9 +89,31 @@ describe("AdminPanel", () => { expect(screen.getByRole("button", { name: /ui access control/i })).toBeInTheDocument(); }); + it("opens the tab named in the URL", () => { + renderWithProviders(, { searchParams: "?tab=scim" }); + expect(screen.getByRole("tab", { name: /scim/i })).toHaveAttribute("aria-selected", "true"); + expect(screen.getByRole("tabpanel")).toHaveTextContent("SCIM Config"); + }); + + it("falls back to SSO Settings when the URL names an unknown tab", () => { + renderWithProviders(, { searchParams: "?tab=billing" }); + expect(screen.getByRole("tab", { name: /sso settings/i })).toHaveAttribute("aria-selected", "true"); + expect(screen.getByRole("tabpanel")).toHaveTextContent("SSO Settings"); + }); + + it("writes the clicked tab to the URL and clears it when returning to the first tab", async () => { + const user = userEvent.setup(); + const onUrlUpdate = vi.fn(); + renderWithProviders(, { onUrlUpdate }); + await user.click(screen.getByRole("tab", { name: /ui settings/i })); + await waitFor(() => expect(onUrlUpdate.mock.calls.at(-1)?.[0].searchParams.get("tab")).toBe("ui-settings")); + await user.click(screen.getByRole("tab", { name: /sso settings/i })); + await waitFor(() => expect(onUrlUpdate.mock.calls.at(-1)?.[0].searchParams.has("tab")).toBe(false)); + }); + it("should display SCIM content when SCIM tab is clicked", async () => { const user = userEvent.setup(); - render(); + renderWithProviders(); const scimTab = screen.getByRole("tab", { name: /scim/i }); await user.click(scimTab); expect(screen.getByText("SCIM Config")).toBeInTheDocument(); @@ -99,7 +122,7 @@ describe("AdminPanel", () => { describe("SSO Configuration", () => { it("should check SSO configuration on mount when accessToken is available", async () => { - render(); + renderWithProviders(); await waitFor(() => { expect(mockGetSSOSettings).toHaveBeenCalledWith("test-token"); }); @@ -110,7 +133,7 @@ describe("AdminPanel", () => { mockGetSSOSettings.mockResolvedValue({ values: {}, }); - render(); + renderWithProviders(); const securityTab = screen.getByRole("tab", { name: /security settings/i }); await user.click(securityTab); await waitFor(() => { @@ -126,7 +149,7 @@ describe("AdminPanel", () => { google_client_secret: "test-secret", }, }); - render(); + renderWithProviders(); const securityTab = screen.getByRole("tab", { name: /security settings/i }); await user.click(securityTab); await waitFor(() => { @@ -141,7 +164,7 @@ describe("AdminPanel", () => { google_client_secret: "test-secret", }, }); - render(); + renderWithProviders(); await waitFor(() => { expect(mockGetSSOSettings).toHaveBeenCalled(); }); @@ -154,7 +177,7 @@ describe("AdminPanel", () => { microsoft_client_secret: "test-secret", }, }); - render(); + renderWithProviders(); await waitFor(() => { expect(mockGetSSOSettings).toHaveBeenCalled(); }); @@ -167,7 +190,7 @@ describe("AdminPanel", () => { generic_client_secret: "test-secret", }, }); - render(); + renderWithProviders(); await waitFor(() => { expect(mockGetSSOSettings).toHaveBeenCalled(); }); @@ -175,7 +198,7 @@ describe("AdminPanel", () => { it("should handle SSO configuration check error gracefully", async () => { mockGetSSOSettings.mockRejectedValue(new Error("Network error")); - render(); + renderWithProviders(); await waitFor(() => { expect(mockGetSSOSettings).toHaveBeenCalled(); }); @@ -190,7 +213,7 @@ describe("AdminPanel", () => { accessToken: "test-token", userId: "user-1", }); - render(); + renderWithProviders(); const securityTab = screen.getByRole("tab", { name: /security settings/i }); await user.click(securityTab); }); @@ -268,7 +291,7 @@ describe("AdminPanel", () => { accessToken: "test-token", userId: "user-1", }); - render(); + renderWithProviders(); const securityTab = screen.getByRole("tab", { name: /security settings/i }); await user.click(securityTab); const uiAccessControlButton = screen.getByRole("button", { name: /ui access control/i }); @@ -285,7 +308,7 @@ describe("AdminPanel", () => { accessToken: "test-token", userId: "user-1", }); - render(); + renderWithProviders(); const securityTab = screen.getByRole("tab", { name: /security settings/i }); await user.click(securityTab); const uiAccessControlButton = screen.getByRole("button", { name: /ui access control/i }); @@ -300,7 +323,7 @@ describe("AdminPanel", () => { describe("Login without SSO", () => { it("should display fallback login URL", async () => { const user = userEvent.setup(); - render(); + renderWithProviders(); const securityTab = screen.getByRole("tab", { name: /security settings/i }); await user.click(securityTab); const link = screen.getByRole("link", { name: /http:\/\/localhost:4000\/fallback\/login/i }); @@ -313,7 +336,7 @@ describe("AdminPanel", () => { describe("SSO Configuration Deprecation Warning", () => { it("should display deprecation warning in Security Settings tab", async () => { const user = userEvent.setup(); - render(); + renderWithProviders(); const securityTab = screen.getByRole("tab", { name: /security settings/i }); await user.click(securityTab); await waitFor(() => { @@ -339,7 +362,7 @@ describe("AdminPanel add allowed IP form", () => { mockAddAllowedIP.mockResolvedValue({}); const user = userEvent.setup(); - render(); + renderWithProviders(); await user.click(screen.getByRole("tab", { name: /security settings/i })); await user.click(screen.getByRole("button", { name: /allowed ips/i })); const manageDialog = await screen.findByRole("dialog", { name: /manage allowed ip addresses/i }); diff --git a/ui/litellm-dashboard/src/app/(dashboard)/admin-panel/_components/AdminPanel.tsx b/ui/litellm-dashboard/src/app/(dashboard)/admin-panel/_components/AdminPanel.tsx index 1c8425251fd..54d393198a9 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/admin-panel/_components/AdminPanel.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/admin-panel/_components/AdminPanel.tsx @@ -8,6 +8,7 @@ import { Button } from "@/components/ui/button"; import { Card } from "@/components/ui/card"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; +import { useUrlTab } from "@/hooks/useUrlTab"; import { Info, TriangleAlert } from "lucide-react"; import React, { useEffect, useState } from "react"; import { useBaseUrl } from "@/components/constants"; @@ -409,12 +410,16 @@ const AdminPanel: React.FC = ({ proxySettings }) => { children: , }, ]; + const [activeTab, setActiveTab] = useUrlTab( + tabItems.map((item) => item.key), + tabItems[0].key, + ); return (

Admin Access

Go to 'Internal Users' page to add other admins.

- + {tabItems.map((item) => ( diff --git a/ui/litellm-dashboard/src/app/(dashboard)/api-reference/_components/APIReferenceView.test.tsx b/ui/litellm-dashboard/src/app/(dashboard)/api-reference/_components/APIReferenceView.test.tsx index dae19032e20..706de128fd1 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/api-reference/_components/APIReferenceView.test.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/api-reference/_components/APIReferenceView.test.tsx @@ -1,6 +1,7 @@ -import { render, screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; +import type { OnUrlUpdateFunction } from "nuqs/adapters/testing"; import { describe, expect, it, vi } from "vitest"; +import { renderWithProviders, screen } from "../../../../../tests/test-utils"; import APIReferenceView from "./APIReferenceView"; vi.mock("@/components/CodeBlock", () => ({ @@ -13,7 +14,7 @@ describe("APIReferenceView", () => { it("uses the API doc base url when provided", () => { const apiDocUrl = "https://docs.litellm.test"; - render(); + renderWithProviders(); const codeBlocks = screen.getAllByTestId(codeBlockTestId); expect(codeBlocks[0]).toHaveTextContent(new RegExp(apiDocUrl)); @@ -21,7 +22,7 @@ describe("APIReferenceView", () => { it("falls back to the proxy base url when the docs url is missing", () => { const proxyUrl = "https://proxy.litellm.test"; - render(); + renderWithProviders(); const codeBlocks = screen.getAllByTestId(codeBlockTestId); expect(codeBlocks[0]).toHaveTextContent(new RegExp(proxyUrl)); @@ -31,7 +32,7 @@ describe("APIReferenceView", () => { const apiDocUrl = "https://docs-preferred.litellm.test"; const proxyUrl = "https://proxy-backup.litellm.test"; - render( + renderWithProviders( { }); it("renders the page title, blurb and docs link", () => { - render(); + renderWithProviders(); expect(screen.getByText("OpenAI Compatible Proxy: API Reference")).toBeInTheDocument(); expect(screen.getByText(/LiteLLM is OpenAI Compatible/)).toBeInTheDocument(); @@ -58,7 +59,7 @@ describe("APIReferenceView", () => { }); it("exposes the three SDK tabs with the first selected by default", () => { - render(); + renderWithProviders(); expect(screen.getAllByRole("tab").map((tab) => tab.textContent)).toEqual([ "OpenAI Python SDK", @@ -79,7 +80,7 @@ describe("APIReferenceView", () => { ])("selecting %s shows its snippet wired to the base url", async (tabName, marker) => { const proxyUrl = "https://proxy.litellm.test"; const user = userEvent.setup(); - render(); + renderWithProviders(); await user.click(screen.getByRole("tab", { name: tabName })); @@ -89,4 +90,37 @@ describe("APIReferenceView", () => { expect(selectedPanel).toHaveTextContent(new RegExp(marker)); expect(selectedPanel).toHaveTextContent(new RegExp(proxyUrl)); }); + + it("opens the SDK named in the URL", () => { + renderWithProviders(, { + searchParams: "?sdk=langchain", + }); + + expect(screen.getByRole("tab", { name: "Langchain Py" })).toHaveAttribute("aria-selected", "true"); + expect(screen.getByRole("tabpanel")).toHaveTextContent(/from langchain.chat_models import ChatOpenAI/); + }); + + it("falls back to the OpenAI SDK for an unknown sdk in the URL", () => { + renderWithProviders(, { + searchParams: "?sdk=rust", + }); + + expect(screen.getByRole("tab", { name: "OpenAI Python SDK" })).toHaveAttribute("aria-selected", "true"); + expect(screen.getByRole("tabpanel")).toHaveTextContent(/import openai/); + }); + + it("writes the selected SDK to the URL and removes it for the default SDK", async () => { + const user = userEvent.setup(); + const onUrlUpdate = vi.fn(); + renderWithProviders(, { + onUrlUpdate, + }); + + await user.click(screen.getByRole("tab", { name: "LlamaIndex" })); + expect(onUrlUpdate.mock.calls.at(-1)?.[0].searchParams.get("sdk")).toBe("llamaindex"); + expect(onUrlUpdate.mock.calls.at(-1)?.[0].searchParams.has("tab")).toBe(false); + + await user.click(screen.getByRole("tab", { name: "OpenAI Python SDK" })); + expect(onUrlUpdate.mock.calls.at(-1)?.[0].searchParams.has("sdk")).toBe(false); + }); }); diff --git a/ui/litellm-dashboard/src/app/(dashboard)/api-reference/_components/APIReferenceView.tsx b/ui/litellm-dashboard/src/app/(dashboard)/api-reference/_components/APIReferenceView.tsx index 847bd34da3e..627e665879a 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/api-reference/_components/APIReferenceView.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/api-reference/_components/APIReferenceView.tsx @@ -2,8 +2,11 @@ import React from "react"; import CodeBlock from "@/components/CodeBlock"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; +import { useUrlTab } from "@/hooks/useUrlTab"; import DocLink from "./DocLink"; +const SDK_TABS = ["openai", "llamaindex", "langchain"] as const; + interface ApiRefProps { proxySettings: { PROXY_BASE_URL?: string; @@ -12,6 +15,7 @@ interface ApiRefProps { } const APIReferenceView: React.FC = ({ proxySettings }) => { + const [sdk, setSdk] = useUrlTab(SDK_TABS, "openai", "sdk"); let base_url = ""; const customDocBaseUrl = proxySettings?.LITELLM_UI_API_DOC_BASE_URL; if (customDocBaseUrl && customDocBaseUrl.trim()) { @@ -34,7 +38,7 @@ const APIReferenceView: React.FC = ({ proxySettings }) => { point to your litellm proxy. Example Below{" "}

- + OpenAI Python SDK diff --git a/ui/litellm-dashboard/src/app/(dashboard)/router-settings/_components/general_settings.integration.test.tsx b/ui/litellm-dashboard/src/app/(dashboard)/router-settings/_components/general_settings.integration.test.tsx index b4df567e250..0bbad6b79aa 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/router-settings/_components/general_settings.integration.test.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/router-settings/_components/general_settings.integration.test.tsx @@ -1,5 +1,6 @@ import { renderWithProviders, screen, within } from "../../../../../tests/test-utils"; import userEvent from "@testing-library/user-event"; +import type { OnUrlUpdateFunction } from "nuqs/adapters/testing"; import { beforeEach, describe, expect, it, vi } from "vitest"; import GeneralSettings from "./general_settings"; import { deleteConfigFieldSetting, getGeneralSettingsCall, updateConfigFieldSetting } from "@/components/networking"; @@ -157,6 +158,48 @@ describe("GeneralSettings tabs", () => { } expect(screen.queryByRole("tab", { name: /auto.?router/i })).not.toBeInTheDocument(); }); + + it("opens the tab named in the URL so Routing Groups has an address", async () => { + renderWithProviders(, { + searchParams: "?tab=routing-groups", + }); + + expect(await screen.findByRole("tab", { name: "Routing Groups" })).toHaveAttribute("aria-selected", "true"); + expect(screen.getByRole("tab", { name: "Loadbalancing" })).toHaveAttribute("aria-selected", "false"); + }); + + it("writes the clicked tab to the URL and drops it again for Loadbalancing", async () => { + const user = userEvent.setup(); + const onUrlUpdate = vi.fn(); + renderWithProviders(, { onUrlUpdate }); + + await user.click(await screen.findByRole("tab", { name: "Prompt Caching" })); + expect(onUrlUpdate.mock.calls.at(-1)?.[0].searchParams.get("tab")).toBe("prompt-caching"); + expect(screen.getByRole("tab", { name: "Prompt Caching" })).toHaveAttribute("aria-selected", "true"); + + await user.click(screen.getByRole("tab", { name: "Loadbalancing" })); + expect(onUrlUpdate.mock.calls.at(-1)?.[0].searchParams.has("tab")).toBe(false); + }); + + it("selects every tab that is clicked", async () => { + const user = userEvent.setup(); + renderWithProviders(); + + const tabs = await screen.findAllByRole("tab"); + expect(tabs).toHaveLength(5); + for (const tab of tabs) { + await user.click(tab); + expect(tab).toHaveAttribute("aria-selected", "true"); + } + }); + + it("falls back to Loadbalancing for an unknown tab in the URL", async () => { + renderWithProviders(, { + searchParams: "?tab=bogus", + }); + + expect(await screen.findByRole("tab", { name: "Loadbalancing" })).toHaveAttribute("aria-selected", "true"); + }); }); it("should delete only the Default setting and retain explicit false and zero", async () => { diff --git a/ui/litellm-dashboard/src/app/(dashboard)/router-settings/_components/general_settings.tsx b/ui/litellm-dashboard/src/app/(dashboard)/router-settings/_components/general_settings.tsx index 9a718cbe9b8..96f803485ee 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/router-settings/_components/general_settings.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/router-settings/_components/general_settings.tsx @@ -10,12 +10,14 @@ import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { getGeneralSettingsCall, updateConfigFieldSetting, deleteConfigFieldSetting } from "@/components/networking"; import { Trash2 } from "lucide-react"; import { StatusBadge } from "@/components/shared/table_cells"; +import { useUrlTab } from "@/hooks/useUrlTab"; import RouterSettings from "@/components/router_settings"; import Fallbacks from "@/components/Settings/RouterSettings/Fallbacks/Fallbacks"; import RoutingGroups from "@/components/routing_groups"; const PROMPT_CACHING_TAB = "prompt_caching"; +const ROUTER_SETTINGS_TABS = ["loadbalancing", "routing-groups", "fallbacks", "prompt-caching", "general"] as const; const ENABLE_ANTHROPIC_PROMPT_CACHING = "enable_anthropic_prompt_caching"; const ANTHROPIC_PROMPT_CACHING_TTL = "anthropic_prompt_caching_ttl"; const OPENAI_SYSTEM_MESSAGES_FIRST = "openai_system_messages_first"; @@ -200,6 +202,7 @@ export const PromptCachingPanel: React.FC<{ const GeneralSettings: React.FC = ({ accessToken, userRole, userID }) => { const [generalSettings, setGeneralSettings] = useState([]); + const [activeTab, setActiveTab] = useUrlTab(ROUTER_SETTINGS_TABS, "loadbalancing"); useEffect(() => { if (!accessToken) { @@ -270,7 +273,7 @@ const GeneralSettings: React.FC = ({ accessToken, user return (
- + Loadbalancing Routing Groups diff --git a/ui/litellm-dashboard/src/components/Settings/LoggingAndAlerts/LoggingCallbacks/LoggingCallbacksTable.test.tsx b/ui/litellm-dashboard/src/components/Settings/LoggingAndAlerts/LoggingCallbacks/LoggingCallbacksTable.test.tsx index a4d76e2cc06..d0f208afe10 100644 --- a/ui/litellm-dashboard/src/components/Settings/LoggingAndAlerts/LoggingCallbacks/LoggingCallbacksTable.test.tsx +++ b/ui/litellm-dashboard/src/components/Settings/LoggingAndAlerts/LoggingCallbacks/LoggingCallbacksTable.test.tsx @@ -1,8 +1,11 @@ -import { render, screen } from "@testing-library/react"; +import { fireEvent, screen, waitFor } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; +import type { OnUrlUpdateFunction } from "nuqs/adapters/testing"; import { beforeEach, describe, expect, it, vi } from "vitest"; +import { renderWithProviders } from "../../../../../tests/test-utils"; import { LoggingCallbacksTable } from "./LoggingCallbacksTable"; +import type { AlertingObject } from "./types"; const baseVars = { SLACK_WEBHOOK_URL: null, @@ -18,24 +21,24 @@ describe("LoggingCallbacksTable", () => { }); it("should render", () => { - render(); + renderWithProviders(); expect(screen.getByText("Active Logging Callbacks")).toBeInTheDocument(); }); it("should show the empty state when there are no callbacks", () => { - render(); + renderWithProviders(); expect(screen.getByText("No callbacks configured")).toBeInTheDocument(); expect(screen.getByText("Add your first callback to start logging data to external services.")).toBeInTheDocument(); }); it("should show skeleton rows while loading", () => { - render(); + renderWithProviders(); expect(screen.getAllByTestId("skeleton-row").length).toBeGreaterThan(0); expect(screen.queryByText("No callbacks configured")).not.toBeInTheDocument(); }); it('should map "otel" to "OpenTelemetry" on the table', () => { - render( + renderWithProviders( { }); it("should fallback to original callback name when not in availableCallbacks", () => { - render( + renderWithProviders( { it("should call onAdd when the Add Callback button is clicked", async () => { const user = userEvent.setup(); const onAdd = vi.fn(); - render(); + renderWithProviders(); await user.click(screen.getByRole("button", { name: /add callback/i })); expect(onAdd).toHaveBeenCalled(); }); @@ -74,7 +77,7 @@ describe("LoggingCallbacksTable", () => { const onEdit = vi.fn(); const onDelete = vi.fn(); const callback = { name: "langfuse", type: "success" as const, variables: baseVars }; - render( + renderWithProviders( { }); it("shows a read-only label instead of the actions menu for runtime-only callback rows", () => { - render( + renderWithProviders( { // failure registration. Reading `record.type` fixes the badge AND // composing the row id with type avoids React's duplicate-key warning. it("renders distinct Success and Failure badges for same-name dual registration", () => { - render( + renderWithProviders( { expect(screen.getByText("Success")).toBeInTheDocument(); expect(screen.getByText("Failure")).toBeInTheDocument(); }); + + describe("URL state", () => { + const availableCallbacks = { + zeta_raw: { litellm_callback_name: "zeta_raw", litellm_callback_params: [], ui_callback_name: "Alpha Logger" }, + alpha_raw: { litellm_callback_name: "alpha_raw", litellm_callback_params: [], ui_callback_name: "Zulu Logger" }, + }; + const mixedCallbacks: AlertingObject[] = [ + { name: "alpha_raw", type: "success", variables: baseVars }, + { name: "zeta_raw", type: "failure", variables: baseVars }, + { name: "middle_callback", type: "success_and_failure", variables: baseVars }, + ]; + const manyCallbacks: AlertingObject[] = Array.from({ length: 30 }, (_, index) => ({ + name: `callback_${String(index + 1).padStart(2, "0")}`, + type: "success", + variables: baseVars, + })); + + const rowIds = () => + screen + .getAllByRole("row") + .map((row) => row.getAttribute("data-row-id")) + .filter((id): id is string => id !== null); + + const lastUrl = (onUrlUpdate: ReturnType>) => + onUrlUpdate.mock.calls.at(-1)?.[0].searchParams; + + it("sorts by the shown callback name by default", () => { + renderWithProviders(); + expect(rowIds()).toEqual(["zeta_raw-failure", "middle_callback-success_and_failure", "alpha_raw-success"]); + }); + + it("sorts by the column and direction in the URL", () => { + renderWithProviders( + , + { + searchParams: "?callbacks_sort_by=mode&callbacks_sort_order=desc", + }, + ); + expect(rowIds()).toEqual(["middle_callback-success_and_failure", "alpha_raw-success", "zeta_raw-failure"]); + }); + + it("writes header sort clicks to the prefixed sort keys", async () => { + const user = userEvent.setup(); + const onUrlUpdate = vi.fn(); + renderWithProviders( + , + { + onUrlUpdate, + }, + ); + + await user.click(screen.getByTestId("sort-header-mode")); + expect(lastUrl(onUrlUpdate)?.get("callbacks_sort_by")).toBe("mode"); + expect(lastUrl(onUrlUpdate)?.has("sort_by")).toBe(false); + expect(rowIds()).toEqual(["zeta_raw-failure", "alpha_raw-success", "middle_callback-success_and_failure"]); + }); + + it("filters by the search in the URL across name, shown name and mode", () => { + const { unmount } = renderWithProviders( + , + { searchParams: "?callbacks_search=zulu" }, + ); + expect(screen.getByTestId("datatable-search")).toHaveValue("zulu"); + expect(rowIds()).toEqual(["alpha_raw-success"]); + unmount(); + + renderWithProviders( + , + { + searchParams: "?callbacks_search=FAILURE", + }, + ); + expect(rowIds()).toEqual(["zeta_raw-failure", "middle_callback-success_and_failure"]); + }); + + it("shows a no-matches state instead of the setup prompt when the search finds nothing", () => { + renderWithProviders( + , + { + searchParams: "?callbacks_search=nothing-here", + }, + ); + expect(screen.getByText("No matching callbacks")).toBeInTheDocument(); + expect(screen.queryByText("No callbacks configured")).not.toBeInTheDocument(); + }); + + it("writes the search to callbacks_search and returns to the first page", async () => { + const onUrlUpdate = vi.fn(); + renderWithProviders(, { + searchParams: "?callbacks_page=2", + onUrlUpdate, + }); + expect(rowIds()[0]).toBe("callback_26-success"); + + fireEvent.change(screen.getByTestId("datatable-search"), { target: { value: "callback_0" } }); + + await waitFor(() => expect(lastUrl(onUrlUpdate)?.get("callbacks_search")).toBe("callback_0")); + expect(lastUrl(onUrlUpdate)?.has("callbacks_page")).toBe(false); + expect(rowIds()).toHaveLength(9); + expect(rowIds()[0]).toBe("callback_01-success"); + }); + + it("writes page changes to callbacks_page", async () => { + const user = userEvent.setup(); + const onUrlUpdate = vi.fn(); + renderWithProviders(, { + onUrlUpdate, + }); + expect(rowIds()).toHaveLength(25); + + await user.click(screen.getByTestId("pagination-next")); + + expect(lastUrl(onUrlUpdate)?.get("callbacks_page")).toBe("2"); + expect(lastUrl(onUrlUpdate)?.has("page")).toBe(false); + expect(rowIds()).toEqual([ + "callback_26-success", + "callback_27-success", + "callback_28-success", + "callback_29-success", + "callback_30-success", + ]); + }); + }); }); diff --git a/ui/litellm-dashboard/src/components/Settings/LoggingAndAlerts/LoggingCallbacks/LoggingCallbacksTable.tsx b/ui/litellm-dashboard/src/components/Settings/LoggingAndAlerts/LoggingCallbacks/LoggingCallbacksTable.tsx index d27b0cfd340..cd8a0b55af2 100644 --- a/ui/litellm-dashboard/src/components/Settings/LoggingAndAlerts/LoggingCallbacks/LoggingCallbacksTable.tsx +++ b/ui/litellm-dashboard/src/components/Settings/LoggingAndAlerts/LoggingCallbacks/LoggingCallbacksTable.tsx @@ -1,9 +1,16 @@ "use client"; -import { Inbox, Plus } from "lucide-react"; +import type { ColumnDef } from "@tanstack/react-table"; +import { Inbox, Plus, SearchX } from "lucide-react"; import React, { useMemo } from "react"; -import { DataTable } from "@/components/shared/DataTable"; +import { + DataTable, + DataTableSortHeader, + DataTableToolbar, + useUrlTableState, + type UrlTableStateOptions, +} from "@/components/shared/DataTable"; import { Button } from "@/components/ui/button"; import { @@ -24,6 +31,54 @@ type LoggingCallbacksProps = { onAdd?: () => void; }; +const TABLE_STATE_OPTIONS: UrlTableStateOptions = { + sortFields: ["name", "mode"], + defaultSort: { id: "name", desc: false }, + defaultPageSize: 25, + filterColumns: [], + keyPrefix: "callbacks_", +}; + +const callbackDisplayName = (callback: CallbackRow, availableCallbacks: AvailableCallbacks): string => + availableCallbacks[callback.name]?.ui_callback_name || callback.name; + +const withSortableColumns = ( + columns: ColumnDef[], + availableCallbacks: AvailableCallbacks, +): ColumnDef[] => { + const sortValues: Record string> = { + name: (callback) => callbackDisplayName(callback, availableCallbacks), + mode: callbackRowMode, + }; + return columns.map((column): ColumnDef => { + const id = column.id; + if (id === undefined) return column; + const accessorFn = sortValues[id]; + if (accessorFn === undefined) return column; + const title = column.meta?.title ?? id; + return { + ...column, + id, + accessorFn, + enableSorting: true, + sortingFn: "alphanumeric", + header: ({ column: sortable }) => , + }; + }); +}; + +function NoMatchesState() { + return ( +
+
+ +
+
No matching callbacks
+
Try a different search term.
+
+ ); +} + function EmptyState() { return (
@@ -47,11 +102,24 @@ export const LoggingCallbacksTable: React.FC = ({ onDelete = () => {}, onAdd = () => {}, }) => { + const { search, setSearch, sorting, onSortingChange, pagination, onPaginationChange } = + useUrlTableState(TABLE_STATE_OPTIONS); + const columns = useMemo(() => { const deps = { availableCallbacks, onTest, onEdit, onDelete }; - return getLoggingCallbacksTableColumns(deps); + return withSortableColumns(getLoggingCallbacksTableColumns(deps), availableCallbacks); }, [availableCallbacks, onTest, onEdit, onDelete]); + const query = search.trim().toLowerCase(); + const visibleCallbacks = useMemo(() => { + if (!query) return callbacks; + return callbacks.filter((callback) => + [callback.name, callbackDisplayName(callback, availableCallbacks), callbackRowMode(callback)].some((value) => + value.toLowerCase().includes(query), + ), + ); + }, [callbacks, availableCallbacks, query]); + return (

Active Logging Callbacks

@@ -62,12 +130,27 @@ export const LoggingCallbacksTable: React.FC = ({
`${callback.name || index}-${callbackRowMode(callback)}`} + sortingMode="client" + sorting={sorting} + onSortingChange={onSortingChange} + paginationMode="client" + pagination={pagination} + onPaginationChange={onPaginationChange} + toolbar={(table) => ( + + )} isLoading={isLoading} loadingMessage="Loading callbacks…" - noDataMessage={} + noDataMessage={query ? : } size="compact" />
diff --git a/ui/litellm-dashboard/src/components/Settings/LoggingAndAlerts/LoggingCallbacks/useCallbackEditUrlState.ts b/ui/litellm-dashboard/src/components/Settings/LoggingAndAlerts/LoggingCallbacks/useCallbackEditUrlState.ts new file mode 100644 index 00000000000..7ef3f4c1728 --- /dev/null +++ b/ui/litellm-dashboard/src/components/Settings/LoggingAndAlerts/LoggingCallbacks/useCallbackEditUrlState.ts @@ -0,0 +1,41 @@ +import { parseAsString, parseAsStringLiteral, useQueryStates } from "nuqs"; +import { useCallback, useEffect, useMemo } from "react"; + +import { callbackRowMode } from "./LoggingCallbacksTableColumns"; +import { AlertingObject } from "./types"; + +const CALLBACK_MODES = ["success", "failure", "success_and_failure"] as const; +type CallbackMode = (typeof CALLBACK_MODES)[number]; + +const EDIT_URL_STATE = { + callback: parseAsString, + callback_mode: parseAsStringLiteral(CALLBACK_MODES).withDefault("success"), +}; + +const toCallbackMode = (callback: AlertingObject): CallbackMode => + CALLBACK_MODES.find((mode) => mode === callbackRowMode(callback)) ?? "success"; + +export function useCallbackEditUrlState(callbacks: readonly AlertingObject[], isLoading: boolean) { + const [{ callback: name, callback_mode: mode }, setTarget] = useQueryStates(EDIT_URL_STATE, { history: "push" }); + + const editingCallback = useMemo( + () => + callbacks.find( + (callback) => !callback.read_only && callback.name === name && toCallbackMode(callback) === mode, + ) ?? null, + [callbacks, name, mode], + ); + + useEffect(() => { + if (!isLoading && name !== null && editingCallback === null) void setTarget(null, { history: "replace" }); + }, [isLoading, name, editingCallback, setTarget]); + + const openEdit = useCallback( + (callback: AlertingObject) => void setTarget({ callback: callback.name, callback_mode: toCallbackMode(callback) }), + [setTarget], + ); + + const closeEdit = useCallback(() => void setTarget(null), [setTarget]); + + return { editingCallback, openEdit, closeEdit }; +} diff --git a/ui/litellm-dashboard/src/components/routing_groups/RoutingGroupsTable.test.tsx b/ui/litellm-dashboard/src/components/routing_groups/RoutingGroupsTable.test.tsx index 6f14b76e2fd..c54fc65afab 100644 --- a/ui/litellm-dashboard/src/components/routing_groups/RoutingGroupsTable.test.tsx +++ b/ui/litellm-dashboard/src/components/routing_groups/RoutingGroupsTable.test.tsx @@ -1,7 +1,10 @@ -import { render, screen, within } from "@testing-library/react"; +import { screen, within } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; +import type { OnUrlUpdateFunction } from "nuqs/adapters/testing"; import { beforeEach, describe, expect, it, vi } from "vitest"; +import { renderWithProviders } from "../../../tests/test-utils"; + import RoutingGroupsTable from "./RoutingGroupsTable"; import type { RoutingGroup } from "./types"; @@ -21,6 +24,13 @@ describe("RoutingGroupsTable", () => { routing_strategy: "simple-shuffle", }; + const manyGroups: RoutingGroup[] = Array.from({ length: 30 }, (_, index) => ({ + group_name: `group-${String(index).padStart(2, "0")}`, + models: ["gpt-4o"], + routing_strategy: "simple-shuffle", + })); + const groupNames = manyGroups.map((group) => group.group_name); + const defaultProps = { groups: [] as RoutingGroup[], onEdit, @@ -36,24 +46,34 @@ describe("RoutingGroupsTable", () => { return row; }; + const namesInOrder = () => + screen + .getAllByRole("row") + .slice(1) + .map((row) => row.getAttribute("data-row-id")) + .filter((id): id is string => id !== null); + + const lastUrl = (onUrlUpdate: ReturnType>) => + onUrlUpdate.mock.calls.at(-1)?.[0].searchParams; + beforeEach(() => { vi.clearAllMocks(); }); it("should render every column header", () => { - render(); + renderWithProviders(); for (const header of ["Group Name", "Models", "Strategy"]) { expect(screen.getByText(header)).toBeInTheDocument(); } }); it("should show the empty state when there are no groups", () => { - render(); + renderWithProviders(); expect(screen.getByText("No routing groups yet")).toBeInTheDocument(); }); it("should render the group name, its models, and a human-readable strategy label", () => { - render(); + renderWithProviders(); const row = rowFor("prod-group"); expect(within(row).getByText("prod-group")).toBeInTheDocument(); expect(within(row).getByText("gpt-4o")).toBeInTheDocument(); @@ -62,13 +82,15 @@ describe("RoutingGroupsTable", () => { }); it("should fall back to the raw strategy value when it has no friendly label", () => { - render(); + renderWithProviders( + , + ); expect(within(rowFor("prod-group")).getByText("custom-strategy")).toBeInTheDocument(); }); it("should collapse models beyond the first three behind a +N more badge", () => { const wideGroup: RoutingGroup = { ...prodGroup, models: ["a", "b", "c", "d", "e"] }; - render(); + renderWithProviders(); const row = rowFor("prod-group"); expect(within(row).getByText("+2 more")).toBeInTheDocument(); expect(within(row).queryByText("d")).not.toBeInTheDocument(); @@ -76,13 +98,7 @@ describe("RoutingGroupsTable", () => { it("should keep the incoming order until a column is sorted", async () => { const user = userEvent.setup(); - render(); - - const namesInOrder = () => - screen - .getAllByRole("row") - .slice(1) - .map((row) => row.getAttribute("data-row-id")); + renderWithProviders(); expect(namesInOrder()).toEqual(["prod-group", "dev-group"]); @@ -92,7 +108,7 @@ describe("RoutingGroupsTable", () => { it("should toggle the usage panel when the group name is clicked", async () => { const user = userEvent.setup(); - render(); + renderWithProviders(); expect(screen.queryByText("How routing works for this group")).not.toBeInTheDocument(); @@ -105,7 +121,7 @@ describe("RoutingGroupsTable", () => { it("should build the usage snippet from the proxy base url and the group's first model", async () => { const user = userEvent.setup(); - render(); + renderWithProviders(); await user.click(screen.getByRole("button", { name: "prod-group" })); const panel = (await screen.findByText("How routing works for this group")).closest("div")?.parentElement; @@ -115,7 +131,7 @@ describe("RoutingGroupsTable", () => { it("should expand only the clicked group", async () => { const user = userEvent.setup(); - render(); + renderWithProviders(); await user.click(screen.getByRole("button", { name: "dev-group" })); expect(await screen.findAllByText("How routing works for this group")).toHaveLength(1); @@ -124,7 +140,7 @@ describe("RoutingGroupsTable", () => { it("should edit a group through the actions menu", async () => { const user = userEvent.setup(); - render(); + renderWithProviders(); await user.click(screen.getByTestId("routing-group-actions-prod-group")); await user.click(await screen.findByTestId("routing-group-action-edit")); expect(onEdit).toHaveBeenCalledWith(prodGroup); @@ -132,14 +148,139 @@ describe("RoutingGroupsTable", () => { it("should delete a group through the actions menu", async () => { const user = userEvent.setup(); - render(); + renderWithProviders(); await user.click(screen.getByTestId("routing-group-actions-prod-group")); await user.click(await screen.findByTestId("routing-group-action-delete")); expect(onDelete).toHaveBeenCalledWith(prodGroup); }); it("should show skeleton rows instead of the empty state while loading", () => { - render(); + renderWithProviders(); expect(screen.queryByText("No routing groups yet")).not.toBeInTheDocument(); }); + + describe("URL state", () => { + it("sorts by the column and direction in the URL", () => { + renderWithProviders(, { + searchParams: "?sort_by=routing_strategy&sort_order=desc", + }); + expect(namesInOrder()).toEqual(["prod-group", "dev-group"]); + }); + + it("ignores a sort_by that is not a sortable column instead of handing it to the table", () => { + const consoleError = vi.spyOn(console, "error").mockImplementation(() => {}); + try { + renderWithProviders(, { + searchParams: "?sort_by=bogus&sort_order=desc", + }); + expect(namesInOrder()).toEqual(["prod-group", "dev-group"]); + expect(consoleError).not.toHaveBeenCalledWith(expect.stringContaining("Column with id 'bogus' does not exist")); + } finally { + consoleError.mockRestore(); + } + }); + + it("writes header sort clicks to sort_by and sort_order", async () => { + const user = userEvent.setup(); + const onUrlUpdate = vi.fn(); + renderWithProviders(, { onUrlUpdate }); + + await user.click(screen.getByTestId("sort-header-group_name")); + expect(lastUrl(onUrlUpdate)?.get("sort_by")).toBe("group_name"); + expect(lastUrl(onUrlUpdate)?.has("sort_order")).toBe(false); + + await user.click(screen.getByTestId("sort-header-group_name")); + expect(lastUrl(onUrlUpdate)?.get("sort_by")).toBe("group_name"); + expect(lastUrl(onUrlUpdate)?.get("sort_order")).toBe("desc"); + expect(namesInOrder()).toEqual(["prod-group", "dev-group"]); + }); + + it("shows the page and page size from the URL", () => { + renderWithProviders(, { + searchParams: "?page=2", + }); + expect(namesInOrder()).toEqual(groupNames.slice(25)); + expect(screen.getByTestId("pagination-page")).toHaveTextContent("Page 2 of 2"); + }); + + it("shows every row when page_size in the URL covers them", () => { + renderWithProviders(, { + searchParams: "?page_size=50", + }); + expect(namesInOrder()).toEqual(groupNames); + expect(screen.getByTestId("pagination-page")).toHaveTextContent("Page 1 of 1"); + }); + + it("opens an expanded group that sits on the page named in the URL", async () => { + renderWithProviders(, { + searchParams: "?page=2&expanded=group-27", + }); + expect(await screen.findAllByText("How routing works for this group")).toHaveLength(1); + expect(namesInOrder()).toContain("group-27"); + }); + + it("writes page changes to page and drops it on the first page", async () => { + const user = userEvent.setup(); + const onUrlUpdate = vi.fn(); + renderWithProviders(, { onUrlUpdate }); + + await user.click(screen.getByRole("button", { name: "Go to next page" })); + expect(lastUrl(onUrlUpdate)?.get("page")).toBe("2"); + expect(namesInOrder()).toEqual(groupNames.slice(25)); + + await user.click(screen.getByRole("button", { name: "Go to previous page" })); + expect(lastUrl(onUrlUpdate)?.has("page")).toBe(false); + expect(namesInOrder()).toEqual(groupNames.slice(0, 25)); + }); + + it("returns to the first page when the sort changes", async () => { + const user = userEvent.setup(); + const onUrlUpdate = vi.fn(); + renderWithProviders(, { + searchParams: "?page=2", + onUrlUpdate, + }); + + await user.click(screen.getByTestId("sort-header-group_name")); + expect(lastUrl(onUrlUpdate)?.get("sort_by")).toBe("group_name"); + expect(lastUrl(onUrlUpdate)?.has("page")).toBe(false); + expect(screen.getByTestId("pagination-page")).toHaveTextContent("Page 1 of 2"); + }); + + it("opens the usage panel for every group named in expanded", async () => { + renderWithProviders(, { + searchParams: "?expanded=dev-group", + }); + expect(await screen.findAllByText("How routing works for this group")).toHaveLength(1); + const rowSequence = screen + .getAllByRole("row") + .slice(1) + .map( + (row) => + row.getAttribute("data-row-id") ?? + (within(row).queryByText("How routing works for this group") ? "usage-panel" : "other"), + ); + expect(rowSequence).toEqual(["prod-group", "dev-group", "usage-panel"]); + }); + + it("adds and removes group names in expanded as panels are toggled", async () => { + const user = userEvent.setup(); + const onUrlUpdate = vi.fn(); + renderWithProviders(, { onUrlUpdate }); + + await user.click(screen.getByRole("button", { name: "prod-group" })); + expect(lastUrl(onUrlUpdate)?.get("expanded")).toBe("prod-group"); + + await user.click(screen.getByRole("button", { name: "dev-group" })); + expect(lastUrl(onUrlUpdate)?.get("expanded")).toBe("prod-group,dev-group"); + expect(await screen.findAllByText("How routing works for this group")).toHaveLength(2); + + await user.click(screen.getByRole("button", { name: "prod-group" })); + expect(lastUrl(onUrlUpdate)?.get("expanded")).toBe("dev-group"); + + await user.click(screen.getByRole("button", { name: "dev-group" })); + expect(lastUrl(onUrlUpdate)?.has("expanded")).toBe(false); + expect(screen.queryByText("How routing works for this group")).not.toBeInTheDocument(); + }); + }); }); diff --git a/ui/litellm-dashboard/src/components/routing_groups/RoutingGroupsTable.tsx b/ui/litellm-dashboard/src/components/routing_groups/RoutingGroupsTable.tsx index 1d2ef75361f..eb73a1b7251 100644 --- a/ui/litellm-dashboard/src/components/routing_groups/RoutingGroupsTable.tsx +++ b/ui/litellm-dashboard/src/components/routing_groups/RoutingGroupsTable.tsx @@ -1,14 +1,25 @@ "use client"; -import type { ExpandedState, SortingState } from "@tanstack/react-table"; +import { + functionalUpdate, + type ExpandedState, + type OnChangeFn, + type PaginationState, + type SortingState, +} from "@tanstack/react-table"; import { Inbox } from "lucide-react"; -import React, { useCallback, useMemo, useState } from "react"; +import React, { useCallback, useMemo } from "react"; import { DataTable } from "@/components/shared/DataTable"; import { RoutingGroupUsagePanel } from "./RoutingGroupUsagePanel"; import { getRoutingGroupsTableColumns } from "./RoutingGroupsTableColumns"; import type { RoutingGroup } from "./types"; +import { + ROUTING_GROUP_SORT_COLUMNS, + useExpandedRoutingGroups, + useRoutingGroupsTableUrlState, +} from "./routingGroupsUrlState"; interface RoutingGroupsTableProps { groups: RoutingGroup[]; @@ -45,16 +56,60 @@ const RoutingGroupsTable: React.FC = ({ onDelete, proxyBaseUrl, }) => { - const [sorting, setSorting] = useState([]); - const [expanded, setExpanded] = useState({}); + const [{ sort_by: sortBy, sort_order: sortOrder, page, page_size: pageSize }, setTableState] = + useRoutingGroupsTableUrlState(); + const [expandedGroups, setExpandedGroups] = useExpandedRoutingGroups(); const baseUrl = resolveBaseUrl(proxyBaseUrl); - const toggleUsage = useCallback((group: RoutingGroup) => { - setExpanded((previous) => { - const current = previous === true ? {} : previous; - return { ...current, [group.group_name]: current[group.group_name] !== true }; - }); - }, []); + const sorting = useMemo( + () => (sortBy ? [{ id: sortBy, desc: sortOrder === "desc" }] : []), + [sortBy, sortOrder], + ); + + const onSortingChange = useCallback>( + (updaterOrValue) => { + const active = functionalUpdate(updaterOrValue, sorting)[0]; + const column = ROUTING_GROUP_SORT_COLUMNS.find((id) => id === active?.id) ?? null; + void setTableState({ sort_by: column, sort_order: column && active?.desc ? "desc" : null, page: null }); + }, + [setTableState, sorting], + ); + + const pagination = useMemo(() => ({ pageIndex: page - 1, pageSize }), [page, pageSize]); + + const onPaginationChange = useCallback>( + (updaterOrValue) => { + const next = functionalUpdate(updaterOrValue, pagination); + void setTableState({ page: next.pageIndex + 1, page_size: next.pageSize }); + }, + [pagination, setTableState], + ); + + const expanded = useMemo( + () => Object.fromEntries(expandedGroups.map((name) => [name, true])), + [expandedGroups], + ); + + const onExpandedChange = useCallback>( + (updaterOrValue) => { + const next = functionalUpdate(updaterOrValue, expanded); + void setExpandedGroups( + next === true ? groups.map((group) => group.group_name) : Object.keys(next).filter((name) => next[name]), + ); + }, + [expanded, groups, setExpandedGroups], + ); + + const toggleUsage = useCallback( + (group: RoutingGroup) => { + void setExpandedGroups((previous) => + previous.includes(group.group_name) + ? previous.filter((name) => name !== group.group_name) + : [...previous, group.group_name], + ); + }, + [setExpandedGroups], + ); const columns = useMemo(() => { const deps = { onEdit, onDelete, onToggleUsage: toggleUsage }; @@ -65,13 +120,15 @@ const RoutingGroupsTable: React.FC = ({ group.group_name} sortingMode="client" sorting={sorting} - onSortingChange={setSorting} + onSortingChange={onSortingChange} expanded={expanded} - onExpandedChange={setExpanded} + onExpandedChange={onExpandedChange} getRowCanExpand={() => true} renderSubComponent={({ row }) => } isLoading={isLoading} diff --git a/ui/litellm-dashboard/src/components/routing_groups/index.integration.test.tsx b/ui/litellm-dashboard/src/components/routing_groups/index.integration.test.tsx index ebe45d5c117..2db4aadfc7c 100644 --- a/ui/litellm-dashboard/src/components/routing_groups/index.integration.test.tsx +++ b/ui/litellm-dashboard/src/components/routing_groups/index.integration.test.tsx @@ -1,7 +1,11 @@ -import { render, screen, within } from "@testing-library/react"; +import { fireEvent, render, screen, waitFor, within } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; +import { NuqsTestingAdapter, type OnUrlUpdateFunction } from "nuqs/adapters/testing"; +import type { ReactNode } from "react"; import { beforeEach, describe, expect, it, vi } from "vitest"; +import { renderWithProviders } from "../../../tests/test-utils"; + import RoutingGroups from "./index"; import type { RoutingGroup } from "./types"; import { useRoutingGroups, useSaveRoutingGroups } from "@/app/(dashboard)/hooks/routingGroups/useRoutingGroups"; @@ -46,10 +50,12 @@ const devGroup: RoutingGroup = { routing_strategy: "simple-shuffle", }; -const setup = (overrides: { mutateAsync?: ReturnType; isPending?: boolean } = {}) => { +const setup = ( + overrides: { mutateAsync?: ReturnType; isPending?: boolean; groups?: RoutingGroup[] } = {}, +) => { const mutateAsync = overrides.mutateAsync ?? vi.fn().mockResolvedValue(undefined); vi.mocked(useRoutingGroups).mockReturnValue({ - data: { routingGroups: [prodGroup, devGroup], availableStrategies: [] }, + data: { routingGroups: overrides.groups ?? [prodGroup, devGroup], availableStrategies: [] }, isLoading: false, refetch: vi.fn(), isFetching: false, @@ -61,6 +67,28 @@ const setup = (overrides: { mutateAsync?: ReturnType; isPending?: return { mutateAsync }; }; +const USAGE_PANEL_TITLE = "How routing works for this group"; + +const renderRetainingMountWrites = (searchParams: string, onUrlUpdate: OnUrlUpdateFunction) => + render(, { + wrapper: ({ children }: { children: ReactNode }) => ( + + {children} + + ), + }); + +const visibleGroupNames = () => + screen + .getAllByRole("row") + .map((row) => row.getAttribute("data-row-id")) + .filter((id): id is string => id !== null); + const openDeleteConfirm = async (user: ReturnType) => { await user.click(screen.getByTestId("routing-group-actions-prod-group")); await user.click(await screen.findByTestId("routing-group-action-delete")); @@ -74,7 +102,7 @@ describe("RoutingGroups delete confirmation", () => { it("should not show the confirmation until a group is chosen for deletion", () => { setup(); - render(); + renderWithProviders(); expect(screen.queryByRole("dialog")).not.toBeInTheDocument(); expect(screen.queryByText("Delete routing group?")).not.toBeInTheDocument(); }); @@ -82,7 +110,7 @@ describe("RoutingGroups delete confirmation", () => { it("should name the group being deleted in the confirmation", async () => { const user = userEvent.setup(); setup(); - render(); + renderWithProviders(); const dialog = await openDeleteConfirm(user); @@ -94,7 +122,7 @@ describe("RoutingGroups delete confirmation", () => { it("should save the remaining groups and report success when confirmed", async () => { const user = userEvent.setup(); const { mutateAsync } = setup(); - render(); + renderWithProviders(); const dialog = await openDeleteConfirm(user); await user.click(within(dialog).getByRole("button", { name: "Delete" })); @@ -107,7 +135,7 @@ describe("RoutingGroups delete confirmation", () => { const user = userEvent.setup(); const mutateAsync = vi.fn().mockRejectedValue(new Error("boom")); setup({ mutateAsync }); - render(); + renderWithProviders(); const dialog = await openDeleteConfirm(user); await user.click(within(dialog).getByRole("button", { name: "Delete" })); @@ -119,7 +147,7 @@ describe("RoutingGroups delete confirmation", () => { it("should dismiss without saving when cancelled", async () => { const user = userEvent.setup(); const { mutateAsync } = setup(); - render(); + renderWithProviders(); const dialog = await openDeleteConfirm(user); await user.click(within(dialog).getByRole("button", { name: "Cancel" })); @@ -128,3 +156,88 @@ describe("RoutingGroups delete confirmation", () => { expect(screen.queryByRole("dialog")).not.toBeInTheDocument(); }); }); + +describe("RoutingGroups URL state", () => { + beforeEach(() => { + vi.clearAllMocks(); + setup(); + }); + + it("filters by group_search from the URL", () => { + renderWithProviders(, { searchParams: "?group_search=shuffle" }); + expect(screen.getByPlaceholderText("Search groups...")).toHaveValue("shuffle"); + expect(visibleGroupNames()).toEqual(["dev-group"]); + expect(screen.getByText("Showing 1 result")).toBeInTheDocument(); + }); + + it("writes the search to group_search and removes it when cleared", async () => { + const user = userEvent.setup(); + const onUrlUpdate = vi.fn(); + renderWithProviders(, { onUrlUpdate }); + + fireEvent.change(screen.getByPlaceholderText("Search groups..."), { target: { value: "prod" } }); + await waitFor(() => expect(onUrlUpdate.mock.calls.at(-1)?.[0].searchParams.get("group_search")).toBe("prod")); + expect(visibleGroupNames()).toEqual(["prod-group"]); + + await user.click(screen.getByRole("button", { name: "Clear search" })); + await waitFor(() => expect(onUrlUpdate.mock.calls.at(-1)?.[0].searchParams.has("group_search")).toBe(false)); + expect(visibleGroupNames()).toEqual(["prod-group", "dev-group"]); + }); + + it("returns the table to the first page when the search changes", async () => { + setup({ + groups: Array.from({ length: 30 }, (_, index) => ({ + group_name: `group-${String(index).padStart(2, "0")}`, + models: ["gpt-4o"], + routing_strategy: "simple-shuffle", + })), + }); + const onUrlUpdate = vi.fn(); + renderWithProviders(, { searchParams: "?page=2&sort_by=group_name", onUrlUpdate }); + expect(screen.getByTestId("pagination-page")).toHaveTextContent("Page 2 of 2"); + + fireEvent.change(screen.getByPlaceholderText("Search groups..."), { target: { value: "group" } }); + await waitFor(() => expect(onUrlUpdate.mock.calls.at(-1)?.[0].searchParams.get("group_search")).toBe("group")); + const params = onUrlUpdate.mock.calls.at(-1)?.[0].searchParams; + expect(params?.has("page")).toBe(false); + expect(params?.get("sort_by")).toBe("group_name"); + expect(screen.getByTestId("pagination-page")).toHaveTextContent("Page 1 of 2"); + }); + + it("drops expanded names that no longer match a routing group", async () => { + const onUrlUpdate = vi.fn(); + renderRetainingMountWrites("?expanded=ghost-group,prod-group", onUrlUpdate); + + expect(await screen.findAllByText(USAGE_PANEL_TITLE)).toHaveLength(1); + await waitFor(() => expect(onUrlUpdate).toHaveBeenCalled()); + expect(onUrlUpdate.mock.calls.at(-1)?.[0].searchParams.get("expanded")).toBe("prod-group"); + }); + + it("keeps an expanded group that is only hidden by the search", async () => { + const user = userEvent.setup(); + const onUrlUpdate = vi.fn(); + renderRetainingMountWrites("?expanded=prod-group&group_search=dev", onUrlUpdate); + + expect(visibleGroupNames()).toEqual(["dev-group"]); + expect(screen.queryByText(USAGE_PANEL_TITLE)).not.toBeInTheDocument(); + + await user.click(screen.getByRole("button", { name: "Clear search" })); + + expect(await screen.findAllByText(USAGE_PANEL_TITLE)).toHaveLength(1); + expect(onUrlUpdate.mock.calls.at(-1)?.[0].searchParams.get("expanded")).toBe("prod-group"); + }); + + it("leaves expanded names alone until the routing groups have loaded", async () => { + vi.mocked(useRoutingGroups).mockReturnValue({ + data: undefined, + isLoading: true, + refetch: vi.fn(), + isFetching: true, + } as unknown as ReturnType); + const onUrlUpdate = vi.fn(); + renderRetainingMountWrites("?expanded=prod-group", onUrlUpdate); + + await new Promise((resolve) => setTimeout(resolve, 100)); + expect(onUrlUpdate).not.toHaveBeenCalled(); + }); +}); diff --git a/ui/litellm-dashboard/src/components/routing_groups/index.tsx b/ui/litellm-dashboard/src/components/routing_groups/index.tsx index 7da581be6a9..20c9fbbc52d 100644 --- a/ui/litellm-dashboard/src/components/routing_groups/index.tsx +++ b/ui/litellm-dashboard/src/components/routing_groups/index.tsx @@ -1,6 +1,6 @@ "use client"; -import React, { useMemo, useState } from "react"; +import React, { useEffect, useMemo, useState } from "react"; import { Button } from "@/components/ui/button"; import { Card, CardContent } from "@/components/ui/card"; import { InputGroup, InputGroupAddon, InputGroupButton, InputGroupInput } from "@/components/ui/input-group"; @@ -14,6 +14,7 @@ import RoutingGroupsTable from "./RoutingGroupsTable"; import RoutingGroupModal from "./RoutingGroupModal"; import { toast } from "@/lib/toast"; import type { RoutingGroup } from "./types"; +import { useExpandedRoutingGroups, useRoutingGroupsSearchUrlState } from "./routingGroupsUrlState"; import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog"; const RoutingGroups: React.FC = () => { @@ -24,13 +25,21 @@ const RoutingGroups: React.FC = () => { const proxySettings = useProxySettings(accessToken); const saveMutation = useSaveRoutingGroups(); - const [searchQuery, setSearchQuery] = useState(""); + const [{ group_search: searchQuery }, setSearchState] = useRoutingGroupsSearchUrlState(); + const [expandedGroups, setExpandedGroups] = useExpandedRoutingGroups(); const [drawerOpen, setDrawerOpen] = useState(false); const [drawerMode, setDrawerMode] = useState<"create" | "edit">("create"); const [editingGroup, setEditingGroup] = useState(null); const [deletingGroup, setDeletingGroup] = useState(null); - const groups = data?.routingGroups ?? []; + const loadedGroups = data?.routingGroups; + const groups = loadedGroups ?? []; + + useEffect(() => { + if (!loadedGroups) return; + const existing = expandedGroups.filter((name) => loadedGroups.some((group) => group.group_name === name)); + if (existing.length !== expandedGroups.length) void setExpandedGroups(existing); + }, [loadedGroups, expandedGroups, setExpandedGroups]); const filteredGroups = useMemo(() => { const q = searchQuery.trim().toLowerCase(); @@ -57,6 +66,8 @@ const RoutingGroups: React.FC = () => { return Array.from(new Set(names)); }, [modelHub]); + const setSearchQuery = (value: string) => void setSearchState({ group_search: value || null, page: null }); + const openCreate = () => { setDrawerMode("create"); setEditingGroup(null); diff --git a/ui/litellm-dashboard/src/components/routing_groups/routingGroupsUrlState.ts b/ui/litellm-dashboard/src/components/routing_groups/routingGroupsUrlState.ts new file mode 100644 index 00000000000..99b92175d31 --- /dev/null +++ b/ui/litellm-dashboard/src/components/routing_groups/routingGroupsUrlState.ts @@ -0,0 +1,42 @@ +import { + createParser, + parseAsArrayOf, + parseAsInteger, + parseAsString, + parseAsStringLiteral, + useQueryState, + useQueryStates, +} from "nuqs"; + +export const ROUTING_GROUP_SORT_COLUMNS = ["group_name", "routing_strategy"] as const; + +const boundedInteger = (min: number, max: number) => + createParser({ + parse: (value: string) => { + const parsed = parseAsInteger.parse(value); + return parsed === null ? null : Math.min(Math.max(parsed, min), max); + }, + serialize: String, + }); + +const pageParser = boundedInteger(1, 100_000).withDefault(1); + +const TABLE_URL_STATE = { + sort_by: parseAsStringLiteral(ROUTING_GROUP_SORT_COLUMNS), + sort_order: parseAsStringLiteral(["asc", "desc"] as const).withDefault("asc"), + page: pageParser, + page_size: boundedInteger(1, 100).withDefault(25), +}; + +const SEARCH_URL_STATE = { + group_search: parseAsString.withDefault(""), + page: pageParser, +}; + +const expandedGroupsParser = parseAsArrayOf(parseAsString).withDefault([]); + +export const useRoutingGroupsTableUrlState = () => useQueryStates(TABLE_URL_STATE); + +export const useRoutingGroupsSearchUrlState = () => useQueryStates(SEARCH_URL_STATE); + +export const useExpandedRoutingGroups = () => useQueryState("expanded", expandedGroupsParser); diff --git a/ui/litellm-dashboard/src/components/settings.test.tsx b/ui/litellm-dashboard/src/components/settings.test.tsx index 4ba5dd23fd1..b70d40b9ccb 100644 --- a/ui/litellm-dashboard/src/components/settings.test.tsx +++ b/ui/litellm-dashboard/src/components/settings.test.tsx @@ -1,7 +1,10 @@ import { act, fireEvent, render, screen, waitFor, within } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; +import { NuqsTestingAdapter, type OnUrlUpdateFunction } from "nuqs/adapters/testing"; +import type { ReactNode } from "react"; import { FormProvider, useForm } from "react-hook-form"; import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; +import { renderWithProviders } from "../../tests/test-utils"; import { alertingSettingsCall, getCallbackConfigsCall, getCallbacksCall, setCallbacksCall } from "./networking"; import Settings, { backendCallbackLogoSrc, CallbackSelector } from "./settings"; @@ -77,7 +80,7 @@ describe("Settings", () => { }); it("should render the logging callbacks tab when access token is provided", async () => { - render(); + renderWithProviders(); await waitFor(() => { expect(screen.getByText("Active Logging Callbacks")).toBeInTheDocument(); @@ -85,7 +88,7 @@ describe("Settings", () => { }); it("should display additional settings tabs", async () => { - render(); + renderWithProviders(); await waitFor(() => { expect(screen.getByText("CloudZero Cost Tracking")).toBeInTheDocument(); @@ -96,7 +99,7 @@ describe("Settings", () => { }); it("should load callback configs from the backend when access token is provided", async () => { - render(); + renderWithProviders(); await waitFor(() => { expect(mockGetCallbackConfigsCall).toHaveBeenCalledWith(defaultProps.accessToken); @@ -140,7 +143,7 @@ describe("Settings", () => { ]); const user = userEvent.setup(); - render(); + renderWithProviders(); await waitFor(() => { expect(screen.getByText("Active Logging Callbacks")).toBeInTheDocument(); @@ -250,7 +253,7 @@ describe("Settings", () => { const openOtelEditModal = async () => { const user = userEvent.setup(); - render(); + renderWithProviders(); await user.click(await screen.findByTestId("callback-actions-otel-success")); await user.click(await screen.findByTestId("callback-action-edit")); return user; @@ -333,7 +336,7 @@ describe("Settings", () => { const openS3EditModal = async (callbackName = "s3") => { const user = userEvent.setup(); - render(); + renderWithProviders(); await user.click(await screen.findByTestId(`callback-actions-${callbackName}-success`)); await user.click(await screen.findByTestId("callback-action-edit")); return user; @@ -411,7 +414,7 @@ describe("Settings", () => { it("should send the typed webhook url for an alert type when the alerting tab is saved", async () => { const user = userEvent.setup(); - render(); + renderWithProviders(); await user.click(await screen.findByRole("tab", { name: "Alerting Types" })); @@ -444,7 +447,7 @@ describe("Settings", () => { }), ); - render(); + renderWithProviders(); expect(screen.getAllByTestId("skeleton-row").length).toBeGreaterThan(0); @@ -459,7 +462,7 @@ describe("Settings", () => { }); it("should resolve loading without fetching when the user id is missing", async () => { - render(); + renderWithProviders(); await waitFor(() => { expect(screen.queryByTestId("skeleton-row")).not.toBeInTheDocument(); @@ -468,8 +471,188 @@ describe("Settings", () => { expect(screen.getByText("No callbacks configured")).toBeInTheDocument(); }); + describe("URL state", () => { + const langfuseVariables = (host: string) => ({ + LANGFUSE_PUBLIC_KEY: "test-public-key", + LANGFUSE_SECRET_KEY: "test-secret-key", + LANGFUSE_HOST: host, + SLACK_WEBHOOK_URL: null, + OPENMETER_API_KEY: null, + }); + + const mockLangfuseRows = () => { + mockGetCallbacksCall.mockResolvedValue({ + callbacks: [ + { name: "langfuse", type: "success", variables: langfuseVariables("https://success.langfuse.com") }, + { name: "langfuse", type: "failure", variables: langfuseVariables("https://failure.langfuse.com") }, + { name: "datadog", type: "success", variables: langfuseVariables(""), read_only: true }, + ], + available_callbacks: {}, + alerts: [], + }); + mockGetCallbackConfigsCall.mockResolvedValue([ + { + id: "langfuse", + displayName: "Langfuse", + dynamic_params: { + LANGFUSE_PUBLIC_KEY: { type: "text", ui_name: "Public Key", required: true }, + LANGFUSE_SECRET_KEY: { type: "password", ui_name: "Secret Key", required: true }, + LANGFUSE_HOST: { type: "text", ui_name: "Host", required: false }, + }, + }, + ]); + }; + + const renderRetainingMountWrites = (searchParams: string, onUrlUpdate: OnUrlUpdateFunction) => + render(, { + wrapper: ({ children }: { children: ReactNode }) => ( + + {children} + + ), + }); + + const lastUrl = (onUrlUpdate: ReturnType>) => onUrlUpdate.mock.calls.at(-1)?.[0]; + + it("opens the tab named in the URL", async () => { + renderWithProviders(, { searchParams: "?tab=ms-teams-alerts" }); + expect(await screen.findByRole("tab", { name: "MS Teams Alerts" })).toHaveAttribute("aria-selected", "true"); + expect(screen.getByRole("tab", { name: "Logging Callbacks" })).toHaveAttribute("aria-selected", "false"); + }); + + it("writes the clicked tab to the URL and drops it for Logging Callbacks", async () => { + const user = userEvent.setup(); + const onUrlUpdate = vi.fn(); + renderWithProviders(, { onUrlUpdate }); + + await user.click(await screen.findByRole("tab", { name: "Alerting Settings" })); + expect(lastUrl(onUrlUpdate)?.searchParams.get("tab")).toBe("alerting-settings"); + expect(screen.getByRole("tab", { name: "Alerting Settings" })).toHaveAttribute("aria-selected", "true"); + + await user.click(screen.getByRole("tab", { name: "Logging Callbacks" })); + expect(lastUrl(onUrlUpdate)?.searchParams.has("tab")).toBe(false); + }); + + it("selects every tab that is clicked", async () => { + const user = userEvent.setup(); + renderWithProviders(); + + const tabs = await screen.findAllByRole("tab"); + expect(tabs).toHaveLength(6); + for (const tab of tabs) { + await user.click(tab); + expect(tab).toHaveAttribute("aria-selected", "true"); + } + }); + + it("falls back to Logging Callbacks for an unknown tab in the URL", async () => { + renderWithProviders(, { searchParams: "?tab=billing" }); + expect(await screen.findByRole("tab", { name: "Logging Callbacks" })).toHaveAttribute("aria-selected", "true"); + }); + + it("opens the edit dialog for the callback and mode in the URL", async () => { + mockLangfuseRows(); + renderWithProviders(, { searchParams: "?callback=langfuse&callback_mode=failure" }); + + const dialog = await screen.findByRole("dialog", { name: "Edit Callback Settings" }); + await waitFor(() => expect(within(dialog).getByLabelText("Host")).toHaveValue("https://failure.langfuse.com")); + }); + + it("treats a callback link without a mode as the success registration", async () => { + mockLangfuseRows(); + renderWithProviders(, { searchParams: "?callback=langfuse" }); + + const dialog = await screen.findByRole("dialog", { name: "Edit Callback Settings" }); + await waitFor(() => expect(within(dialog).getByLabelText("Host")).toHaveValue("https://success.langfuse.com")); + }); + + it("pushes the edited callback and its mode into the URL", async () => { + mockLangfuseRows(); + const user = userEvent.setup(); + const onUrlUpdate = vi.fn(); + renderWithProviders(, { onUrlUpdate }); + + await user.click(await screen.findByTestId("callback-actions-langfuse-failure")); + await user.click(await screen.findByTestId("callback-action-edit")); + + expect(lastUrl(onUrlUpdate)?.searchParams.get("callback")).toBe("langfuse"); + expect(lastUrl(onUrlUpdate)?.searchParams.get("callback_mode")).toBe("failure"); + expect(lastUrl(onUrlUpdate)?.options.history).toBe("push"); + const dialog = await screen.findByRole("dialog", { name: "Edit Callback Settings" }); + await waitFor(() => expect(within(dialog).getByLabelText("Host")).toHaveValue("https://failure.langfuse.com")); + }); + + it("clears the callback from the URL when the edit dialog is cancelled", async () => { + mockLangfuseRows(); + const user = userEvent.setup(); + const onUrlUpdate = vi.fn(); + renderWithProviders(, { + searchParams: "?callback=langfuse&callback_mode=failure", + onUrlUpdate, + }); + + const dialog = await screen.findByRole("dialog", { name: "Edit Callback Settings" }); + await user.click(within(dialog).getByRole("button", { name: "Cancel" })); + + expect(lastUrl(onUrlUpdate)?.searchParams.has("callback")).toBe(false); + expect(lastUrl(onUrlUpdate)?.searchParams.has("callback_mode")).toBe(false); + await waitFor(() => + expect(screen.queryByRole("dialog", { name: "Edit Callback Settings" })).not.toBeInTheDocument(), + ); + }); + + it("clears the callback from the URL after a successful save", async () => { + mockLangfuseRows(); + vi.mocked(setCallbacksCall).mockResolvedValue({}); + const user = userEvent.setup(); + const onUrlUpdate = vi.fn(); + renderWithProviders(, { searchParams: "?callback=langfuse", onUrlUpdate }); + + const dialog = await screen.findByRole("dialog", { name: "Edit Callback Settings" }); + await waitFor(() => expect(within(dialog).getByLabelText("Host")).toHaveValue("https://success.langfuse.com")); + await user.click(within(dialog).getByRole("button", { name: "Save Changes" })); + + await waitFor(() => expect(lastUrl(onUrlUpdate)?.searchParams.has("callback")).toBe(false)); + expect(vi.mocked(setCallbacksCall)).toHaveBeenCalledTimes(1); + }); + + it.each([ + ["an unknown callback", "?callback=missing&tab=email-alerts"], + [ + "a mode the callback is not registered for", + "?callback=langfuse&callback_mode=success_and_failure&tab=email-alerts", + ], + ["a read-only callback", "?callback=datadog&tab=email-alerts"], + ])("drops a link to %s once callbacks load", async (_label, searchParams) => { + mockLangfuseRows(); + const onUrlUpdate = vi.fn(); + renderRetainingMountWrites(searchParams, onUrlUpdate); + + await waitFor(() => expect(onUrlUpdate).toHaveBeenCalled()); + expect(lastUrl(onUrlUpdate)?.searchParams.has("callback")).toBe(false); + expect(lastUrl(onUrlUpdate)?.searchParams.has("callback_mode")).toBe(false); + expect(lastUrl(onUrlUpdate)?.searchParams.get("tab")).toBe("email-alerts"); + expect(lastUrl(onUrlUpdate)?.options.history).toBe("replace"); + expect(screen.queryByRole("dialog", { name: "Edit Callback Settings" })).not.toBeInTheDocument(); + }); + + it("keeps a callback link while callbacks are still loading", async () => { + mockGetCallbacksCall.mockReturnValue(new Promise(() => {})); + const onUrlUpdate = vi.fn(); + renderRetainingMountWrites("?callback=langfuse", onUrlUpdate); + + await new Promise((resolve) => setTimeout(resolve, 100)); + expect(onUrlUpdate).not.toHaveBeenCalled(); + }); + }); + it("should display CloudZero Cost Tracking tab", async () => { - render(); + renderWithProviders(); await waitFor(() => { expect(screen.getByText("Active Logging Callbacks")).toBeInTheDocument(); diff --git a/ui/litellm-dashboard/src/components/settings.tsx b/ui/litellm-dashboard/src/components/settings.tsx index 9247f22ec28..5a262a0e346 100644 --- a/ui/litellm-dashboard/src/components/settings.tsx +++ b/ui/litellm-dashboard/src/components/settings.tsx @@ -18,6 +18,7 @@ import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@ import { Switch } from "@/components/ui/switch"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; +import { useUrlTab } from "@/hooks/useUrlTab"; import EmailSettings from "./email_settings"; import MSTeamsSettings from "./MSTeamsSettings"; import { Logo } from "@/components/molecules/logo/Logo"; @@ -35,6 +36,7 @@ import { } from "./networking"; import { LoggingCallbacksTable } from "./Settings/LoggingAndAlerts/LoggingCallbacks/LoggingCallbacksTable"; import { AlertingObject } from "./Settings/LoggingAndAlerts/LoggingCallbacks/types"; +import { useCallbackEditUrlState } from "./Settings/LoggingAndAlerts/LoggingCallbacks/useCallbackEditUrlState"; import { parseErrorMessage } from "./shared/errorUtils"; interface SettingsPageProps { accessToken: string | null; @@ -47,6 +49,15 @@ type CallbackFormValues = Record; const assetsLogoFolder = "/ui/assets/logos/"; +const LOGGING_AND_ALERTS_TABS = [ + "logging-callbacks", + "cloudzero-cost-tracking", + "alerting-types", + "alerting-settings", + "email-alerts", + "ms-teams-alerts", +] as const; + export const backendCallbackLogoSrc = (logo: string | null | undefined): string | undefined => { if (!logo) return undefined; if (logo.includes("/") || logo.startsWith("data:") || logo.startsWith("http")) return logo; @@ -316,8 +327,12 @@ const Settings: React.FC = ({ accessToken, userRole, userID, const [selectedCallbackParams, setSelectedCallbackParams] = useState([]); - const [showEditCallback, setShowEditCallback] = useState(false); - const [selectedEditCallback, setSelectedEditCallback] = useState(null); + const [activeTab, setActiveTab] = useUrlTab(LOGGING_AND_ALERTS_TABS, "logging-callbacks"); + const { + editingCallback: selectedEditCallback, + openEdit: openEditCallback, + closeEdit: closeEditCallback, + } = useCallbackEditUrlState(callbacks, isLoadingCallbacks); const [showDeleteConfirmModal, setShowDeleteConfirmModal] = useState(false); const [callbackToDelete, setCallbackToDelete] = useState(null); const [isUpdatingCallback, setIsUpdatingCallback] = useState(false); @@ -338,7 +353,7 @@ const Settings: React.FC = ({ accessToken, userRole, userID, }, [accessToken]); useEffect(() => { - if (showEditCallback && selectedEditCallback) { + if (selectedEditCallback) { const params = getDynamicParamsForCallback( selectedEditCallback.name, callbackConfigs, @@ -354,7 +369,7 @@ const Settings: React.FC = ({ accessToken, userRole, userID, callback: selectedEditCallback.name, }); } - }, [showEditCallback, selectedEditCallback, editForm, callbackConfigs]); + }, [selectedEditCallback, editForm, callbackConfigs]); const handleSwitchChange = (alertName: string) => { if (activeAlerts.includes(alertName)) { @@ -432,9 +447,8 @@ const Settings: React.FC = ({ accessToken, userRole, userID, toast.success(isEdit ? "Callback updated successfully" : `Callback ${callbackName} added successfully`); if (isEdit) { - setShowEditCallback(false); + closeEditCallback(); editForm.reset(); - setSelectedEditCallback(null); } else { setShowAddCallbacksModal(false); addForm.reset(); @@ -491,8 +505,7 @@ const Settings: React.FC = ({ accessToken, userRole, userID, }; const closeEditCallbackModal = () => { - setShowEditCallback(false); - setSelectedEditCallback(null); + closeEditCallback(); editForm.reset(); }; @@ -561,7 +574,7 @@ const Settings: React.FC = ({ accessToken, userRole, userID, return (
- + Logging Callbacks CloudZero Cost Tracking @@ -576,10 +589,7 @@ const Settings: React.FC = ({ accessToken, userRole, userID, availableCallbacks={allCallbacks} isLoading={isLoadingCallbacks} onAdd={() => setShowAddCallbacksModal(true)} - onEdit={(cb) => { - setSelectedEditCallback(cb); - setShowEditCallback(true); - }} + onEdit={openEditCallback} onDelete={(cb) => handleDeleteCallback(cb)} onTest={async (cb) => { try { @@ -735,7 +745,7 @@ const Settings: React.FC = ({ accessToken, userRole, userID, - !open && closeEditCallbackModal()}> + !open && closeEditCallbackModal()}> Edit Callback Settings