diff --git a/apps/dashboard/app/(app)/apis/[apiId]/api-id-navbar.tsx b/apps/dashboard/app/(app)/apis/[apiId]/api-id-navbar.tsx index 5340c48881..c6fed0b1c4 100644 --- a/apps/dashboard/app/(app)/apis/[apiId]/api-id-navbar.tsx +++ b/apps/dashboard/app/(app)/apis/[apiId]/api-id-navbar.tsx @@ -46,6 +46,11 @@ export const ApisNavbar = ({ ) => { + const pathname = usePathname(); const router = useRouter(); const [open, setOpen] = useState(false); const [focusedIndex, setFocusedIndex] = useState(null); @@ -111,6 +112,17 @@ export const QuickNavPopover = ({ } }; + // Find the active item index for initial focus + const findActiveItemIndex = useCallback(() => { + if (!pathname) { + return 0; + } + + const activeIndex = items.findIndex((item) => item.href && checkIsActive(item.href, pathname)); + + return activeIndex >= 0 ? activeIndex : 0; + }, [items, pathname]); + // Scroll to focused item when using virtualization useEffect(() => { if (focusedIndex !== null && rowVirtualizer) { @@ -121,11 +133,11 @@ export const QuickNavPopover = ({ // Set initial focus when opening popover useEffect(() => { if (open) { - setFocusedIndex(0); + setFocusedIndex(findActiveItemIndex()); } else { setFocusedIndex(null); } - }, [open]); + }, [open, findActiveItemIndex]); return ( @@ -161,6 +173,8 @@ export const QuickNavPopover = ({ > {rowVirtualizer!.getVirtualItems().map((virtualRow) => { const item = items[virtualRow.index]; + const isActive = Boolean(item.href && checkIsActive(item.href, pathname)); + return (
handleItemSelect(item)} /> @@ -181,15 +196,19 @@ export const QuickNavPopover = ({ })}
) : ( - // Simple list rendering for few items - items.map((item, index) => ( - handleItemSelect(item)} - /> - )) + items.map((item, index) => { + const isActive = Boolean(item.href && checkIsActive(item.href, pathname)); + + return ( + handleItemSelect(item)} + /> + ); + }) )} @@ -220,6 +239,7 @@ type PopoverItemProps = QuickNavItem & { const PopoverItem = ({ label, isFocused, + isActive, onSelect, className, itemClassName, @@ -232,7 +252,6 @@ const PopoverItem = ({ itemRef.current.focus(); } }, [isFocused]); - const labelText = typeof label === "string" ? label : ""; return ( )} ); }; + +const checkIsActive = (itemPath: string, currentPath: string | null) => { + if (!itemPath) { + return false; + } + + // Normalize paths by removing trailing slashes + const normalizedItemPath = itemPath.endsWith("/") ? itemPath.slice(0, -1) : itemPath; + const normalizedCurrentPath = currentPath?.endsWith("/") ? currentPath.slice(0, -1) : currentPath; + + // Exact match check + if (normalizedItemPath === normalizedCurrentPath) { + return true; + } + + return false; +};