Skip to content

Commit eed19fb

Browse files
committed
slight improvement for Sidebar2
1 parent 6529ed4 commit eed19fb

File tree

2 files changed

+39
-22
lines changed

2 files changed

+39
-22
lines changed

ui/litellm-dashboard/src/app/(dashboard)/components/Sidebar2.tsx

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
"use client";
22

33
import React from "react";
4-
import Link from "next/link";
5-
import { usePathname, useSearchParams } from "next/navigation";
4+
import { usePathname, useSearchParams, useRouter } from "next/navigation";
65
import { Layout, Menu, ConfigProvider } from "antd";
76
import {
87
KeyOutlined,
@@ -62,6 +61,11 @@ const withBase = (relativePath: string) => {
6261
};
6362

6463
const Sidebar2: React.FC<SidebarProps> = ({ accessToken, userRole, defaultSelectedKey, collapsed = false }) => {
64+
const router = useRouter();
65+
const pathname = usePathname();
66+
const searchParams = useSearchParams();
67+
68+
/** ---------- Menu model ---------- */
6569
const menuItems: MenuItem[] = [
6670
{ key: "1", page: "api-keys", label: "Virtual Keys", icon: <KeyOutlined style={{ fontSize: 18 }} /> },
6771
{
@@ -217,9 +221,7 @@ const Sidebar2: React.FC<SidebarProps> = ({ accessToken, userRole, defaultSelect
217221
children: item.children?.filter((child) => !child.roles || child.roles.includes(userRole)),
218222
}));
219223

220-
// Highlight selection based on pathname or ?page=
221-
const pathname = usePathname();
222-
const searchParams = useSearchParams();
224+
/** ---------- Selection state ---------- */
223225
const pageParam = searchParams.get("page") || undefined;
224226

225227
const findMenuItemKey = (page: string): string => {
@@ -244,31 +246,47 @@ const Sidebar2: React.FC<SidebarProps> = ({ accessToken, userRole, defaultSelect
244246
? findMenuItemKey(defaultSelectedKey)
245247
: "1";
246248

247-
// Root-only routing helper: always replace everything after the domain, honoring base path
248-
const rootWithPage = (p: string) => ({
249-
pathname: getBasePath() || "/",
250-
query: { page: p },
251-
});
249+
/** ---------- Navigation helpers (SPA only) ---------- */
250+
// Build a root URL ("/" or "/base/") with an updated ?page=...
251+
const goTo = (p: string) => {
252+
const base = getBasePath() || "/";
253+
const root = base.endsWith("/") ? base : `${base}/`;
254+
const sp = new URLSearchParams(typeof window !== "undefined" ? window.location.search : "");
255+
sp.set("page", p);
256+
// Use Next router for client navigation on the SAME route (no hard fetch)
257+
router.replace(`${root}?${sp.toString()}`, { scroll: false });
258+
};
252259

253-
// Convert to AntD Menu items:
254-
// - "Virtual Keys" routes to "/<BASE>/virtual-keys"
255-
// - All other items (and children) route to "/<BASE>/?page=<page>"
260+
// Keep the /virtual-keys path the same, but avoid route fetches/hard reloads.
261+
const goToVirtualKeys = () => {
262+
const base = getBasePath() || "/";
263+
const root = base.endsWith("/") ? base : `${base}/`;
264+
const sp = new URLSearchParams(typeof window !== "undefined" ? window.location.search : "");
265+
sp.set("page", "api-keys");
266+
267+
// 1) Client transition to the root with ?page=api-keys so the view updates.
268+
router.replace(`${root}?${sp.toString()}`, { scroll: false });
269+
270+
// 2) Cosmetic URL swap to ".../virtual-keys" without navigation (keeps path the same).
271+
const vk = withBase("virtual-keys");
272+
if (typeof window !== "undefined") {
273+
window.history.replaceState(null, "", vk);
274+
}
275+
};
276+
277+
/** ---------- AntD items with onClick handlers ---------- */
256278
const antdItems = filteredMenuItems.map((item) => {
257279
const isVirtualKeys = item.key === "1";
258-
const label = isVirtualKeys ? (
259-
<Link href={withBase("virtual-keys")}>Virtual Keys</Link>
260-
) : (
261-
<Link href={rootWithPage(item.page)}>{item.label}</Link>
262-
);
263-
264280
return {
265281
key: item.key,
266282
icon: item.icon,
267-
label,
283+
label: item.label, // plain text; click handled via onClick
284+
onClick: !item.children ? (isVirtualKeys ? goToVirtualKeys : () => goTo(item.page)) : undefined,
268285
children: item.children?.map((child) => ({
269286
key: child.key,
270287
icon: child.icon,
271-
label: <Link href={rootWithPage(child.page)}>{child.label}</Link>,
288+
label: child.label,
289+
onClick: () => goTo(child.page),
272290
})),
273291
};
274292
});

ui/litellm-dashboard/src/app/page.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import VectorStoreManagement from "@/components/vector_store_management";
3939
import UIThemeSettings from "@/components/ui_theme_settings";
4040
import { UiLoadingSpinner } from "@/components/ui/ui-loading-spinner";
4141
import { cx } from "@/lib/cva.config";
42-
import useFeatureFlags, { FeatureFlagsProvider } from "@/hooks/useFeatureFlags";
4342
import Sidebar2 from "@/app/(dashboard)/components/Sidebar2";
4443
import SidebarProvider from "@/app/(dashboard)/components/SidebarProvider";
4544

0 commit comments

Comments
 (0)