1
1
import { Layout , Menu } from "antd" ;
2
- import { useRouter , usePathname } from "next/navigation" ; // UPDATED: also usePathname to detect current route
2
+ import { usePathname } from "next/navigation" ;
3
3
import {
4
4
KeyOutlined ,
5
5
PlayCircleOutlined ,
@@ -44,23 +44,29 @@ interface MenuItem {
44
44
icon ?: React . ReactNode ;
45
45
}
46
46
47
- /** ---- BASE URL HELPERS (shared pattern across files) ---- */
47
+ /** ---- BASE URL HELPERS ---- */
48
48
function normalizeBasePrefix ( raw : string | undefined | null ) : string {
49
49
const trimmed = ( raw ?? "" ) . trim ( ) ;
50
50
if ( ! trimmed ) return "" ; // no base
51
- // strip leading/trailing slashes then rebuild as "/segment/"
52
51
const core = trimmed . replace ( / ^ \/ + / , "" ) . replace ( / \/ + $ / , "" ) ;
53
- return core ? `/${ core } / ` : "/ " ;
52
+ return core ? `/${ core } ` : "" ;
54
53
}
55
54
const BASE_PREFIX = normalizeBasePrefix ( process . env . NEXT_PUBLIC_BASE_URL ) ;
56
- /** Builds an app-relative path under the configured base prefix. */
55
+
56
+ /** Build an absolute path under the configured base. */
57
57
function withBase ( path : string ) : string {
58
- // Accepts paths like "/virtual-keys" or "/?page=..." and prefixes with BASE_PREFIX when present.
59
- const body = path . startsWith ( "/" ) ? path . slice ( 1 ) : path ;
60
- const combined = `${ BASE_PREFIX } ${ body } ` ;
61
- return combined . startsWith ( "/" ) ? combined : `/${ combined } ` ;
58
+ // path can be "/virtual-keys" or "/?page=..."
59
+ const p = path . startsWith ( "/" ) ? path : `/${ path } ` ;
60
+ return `${ BASE_PREFIX } ${ p } ` || p ;
61
+ }
62
+
63
+ /** History-based navigation to prevent hard reloads / suspense flashes */
64
+ function softNavigate ( url : string , replace = false ) {
65
+ if ( typeof window === "undefined" ) return ;
66
+ if ( replace ) window . history . replaceState ( null , "" , url ) ;
67
+ else window . history . pushState ( null , "" , url ) ;
62
68
}
63
- /** -------------------------------------------------------- */
69
+ /** -------------------------------- */
64
70
65
71
const Sidebar2 : React . FC < SidebarProps > = ( {
66
72
accessToken,
@@ -69,7 +75,6 @@ const Sidebar2: React.FC<SidebarProps> = ({
69
75
defaultSelectedKey,
70
76
collapsed = false ,
71
77
} ) => {
72
- const router = useRouter ( ) ;
73
78
const pathname = usePathname ( ) ;
74
79
const { refactoredUIFlag } = useFeatureFlags ( ) ;
75
80
@@ -243,38 +248,30 @@ const Sidebar2: React.FC<SidebarProps> = ({
243
248
return true ;
244
249
} ) ;
245
250
246
- // Helper: go to /?page=...; when on /virtual-keys with refactored UI, replace instead of appending
251
+ // Helper: update /?page=... under the configured base, WITHOUT triggering App Router nav
247
252
const pushToRootWithPage = ( page : string , useReplace = false ) => {
248
253
const params = new URLSearchParams ( ) ;
249
254
params . set ( "page" , page ) ;
250
255
const url = withBase ( `/?${ params . toString ( ) } ` ) ;
251
- if ( useReplace ) {
252
- router . replace ( url ) ;
253
- } else {
254
- router . push ( url ) ;
255
- }
256
+ softNavigate ( url , useReplace ) ;
256
257
} ;
257
258
258
- // Centralized navigation that satisfies the requirement
259
259
const navigateToPage = ( page : string ) => {
260
- // Special-case: Virtual Keys target
261
260
if ( page === "api-keys" ) {
262
261
if ( refactoredUIFlag ) {
263
- // Go to the dedicated /virtual-keys page (always under base)
264
- router . push ( withBase ( "/virtual-keys" ) ) ;
265
- return ; // do not call setPage here ( parity with previous behavior)
262
+ // vanity URL, keep SPA alive
263
+ softNavigate ( withBase ( "/virtual-keys" ) ) ;
264
+ return ; // don't call setPage to keep parity, UI already shows api-keys by default
266
265
}
267
- // Legacy behavior
268
266
pushToRootWithPage ( page ) ;
269
267
setPage ( page ) ;
270
268
return ;
271
269
}
272
270
273
- // All other pages
274
271
if ( refactoredUIFlag ) {
275
- // If currently on /virtual-keys, REPLACE it with /?page=...
276
- const onVirtualKeys = pathname ? .startsWith ( withBase ( "/virtual-keys" ) ) ;
277
- pushToRootWithPage ( page , ! ! onVirtualKeys ) ;
272
+ const onVirtualKeys =
273
+ typeof window !== "undefined" && window . location . pathname . startsWith ( withBase ( "/virtual-keys" ) ) ;
274
+ pushToRootWithPage ( page , onVirtualKeys ) ;
278
275
} else {
279
276
pushToRootWithPage ( page ) ;
280
277
}
@@ -308,15 +305,9 @@ const Sidebar2: React.FC<SidebarProps> = ({
308
305
key : child . key ,
309
306
icon : child . icon ,
310
307
label : child . label ,
311
- onClick : ( ) => {
312
- navigateToPage ( child . page ) ;
313
- } ,
308
+ onClick : ( ) => navigateToPage ( child . page ) ,
314
309
} ) ) ,
315
- onClick : ! item . children
316
- ? ( ) => {
317
- navigateToPage ( item . page ) ;
318
- }
319
- : undefined ,
310
+ onClick : ! item . children ? ( ) => navigateToPage ( item . page ) : undefined ,
320
311
} ) ) }
321
312
/>
322
313
</ ConfigProvider >
0 commit comments