Skip to content

Commit 79fe0b4

Browse files
committed
fix(webapp): preserve active side menu state on detail pages
1 parent bdcbc61 commit 79fe0b4

7 files changed

Lines changed: 178 additions & 8 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
area: webapp
3+
type: fix
4+
---
5+
6+
Keep side menu items highlighted when viewing detail pages and sub-routes within a section.
7+

‎apps/webapp/app/components/navigation/OrganizationSettingsSideMenu.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ export function OrganizationSettingsSideMenu({
154154
activeIconColor="text-text-bright"
155155
inactiveIconColor="text-text-dimmed"
156156
to={v3PrivateConnectionsPath(organization)}
157+
matchPrefix
157158
data-action="private-connections"
158159
/>
159160
)}

‎apps/webapp/app/components/navigation/SideMenu.tsx‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,8 @@ export function SideMenu({
743743
? "hidden"
744744
: "shown";
745745

746+
const environmentPath = v3EnvironmentPath(organization, project, environment);
747+
746748
// The customizable sections (everything except Tasks/Runs/Sessions), in DEFAULT order. The
747749
// user's saved order/hidden preferences are applied at render below.
748750
const staticSections = buildSideMenuSections({
@@ -941,7 +943,12 @@ export function SideMenu({
941943
icon={TasksIcon}
942944
activeIconColor="text-tasks"
943945
inactiveIconColor="text-text-dimmed"
944-
to={v3EnvironmentPath(organization, project, environment)}
946+
to={environmentPath}
947+
activePathPrefix={[
948+
`${environmentPath}/tasks`,
949+
`${environmentPath}/agents`,
950+
`${environmentPath}/schedules`,
951+
]}
945952
data-action="tasks"
946953
isCollapsed={isCollapsed}
947954
yieldActiveToFavorite
@@ -952,6 +959,7 @@ export function SideMenu({
952959
activeIconColor="text-runs"
953960
inactiveIconColor="text-text-dimmed"
954961
to={v3RunsPath(organization, project, environment)}
962+
matchPrefix
955963
data-action="runs"
956964
isCollapsed={isCollapsed}
957965
yieldActiveToFavorite
@@ -962,6 +970,7 @@ export function SideMenu({
962970
activeIconColor="text-sessions"
963971
inactiveIconColor="text-text-dimmed"
964972
to={v3SessionsPath(organization, project, environment)}
973+
matchPrefix
965974
data-action="sessions"
966975
isCollapsed={isCollapsed}
967976
yieldActiveToFavorite
@@ -976,6 +985,7 @@ export function SideMenu({
976985
activeIconColor="text-webhooks"
977986
inactiveIconColor="text-text-dimmed"
978987
to={v3WebhooksPath(organization, project, environment)}
988+
matchPrefix
979989
data-action="webhooks"
980990
badge={<BetaBadge />}
981991
isCollapsed={isCollapsed}
@@ -1141,7 +1151,7 @@ function CustomizableSideMenuSection({
11411151
activeIconColor={item.activeIconColor}
11421152
inactiveIconColor={item.inactiveIconColor ?? "text-text-dimmed"}
11431153
to={item.to}
1144-
matchPrefix={item.matchPrefix}
1154+
matchPrefix={item.matchPrefix ?? true}
11451155
data-action={item.dataAction}
11461156
badge={item.badge}
11471157
isCollapsed={isCollapsed}

‎apps/webapp/app/components/navigation/SideMenuItem.tsx‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,50 @@ export function SideMenuLabel({
6161
);
6262
}
6363

64+
function matchesPathPrefix(pathName: string, prefix: string): boolean {
65+
const normalized = prefix.replace(/\/+$/, "");
66+
if (normalized.length === 0) {
67+
return false;
68+
}
69+
return pathName === normalized || pathName.startsWith(`${normalized}/`);
70+
}
71+
72+
/**
73+
* Returns whether a side menu item should appear active for `pathName`.
74+
* Matches `to` exactly, `to` on a `/` boundary when `matchPrefix` is true, or
75+
* any prefix in `activePathPrefix` (for items whose `to` is a shared root such
76+
* as the environment URL while its sub-pages live under dedicated segments).
77+
*/
78+
export function isSideMenuPathActive({
79+
pathName,
80+
to,
81+
matchPrefix = false,
82+
activePathPrefix,
83+
}: {
84+
pathName: string;
85+
to: string;
86+
matchPrefix?: boolean;
87+
activePathPrefix?: string | readonly string[];
88+
}): boolean {
89+
if (pathName === to) {
90+
return true;
91+
}
92+
93+
const prefixes: string[] = [];
94+
if (matchPrefix) {
95+
prefixes.push(to);
96+
}
97+
if (activePathPrefix !== undefined) {
98+
if (typeof activePathPrefix === "string") {
99+
prefixes.push(activePathPrefix);
100+
} else {
101+
prefixes.push(...activePathPrefix);
102+
}
103+
}
104+
105+
return prefixes.some((prefix) => matchesPathPrefix(pathName, prefix));
106+
}
107+
64108
export function SideMenuItem({
65109
icon,
66110
activeIconColor,
@@ -80,6 +124,7 @@ export function SideMenuItem({
80124
isActive: isActiveOverride,
81125
yieldActiveToFavorite = false,
82126
matchPrefix = false,
127+
activePathPrefix,
83128
"data-action": dataAction,
84129
}: {
85130
icon?: RenderIcon;
@@ -101,6 +146,8 @@ export function SideMenuItem({
101146
/** Overrides the default pathname === to active check (e.g. favorites match on full URL). */
102147
isActive?: boolean;
103148
matchPrefix?: boolean;
149+
/** Additional path prefixes (matched on a `/` boundary) that activate this item. */
150+
activePathPrefix?: string | readonly string[];
104151
/**
105152
* In menus that render the Favorites section (the main project menu), an active favorite owns
106153
* the highlight, so the plain item yields its active state to it. Menus without a favorites
@@ -116,7 +163,7 @@ export function SideMenuItem({
116163
const activeFavoriteId = useActiveFavoriteId();
117164
const isActive =
118165
isActiveOverride ??
119-
((pathName === to || (matchPrefix && pathName.startsWith(`${to}/`))) &&
166+
(isSideMenuPathActive({ pathName, to, matchPrefix, activePathPrefix }) &&
120167
(!yieldActiveToFavorite || activeFavoriteId === undefined));
121168

122169
const isIndented = indented && !isCollapsed;

‎apps/webapp/app/components/navigation/sideMenuSections.tsx‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,7 @@ export function buildSideMenuSections({
154154
id: "metrics",
155155
title: "Observability",
156156
items: [
157-
...(alertsInObservability
158-
? [{ ...alertsItem, matchPrefix: true, badge: <NewBadge /> }]
159-
: []),
157+
...(alertsInObservability ? [{ ...alertsItem, badge: <NewBadge /> }] : []),
160158
...(isAdmin || featureFlags.hasLogsPageAccess
161159
? [
162160
{
@@ -206,6 +204,7 @@ export function buildSideMenuSections({
206204
icon: ChartBarIcon,
207205
activeIconColor: "text-metrics",
208206
to: v3DashboardsLandingPath(organization, project, environment),
207+
matchPrefix: false,
209208
dataAction: "dashboards-landing",
210209
action: dashboards?.action,
211210
after: dashboards?.after,
@@ -355,5 +354,13 @@ export function buildSideMenuSections({
355354
],
356355
});
357356

358-
return staticSections;
357+
// Stay highlighted on detail/sub-pages unless an item explicitly opts out (e.g. Dashboards,
358+
// whose child routes are rendered as their own menu items).
359+
return staticSections.map((section) => ({
360+
...section,
361+
items: section.items.map((item) => ({
362+
...item,
363+
matchPrefix: item.matchPrefix ?? true,
364+
})),
365+
}));
359366
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { describe, expect, it } from "vitest";
2+
import { isSideMenuPathActive } from "~/components/navigation/SideMenuItem";
3+
4+
describe("isSideMenuPathActive", () => {
5+
// Catches: prefix-matched items losing active state on detail pages or matching sibling routes with a shared prefix
6+
it("matches exact paths and sub-paths on a '/' boundary without bleeding into sibling routes", () => {
7+
const to = "/orgs/org-1/projects/proj-1/env/dev/concurrency";
8+
9+
expect(isSideMenuPathActive({ pathName: to, to, matchPrefix: true })).toBe(true);
10+
expect(isSideMenuPathActive({ pathName: `${to}/queues`, to, matchPrefix: true })).toBe(true);
11+
expect(
12+
isSideMenuPathActive({
13+
pathName: "/orgs/org-1/projects/proj-1/env/dev/concurrency-limits",
14+
to,
15+
matchPrefix: true,
16+
})
17+
).toBe(false);
18+
expect(isSideMenuPathActive({ pathName: `${to}/queues`, to, matchPrefix: false })).toBe(false);
19+
});
20+
21+
// Catches: Tasks activePathPrefix failing to match task, agent, and schedule detail pages or matching unrelated env sections
22+
it("matches Tasks across its dedicated sub-path prefixes without matching sibling environment routes", () => {
23+
const envPath = "/orgs/org-1/projects/proj-1/env/dev";
24+
const activePathPrefix = [`${envPath}/tasks`, `${envPath}/agents`, `${envPath}/schedules`];
25+
26+
expect(isSideMenuPathActive({ pathName: envPath, to: envPath, activePathPrefix })).toBe(true);
27+
expect(
28+
isSideMenuPathActive({
29+
pathName: `${envPath}/tasks/standard/my-task`,
30+
to: envPath,
31+
activePathPrefix,
32+
})
33+
).toBe(true);
34+
expect(
35+
isSideMenuPathActive({
36+
pathName: `${envPath}/agents/my-agent`,
37+
to: envPath,
38+
activePathPrefix,
39+
})
40+
).toBe(true);
41+
expect(
42+
isSideMenuPathActive({
43+
pathName: `${envPath}/schedules/sched_123`,
44+
to: envPath,
45+
activePathPrefix,
46+
})
47+
).toBe(true);
48+
49+
expect(
50+
isSideMenuPathActive({
51+
pathName: `${envPath}/runs/run_123`,
52+
to: envPath,
53+
activePathPrefix,
54+
})
55+
).toBe(false);
56+
expect(
57+
isSideMenuPathActive({
58+
pathName: `${envPath}/webhooks/wh_123`,
59+
to: envPath,
60+
activePathPrefix,
61+
})
62+
).toBe(false);
63+
});
64+
65+
// Catches: root "/" or trailing-slash prefixes matching unrelated routes or failing to match exact normalized paths
66+
it("ignores empty or root prefixes while normalizing trailing slashes", () => {
67+
expect(isSideMenuPathActive({ pathName: "/orgs/org-1", to: "/", matchPrefix: true })).toBe(
68+
false
69+
);
70+
expect(
71+
isSideMenuPathActive({
72+
pathName: "/orgs/org-1/settings/private-connections",
73+
to: "/orgs/org-1/settings/private-connections/",
74+
matchPrefix: true,
75+
})
76+
).toBe(true);
77+
expect(
78+
isSideMenuPathActive({
79+
pathName: "/orgs/org-1/settings/private-connections/new",
80+
to: "/orgs/org-1/settings/private-connections/",
81+
matchPrefix: true,
82+
})
83+
).toBe(true);
84+
});
85+
});

‎apps/webapp/test/sideMenuSections.test.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,20 @@ describe("buildSideMenuSections alerts placement", () => {
7878
expect(ids.indexOf("alerts")).toBe(ids.indexOf("bulk-actions") + 1);
7979
const alerts = manage.items.find((i) => i.id === "alerts")!;
8080
expect(alerts.badge).toBeUndefined();
81-
expect(alerts.matchPrefix).toBeUndefined();
81+
expect(alerts.matchPrefix).toBe(true);
82+
});
83+
84+
// Catches: section items losing active highlight on detail pages or Dashboards overriding child dashboard links
85+
it("defaults section items to matchPrefix: true while keeping Dashboards exact", () => {
86+
const items = build(true).flatMap((s) => s.items);
87+
const dashboards = items.find((i) => i.id === "dashboards")!;
88+
expect(dashboards.matchPrefix).toBe(false);
89+
90+
const otherItems = items.filter((i) => i.id !== "dashboards");
91+
expect(otherItems.length).toBeGreaterThan(0);
92+
for (const item of otherItems) {
93+
expect(item.matchPrefix, `expected ${item.id} to match prefix`).toBe(true);
94+
}
8295
});
8396

8497
it("moves Alerts into Observability when the alerts flag is on", () => {

0 commit comments

Comments
 (0)