diff --git a/.server-changes/retain-sidebar-active-highlight-detail-pages.md b/.server-changes/retain-sidebar-active-highlight-detail-pages.md new file mode 100644 index 00000000000..2b94c012dd5 --- /dev/null +++ b/.server-changes/retain-sidebar-active-highlight-detail-pages.md @@ -0,0 +1,6 @@ +--- +area: webapp +type: fix +--- + +Retain sidebar active highlight when navigating to nested detail pages like tasks and runs diff --git a/apps/webapp/app/components/navigation/SideMenu.tsx b/apps/webapp/app/components/navigation/SideMenu.tsx index 3dbf49ee09f..f178e894329 100644 --- a/apps/webapp/app/components/navigation/SideMenu.tsx +++ b/apps/webapp/app/components/navigation/SideMenu.tsx @@ -950,6 +950,10 @@ export function SideMenu({ to={v3EnvironmentPath(organization, project, environment)} data-action="tasks" isCollapsed={isCollapsed} + matchPrefix={[ + `${v3EnvironmentPath(organization, project, environment)}/tasks`, + `${v3EnvironmentPath(organization, project, environment)}/agents`, + ]} yieldActiveToFavorite /> {(isManagedCloud || @@ -985,6 +991,7 @@ export function SideMenu({ data-action="webhooks" badge={} isCollapsed={isCollapsed} + matchPrefix yieldActiveToFavorite /> )} diff --git a/apps/webapp/app/components/navigation/SideMenuItem.tsx b/apps/webapp/app/components/navigation/SideMenuItem.tsx index 6b959783c01..aa07a57a188 100644 --- a/apps/webapp/app/components/navigation/SideMenuItem.tsx +++ b/apps/webapp/app/components/navigation/SideMenuItem.tsx @@ -61,6 +61,38 @@ export function SideMenuLabel({ ); } +function matchesPrefixPath(pathName: string, prefix: string): boolean { + const normalizedPrefix = prefix.replace(/\/+$/, ""); + return pathName === normalizedPrefix || pathName.startsWith(`${normalizedPrefix}/`); +} + +export function isRouteActive({ + pathName, + to, + matchPrefix, +}: { + pathName: string; + to: string; + matchPrefix?: boolean | string | string[]; +}): boolean { + if (pathName === to) return true; + if (!matchPrefix) return false; + + if (typeof matchPrefix === "boolean") { + return matchesPrefixPath(pathName, to); + } + + if (typeof matchPrefix === "string") { + return matchesPrefixPath(pathName, matchPrefix); + } + + if (Array.isArray(matchPrefix)) { + return matchPrefix.some((prefix) => matchesPrefixPath(pathName, prefix)); + } + + return false; +} + export function SideMenuItem({ icon, activeIconColor, @@ -100,7 +132,7 @@ export function SideMenuItem({ indented?: boolean; /** Overrides the default pathname === to active check (e.g. favorites match on full URL). */ isActive?: boolean; - matchPrefix?: boolean; + matchPrefix?: boolean | string | string[]; /** * In menus that render the Favorites section (the main project menu), an active favorite owns * the highlight, so the plain item yields its active state to it. Menus without a favorites @@ -116,7 +148,7 @@ export function SideMenuItem({ const activeFavoriteId = useActiveFavoriteId(); const isActive = isActiveOverride ?? - ((pathName === to || (matchPrefix && pathName.startsWith(`${to}/`))) && + (isRouteActive({ pathName, to, matchPrefix }) && (!yieldActiveToFavorite || activeFavoriteId === undefined)); const isIndented = indented && !isCollapsed; diff --git a/apps/webapp/app/components/navigation/sideMenuSections.tsx b/apps/webapp/app/components/navigation/sideMenuSections.tsx index 9e17aa3fbdf..0c5955ae10b 100644 --- a/apps/webapp/app/components/navigation/sideMenuSections.tsx +++ b/apps/webapp/app/components/navigation/sideMenuSections.tsx @@ -62,7 +62,7 @@ type SideMenuItemConfig = { activeIconColor: string; inactiveIconColor?: string; to: string; - matchPrefix?: boolean; + matchPrefix?: boolean | string | string[]; dataAction?: string; badge?: ReactNode; trailingIconClassName?: string; @@ -122,6 +122,7 @@ export function buildSideMenuSections({ activeIconColor: "text-aiPrompts", to: v3PromptsPath(organization, project, environment), dataAction: "prompts", + matchPrefix: true, }, { id: "models", @@ -130,6 +131,7 @@ export function buildSideMenuSections({ activeIconColor: "text-models", to: v3ModelsPath(organization, project, environment), dataAction: "models", + matchPrefix: true, }, ], }); @@ -179,6 +181,7 @@ export function buildSideMenuSections({ activeIconColor: "text-errors", to: v3ErrorsPath(organization, project, environment), dataAction: "errors", + matchPrefix: true, } satisfies SideMenuItemConfig, ] : []), @@ -199,6 +202,7 @@ export function buildSideMenuSections({ activeIconColor: "text-queues", to: concurrencyPath(organization, project, environment), dataAction: "queues", + matchPrefix: true, } satisfies SideMenuItemConfig, { id: "dashboards", @@ -207,6 +211,7 @@ export function buildSideMenuSections({ activeIconColor: "text-metrics", to: v3DashboardsLandingPath(organization, project, environment), dataAction: "dashboards-landing", + matchPrefix: true, action: dashboards?.action, after: dashboards?.after, } satisfies SideMenuItemConfig, @@ -227,6 +232,7 @@ export function buildSideMenuSections({ activeIconColor: "text-deployments", to: v3DeploymentsPath(organization, project, environment), dataAction: "deployments", + matchPrefix: true, }, { id: "preview-branches", @@ -258,6 +264,7 @@ export function buildSideMenuSections({ activeIconColor: "text-text-bright", to: v3ApiKeysPath(organization, project, environment), dataAction: "api keys", + matchPrefix: true, }, { id: "environment-variables", @@ -266,6 +273,7 @@ export function buildSideMenuSections({ activeIconColor: "text-environmentVariables", to: v3EnvironmentVariablesPath(organization, project, environment), dataAction: "environment variables", + matchPrefix: true, }, ...(featureFlags.trustedDeploysEnabled ? [ @@ -306,6 +314,7 @@ export function buildSideMenuSections({ activeIconColor: "text-sky-500", to: v3WaitpointTokensPath(organization, project, environment), dataAction: "waitpoint-tokens", + matchPrefix: true, }, { id: "batches", @@ -314,6 +323,7 @@ export function buildSideMenuSections({ activeIconColor: "text-batches", to: v3BatchesPath(organization, project, environment), dataAction: "batches", + matchPrefix: true, }, { id: "bulk-actions", @@ -322,6 +332,7 @@ export function buildSideMenuSections({ activeIconColor: "text-text-bright", to: v3BulkActionsPath(organization, project, environment), dataAction: "bulk actions", + matchPrefix: true, }, ...(alertsInObservability ? [] : [alertsItem]), ...(isManagedCloud diff --git a/apps/webapp/test/sideMenuItem.test.ts b/apps/webapp/test/sideMenuItem.test.ts new file mode 100644 index 00000000000..215268abaf4 --- /dev/null +++ b/apps/webapp/test/sideMenuItem.test.ts @@ -0,0 +1,225 @@ +import { describe, expect, it } from "vitest"; +import { isRouteActive } from "~/components/navigation/SideMenuItem"; + +describe("isRouteActive", () => { + const envPath = "/orgs/my-org/projects/my-proj/env/dev"; + + describe("exact matching", () => { + it("returns true when pathName matches to exactly", () => { + expect(isRouteActive({ pathName: `${envPath}/runs`, to: `${envPath}/runs` })).toBe(true); + }); + + it("returns false when pathName does not match and matchPrefix is false", () => { + expect( + isRouteActive({ + pathName: `${envPath}/runs/run_123`, + to: `${envPath}/runs`, + matchPrefix: false, + }) + ).toBe(false); + }); + }); + + describe("boolean matchPrefix", () => { + it("matches child routes when matchPrefix is true", () => { + expect( + isRouteActive({ + pathName: `${envPath}/runs/run_123`, + to: `${envPath}/runs`, + matchPrefix: true, + }) + ).toBe(true); + + expect( + isRouteActive({ + pathName: `${envPath}/runs/run_123/stream`, + to: `${envPath}/runs`, + matchPrefix: true, + }) + ).toBe(true); + }); + + it("does not match sibling routes with matching prefix text but different segment", () => { + expect( + isRouteActive({ + pathName: `${envPath}/runs-metrics`, + to: `${envPath}/runs`, + matchPrefix: true, + }) + ).toBe(false); + expect( + isRouteActive({ + pathName: `${envPath}/concurrency-limits`, + to: `${envPath}/concurrency`, + matchPrefix: true, + }) + ).toBe(false); + }); + + it("matches exact route even with matchPrefix: true", () => { + expect( + isRouteActive({ + pathName: `${envPath}/runs`, + to: `${envPath}/runs`, + matchPrefix: true, + }) + ).toBe(true); + }); + }); + + describe("custom string matchPrefix", () => { + it("matches when pathName equals the custom prefix", () => { + expect( + isRouteActive({ + pathName: `${envPath}/tasks`, + to: envPath, + matchPrefix: `${envPath}/tasks`, + }) + ).toBe(true); + }); + + it("matches child routes of the custom prefix", () => { + expect( + isRouteActive({ + pathName: `${envPath}/tasks/standard/my-task`, + to: envPath, + matchPrefix: `${envPath}/tasks`, + }) + ).toBe(true); + expect( + isRouteActive({ + pathName: `${envPath}/tasks/scheduled/cron-task`, + to: envPath, + matchPrefix: `${envPath}/tasks`, + }) + ).toBe(true); + expect( + isRouteActive({ + pathName: `${envPath}/tasks/stream`, + to: envPath, + matchPrefix: `${envPath}/tasks`, + }) + ).toBe(true); + }); + + it("does not match other environment routes with custom prefix", () => { + expect( + isRouteActive({ + pathName: `${envPath}/runs`, + to: envPath, + matchPrefix: `${envPath}/tasks`, + }) + ).toBe(false); + expect( + isRouteActive({ + pathName: `${envPath}/batches/batch_123`, + to: envPath, + matchPrefix: `${envPath}/tasks`, + }) + ).toBe(false); + }); + + it("handles trailing slashes gracefully", () => { + expect( + isRouteActive({ + pathName: `${envPath}/tasks/standard/task-1`, + to: envPath, + matchPrefix: `${envPath}/tasks/`, + }) + ).toBe(true); + }); + }); + + describe("custom string array matchPrefix (Tasks & Agents)", () => { + const tasksPrefixes = [`${envPath}/tasks`, `${envPath}/agents`]; + + it("matches Tasks root page via exact to match", () => { + expect( + isRouteActive({ + pathName: envPath, + to: envPath, + matchPrefix: tasksPrefixes, + }) + ).toBe(true); + }); + + it("matches task detail routes under /tasks", () => { + expect( + isRouteActive({ + pathName: `${envPath}/tasks/standard/my-task`, + to: envPath, + matchPrefix: tasksPrefixes, + }) + ).toBe(true); + expect( + isRouteActive({ + pathName: `${envPath}/tasks/scheduled/my-task`, + to: envPath, + matchPrefix: tasksPrefixes, + }) + ).toBe(true); + expect( + isRouteActive({ + pathName: `${envPath}/tasks/dashboard`, + to: envPath, + matchPrefix: tasksPrefixes, + }) + ).toBe(true); + }); + + it("matches agent task detail routes under /agents", () => { + expect( + isRouteActive({ + pathName: `${envPath}/agents/my-agent`, + to: envPath, + matchPrefix: tasksPrefixes, + }) + ).toBe(true); + }); + + it("does not match sibling sections in the environment", () => { + expect( + isRouteActive({ + pathName: `${envPath}/runs`, + to: envPath, + matchPrefix: tasksPrefixes, + }) + ).toBe(false); + expect( + isRouteActive({ + pathName: `${envPath}/runs/run_123`, + to: envPath, + matchPrefix: tasksPrefixes, + }) + ).toBe(false); + expect( + isRouteActive({ + pathName: `${envPath}/sessions`, + to: envPath, + matchPrefix: tasksPrefixes, + }) + ).toBe(false); + expect( + isRouteActive({ + pathName: `${envPath}/webhooks`, + to: envPath, + matchPrefix: tasksPrefixes, + }) + ).toBe(false); + expect( + isRouteActive({ + pathName: `${envPath}/deployments`, + to: envPath, + matchPrefix: tasksPrefixes, + }) + ).toBe(false); + expect( + isRouteActive({ + pathName: `${envPath}/settings`, + to: envPath, + matchPrefix: tasksPrefixes, + }) + ).toBe(false); + }); + }); +}); diff --git a/apps/webapp/test/sideMenuSections.test.ts b/apps/webapp/test/sideMenuSections.test.ts index ee8019dbccc..f5d7daecd5c 100644 --- a/apps/webapp/test/sideMenuSections.test.ts +++ b/apps/webapp/test/sideMenuSections.test.ts @@ -104,4 +104,37 @@ describe("buildSideMenuSections alerts placement", () => { const ids = sections.find((s) => s.id === "manage")!.items.map((i) => i.id); expect(ids.indexOf("alerts")).toBe(ids.indexOf("bulk-actions") + 1); }); + + it("enables matchPrefix on detail-bearing sections", () => { + const sections = buildSideMenuSections({ + organization: { slug: "org" }, + project: { slug: "proj" }, + environment: { slug: "dev" }, + isAdmin: true, + alertsConfigured: true, + featureFlags: { hasAiAccess: true, hasQueryAccess: true, hasLogsPageAccess: true }, + isManagedCloud: true, + }); + const allItems = sections.flatMap((s) => s.items); + + const matchPrefixIds = [ + "prompts", + "models", + "errors", + "queues", + "dashboards", + "deployments", + "api-keys", + "environment-variables", + "waitpoint-tokens", + "batches", + "bulk-actions", + ]; + + for (const id of matchPrefixIds) { + const item = allItems.find((i) => i.id === id); + expect(item, `expected item with id '${id}' to exist`).toBeDefined(); + expect(item?.matchPrefix, `expected item '${id}' to have matchPrefix: true`).toBe(true); + } + }); });