mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-16 23:41:43 +00:00
refactor(ui): adopt shared tab-routing helpers + anchor TabRouteBar in Logs
Replace the page's hand-written tabRoutes.ts and layout routing engine with createTabRoutes + useTabRouting + the shared <TabRouteBar>, keeping the credentials loading-spinner guard inline. The per-page tabRoutes.test.ts is dropped in favor of the central factory test.
This commit is contained in:
parent
fcbdb3b655
commit
92003610c3
6 changed files with 127 additions and 107 deletions
|
|
@ -0,0 +1,56 @@
|
|||
/* @vitest-environment jsdom */
|
||||
import { render } from "@testing-library/react";
|
||||
import userEvent from "@testing-library/user-event";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const { mockPush } = vi.hoisted(() => ({ mockPush: vi.fn() }));
|
||||
vi.mock("next/navigation", () => ({ useRouter: () => ({ push: mockPush }) }));
|
||||
vi.mock("@/components/networking", () => ({ serverRootPath: "" }));
|
||||
|
||||
import { createTabRoutes } from "@/utils/tabRoutes";
|
||||
import { TabRouteBar } from "./TabRouteBar";
|
||||
|
||||
const routes = createTabRoutes("caching", ["health", "settings"] as const);
|
||||
const TABS = [
|
||||
{ key: "analytics", label: "Cache Analytics" },
|
||||
{ key: "health", label: "Cache Health" },
|
||||
{ key: "settings", label: "Cache Settings" },
|
||||
];
|
||||
|
||||
const renderBar = (activeKey = "analytics") =>
|
||||
render(<TabRouteBar routes={routes} baseTabKey="analytics" activeKey={activeKey} tabs={TABS} />);
|
||||
|
||||
describe("TabRouteBar", () => {
|
||||
beforeEach(() => {
|
||||
mockPush.mockClear();
|
||||
});
|
||||
|
||||
it("renders each tab as an anchor with its trailing-slash href (base tab maps to the root)", () => {
|
||||
const { getByRole } = renderBar();
|
||||
expect(getByRole("tab", { name: "Cache Analytics" })).toHaveAttribute("href", "/ui/caching/");
|
||||
expect(getByRole("tab", { name: "Cache Health" })).toHaveAttribute("href", "/ui/caching/health/");
|
||||
expect(getByRole("tab", { name: "Cache Settings" })).toHaveAttribute("href", "/ui/caching/settings/");
|
||||
});
|
||||
|
||||
it("marks the active tab selected from activeKey", () => {
|
||||
const { getByRole } = renderBar("health");
|
||||
expect(getByRole("tab", { name: "Cache Health" })).toHaveAttribute("aria-selected", "true");
|
||||
expect(getByRole("tab", { name: "Cache Analytics" })).toHaveAttribute("aria-selected", "false");
|
||||
});
|
||||
|
||||
it("soft-navigates on a plain left click (preventing the full-page anchor load)", async () => {
|
||||
const user = userEvent.setup();
|
||||
const { getByRole } = renderBar();
|
||||
await user.click(getByRole("tab", { name: "Cache Health" }));
|
||||
expect(mockPush).toHaveBeenCalledWith("/ui/caching/health/");
|
||||
});
|
||||
|
||||
it("lets the browser handle a modifier-click so open-in-new-tab works", async () => {
|
||||
const user = userEvent.setup();
|
||||
const { getByRole } = renderBar();
|
||||
await user.keyboard("[ControlLeft>]");
|
||||
await user.click(getByRole("tab", { name: "Cache Health" }));
|
||||
await user.keyboard("[/ControlLeft]");
|
||||
expect(mockPush).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
"use client";
|
||||
|
||||
import type { MouseEvent } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs";
|
||||
import type { TabRoutes } from "@/utils/tabRoutes";
|
||||
|
||||
export interface TabRouteItem {
|
||||
key: string;
|
||||
label: string;
|
||||
}
|
||||
|
||||
interface TabRouteBarProps {
|
||||
routes: Pick<TabRoutes<string>, "tabHref">;
|
||||
baseTabKey: string;
|
||||
activeKey: string;
|
||||
tabs: readonly TabRouteItem[];
|
||||
className?: string;
|
||||
}
|
||||
|
||||
export function TabRouteBar({ routes, baseTabKey, activeKey, tabs, className }: TabRouteBarProps) {
|
||||
const router = useRouter();
|
||||
|
||||
const navigate = (href: string) => (event: MouseEvent<HTMLAnchorElement>) => {
|
||||
const commandModifier = event.metaKey || event.ctrlKey;
|
||||
const otherModifier = event.shiftKey || event.altKey;
|
||||
if (commandModifier || otherModifier) {
|
||||
return;
|
||||
}
|
||||
event.preventDefault();
|
||||
router.push(href);
|
||||
};
|
||||
|
||||
return (
|
||||
<Tabs value={activeKey} className={className}>
|
||||
<TabsList variant="line">
|
||||
{tabs.map(({ key, label }) => {
|
||||
const href = routes.tabHref(key === baseTabKey ? "" : key);
|
||||
return (
|
||||
<TabsTrigger key={key} value={key} render={<a href={href} onClick={navigate(href)} />}>
|
||||
{label}
|
||||
</TabsTrigger>
|
||||
);
|
||||
})}
|
||||
</TabsList>
|
||||
</Tabs>
|
||||
);
|
||||
}
|
||||
|
|
@ -39,7 +39,7 @@ describe("LogsLayout", () => {
|
|||
};
|
||||
});
|
||||
|
||||
it("renders the four log tabs and the active tab's page content", () => {
|
||||
it("renders the four tabs and the active tab's page content", () => {
|
||||
const { getByRole, getByTestId } = renderLayout();
|
||||
for (const name of ["Request Logs", "Audit Logs", "Deleted Keys", "Deleted Teams"]) {
|
||||
expect(getByRole("tab", { name })).toBeInTheDocument();
|
||||
|
|
@ -50,24 +50,13 @@ describe("LogsLayout", () => {
|
|||
it("marks the base route's Request Logs tab active", () => {
|
||||
const { getByRole } = renderLayout();
|
||||
expect(getByRole("tab", { name: "Request Logs" })).toHaveAttribute("aria-selected", "true");
|
||||
expect(getByRole("tab", { name: "Audit Logs" })).toHaveAttribute("aria-selected", "false");
|
||||
});
|
||||
|
||||
it("navigates to a tab's path when its tab is clicked", async () => {
|
||||
it("derives the active tab from a nested pathname", () => {
|
||||
navState.pathname = "/ui/logs/audit";
|
||||
const { getByRole } = renderLayout();
|
||||
await act(async () => {
|
||||
getByRole("tab", { name: "Audit Logs" }).click();
|
||||
});
|
||||
expect(mockPush).toHaveBeenCalledWith(expect.stringMatching(/\/logs\/audit\/$/));
|
||||
});
|
||||
|
||||
it("routes the base tab back to the logs root (no slug)", async () => {
|
||||
navState.pathname = "/logs/audit";
|
||||
const { getByRole } = renderLayout();
|
||||
await act(async () => {
|
||||
getByRole("tab", { name: "Request Logs" }).click();
|
||||
});
|
||||
expect(mockPush).toHaveBeenCalledWith(expect.stringMatching(/\/logs\/$/));
|
||||
expect(getByRole("tab", { name: "Audit Logs" })).toHaveAttribute("aria-selected", "true");
|
||||
expect(getByRole("tab", { name: "Request Logs" })).toHaveAttribute("aria-selected", "false");
|
||||
});
|
||||
|
||||
it("redirects to the base logs path when the tab slug is unknown", async () => {
|
||||
|
|
|
|||
|
|
@ -1,38 +1,28 @@
|
|||
"use client";
|
||||
|
||||
import type { ReactNode } from "react";
|
||||
import { useEffect } from "react";
|
||||
import { usePathname, useRouter } from "next/navigation";
|
||||
import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs";
|
||||
import { AntDLoadingSpinner } from "@/components/ui/AntDLoadingSpinner";
|
||||
import useAuthorized from "@/app/(dashboard)/hooks/useAuthorized";
|
||||
import { logsTabHref, slugFromPathname, type LogsTabSlug } from "@/app/(dashboard)/logs/tabRoutes";
|
||||
import { logsRoutes } from "@/app/(dashboard)/logs/tabRoutes";
|
||||
import { useTabRouting } from "@/app/(dashboard)/hooks/useTabRouting";
|
||||
import { TabRouteBar } from "@/app/(dashboard)/components/TabRouteBar";
|
||||
|
||||
const BASE_TAB_KEY = "request-logs";
|
||||
|
||||
const ORDERED_KEYS: Array<"" | LogsTabSlug> = ["", "audit", "deleted-keys", "deleted-teams"];
|
||||
|
||||
const TAB_LABELS: Record<"" | LogsTabSlug, string> = {
|
||||
"": "Request Logs",
|
||||
audit: "Audit Logs",
|
||||
"deleted-keys": "Deleted Keys",
|
||||
"deleted-teams": "Deleted Teams",
|
||||
};
|
||||
const TABS = [
|
||||
{ key: BASE_TAB_KEY, label: "Request Logs" },
|
||||
{ key: "audit", label: "Audit Logs" },
|
||||
{ key: "deleted-keys", label: "Deleted Keys" },
|
||||
{ key: "deleted-teams", label: "Deleted Teams" },
|
||||
] as const;
|
||||
|
||||
export default function LogsLayout({ children }: { children: ReactNode }) {
|
||||
const { accessToken, token, userRole, userId } = useAuthorized();
|
||||
const pathname = usePathname();
|
||||
const router = useRouter();
|
||||
|
||||
const activeSlug = slugFromPathname(pathname);
|
||||
const isKnownSlug = ORDERED_KEYS.some((slug) => slug === activeSlug);
|
||||
const activeKey = isKnownSlug ? activeSlug || BASE_TAB_KEY : BASE_TAB_KEY;
|
||||
|
||||
useEffect(() => {
|
||||
if (activeSlug !== "" && !isKnownSlug) {
|
||||
window.location.replace(logsTabHref(""));
|
||||
}
|
||||
}, [activeSlug, isKnownSlug]);
|
||||
const { activeKey } = useTabRouting({
|
||||
routes: logsRoutes,
|
||||
baseTabKey: BASE_TAB_KEY,
|
||||
visibleKeys: logsRoutes.slugs,
|
||||
});
|
||||
|
||||
const hasCredentials = Boolean(accessToken && token);
|
||||
const hasIdentity = Boolean(userRole && userId);
|
||||
|
|
@ -47,16 +37,7 @@ export default function LogsLayout({ children }: { children: ReactNode }) {
|
|||
|
||||
return (
|
||||
<div className="w-full p-6 overflow-x-hidden box-border">
|
||||
<Tabs value={activeKey} onValueChange={(key) => router.push(logsTabHref(key === BASE_TAB_KEY ? "" : key))}>
|
||||
<TabsList variant="line">
|
||||
{ORDERED_KEYS.map((slug) => (
|
||||
<TabsTrigger key={slug || BASE_TAB_KEY} value={slug || BASE_TAB_KEY}>
|
||||
{TAB_LABELS[slug]}
|
||||
</TabsTrigger>
|
||||
))}
|
||||
</TabsList>
|
||||
</Tabs>
|
||||
|
||||
<TabRouteBar routes={logsRoutes} baseTabKey={BASE_TAB_KEY} activeKey={activeKey} tabs={TABS} />
|
||||
<div className="mt-4">{children}</div>
|
||||
</div>
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,38 +0,0 @@
|
|||
/* @vitest-environment jsdom */
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
vi.mock("@/components/networking", () => ({ serverRootPath: "" }));
|
||||
|
||||
import { LOGS_TAB_SLUGS, logsTabHref, slugFromPathname } from "./tabRoutes";
|
||||
|
||||
describe("slugFromPathname", () => {
|
||||
it("returns empty string for the base path with or without a trailing slash", () => {
|
||||
expect(slugFromPathname("/logs")).toBe("");
|
||||
expect(slugFromPathname("/logs/")).toBe("");
|
||||
});
|
||||
|
||||
it("extracts the tab slug from dev and proxy-mounted (/ui) paths", () => {
|
||||
expect(slugFromPathname("/logs/audit")).toBe("audit");
|
||||
expect(slugFromPathname("/ui/logs/deleted-teams/")).toBe("deleted-teams");
|
||||
});
|
||||
|
||||
it("returns the raw segment for an unknown tab so the layout can redirect to base", () => {
|
||||
expect(slugFromPathname("/ui/logs/bogus")).toBe("bogus");
|
||||
});
|
||||
|
||||
it("returns empty string when the logs base segment is not in the path", () => {
|
||||
expect(slugFromPathname("/teams")).toBe("");
|
||||
});
|
||||
});
|
||||
|
||||
describe("logsTabHref", () => {
|
||||
it("builds the trailing-slash base href for the empty slug", () => {
|
||||
expect(logsTabHref("")).toBe("/ui/logs/");
|
||||
});
|
||||
|
||||
it("builds a trailing-slash href for every tab slug (required by static export)", () => {
|
||||
for (const slug of LOGS_TAB_SLUGS) {
|
||||
expect(logsTabHref(slug)).toBe(`/ui/logs/${slug}/`);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
@ -1,21 +1,5 @@
|
|||
import { migratedHref } from "@/utils/migratedPages";
|
||||
import { createTabRoutes } from "@/utils/tabRoutes";
|
||||
|
||||
export const LOGS_BASE_SEGMENT = "logs";
|
||||
export const logsRoutes = createTabRoutes("logs", ["audit", "deleted-keys", "deleted-teams"] as const);
|
||||
|
||||
export const LOGS_TAB_SLUGS = ["audit", "deleted-keys", "deleted-teams"] as const;
|
||||
|
||||
export type LogsTabSlug = (typeof LOGS_TAB_SLUGS)[number];
|
||||
|
||||
export function logsTabHref(slug: string): string {
|
||||
const base = migratedHref(LOGS_BASE_SEGMENT);
|
||||
return slug ? `${base}/${slug}/` : `${base}/`;
|
||||
}
|
||||
|
||||
export function slugFromPathname(pathname: string): string {
|
||||
const parts = pathname.split("/").filter(Boolean);
|
||||
const idx = parts.indexOf(LOGS_BASE_SEGMENT);
|
||||
if (idx === -1) {
|
||||
return "";
|
||||
}
|
||||
return parts[idx + 1] ?? "";
|
||||
}
|
||||
export type LogsTabSlug = (typeof logsRoutes.slugs)[number];
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue