diff --git a/src/Layout.test.tsx b/src/Layout.test.tsx new file mode 100644 index 0000000..4b913c0 --- /dev/null +++ b/src/Layout.test.tsx @@ -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 ( + + ) +} + +function setup() { + const router = createMemoryRouter([{ path: '/', element: , children: [{ index: true, element: }, { path: 'connect', element:

connect page

}] }]) + render( + + + + + , + ) + 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() +}) diff --git a/src/Layout.tsx b/src/Layout.tsx index 30023cb..f930ee7 100644 --- a/src/Layout.tsx +++ b/src/Layout.tsx @@ -26,10 +26,16 @@ export function Layout() { {l.label} ))} - {client && tokenInfo && {tokenInfo.display_name || 'token'}} - - {client ? 'Connection' : 'Connect'} - + {client ? ( + // Signed in: the name is the way back to the connection details and Disconnect. + + {tokenInfo?.display_name || 'token'} + + ) : ( + + Connect + + )} {notice && (
diff --git a/src/features/graph/GraphEdge.tsx b/src/features/graph/GraphEdge.tsx index a54467d..e5d6d6c 100644 --- a/src/features/graph/GraphEdge.tsx +++ b/src/features/graph/GraphEdge.tsx @@ -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 ))} {p.edges.map((ed) => ( - ))} @@ -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') +}) diff --git a/src/features/graph/GraphPage.tsx b/src/features/graph/GraphPage.tsx index abd5ed6..3109810 100644 --- a/src/features/graph/GraphPage.tsx +++ b/src/features/graph/GraphPage.tsx @@ -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(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 } diff --git a/src/index.css b/src/index.css index 3462ed9..4530623 100644 --- a/src/index.css +++ b/src/index.css @@ -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;