Skip to content

Commit 9fd0eec

Browse files
fix(dashboard): leave the current tab's tenant alone on a ctrl-click
A cross-tenant link switches the tenant context in its click handler so the page it opens renders against the right tenant from the first frame rather than flashing a not-found while the URL parameter is adopted. React runs that handler for a ctrl- or cmd-click too, which the browser turns into a new tab — so opening a tenant beside the current one moved the current one as well, and persisted the move. The handler now runs only for the click that navigates the tab it happened in. The new tab needs nothing from it: `?tenant=` in the href is what makes that tab resolve, which is the whole reason the parameter exists. `WorkloadCell` carried the same handler and gets the same guard. Assisted-By: Claude <[email protected]> Signed-off-by: Myasnikov Daniil <[email protected]>
1 parent e3b4415 commit 9fd0eec

6 files changed

Lines changed: 95 additions & 6 deletions

File tree

‎packages/system/dashboard/images/console/apps/console/src/components/WorkloadCell.tsx‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Link } from "react-router"
33
import { useApplicationDefinitions } from "../lib/app-definitions.ts"
44
import { useTenantContext } from "../lib/tenant-context.tsx"
55
import { TENANT_NAMESPACE_PREFIX } from "../lib/constants.ts"
6+
import { navigatesThisTab } from "../lib/links.ts"
67

78
interface WorkloadCellProps {
89
/** Namespace the workload lives in (tenant-<name> for tenant workloads). */
@@ -17,8 +18,9 @@ interface WorkloadCellProps {
1718
* Renders a consuming workload (the owning application) as a deep-link to its
1819
* Console Workloads tab, with the kind shown as a subtitle. The link is only active for
1920
* real app instances: the kind must resolve to a plural via ApplicationDefinitions
20-
* and the workload must live in a tenant namespace (so the Console tenant
21-
* context can be switched on click). Shared by every per-resource drill-down.
21+
* and the workload must live in a tenant namespace, since the link has to name
22+
* that tenant for the Console to resolve the instance. Shared by every
23+
* per-resource drill-down.
2224
*/
2325
export function WorkloadCell({ namespace, kind, name }: WorkloadCellProps) {
2426
const { data: appDefs } = useApplicationDefinitions()
@@ -47,7 +49,10 @@ export function WorkloadCell({ namespace, kind, name }: WorkloadCellProps) {
4749
{href ? (
4850
<Link
4951
to={href}
50-
onClick={() => tenant && selectTenant(tenant)}
52+
onClick={(e) => {
53+
if (!navigatesThisTab(e) || !tenant) return
54+
selectTenant(tenant)
55+
}}
5156
className="font-medium text-blue-700 hover:text-blue-800 hover:underline"
5257
>
5358
{name}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { describe, it, expect } from "vitest"
2+
import type { MouseEvent } from "react"
3+
import { navigatesThisTab } from "./links.ts"
4+
5+
function click(overrides: Partial<MouseEvent> = {}): MouseEvent {
6+
return {
7+
button: 0,
8+
metaKey: false,
9+
altKey: false,
10+
ctrlKey: false,
11+
shiftKey: false,
12+
...overrides,
13+
} as MouseEvent
14+
}
15+
16+
describe("navigatesThisTab", () => {
17+
it("accepts a plain primary click", () => {
18+
expect(navigatesThisTab(click())).toBe(true)
19+
})
20+
21+
// Each of these hands the navigation to a new tab or window, so the tab the
22+
// click happened in keeps whatever tenant it was showing.
23+
it.each(["metaKey", "ctrlKey", "shiftKey", "altKey"] as const)(
24+
"refuses a click modified with %s",
25+
(modifier) => {
26+
expect(navigatesThisTab(click({ [modifier]: true }))).toBe(false)
27+
},
28+
)
29+
30+
it("refuses a non-primary button", () => {
31+
expect(navigatesThisTab(click({ button: 1 }))).toBe(false)
32+
})
33+
})
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { MouseEvent } from "react"
2+
3+
/**
4+
* Whether a click on an anchor will navigate the tab it happened in, rather
5+
* than open a new tab or window. React runs `onClick` either way, and a
6+
* cross-tenant link's handler aligns the tenant context of the tab it is
7+
* leaving — which is wrong when that tab is staying where it is. Same button
8+
* and modifier test react-router's own `Link` applies before it intercepts a
9+
* click, minus the `target` check these links have no attribute for.
10+
*/
11+
export function navigatesThisTab(e: MouseEvent): boolean {
12+
return e.button === 0 && !e.metaKey && !e.altKey && !e.ctrlKey && !e.shiftKey
13+
}

‎packages/system/dashboard/images/console/apps/console/src/lib/tenant-context.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export function useTenantContext(): TenantContextValue {
117117
* tenants under different parents can share a relative CR name, so the name
118118
* alone would resolve against whichever tenant happened to be selected last.
119119
* Applied once per navigation rather than on every render, so the tenant picker
120-
* -- which switches the tenant without leaving the page -- is not dragged back
120+
* — which switches the tenant without leaving the page — is not dragged back
121121
* to the URL under the user, while returning to the entry through history
122122
* asserts it again. The provider's own fallback stays free to reject a tenant
123123
* the user cannot see.

‎packages/system/dashboard/images/console/apps/console/src/routes/TenantsPage.test.tsx‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, it, expect, vi, beforeAll } from "vitest"
2-
import { screen } from "@testing-library/react"
2+
import { fireEvent, screen } from "@testing-library/react"
33
import userEvent from "@testing-library/user-event"
44
import {
55
K8sClient,
@@ -182,6 +182,35 @@ describe("TenantsPage under a visible root", () => {
182182
// from itself and leave nothing.
183183
const SELF_ROOT = [tn("tenant-whmcs", [])]
184184

185+
describe("TenantsPage cross-tenant link", () => {
186+
// The href carries the tenant, so the tab that opens resolves on its own.
187+
// The click handler exists only to spare the tab that navigates a render
188+
// against the previous tenant — a ctrl-click is not that tab.
189+
it("does not switch the current tenant on a ctrl-click", async () => {
190+
localStorage.setItem(SELECTED_TENANT_KEY, "root")
191+
192+
renderWithK8sProvider(
193+
<TenantProvider>
194+
<TenantsPage />
195+
</TenantProvider>,
196+
{ client: makeClient(ROOT_VISIBLE), initialRoute: "/admin/tenants" },
197+
)
198+
199+
// "eu" hangs under acme, so a plain click here would move the selection
200+
// off root and the assertion below could not tell the two apart.
201+
const link = await screen.findByRole("link", { name: "eu" })
202+
// jsdom cannot follow an href, and a ctrl-click is exactly the case
203+
// react-router leaves to the browser. Swallow the default so the run does
204+
// not carry a "navigation to another Document" line; the handler under
205+
// test has already run by then, since React's own listener sits on the
206+
// container and this one is on the anchor.
207+
link.addEventListener("click", (e) => e.preventDefault())
208+
fireEvent.click(link, { ctrlKey: true })
209+
210+
expect(localStorage.getItem(SELECTED_TENANT_KEY)).toBe("root")
211+
})
212+
})
213+
185214
describe("TenantsPage self-referential root", () => {
186215
it("names the CR of a root that is not tenant-root", async () => {
187216
localStorage.setItem(SELECTED_TENANT_KEY, "whmcs")

‎packages/system/dashboard/images/console/apps/console/src/routes/TenantsPage.tsx‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
type TenantTreeNode,
1515
} from "../lib/tenant-tree.ts"
1616
import { ROOT_TENANT_NAMESPACE, TENANT_NAMESPACE_PREFIX } from "../lib/constants.ts"
17+
import { navigatesThisTab } from "../lib/links.ts"
1718
import { formatAge } from "../lib/status.ts"
1819
import { TenantQuotaCompact } from "../components/QuotaDisplay.tsx"
1920
import type { ResourceQuota } from "../components/QuotaDisplay.tsx"
@@ -233,7 +234,15 @@ export function TenantsPage() {
233234
{route ? (
234235
<Link
235236
to={route.href}
236-
onClick={() => selectTenant(route.parentTenant)}
237+
// The href alone would leave the detail page's
238+
// first render on the previous tenant, flashing
239+
// a not-found before the URL is adopted. Aligning
240+
// the context here is for the tab that navigates
241+
// — a ctrl-click leaves this one where it is.
242+
onClick={(e) => {
243+
if (!navigatesThisTab(e)) return
244+
selectTenant(route.parentTenant)
245+
}}
237246
className="block truncate text-sm font-medium text-slate-900 hover:underline"
238247
>
239248
{relativeTenantName(node)}

0 commit comments

Comments
 (0)