Graph: highlight a selected node's links; nav: Connect only when signed out

Selecting a node highlights every edge into and out of it, with their
labels and the boxes at the far ends, and dims the rest. The Connect
menu item is shown only while signed out; when signed in, the name in
the menu links to the connection page so Disconnect stays reachable.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
mattandClaude Sonnet 5.5 committed 2026-11-09 14:09:42 +00:00
1 parent 11c9483a88
commit 44e4eb75c9
6 files changed
+133 -16

No files matched your search

+50
View File
@@ -0,0 +1,50 @@
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { createMemoryRouter, RouterProvider } from 'react-router'
import { expect, test } from 'vitest'
import { BaoClient } from './api/bao'
import { SessionProvider } from './api/session'
import { useSession } from './api/useSession'
import { Layout } from './Layout'
import { Providers } from './test/providers'
function SignIn() {
const { connect } = useSession()
return (
<button type="button" onClick={() => connect(new BaoClient('http://b', 't'), { accessor: 'a', display_name: 'alice', policies: [], ttl: 0, renewable: false })}>
sign in
</button>
)
}
function setup() {
const router = createMemoryRouter([{ path: '/', element: <Layout />, children: [{ index: true, element: <SignIn /> }, { path: 'connect', element: <p>connect page</p> }] }])
render(
<Providers>
<SessionProvider>
<RouterProvider router={router} />
</SessionProvider>
</Providers>,
)
return userEvent.setup()
}
test('offers Connect only while signed out', async () => {
const user = setup()
const nav = screen.getByRole('navigation', { name: 'Main' })
expect(nav).toHaveTextContent('Connect')
expect(screen.getByRole('link', { name: 'Connect' })).toHaveAttribute('href', '/connect')
await user.click(screen.getByRole('button', { name: 'sign in' }))
expect(screen.queryByRole('link', { name: 'Connect' })).not.toBeInTheDocument()
expect(screen.queryByRole('link', { name: 'Connection' })).not.toBeInTheDocument()
})
test('while signed in, the signed-in name links to the connection details', async () => {
const user = setup()
await user.click(screen.getByRole('button', { name: 'sign in' }))
const me = screen.getByRole('link', { name: 'alice' })
expect(me).toHaveAttribute('href', '/connect')
await user.click(me)
expect(screen.getByText('connect page')).toBeInTheDocument()
})
+10 -4
View File
@@ -26,10 +26,16 @@ export function Layout() {
{l.label}
</NavLink>
))}
{client && tokenInfo && <span className="whoami">{tokenInfo.display_name || 'token'}</span>}
<NavLink to="/connect" className="connect">
{client ? 'Connection' : 'Connect'}
</NavLink>
{client ? (
// Signed in: the name is the way back to the connection details and Disconnect.
<NavLink to="/connect" className="whoami" title="Connection details and sign out">
{tokenInfo?.display_name || 'token'}
</NavLink>
) : (
<NavLink to="/connect" className="connect">
Connect
</NavLink>
)}
</nav>
{notice && (
<div role="alert" className="notice">
+6 -2
View File
@@ -4,6 +4,10 @@ export interface GrantEdgeData {
label: string
/** Vertical nudge so labels on edges into the same node don't sit on top of each other. */
offset: number
/** Touches the selected node. */
linked?: boolean
/** Another node is selected and this edge isn't one of its links. */
dim?: boolean
onSelect: () => void
}
@@ -12,7 +16,7 @@ export interface GrantEdgeData {
* fanned out to different rows, instead of at the midpoint where edges from one policy bunch up.
*/
export function GrantEdge({ id, sourceX, sourceY, targetX, targetY, sourcePosition, targetPosition, markerEnd, selected, data }: EdgeProps) {
const { label, offset, onSelect } = data as unknown as GrantEdgeData
const { label, offset, linked, dim, onSelect } = data as unknown as GrantEdgeData
const [path] = getBezierPath({ sourceX, sourceY, sourcePosition, targetX, targetY, targetPosition })
return (
<>
@@ -20,7 +24,7 @@ export function GrantEdge({ id, sourceX, sourceY, targetX, targetY, sourcePositi
<EdgeLabelRenderer>
<button
type="button"
className={`nodrag nopan edge-label${selected ? ' edge-label-selected' : ''}`}
className={`nodrag nopan edge-label${selected ? ' edge-label-selected' : ''}${linked ? ' edge-label-linked' : ''}${dim ? ' edge-label-dim' : ''}`}
style={{ transform: `translate(-100%, -50%) translate(${targetX - 18}px, ${targetY + offset}px)` }}
onClick={onSelect}
>
+35 -4
View File
@@ -16,19 +16,19 @@ import { GraphPage } from './GraphPage'
// jsdom can't lay out React Flow, so render nodes/edges as plain buttons that fire the same callbacks.
vi.mock('@xyflow/react', () => ({
ReactFlow: (p: {
nodes: { id: string; data: { label: string } }[]
edges: { id: string; label?: string }[]
nodes: { id: string; className?: string; data: { label: string } }[]
edges: { id: string; className?: string; label?: string }[]
onNodeClick: (e: unknown, n: { id: string }) => void
onEdgeClick: (e: unknown, ed: { id: string }) => void
}) => (
<div>
{p.nodes.map((n) => (
<button key={n.id} type="button" onClick={(e) => p.onNodeClick(e, n)}>
<button key={n.id} type="button" data-class={n.className} onClick={(e) => p.onNodeClick(e, n)}>
node {n.data.label}
</button>
))}
{p.edges.map((ed) => (
<button key={ed.id} type="button" onClick={(e) => p.onEdgeClick(e, ed)}>
<button key={ed.id} type="button" data-class={ed.className} onClick={(e) => p.onEdgeClick(e, ed)}>
edge {ed.id}
</button>
))}
@@ -140,3 +140,34 @@ test('grants in unmanaged policies cannot be revoked', async () => {
expect(screen.queryByRole('button', { name: 'Revoke' })).not.toBeInTheDocument()
expect(screen.getByText(/does not manage/)).toBeInTheDocument()
})
const cls = (name: string) => screen.getByRole('button', { name }).getAttribute('data-class') ?? ''
test('selecting a node highlights its links and dims the rest', async () => {
const user = await open()
// nothing is dimmed until something is selected
expect(cls('node bob')).not.toContain('gnode-dim')
expect(cls('edge policy:legacy->path:secret/data/other')).not.toContain('edge-')
await user.click(screen.getByRole('button', { name: 'node envbao-user-alice' }))
// its links, in and out, are highlighted, with the boxes at their far ends
expect(cls('edge policy:envbao-user-alice->path:secret/data/app/*')).toContain('edge-linked')
expect(cls('edge identity:user:alice->policy:envbao-user-alice')).toContain('edge-linked')
expect(cls('node alice')).toContain('gnode-linked')
expect(cls('node secret/data/app/*')).toContain('gnode-linked')
expect(cls('node envbao-user-alice')).toContain('gnode-selected')
// an unrelated chain steps back
expect(cls('edge policy:legacy->path:secret/data/other')).toContain('edge-dim')
expect(cls('node bob')).toContain('gnode-dim')
expect(cls('node legacy')).toContain('gnode-dim')
})
test('clearing the selection, or selecting an edge, removes the highlight', async () => {
const user = await open()
await user.click(screen.getByRole('button', { name: 'node alice' }))
expect(cls('node bob')).toContain('gnode-dim')
await user.click(screen.getByRole('button', { name: 'edge policy:legacy->path:secret/data/other' }))
expect(cls('node bob')).not.toContain('gnode-dim')
expect(cls('edge policy:envbao-user-alice->path:secret/data/app/*')).not.toContain('edge-dim')
})
+11
View File
@@ -34,6 +34,11 @@ function capabilityLabel(caps: string[] | undefined): string | undefined {
function toFlow(graph: AccessGraph, selected: Selection | null, onEdgeSelect: (id: string) => void): { nodes: Node[]; edges: Edge[] } {
const pos = layoutColumns(graph)
// Selecting a node highlights every edge into or out of it, and the boxes at their far ends.
const focus = selected?.kind === 'node' ? selected.id : null
const linkedEdges = new Set(graph.edges.filter((e) => e.source === focus || e.target === focus).map((e) => e.id))
const linkedNodes = new Set<string>(focus ? [focus] : [])
for (const e of graph.edges) if (linkedEdges.has(e.id)) linkedNodes.add(e.source).add(e.target)
const nodes: Node[] = graph.nodes.map((n) => ({
id: n.id,
position: pos.get(n.id)!,
@@ -45,6 +50,8 @@ function toFlow(graph: AccessGraph, selected: Selection | null, onEdgeSelect: (i
n.type === 'policy' && n.missing ? 'gnode-missing' : '',
n.type === 'policy' && n.managed ? 'gnode-managed' : '',
selected?.kind === 'node' && selected.id === n.id ? 'gnode-selected' : '',
focus && n.id !== focus && linkedNodes.has(n.id) ? 'gnode-linked' : '',
focus && !linkedNodes.has(n.id) ? 'gnode-dim' : '',
].join(' '),
sourcePosition: Position.Right,
targetPosition: Position.Left,
@@ -59,6 +66,8 @@ function toFlow(graph: AccessGraph, selected: Selection | null, onEdgeSelect: (i
target: e.target,
markerEnd: { type: MarkerType.ArrowClosed },
selected: selected?.kind === 'edge' && selected.id === e.id,
className: focus ? (linkedEdges.has(e.id) ? 'edge-linked' : 'edge-dim') : undefined,
zIndex: linkedEdges.has(e.id) ? 1 : 0,
}
const label = capabilityLabel(e.capabilities)
if (!label) return base
@@ -66,6 +75,8 @@ function toFlow(graph: AccessGraph, selected: Selection | null, onEdgeSelect: (i
const data: GrantEdgeData = {
label,
offset: (siblings.indexOf(e.id) - (siblings.length - 1) / 2) * 24,
linked: linkedEdges.has(e.id),
dim: focus !== null && !linkedEdges.has(e.id),
onSelect: () => onEdgeSelect(e.id),
}
return { ...base, type: 'grant', data: data as unknown as Record<string, unknown> }
+21 -6
View File
@@ -173,7 +173,6 @@ main:focus {
align-items: center;
margin-left: auto;
padding: 0 var(--s3);
color: var(--ink-faint);
font-family: var(--mono);
font-size: var(--text-sm);
}
@@ -182,10 +181,6 @@ main:focus {
margin-left: auto;
}
.shell > nav .whoami + .connect {
margin-left: 0;
}
.shell main {
max-width: var(--measure);
margin: 0 auto;
@@ -888,6 +883,21 @@ td:last-child button + button {
box-shadow: 0 0 0 2px var(--accent);
}
/* With a node selected, its own links stand out and everything else steps back. */
.gnode-linked .gnode-card {
box-shadow: 0 0 0 1px var(--accent);
}
.gnode-dim,
.edge-dim {
opacity: 0.25;
}
.react-flow__edge.edge-linked .react-flow__edge-path {
stroke: var(--accent);
stroke-width: 2.5;
}
/* Handles anchor the edges but aren't interactive. */
.gnode-card .react-flow__handle {
width: 1px;
@@ -1427,11 +1437,16 @@ button.danger-solid:hover:not(:disabled) {
cursor: pointer;
}
.edge-label-selected {
.edge-label-selected,
.edge-label-linked {
border-color: var(--accent);
color: var(--accent);
}
.edge-label-dim {
opacity: 0.25;
}
/* Inline "Grant access" form inside the secret detail card. */
.grant-here {
max-width: none;