Skip to content

Commit aa017ad

Browse files
fix(dashboard): split the tenant row link out of this branch
The #3822 work moves to its own PR. It needs more than a link: the row link never renders for a direct child of tenant-root, because realParentNamespace matches a parent by name prefix and a root-level tenant is named tenant-<word> rather than tenant-root-<word>, so it resolves to undefined and the row stays plain text. That is most tenants on a normal cluster, so the fix as written barely applies. Carrying the tenant in the URL also turned out to be entangled with the tenant list's caching and with an apiserver that ignores the label selector on LIST while honouring it on WATCH. Settling that does not belong in a batch of small fixes. The six remaining fixes are unaffected: they never depended on the tenant link or on the URL parameter. Assisted-By: Claude <[email protected]> Signed-off-by: Myasnikov Daniil <[email protected]>
1 parent e5deb54 commit aa017ad

10 files changed

Lines changed: 18 additions & 242 deletions

File tree

‎packages/system/dashboard/images/console/CLAUDE.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ same paths to `kubernetes.default.svc` using the pod's service-account token.
7777
to a chunked-encoding WATCH against the same `resourceVersion`. Don't
7878
poll. Don't add `refetchInterval`. If you need a one-shot, pass
7979
`{ watch: false }`.
80-
4. **Tenant scoping.** Most resources live in `tenant-<name>` namespaces. Pull the active tenant from `useTenantContext()` — never read the namespace from a URL param or guess it. A link that crosses tenants is the one exception, and it still does not read the namespace: it names the tenant in `?tenant=`, which `useTenantFromUrl` feeds into the context on each navigation. Without it a real `<a href>` opened by middle click or in a new tab runs no `onClick`, and a relative CR name resolves against whichever tenant was selected last — a different tenant's resource, behind a Delete confirm that shows only that name.
80+
4. **Tenant scoping.** Most resources live in `tenant-<name>` namespaces.
81+
Pull the active tenant from `useTenantContext()` — never read the
82+
namespace from a URL param or guess it.
8183
5. **Auth.** In production the SPA sits behind oauth2-proxy. The client
8284
relies on cookies forwarded by nginx, and `/oauth2/userinfo` returns the
8385
logged-in user. There is no token handling in the SPA itself.

‎packages/system/dashboard/images/console/apps/console/src/App.routing.test.tsx‎

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
type APIGroupList,
77
} from "@cozystack/k8s-client"
88
import App from "./App.tsx"
9-
import { SELECTED_TENANT_KEY } from "./lib/constants.ts"
109
import { renderWithK8sProvider } from "./test-utils/render.tsx"
1110

1211
function makeClient(tenants: unknown[] = []): K8sClient {
@@ -73,26 +72,6 @@ describe("default landing", () => {
7372
})
7473
})
7574

76-
describe("tenant in the URL", () => {
77-
// The shell has to call useTenantFromUrl for a cross-tenant link to survive
78-
// a middle click or a pasted URL; the hook's own tests cannot see whether it
79-
// is wired in, so pin it against the real App.
80-
it("adopts the tenant named in the query string", async () => {
81-
const client = makeClient()
82-
localStorage.setItem(SELECTED_TENANT_KEY, "globex")
83-
84-
// Any route will do — the shell adopts the param regardless of which page
85-
// renders under it; the tenant list just gives a stable marker to await.
86-
renderWithK8sProvider(<App />, {
87-
client,
88-
initialRoute: "/admin/tenants?tenant=acme",
89-
})
90-
91-
await screen.findByRole("heading", { name: "Tenants" })
92-
expect(localStorage.getItem(SELECTED_TENANT_KEY)).toBe("acme")
93-
})
94-
})
95-
9675
describe("shell subtitle", () => {
9776
// The picker only renders its "Tenant /" label once a tenant list has
9877
// arrived; asserting against an empty list passes whether or not the picker

‎packages/system/dashboard/images/console/apps/console/src/App.tsx‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Navigate, Route, Routes, useLocation } from "react-router"
22
import { AppShell } from "@cozystack/ui"
3-
import { TenantProvider, useTenantFromUrl } from "./lib/tenant-context.tsx"
3+
import { TenantProvider } from "./lib/tenant-context.tsx"
44
import { Breadcrumb } from "./components/Breadcrumb.tsx"
55
import { MarketplacePage } from "./routes/MarketplacePage.tsx"
66
import { ConsolePage } from "./routes/ConsolePage.tsx"
@@ -24,7 +24,6 @@ interface ShellProps {
2424

2525
function Shell({ config, username }: ShellProps) {
2626
const { pathname } = useLocation()
27-
useTenantFromUrl()
2827
const inMarketplace = pathname.startsWith("/marketplace")
2928
const inAdmin = pathname.startsWith("/admin")
3029
const marketplaceSections = useMarketplaceSidebarSections()

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,7 @@ export function WorkloadCell({ namespace, kind, name }: WorkloadCellProps) {
3434
const tenant = namespace.startsWith(TENANT_NAMESPACE_PREFIX)
3535
? namespace.slice(TENANT_NAMESPACE_PREFIX.length)
3636
: null
37-
// These drill-downs list workloads from every namespace, so the tenant has to
38-
// travel in the URL: onClick alone leaves middle click and open-in-new-tab
39-
// resolving the name against the previously selected tenant.
40-
const href =
41-
plural && tenant
42-
? `/console/${plural}/${name}/workloads?tenant=${encodeURIComponent(tenant)}`
43-
: null
37+
const href = plural && tenant ? `/console/${plural}/${name}/workloads` : null
4438

4539
return (
4640
<>

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

Lines changed: 0 additions & 120 deletions
This file was deleted.

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

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ import {
33
useContext,
44
useEffect,
55
useMemo,
6-
useRef,
76
useState,
87
type ReactNode,
98
} from "react"
10-
import { useLocation, useSearchParams } from "react-router"
119
import { useK8sList } from "@cozystack/k8s-client"
1210
import type { TenantNamespace } from "@cozystack/types"
1311
import { SELECTED_TENANT_KEY, TENANT_NAMESPACE_PREFIX } from "./constants.ts"
@@ -100,34 +98,6 @@ export function useTenantContext(): TenantContextValue {
10098
return ctx
10199
}
102100

103-
/**
104-
* The detail routes take the resource name from the URL and the namespace from
105-
* this context, so a link that crosses tenants has to name its tenant in the
106-
* URL: the browser-native paths a real anchor enables — middle click, open in
107-
* new tab, a bookmarked or pasted URL — never run React's onClick, and two
108-
* tenants under different parents can share a relative CR name, so the name
109-
* alone would resolve against whichever tenant happened to be selected last.
110-
* Applied once per navigation rather than on every render, so the tenant picker
111-
* -- which switches the tenant without leaving the page -- is not dragged back
112-
* to the URL under the user, while returning to the entry through history
113-
* asserts it again. The provider's own fallback stays free to reject a tenant
114-
* the user cannot see.
115-
*/
116-
export function useTenantFromUrl() {
117-
const { key } = useLocation()
118-
const [params] = useSearchParams()
119-
const wanted = params.get("tenant")
120-
const { selectTenant } = useTenantContext()
121-
const navigated = useRef<string | null>(null)
122-
123-
useEffect(() => {
124-
const previous = navigated.current
125-
navigated.current = key
126-
if (!wanted || previous === key) return
127-
selectTenant(wanted)
128-
}, [key, wanted, selectTenant])
129-
}
130-
131101
/**
132102
* Pull the display name of a TenantNamespace (no `tenant-` prefix).
133103
*/

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,7 @@ describe("ClusterUsageResourcePage", () => {
181181
renderResource(client, GPU)
182182

183183
const link = await screen.findByRole("link", { name: "demo-vm" })
184-
// The tenant travels in the URL: this table lists workloads from every
185-
// namespace, and a middle click never runs the link's onClick.
186-
expect(link).toHaveAttribute(
187-
"href",
188-
"/console/vminstances/demo-vm/workloads?tenant=root",
189-
)
184+
expect(link).toHaveAttribute("href", "/console/vminstances/demo-vm/workloads")
190185
})
191186

192187
it("does not link a consumer whose kind is not a known application", async () => {

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,7 @@ describe("StorageClassUsagePage", () => {
118118
)
119119
renderPage(client, "replicated")
120120
const link = await screen.findByRole("link", { name: "demo-vm" })
121-
// Same cross-namespace listing as the cluster drill-down, same need to
122-
// carry the tenant rather than rely on the click handler.
123-
expect(link).toHaveAttribute(
124-
"href",
125-
"/console/vminstances/demo-vm/workloads?tenant=root",
126-
)
121+
expect(link).toHaveAttribute("href", "/console/vminstances/demo-vm/workloads")
127122
})
128123

129124
it("shows an empty state when nothing uses the class", async () => {

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,6 @@ describe("TenantsPage tree", () => {
8989
expect(editButtons).toHaveLength(1)
9090
expect(screen.getByTitle("Edit tenant whmcs-x")).toBeInTheDocument()
9191
expect(screen.queryByTitle("Edit tenant whmcs-a-b")).not.toBeInTheDocument()
92-
93-
// A reachable Tenant CR links directly to its detail page, where Delete is
94-
// available. A bridged row with an unreadable real parent stays plain text.
95-
// The CR name is relative to the parent, so the link has to name the tenant
96-
// too: middle click and open-in-new-tab never run onClick, and the detail
97-
// page would otherwise resolve "x" against the previously selected tenant.
98-
expect(screen.getByRole("link", { name: "x" })).toHaveAttribute(
99-
"href",
100-
"/admin/tenants/x?tenant=whmcs",
101-
)
102-
expect(screen.queryByRole("link", { name: "a-b" })).toBeNull()
10392
})
10493

10594
it("collapses a subtree via the row toggle", async () => {

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

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useMemo, useState } from "react"
2-
import { Link, useNavigate } from "react-router"
2+
import { useNavigate } from "react-router"
33
import { Plus, Edit, Info, ChevronDown, ChevronRight, CornerDownRight } from "lucide-react"
44
import { Spinner, Section, Button } from "@cozystack/ui"
55
import { useK8sList } from "@cozystack/k8s-client"
@@ -125,8 +125,8 @@ export function TenantsPage() {
125125
// A node's Tenant CR lives in its REAL parent's namespace, which is not
126126
// necessarily the parent it hangs under: a node whose real parent is
127127
// inaccessible is bridged onto the nearest visible ancestor. Deriving the CR
128-
// from that bridged ancestor names a CR that does not exist, so Tenant CR
129-
// routes come from realParentNamespace instead — and are offered only when
128+
// from that bridged ancestor names a CR that does not exist, so the edit
129+
// target comes from realParentNamespace instead — and is offered only when
130130
// that namespace is actually readable, since otherwise there is no CR the
131131
// user could open. The hierarchy root (no ancestor labels at all) is
132132
// self-referential: its CR (`root`) lives in its own namespace.
@@ -137,28 +137,12 @@ export function TenantsPage() {
137137
const real = realParentNamespace(node.tn)
138138
return real && visibleNs.has(real) ? real : undefined
139139
}
140-
const tenantRoute = (node: TreeNode) => {
141-
const parentNs = editParentNs(node)
142-
if (!parentNs) return undefined
143-
const parentTenant = parentNs.slice(TENANT_NAMESPACE_PREFIX.length)
144-
const path = `${basePath}/tenants/${tenantCrName(node.tn.metadata.name, parentNs)}`
145-
return {
146-
parentTenant,
147-
path,
148-
// The CR name is relative to the parent, so two tenants under different
149-
// parents share one path; the detail page reads the namespace from the
150-
// tenant context, which only the query param can set on a new tab.
151-
href: `${path}?tenant=${encodeURIComponent(parentTenant)}`,
152-
}
153-
}
154-
const canEdit = (node: TreeNode) => !!tenantRoute(node)
140+
const canEdit = (node: TreeNode) => !!editParentNs(node)
155141
const editNode = (node: TreeNode) => {
156-
const route = tenantRoute(node)
157-
if (!route) return
158-
selectTenant(route.parentTenant)
159-
navigate(
160-
`${route.path}/edit?tenant=${encodeURIComponent(route.parentTenant)}`,
161-
)
142+
const parentNs = editParentNs(node)
143+
if (!parentNs) return
144+
selectTenant(parentNs.slice(TENANT_NAMESPACE_PREFIX.length))
145+
navigate(`${basePath}/tenants/${tenantCrName(node.tn.metadata.name, parentNs)}/edit`)
162146
}
163147

164148
return (
@@ -199,7 +183,6 @@ export function TenantsPage() {
199183
const modules = modulesByNamespace.get(ns) ?? []
200184
const tenantQuotas = quotasByNamespace.get(ns) ?? []
201185
const host = node.tn.metadata.labels?.[HOST_LABEL]
202-
const route = tenantRoute(node)
203186
return (
204187
<tr key={ns} className="hover:bg-slate-50">
205188
<td className="px-4 py-3">
@@ -224,19 +207,9 @@ export function TenantsPage() {
224207
<CornerDownRight className="size-3.5 shrink-0 text-slate-300" />
225208
) : null}
226209
<div className="min-w-0">
227-
{route ? (
228-
<Link
229-
to={route.href}
230-
onClick={() => selectTenant(route.parentTenant)}
231-
className="block truncate text-sm font-medium text-slate-900 hover:underline"
232-
>
233-
{relativeTenantName(node)}
234-
</Link>
235-
) : (
236-
<p className="truncate text-sm font-medium text-slate-900">
237-
{relativeTenantName(node)}
238-
</p>
239-
)}
210+
<p className="truncate text-sm font-medium text-slate-900">
211+
{relativeTenantName(node)}
212+
</p>
240213
<p className="truncate font-mono text-[11px] text-slate-400">
241214
{ns}
242215
</p>

0 commit comments

Comments
 (0)